Skip to content

Commit 5a26066

Browse files
authored
Merge pull request #1 from farhoudi/develop
Initial commit
2 parents 16c1201 + f3a9afd commit 5a26066

File tree

7 files changed

+422
-0
lines changed

7 files changed

+422
-0
lines changed

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "farhoudi/laravel-rbac",
3+
"description": "Role based access control for Laravel 5+",
4+
"keywords": [
5+
"laravel",
6+
"rbac",
7+
"role",
8+
"permission",
9+
"acl"
10+
],
11+
"type": "library",
12+
"authors": [
13+
{
14+
"name": "Ali Farhoudi",
15+
"email": "af.farhoudi@gmail.com"
16+
}
17+
],
18+
"license": "MIT",
19+
"require": {
20+
"php": ">=5.5.9",
21+
"illuminate/support": "5.2.*"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Farhoudi\\Rbac\\": "src/"
26+
}
27+
}
28+
}

src/Models/Permission.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Farhoudi\Rbac\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* Class Permission
9+
* @package Farhoudi\Rbac\Models
10+
*/
11+
class Permission extends Model {
12+
13+
public $table = 'rbac_permissions';
14+
15+
/**
16+
* The attributes that are mass assignable.
17+
*
18+
* @var array
19+
*/
20+
protected $fillable = [
21+
'name',
22+
'alias',
23+
'description',
24+
];
25+
26+
/**
27+
* The attributes that should be casted to native types.
28+
*
29+
* @var array
30+
*/
31+
protected $casts = [
32+
'name' => 'string',
33+
'alias' => 'string',
34+
'description' => 'string',
35+
];
36+
37+
/**
38+
* Validation rules
39+
*
40+
* @var array
41+
*/
42+
public static $rules = [
43+
'name' => 'required|string',
44+
'alias' => 'nullable|string|unique:rbac_permissions,alias',
45+
'description' => 'nullable|string',
46+
];
47+
48+
protected $hidden = [
49+
'created_at',
50+
'updated_at',
51+
'description',
52+
];
53+
54+
public function roles() {
55+
return $this->belongsToMany(Role::class, 'rbac_permission_role', 'permission_id', 'role_id');
56+
}
57+
58+
}

src/Models/PermissionGroup.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Farhoudi\Rbac\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* Class Permission
9+
* @package Farhoudi\Rbac\Models
10+
*/
11+
class PermissionGroup extends Model {
12+
13+
public $table = 'rbac_permission_groups';
14+
15+
/**
16+
* The attributes that are mass assignable.
17+
*
18+
* @var array
19+
*/
20+
protected $fillable = [
21+
'name',
22+
'description',
23+
];
24+
25+
/**
26+
* Validation rules
27+
*
28+
* @var array
29+
*/
30+
public static $rules = [
31+
'name' => 'required|unique:rbac_permissions,alias',
32+
'description' => 'nullable|string',
33+
];
34+
35+
protected $hidden = [
36+
'created_at',
37+
'updated_at',
38+
'description',
39+
];
40+
41+
public function permissions() {
42+
return $this->hasMany(Permission::class, 'group_id', 'id');
43+
}
44+
45+
}

src/Models/Role.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Farhoudi\Rbac\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Farhoudi\User\Models\User;
7+
8+
/**
9+
* Class Role
10+
* @package Farhoudi\Rbac\Models
11+
*/
12+
class Role extends Model {
13+
14+
public $table = 'rbac_roles';
15+
16+
/**
17+
* The attributes that are mass assignable.
18+
*
19+
* @var array
20+
*/
21+
protected $fillable = [
22+
'name',
23+
'alias',
24+
'description',
25+
];
26+
27+
/**
28+
* The attributes that should be casted to native types.
29+
*
30+
* @var array
31+
*/
32+
protected $casts = [
33+
'name' => 'string',
34+
'alias' => 'string',
35+
'description' => 'string',
36+
];
37+
38+
/**
39+
* Validation rules
40+
*
41+
* @var array
42+
*/
43+
public static $rules = [
44+
'name' => 'required|string',
45+
'alias' => 'nullable|string|unique:rbac_roles,alias',
46+
'description' => 'nullable|string',
47+
];
48+
49+
protected $hidden = [
50+
'created_at',
51+
'updated_at',
52+
'description',
53+
];
54+
55+
public function permissions() {
56+
return $this->belongsToMany(Permission::class, 'rbac_permission_role', 'role_id', 'permission_id');
57+
}
58+
59+
public function users() {
60+
return $this->belongsToMany(User::class, 'rbac_role_user', 'role_id', 'user_id');
61+
}
62+
63+
public function attachPermission($permission) {
64+
if (is_array($permission)) {
65+
foreach ($permission as $item) {
66+
$this->attachPermission($item);
67+
}
68+
}
69+
70+
if ($permission instanceof Permission) {
71+
$this->permissions()->save($permission);
72+
} else if (is_string($permission)) {
73+
$permission = Permission::where('alias', $permission)->first();
74+
if (!empty($permission)) {
75+
$this->permissions()->save($permission);
76+
}
77+
}
78+
}
79+
80+
}

