1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Console; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Auth\Database\Permission; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
class PermissionCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'admin:permissions {--tables=}'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'generate admin permission base on table name'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new command instance. |
28
|
|
|
* |
29
|
|
|
* @return void |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
parent::__construct(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Execute the console command. |
38
|
|
|
* |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function handle() |
42
|
|
|
{ |
43
|
|
|
$all_tables = $this->getAllTables(); |
44
|
|
|
|
45
|
|
|
$tables = $this->option('tables') ? explode(',', $this->option('tables')) : []; |
46
|
|
|
if (empty($tables)) { |
47
|
|
|
$ignore_tables = $this->getIgnoreTables(); |
48
|
|
|
$tables = array_diff($all_tables, $ignore_tables); |
49
|
|
|
} else { |
50
|
|
|
$tables = array_intersect($all_tables, $tables); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (empty($tables)) { |
54
|
|
|
$this->info('table is not existed'); |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$permissions = $this->getPermissions(); |
59
|
|
|
foreach ($tables as $table) { |
60
|
|
|
foreach ($permissions as $permission => $permission_lang) { |
61
|
|
|
$http_method = $this->generateHttpMethod($permission); |
62
|
|
|
$http_path = $this->generateHttpPath($table, $permission); |
63
|
|
|
$slug = $this->generateSlug($table, $permission); |
64
|
|
|
$name = $this->generateName($table, $permission_lang); |
65
|
|
|
$exists = Permission::where('slug', $slug)->exists(); |
66
|
|
|
if (!$exists) { |
67
|
|
|
Permission::create([ |
68
|
|
|
'name' => $name, |
69
|
|
|
'slug' => $slug, |
70
|
|
|
'http_method' => $http_method, |
71
|
|
|
'http_path' => $http_path, |
72
|
|
|
]); |
73
|
|
|
$this->info("$slug is generated"); |
74
|
|
|
} else { |
75
|
|
|
$this->warn("$slug is existed"); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getAllTables() |
82
|
|
|
{ |
83
|
|
|
return array_map('current', DB::select('SHOW TABLES')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function getIgnoreTables() |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
config('admin.database.users_table'), |
90
|
|
|
config('admin.database.roles_table'), |
91
|
|
|
config('admin.database.permissions_table'), |
92
|
|
|
config('admin.database.menu_table'), |
93
|
|
|
config('admin.database.operation_log_table'), |
94
|
|
|
config('admin.database.user_permissions_table'), |
95
|
|
|
config('admin.database.role_users_table'), |
96
|
|
|
config('admin.database.role_permissions_table'), |
97
|
|
|
config('admin.database.role_menu_table'), |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function getPermissions() |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
|
|
'list' => __('admin.list'), |
105
|
|
|
'view' => __('admin.view'), |
106
|
|
|
'create' => __('admin.create'), |
107
|
|
|
'edit' => __('admin.edit'), |
108
|
|
|
'delete' => __('admin.delete'), |
109
|
|
|
'export' => __('admin.export'), |
110
|
|
|
'filter' => __('admin.filter'), |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function generateHttpMethod($permission) |
115
|
|
|
{ |
116
|
|
|
switch ($permission) { |
117
|
|
|
case 'create': |
118
|
|
|
$http_method = ['POST']; |
119
|
|
|
break; |
120
|
|
|
case 'edit': |
121
|
|
|
$http_method = ['PUT', 'PATCH']; |
122
|
|
|
break; |
123
|
|
|
case 'delete': |
124
|
|
|
$http_method = ['DELETE']; |
125
|
|
|
break; |
126
|
|
|
case 'filter': |
127
|
|
|
$http_method = []; |
128
|
|
|
break; |
129
|
|
|
default: |
130
|
|
|
$http_method = ['GET']; |
131
|
|
|
} |
132
|
|
|
return $http_method; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function generateHttpPath($table, $permission) |
136
|
|
|
{ |
137
|
|
|
$resource = Str::kebab(Str::camel($table)); |
138
|
|
|
switch ($permission) { |
139
|
|
|
case 'create': |
140
|
|
|
$http_path = '/' . $resource; |
141
|
|
|
break; |
142
|
|
|
case 'edit': |
143
|
|
|
$http_path = '/' . $resource . '/*'; |
144
|
|
|
break; |
145
|
|
|
case 'delete': |
146
|
|
|
$http_path = '/' . $resource . '/*'; |
147
|
|
|
break; |
148
|
|
|
case 'index': |
149
|
|
|
$http_path = '/' . $resource; |
150
|
|
|
break; |
151
|
|
|
case 'view': |
152
|
|
|
$http_path = '/' . $resource . '/*'; |
153
|
|
|
break; |
154
|
|
|
default: |
155
|
|
|
$http_path = ''; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $http_path; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function generateSlug($table, $permission) |
162
|
|
|
{ |
163
|
|
|
return Str::kebab(Str::camel($table)) . '.' . $permission; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function generateName($table, $permission_lang) |
167
|
|
|
{ |
168
|
|
|
return Str::upper(Str::kebab(Str::camel($table))) . $permission_lang; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.