|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mamikon\RoleManager; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Foundation\Auth\User; |
|
7
|
|
|
use Illuminate\Support\Facades\Gate; |
|
8
|
|
|
use Illuminate\Support\Facades\Schema; |
|
9
|
|
|
use Mamikon\RoleManager\Models\Permissions; |
|
10
|
|
|
use Mamikon\RoleManager\Models\Roles; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class RoleManager |
|
14
|
|
|
* |
|
15
|
|
|
* @category Laravel_Package |
|
16
|
|
|
* @package Mamikon\RoleManager |
|
17
|
|
|
* @author Mamikon Arakelyan <[email protected]> |
|
18
|
|
|
* @license https://github.com/mamikon/role-manager/blob/master/LICENSE.md MIT |
|
19
|
|
|
* @link https://github.com/mamikon/role-manager |
|
20
|
|
|
*/ |
|
21
|
|
|
class RoleManager |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Define all permission and make usable from laravel application |
|
25
|
|
|
* |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public function defineAllPermissions() |
|
29
|
|
|
{ |
|
30
|
|
|
foreach ($this->getPermissions() as $permission) { |
|
31
|
|
|
|
|
32
|
|
|
Gate::define( |
|
33
|
|
|
$permission->name, |
|
34
|
|
|
function ($user, ...$arguments) use ($permission) { |
|
35
|
|
|
foreach ($permission->roles as $role) { |
|
36
|
|
|
if ($role->belongsToUser($user)) { |
|
37
|
|
|
if (!empty($permission->class) |
|
38
|
|
|
AND !empty($permission->method) |
|
|
|
|
|
|
39
|
|
|
AND class_exists($permission->class) |
|
|
|
|
|
|
40
|
|
|
) { |
|
41
|
|
|
$container = resolve($permission->class); |
|
42
|
|
|
if (method_exists($container, $permission->method)) { |
|
43
|
|
|
array_unshift($arguments, $user); |
|
44
|
|
|
return |
|
45
|
|
|
call_user_func_array( |
|
46
|
|
|
[$container, $permission->method], |
|
47
|
|
|
$arguments |
|
48
|
|
|
); |
|
49
|
|
|
} else { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Assign Role to user |
|
65
|
|
|
* |
|
66
|
|
|
* @param int|User $user User Instance or user id |
|
67
|
|
|
* @param int|string|Roles $role Role Instance, role name, or role id |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
public function assignRole($user, $role) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
if (is_int($user) and !$user = User::where('id', $user)->first()) { |
|
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
if (is_int($role) and !$role = Roles::where('id', $role)->first()) { |
|
|
|
|
|
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
if (is_string($role) and !$role = Roles::where('name', $role)->first()) { |
|
|
|
|
|
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
if (!($user instanceof User) or !($role instanceof Roles)) { |
|
|
|
|
|
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
return $role->assignToUser($user); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Remove Role from user |
|
90
|
|
|
* |
|
91
|
|
|
* @param int|User $user User Instance or user id |
|
92
|
|
|
* @param int|string|Roles $role Role Instance, role name, or role id |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
View Code Duplication |
public function removeRole($user, $role) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
if (is_int($user) and !$user = User::where('id', $user)->first()) { |
|
|
|
|
|
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
if (is_int($role) and !$role = Roles::where('id', $role)->first()) { |
|
|
|
|
|
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
if (is_string($role) and !$role = Roles::where('name', $role)->first()) { |
|
|
|
|
|
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
if (!($user instanceof User) or !($role instanceof Roles)) { |
|
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
$role->users()->detach($user->id); |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get All permissions |
|
116
|
|
|
* |
|
117
|
|
|
* @return array|\Illuminate\Database\Eloquent\Collection|static[] |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getPermissions() |
|
120
|
|
|
{ |
|
121
|
|
|
if (Schema::hasTable(config('roleManager.permissionsTable'))) { |
|
122
|
|
|
return Permissions::with('roles')->get(); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
return []; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.