Skip to content

Commit c5f14fe

Browse files
committed
moved models into respective directory
1 parent 1c6e3ea commit c5f14fe

File tree

6 files changed

+175
-10
lines changed

6 files changed

+175
-10
lines changed

src/Kodeine/Acl/AclServiceProvider.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
class AclServiceProvider extends ServiceProvider
66
{
7+
/**
8+
* Indicates if loading of the provider is deferred.
9+
*
10+
* @var bool
11+
*/
12+
protected $defer = false;
713

814
/**
915
* Bootstrap any application services.
@@ -12,10 +18,8 @@ class AclServiceProvider extends ServiceProvider
1218
*/
1319
public function boot()
1420
{
15-
$this->publishes([
16-
__DIR__ . '/../../config/acl.php' => config_path('acl.php'),
17-
__DIR__ . '/../../migrations/' => base_path('/database/migrations'),
18-
], 'migrations');
21+
$this->publishConfig();
22+
$this->publishMigration();
1923
}
2024

2125
/**
@@ -27,4 +31,24 @@ public function register()
2731
{
2832
//
2933
}
34+
35+
/**
36+
* Publish the config file to the application config directory
37+
*/
38+
public function publishConfig()
39+
{
40+
$this->publishes([
41+
__DIR__ . '/../../config/acl.php' => config_path('acl.php'),
42+
], 'config');
43+
}
44+
45+
/**
46+
* Publish the migration to the application migration folder
47+
*/
48+
public function publishMigration()
49+
{
50+
$this->publishes([
51+
__DIR__ . '/../../migrations/' => base_path('/database/migrations'),
52+
], 'migrations');
53+
}
3054
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php namespace Kodeine\Acl;
2+
3+
use Config;
4+
use Illuminate\Database\Eloquent\Model;
5+
6+
class Permission extends Model
7+
{
8+
/**
9+
* The attributes that are fillable via mass assignment.
10+
*
11+
* @var array
12+
*/
13+
protected $fillable = ['name', 'slug', 'description'];
14+
15+
/**
16+
* The database table used by the model.
17+
*
18+
* @var string
19+
*/
20+
protected $table = 'permissions';
21+
22+
/**
23+
* Permissions can belong to many roles.
24+
*
25+
* @return Model
26+
*/
27+
public function roles()
28+
{
29+
$model = Config::get('acl.role', 'Kodeine\Acl\Models\Eloquent\Role');
30+
return $this->belongsToMany($model)->withTimestamps();
31+
}
32+
33+
/**
34+
* Permissions can belong to many users.
35+
*
36+
* @return Model
37+
*/
38+
public function users()
39+
{
40+
return $this->belongsToMany(Config::get('auth.model'))->withTimestamps();
41+
}
42+
43+
/**
44+
* @param $value
45+
* @return array
46+
*/
47+
public function getSlugAttribute($value)
48+
{
49+
return json_decode($value, true);
50+
}
51+
52+
/**
53+
* @param $value
54+
*/
55+
public function setSlugAttribute($value)
56+
{
57+
$value = is_array($value) ? $value : [$value => true];
58+
59+
// if attribute is being updated.
60+
if ( isset($this->original['slug']) ) {
61+
$value = $value + json_decode($this->original['slug'], true);
62+
}
63+
64+
// remove null values.
65+
$value = array_filter($value, 'is_bool');
66+
67+
// store as json.
68+
$this->attributes['slug'] = json_encode($value);
69+
}
70+
71+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php namespace Kodeine\Acl;
2+
3+
use Config;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Kodeine\Acl\Traits\HasPermission;
6+
7+
class Role extends Model
8+
{
9+
use HasPermission;
10+
11+
/**
12+
* The attributes that are fillable via mass assignment.
13+
*
14+
* @var array
15+
*/
16+
protected $fillable = ['name', 'slug', 'description'];
17+
18+
/**
19+
* The database table used by the model.
20+
*
21+
* @var string
22+
*/
23+
protected $table = 'roles';
24+
25+
/**
26+
* Roles can belong to many users.
27+
*
28+
* @return Model
29+
*/
30+
public function users()
31+
{
32+
return $this->belongsToMany(Config::get('auth.model'))->withTimestamps();
33+
}
34+
35+
/**
36+
* List all permissions
37+
*
38+
* @return mixed
39+
*/
40+
public function getPermissions()
41+
{
42+
return $this->permissions->lists('slug', 'name');
43+
}
44+
45+
/**
46+
* Checks if the role has the given permission.
47+
*
48+
* @param string $permission
49+
* @param array $mergePermissions
50+
* @return bool
51+
*/
52+
public function can($permission, $mergePermissions = [])
53+
{
54+
$permission = $this->hasDelimiterToArray($permission);
55+
$permissions = $this->getPermissions() + $mergePermissions;
56+
57+
// make permissions to dot notation.
58+
// create.user, delete.admin etc.
59+
$permissions = $this->toDotPermissions($permissions);
60+
61+
if ( is_array($permission) ) {
62+
$intersect = array_intersect_key($permissions, array_flip($permission));
63+
64+
return count($permission) == count($intersect);
65+
}
66+
67+
return isset($permissions[$permission]) && $permissions[$permission] == true;
68+
}
69+
70+
}

src/Kodeine/Acl/Traits/HasPermission.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait HasPermission
2020
*/
2121
public function permissions()
2222
{
23-
$model = \Config::get('acl.permission', 'Kodeine\Acl\Permission');
23+
$model = \Config::get('acl.permission', 'Kodeine\Acl\Models\Eloquent\Permission');
2424
return $this->belongsToMany($model)->withTimestamps();
2525
}
2626

@@ -149,7 +149,7 @@ public function revokeAllPermissions()
149149
protected function parsePermissionId($permission)
150150
{
151151
if ( is_string($permission) ) {
152-
$model = new \Kodeine\Acl\Permission;
152+
$model = new \Kodeine\Acl\Models\Eloquent\Permission;
153153
$find = $model->whereSlug($permission)->first();
154154

155155
if ( ! is_object($find) ) {

src/Kodeine/Acl/Traits/HasRole.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ trait HasRole
1919
*/
2020
public function roles()
2121
{
22-
$model = \Config::get('acl.role', 'Kodeine\Acl\Role');
22+
$model = \Config::get('acl.role', 'Kodeine\Acl\Models\Eloquent\Role');
2323
return $this->belongsToMany($model)->withTimestamps();
2424
}
2525

@@ -130,7 +130,7 @@ public function revokeAllRoles()
130130
protected function parseRoleId($role)
131131
{
132132
if ( is_string($role) ) {
133-
$model = new \Kodeine\Acl\Role;
133+
$model = new \Kodeine\Acl\Models\Eloquent\Role;
134134
$find = $model->whereSlug($role)->first();
135135

136136
if ( ! is_object($find) ) {

src/config/acl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

33
return [
4-
'role' => 'Kodeine\Acl\Role',
5-
'permission' => 'Kodeine\Acl\Permission',
4+
'role' => 'Kodeine\Acl\Models\Eloquent\Role',
5+
'permission' => 'Kodeine\Acl\Models\Eloquent\Permission',
66
];

0 commit comments

Comments
 (0)