|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Permission\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Spatie\Permission\Contracts\Role as RoleContract; |
|
8
|
|
|
use Spatie\Permission\Contracts\Permission as PermissionContract; |
|
9
|
|
|
|
|
10
|
|
|
class Show extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
protected $signature = 'permission:show |
|
13
|
|
|
{guard? : The name of the guard} |
|
14
|
|
|
{style? : The display style (default|borderless|compact|box)}'; |
|
15
|
|
|
|
|
16
|
|
|
protected $description = 'Show a table of roles and permissions per guard'; |
|
17
|
|
|
|
|
18
|
|
|
public function handle() |
|
19
|
|
|
{ |
|
20
|
|
|
$permissionClass = app(PermissionContract::class); |
|
21
|
|
|
$roleClass = app(RoleContract::class); |
|
22
|
|
|
|
|
23
|
|
|
$style = $this->argument('style') ?? 'default'; |
|
24
|
|
|
$guard = $this->argument('guard'); |
|
25
|
|
|
|
|
26
|
|
|
if ($guard) { |
|
27
|
|
|
$guards = Collection::make([$guard]); |
|
28
|
|
|
} else { |
|
29
|
|
|
$guards = $permissionClass::pluck('guard_name')->merge($roleClass::pluck('guard_name'))->unique(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
foreach ($guards as $guard) { |
|
33
|
|
|
$this->info("Guard: $guard"); |
|
34
|
|
|
|
|
35
|
|
|
$roles = $roleClass::whereGuardName($guard)->orderBy('name')->get()->mapWithKeys(function ($role) { |
|
36
|
|
|
return [$role->name => $role->permissions->pluck('name')]; |
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
$permissions = $permissionClass::whereGuardName($guard)->orderBy('name')->pluck('name'); |
|
40
|
|
|
|
|
41
|
|
|
$body = $permissions->map(function ($permission) use ($roles) { |
|
42
|
|
|
return $roles->map(function (Collection $role_permissions) use ($permission) { |
|
43
|
|
|
return $role_permissions->contains($permission) ? ' ✔' : ' ·'; |
|
44
|
|
|
})->prepend($permission); |
|
45
|
|
|
}); |
|
46
|
|
|
|
|
47
|
|
|
$this->table( |
|
48
|
|
|
$roles->keys()->prepend('')->toArray(), |
|
49
|
|
|
$body->toArray(), |
|
50
|
|
|
$style |
|
|
|
|
|
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.