src/Rbac.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Farhoudi\Rbac;
4+
5+
use Illuminate\Database\Eloquent\Collection;
6+
use Farhoudi\Rbac\Models\Role;
7+
8+
/**
9+
* Trait Rbac
10+
*/
11+
trait Rbac {
12+
13+
private $rbacIsLoaded = false;
14+
15+
private $roles;
16+
17+
private $permissions;
18+
19+
/**
20+
* @param string|array $role
21+
* @return bool
22+
*/
23+
public function hasRole($role) {
24+
if (!$this->rbacIsLoaded) {
25+
$this->loadPermissions();
26+
$this->rbacIsLoaded = true;
27+
}
28+
29+
if (is_array($role)) {
30+
foreach ($role as $r) {
31+
if (!$this->hasRole($r)) {
32+
return false;
33+
}
34+
}
35+
return true;
36+
}
37+
38+
$roles = explode('|', $role);
39+
foreach ($roles as $role) {
40+
if (!empty($this->roles->where('alias', $role)->first())) {
41+
return true;
42+
}
43+
}
44+
return false;
45+
}
46+
47+
/**
48+
* @param string|array $permission
49+
* @return bool
50+
*/
51+
public function hasPermission($permission) {
52+
if (!$this->rbacIsLoaded) {
53+
$this->loadPermissions();
54+
$this->rbacIsLoaded = true;
55+
}
56+
57+
if (is_array($permission)) {
58+
foreach ($permission as $p) {
59+
if (!$this->hasPermission($p)) {
60+
return false;
61+
}
62+
}
63+
return true;
64+
}
65+
66+
$permissions = explode('|', $permission);
67+
foreach ($permissions as $permission) {
68+
if (!empty($this->permissions->where('alias', $permission)->first())) {
69+
return true;
70+
}
71+
}
72+
return false;
73+
}
74+
75+
/**
76+
* Assign Role to User
77+
*
78+
* @param string|Role $role
79+
*/
80+
public function assignRole($role) {
81+
if (is_array($role)) {
82+
foreach ($role as $item) {
83+
$this->assignRole($item);
84+
}
85+
}
86+
87+
if ($role instanceof Role) {
88+
if (!$this->hasRole($role->alias)) {
89+
$this->roles()->save($role);
90+
}
91+
} else if (is_string($role)) {
92+
$role = Role::where('alias', $role)->first();
93+
if (!empty($role)) {
94+
if (!$this->hasRole($role->alias)) {
95+
$this->roles()->save($role);
96+
}
97+
}
98+
}
99+
100+
if ($this->rbacIsLoaded) {
101+
$this->loadPermissions();
102+
}
103+
}
104+
105+
public function loadPermissions() {
106+
$this->roles = $this->roles()->with(['permissions'])->get();
107+
$this->permissions = new Collection();
108+
foreach ($this->roles as $key => $role) {
109+
foreach ($role->permissions as $permission) {
110+
if (empty($this->permissions->where('name', $permission->name)->first()) && empty($this->permissions->where('alias', $permission->alias)->first())) {
111+
$this->permissions->add($permission);
112+
}
113+
unset($role->permissions);
114+
}
115+
}
116+
return $this->permissions;
117+
}
118+
119+
public function roles() {
120+
return $this->belongsToMany(Role::class, 'rbac_role_user', 'user_id', 'role_id');
121+
}
122+
123+
}

src/RbacServiceProvider.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Farhoudi\Rbac;
4+
5+
use Illuminate\Support\Facades\Event;
6+
use Illuminate\Support\Facades\Lang;
7+
use Illuminate\Support\ServiceProvider;
8+
9+
class RbacServiceProvider extends ServiceProvider {
10+
11+
public function register() {
12+
//
13+
}
14+
15+
public function boot() {
16+
$this->publishes([
17+
__DIR__.'/migrations/' => database_path('migrations')
18+
], 'migrations');
19+
}
20+
21+
}

0 commit comments

Comments
 (0)