|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\Generators\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Spatie\Permission\Models\Permission; |
|
7
|
|
|
|
|
8
|
|
|
class PermissionGeneratorBackpackCommand extends Command |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The name and signature of the console command. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $signature = 'backpack:permissions {route : Name of the route e.g admin/teams} {--P|permission=* : A permission you wish to add, can accept multiple instances of -P|permission}'; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The console command description. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $description = 'Will generate permissions for routes and save to the database'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Execute the console command. |
|
26
|
|
|
* |
|
27
|
|
|
* @return mixed |
|
28
|
|
|
*/ |
|
29
|
|
|
public function handle() |
|
30
|
|
|
{ |
|
31
|
|
|
$routeName = $this->argument('route'); |
|
32
|
|
|
$permissions = ['list', 'create', 'update', 'reorder', 'delete']; |
|
33
|
|
|
$permissionsAdded = 0; |
|
34
|
|
|
$customPermissions = $this->option('permission'); |
|
35
|
|
|
|
|
36
|
|
|
if(count($customPermissions) > 0){ |
|
37
|
|
|
$permissions = $customPermissions; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$bar = $this->output->createProgressBar(count($permissions)); |
|
41
|
|
|
|
|
42
|
|
|
foreach($permissions as $permission){ |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$permissionName = trim($routeName, '/').'/'.trim($permission, '/'); |
|
45
|
|
|
$permissionName = strtolower($permissionName); |
|
46
|
|
|
|
|
47
|
|
|
$existingPermission = Permission::where(['name' => $permissionName])->first(); |
|
48
|
|
|
|
|
49
|
|
|
if($existingPermission){ |
|
50
|
|
|
|
|
51
|
|
|
$continueAdding = $this->confirm("$permissionName already exists, do you want to carry on? [y|N]"); |
|
52
|
|
|
|
|
53
|
|
|
if($continueAdding){ |
|
54
|
|
|
$bar->setMessage("Skipping $permissionName"); |
|
55
|
|
|
$bar->advance(); |
|
56
|
|
|
continue; |
|
57
|
|
|
} else { |
|
58
|
|
|
$bar->finish(); |
|
59
|
|
|
return $this->error("\nCancelled adding any more permissions."); |
|
60
|
|
|
} |
|
61
|
|
|
} else { |
|
62
|
|
|
$newPermission = Permission::create(['name' => $permissionName]); |
|
63
|
|
|
$newPermission->save(); |
|
64
|
|
|
|
|
65
|
|
|
if($newPermission->id){ |
|
66
|
|
|
$permissionsAdded++; |
|
67
|
|
|
$bar->setMessage("$newPermission->name added with ID: $newPermission->id"); |
|
68
|
|
|
$bar->advance(); |
|
69
|
|
|
} else { |
|
70
|
|
|
$bar->setMessage("$permissionName could not be created."); |
|
71
|
|
|
$bar->advance(); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$bar->finish(); |
|
77
|
|
|
$this->info("\nFinished adding $permissionsAdded new permissions"); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.