1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mamikon\RoleManager\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Foundation\Auth\User; |
7
|
|
|
use Mamikon\RoleManager\Models\Permissions; |
8
|
|
|
use Mamikon\RoleManager\Models\Roles; |
9
|
|
|
|
10
|
|
|
class RoleMangerCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'permissions:migrate'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Get Permissions And Roles from config, and add to db'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new command instance. |
28
|
|
|
*/ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
parent::__construct(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute the console command. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
* |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
public function handle() |
42
|
|
|
{ |
43
|
|
|
$permissions = config('roleManager.permissions'); |
44
|
|
|
$roles = config('roleManager.roles'); |
45
|
|
|
foreach ($permissions as $permissionName => $permission) { |
46
|
|
|
|
47
|
|
|
$permissionModel = new Permissions(); |
48
|
|
|
if (empty($permissionName)) { |
49
|
|
|
throw new \Exception('Permission Name Required'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!$permissionModel->where('name', $permissionName)->first()) { |
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
$permissionModel->name = $permissionName; |
|
|
|
|
56
|
|
|
|
57
|
|
|
if (isset($permission['description'])) { |
58
|
|
|
$permissionModel->description = $permission['description']; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
if (isset($permission['class'])) { |
61
|
|
|
$permissionModel->class = $permission['class']; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
if (isset($permission['method'])) { |
64
|
|
|
$permissionModel->method = $permission['method']; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
$permissionModel->save(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
foreach ($roles as $roleName => $role) { |
71
|
|
|
$roleModel = new Roles(); |
72
|
|
|
if (empty($roleName)) { |
73
|
|
|
throw new \Exception('Role Name Required'); |
74
|
|
|
} |
75
|
|
|
if (!$roleModel->where('name', $roleName)->first()) { |
|
|
|
|
76
|
|
|
$roleModel->name = $roleName; |
|
|
|
|
77
|
|
|
if (isset($role['description'])) { |
78
|
|
|
$roleModel->description = $role['description']; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
$roleModel->save(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$defaultRoles = config('roleManager.assignPermissionsToRole'); |
85
|
|
|
foreach ($defaultRoles as $roleName => $permissionsList) { |
86
|
|
|
|
87
|
|
|
$roleModel = Roles::where('name', $roleName) |
88
|
|
|
->with('permissions')->first(); |
89
|
|
|
if ($roleModel) { |
90
|
|
|
|
91
|
|
|
if ($permissionsList == '*') { |
92
|
|
View Code Duplication |
foreach ($permissions as $permissionName => $permission) { |
|
|
|
|
93
|
|
|
$permissionModel = Permissions:: |
94
|
|
|
where('name', $permissionName) |
95
|
|
|
->first(); |
96
|
|
|
if ($permissionModel) { |
97
|
|
|
$checker = $roleModel->permissions |
98
|
|
|
->where('name', $permissionName)->first(); |
99
|
|
|
if (!$checker) { |
100
|
|
|
$roleModel->permissions()->attach($permissionModel); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} elseif (is_array($permissionsList)) { |
106
|
|
View Code Duplication |
foreach ($permissions as $permissionName) { |
|
|
|
|
107
|
|
|
|
108
|
|
|
$permissionModel = Permissions:: |
109
|
|
|
where('name', $permissionName)->first(); |
110
|
|
|
|
111
|
|
|
if ($permissionModel) { |
112
|
|
|
|
113
|
|
|
$checker = $roleModel-> |
114
|
|
|
permissions->where('name', $permissionName)->first(); |
115
|
|
|
|
116
|
|
|
if (!$checker) { |
117
|
|
|
$roleModel->permissions()->attach($permissionModel); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$userAssignments = config('roleManager.assignRoleToUser'); |
126
|
|
|
if (!empty($userAssignments) AND is_array($userAssignments)) { |
|
|
|
|
127
|
|
|
foreach ($userAssignments as $roleName => $userEmail) { |
128
|
|
|
$roleModel = Roles::where('name', $roleName)->first(); |
129
|
|
|
if ($user = User::where('email', $userEmail)->first() |
130
|
|
|
AND $roleModel |
|
|
|
|
131
|
|
|
AND !$roleModel->belongsToUser($user) |
|
|
|
|
132
|
|
|
) { |
133
|
|
|
$roleModel->users()->attach($user); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
echo "All permission Migrated Successfully"; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: