1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Microboard\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Console\PolicyMakeCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Microboard\Foundations\Traits\MicroboardPathResolver; |
9
|
|
|
|
10
|
|
|
class ResourcePolicy extends PolicyMakeCommand |
11
|
|
|
{ |
12
|
|
|
use MicroboardPathResolver; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The console command name. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name = 'microboard:policy'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Build the class with the given name. |
23
|
|
|
* |
24
|
|
|
* @param string $name |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
protected function buildClass($name) |
28
|
|
|
{ |
29
|
|
|
$stub = $this->replaceUserNamespace( |
30
|
|
|
parent::buildClass($name) |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$stub = $this->replaceAbilities($stub); |
34
|
|
|
|
35
|
|
|
$model = $this->option('model'); |
36
|
|
|
|
37
|
|
|
return $model ? $this->replaceModel($stub, $model) : $stub; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
* |
43
|
|
|
* @param $stub |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
private function replaceAbilities($stub) |
47
|
|
|
{ |
48
|
|
|
$code = []; |
49
|
|
|
$model = Str::of(class_basename($this->option('model')))->snake()->plural(); |
50
|
|
|
$abilities = [ |
51
|
|
|
'viewAny', 'view', 'create', 'update', 'delete' |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
if ($this->option('abilities')) { |
55
|
|
|
$abilities = array_map(function ($ability) { |
56
|
|
|
return trim($ability); |
57
|
|
|
}, explode(',', $this->option('abilities'))); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
foreach ($abilities as $ability) { |
61
|
|
|
switch ($ability) { |
62
|
|
|
default: |
63
|
|
|
case 'viewAny': |
64
|
|
|
$message = 'Determine whether the user can view any models.'; |
65
|
|
|
$docParams = "* @param {{ user }} " . '$user' . "\r\n\t"; |
66
|
|
|
$params = '{{ user }} $user'; |
67
|
|
|
break; |
68
|
|
|
case 'view': |
69
|
|
|
$message = 'Determine whether the user can view the model.'; |
70
|
|
|
$docParams = "* @param {{ user }} " . '$user' . "\r\n\t* @param {{ model }} " . '${{ modelVariable }}' . "\r\n\t"; |
71
|
|
|
$params = '{{ user }} $user, {{ model }} ${{ modelVariable }}'; |
72
|
|
|
break; |
73
|
|
|
case 'create': |
74
|
|
|
$message = 'Determine whether the user can create models.'; |
75
|
|
|
$docParams = "* @param {{ user }} " . '$user' . "\r\n\t"; |
76
|
|
|
$params = '{{ user }} $user'; |
77
|
|
|
break; |
78
|
|
|
case 'update': |
79
|
|
|
$message = 'Determine whether the user can update the model.'; |
80
|
|
|
$docParams = "* @param {{ user }} " . '$user' . "\r\n\t* @param {{ model }} " . '${{ modelVariable }}' . "\r\n\t"; |
81
|
|
|
$params = '{{ user }} $user, {{ model }} ${{ modelVariable }}'; |
82
|
|
|
break; |
83
|
|
|
case 'delete': |
84
|
|
|
$message = 'Determine whether the user can delete the model.'; |
85
|
|
|
$docParams = "* @param {{ user }} " . '$user' . "\r\n\t* @param {{ model }} " . '${{ modelVariable }}' . "\r\n\t"; |
86
|
|
|
$params = '{{ user }} $user, {{ model }} ${{ modelVariable }}'; |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$code[] = "\r\n\t/**\r\n\t* {$message}\r\n\t" . |
91
|
|
|
"*\r\n\t{$docParams}* @return mixed\r\n\t*/\r\n\tpublic function {$ability}({$params})\r\n\t" . |
92
|
|
|
"{\r\n\t\treturn " . '$user->permissions' . "()->contains('name', '{$model}-{$ability}');\r\n\t}"; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return str_replace('{{ abilities }}', implode("\r\n", $code), $stub); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the console command arguments. |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
protected function getOptions() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
|
|
...$this->pathOptions(), |
|
|
|
|
107
|
|
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the policy applies to'], |
108
|
|
|
['abilities', 'a', InputOption::VALUE_OPTIONAL, 'The abilities which the policy relies on'], |
109
|
|
|
['guard', 'g', InputOption::VALUE_OPTIONAL, 'The guard that the policy relies on'] |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|