|
6 | 6 | use Blueprint\Concerns\HandlesTraits;
|
7 | 7 | use Blueprint\Contracts\Generator;
|
8 | 8 | use Blueprint\Models\Controller;
|
| 9 | +use Blueprint\Models\Policy; |
9 | 10 | use Blueprint\Models\Statements\DispatchStatement;
|
10 | 11 | use Blueprint\Models\Statements\EloquentStatement;
|
11 | 12 | use Blueprint\Models\Statements\FireStatement;
|
@@ -62,22 +63,57 @@ protected function buildMethods(Controller $controller)
|
62 | 63 |
|
63 | 64 | $methods = '';
|
64 | 65 |
|
| 66 | + $controllerModelName = Str::singular($controller->prefix()); |
| 67 | + |
| 68 | + if ($controller->policy()?->authorizeResource()) { |
| 69 | + $methods .= str_replace( |
| 70 | + [ |
| 71 | + '{{ modelClass }}', |
| 72 | + '{{ modelVariable }}', |
| 73 | + ], |
| 74 | + [ |
| 75 | + Str::studly($controllerModelName), |
| 76 | + Str::camel($controllerModelName), |
| 77 | + ], |
| 78 | + $this->filesystem->stub('controller.authorize-resource.stub') |
| 79 | + ); |
| 80 | + } |
| 81 | + |
65 | 82 | foreach ($controller->methods() as $name => $statements) {
|
66 | 83 | $method = str_replace('{{ method }}', $name, $template);
|
67 | 84 |
|
68 | 85 | if (in_array($name, ['edit', 'update', 'show', 'destroy'])) {
|
69 |
| - $context = Str::singular($controller->prefix()); |
70 |
| - $reference = $this->fullyQualifyModelReference($controller->namespace(), Str::camel($context)); |
71 |
| - $variable = '$' . Str::camel($context); |
| 86 | + $reference = $this->fullyQualifyModelReference($controller->namespace(), $controllerModelName); |
| 87 | + $variable = '$' . Str::camel($controllerModelName); |
72 | 88 |
|
73 | 89 | $search = '(Request $request';
|
74 |
| - $method = str_replace($search, $search . ', ' . $context . ' ' . $variable, $method); |
| 90 | + $method = str_replace($search, $search . ', ' . $controllerModelName . ' ' . $variable, $method); |
75 | 91 | $this->addImport($controller, $reference);
|
76 | 92 | }
|
77 | 93 |
|
78 | 94 | $body = '';
|
79 | 95 | $using_validation = false;
|
80 | 96 |
|
| 97 | + if ($controller->policy() && !$controller->policy()->authorizeResource()) { |
| 98 | + if (in_array(Policy::$resourceAbilityMap[$name], $controller->policy()->methods())) { |
| 99 | + $body .= self::INDENT . str_replace( |
| 100 | + [ |
| 101 | + '{{ method }}', |
| 102 | + '{{ modelClass }}', |
| 103 | + '{{ modelVariable }}', |
| 104 | + ], |
| 105 | + [ |
| 106 | + $name, |
| 107 | + Str::studly($controllerModelName), |
| 108 | + '$' . Str::camel($controllerModelName), |
| 109 | + ], |
| 110 | + in_array($name, ['index', 'create', 'store']) |
| 111 | + ? "\$this->authorize('{{ method }}', {{ modelClass }}::class);" |
| 112 | + : "\$this->authorize('{{ method }}', {{ modelVariable }});" |
| 113 | + ) . PHP_EOL . PHP_EOL; |
| 114 | + } |
| 115 | + } |
| 116 | + |
81 | 117 | foreach ($statements as $statement) {
|
82 | 118 | if ($statement instanceof SendStatement) {
|
83 | 119 | $body .= self::INDENT . $statement->output() . PHP_EOL;
|
|
0 commit comments