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
|
|
|
$permissionName = trim($routeName, '/').'/'.trim($permission, '/'); |
44
|
|
|
$permissionName = strtolower($permissionName); |
45
|
|
|
|
46
|
|
|
$existingPermission = Permission::where(['name' => $permissionName])->first(); |
47
|
|
|
|
48
|
|
|
if ($existingPermission) { |
49
|
|
|
$continueAdding = $this->confirm("$permissionName already exists, do you want to carry on? [y|N]"); |
50
|
|
|
|
51
|
|
|
if ($continueAdding) { |
52
|
|
|
$bar->setMessage("Skipping $permissionName"); |
53
|
|
|
$bar->advance(); |
54
|
|
|
continue; |
55
|
|
|
} else { |
56
|
|
|
$bar->finish(); |
57
|
|
|
|
58
|
|
|
return $this->error("\nCancelled adding any more permissions."); |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
$newPermission = Permission::create(['name' => $permissionName]); |
62
|
|
|
$newPermission->save(); |
63
|
|
|
|
64
|
|
|
if ($newPermission->id) { |
65
|
|
|
$permissionsAdded++; |
66
|
|
|
$bar->setMessage("$newPermission->name added with ID: $newPermission->id"); |
67
|
|
|
$bar->advance(); |
68
|
|
|
} else { |
69
|
|
|
$bar->setMessage("$permissionName could not be created."); |
70
|
|
|
$bar->advance(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$bar->finish(); |
76
|
|
|
$this->info("\nFinished adding $permissionsAdded new permissions"); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.