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
|
|
|
|
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$permissions = $this->getPermissions(); |
60
|
|
|
foreach ($tables as $table) { |
61
|
|
|
foreach ($permissions as $permission => $permission_lang) { |
62
|
|
|
$http_method = $this->generateHttpMethod($permission); |
63
|
|
|
$http_path = $this->generateHttpPath($table, $permission); |
64
|
|
|
$slug = $this->generateSlug($table, $permission); |
65
|
|
|
$name = $this->generateName($table, $permission_lang); |
66
|
|
|
$exists = Permission::where('slug', $slug)->exists(); |
67
|
|
|
if (!$exists) { |
68
|
|
|
Permission::create([ |
69
|
|
|
'name' => $name, |
70
|
|
|
'slug' => $slug, |
71
|
|
|
'http_method' => $http_method, |
72
|
|
|
'http_path' => $http_path, |
73
|
|
|
]); |
74
|
|
|
$this->info("$slug is generated"); |
75
|
|
|
} else { |
76
|
|
|
$this->warn("$slug is existed"); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function getAllTables() |
83
|
|
|
{ |
84
|
|
|
return array_map('current', DB::select('SHOW TABLES')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function getIgnoreTables() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
config('admin.database.users_table'), |
91
|
|
|
config('admin.database.roles_table'), |
92
|
|
|
config('admin.database.permissions_table'), |
93
|
|
|
config('admin.database.menu_table'), |
94
|
|
|
config('admin.database.operation_log_table'), |
95
|
|
|
config('admin.database.user_permissions_table'), |
96
|
|
|
config('admin.database.role_users_table'), |
97
|
|
|
config('admin.database.role_permissions_table'), |
98
|
|
|
config('admin.database.role_menu_table'), |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function getPermissions() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'list' => __('admin.list'), |
106
|
|
|
'view' => __('admin.view'), |
107
|
|
|
'create' => __('admin.create'), |
108
|
|
|
'edit' => __('admin.edit'), |
109
|
|
|
'delete' => __('admin.delete'), |
110
|
|
|
'export' => __('admin.export'), |
111
|
|
|
'filter' => __('admin.filter'), |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function generateHttpMethod($permission) |
116
|
|
|
{ |
117
|
|
|
switch ($permission) { |
118
|
|
|
case 'create': |
119
|
|
|
$http_method = ['POST']; |
120
|
|
|
break; |
121
|
|
|
case 'edit': |
122
|
|
|
$http_method = ['PUT', 'PATCH']; |
123
|
|
|
break; |
124
|
|
|
case 'delete': |
125
|
|
|
$http_method = ['DELETE']; |
126
|
|
|
break; |
127
|
|
|
case 'filter': |
128
|
|
|
$http_method = []; |
129
|
|
|
break; |
130
|
|
|
default: |
131
|
|
|
$http_method = ['GET']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $http_method; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function generateHttpPath($table, $permission) |
138
|
|
|
{ |
139
|
|
|
$resource = Str::kebab(Str::camel($table)); |
140
|
|
|
switch ($permission) { |
141
|
|
|
case 'create': |
142
|
|
|
case 'list': |
143
|
|
|
$http_path = '/' . $resource; |
144
|
|
|
break; |
145
|
|
|
case 'edit': |
146
|
|
|
case 'delete': |
147
|
|
|
case 'view': |
148
|
|
|
$http_path = '/' . $resource . '/*'; |
149
|
|
|
break; |
150
|
|
|
default: |
151
|
|
|
$http_path = ''; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $http_path; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function generateSlug($table, $permission) |
158
|
|
|
{ |
159
|
|
|
return Str::kebab(Str::camel($table)).'.'.$permission; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private function generateName($table, $permission_lang) |
163
|
|
|
{ |
164
|
|
|
return Str::upper(Str::kebab(Str::camel($table))).$permission_lang; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
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.