| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Foundation\Auth\Access;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Contracts\Auth\Access\Gate;
|
|
|
6 |
use Illuminate\Support\Str;
|
|
|
7 |
|
|
|
8 |
trait AuthorizesRequests
|
|
|
9 |
{
|
|
|
10 |
/**
|
|
|
11 |
* Authorize a given action for the current user.
|
|
|
12 |
*
|
|
|
13 |
* @param mixed $ability
|
|
|
14 |
* @param mixed|array $arguments
|
|
|
15 |
* @return \Illuminate\Auth\Access\Response
|
|
|
16 |
*
|
|
|
17 |
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
18 |
*/
|
|
|
19 |
public function authorize($ability, $arguments = [])
|
|
|
20 |
{
|
|
|
21 |
[$ability, $arguments] = $this->parseAbilityAndArguments($ability, $arguments);
|
|
|
22 |
|
|
|
23 |
return app(Gate::class)->authorize($ability, $arguments);
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Authorize a given action for a user.
|
|
|
28 |
*
|
|
|
29 |
* @param \Illuminate\Contracts\Auth\Authenticatable|mixed $user
|
|
|
30 |
* @param mixed $ability
|
|
|
31 |
* @param mixed|array $arguments
|
|
|
32 |
* @return \Illuminate\Auth\Access\Response
|
|
|
33 |
*
|
|
|
34 |
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
35 |
*/
|
|
|
36 |
public function authorizeForUser($user, $ability, $arguments = [])
|
|
|
37 |
{
|
|
|
38 |
[$ability, $arguments] = $this->parseAbilityAndArguments($ability, $arguments);
|
|
|
39 |
|
|
|
40 |
return app(Gate::class)->forUser($user)->authorize($ability, $arguments);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Guesses the ability's name if it wasn't provided.
|
|
|
45 |
*
|
|
|
46 |
* @param mixed $ability
|
|
|
47 |
* @param mixed|array $arguments
|
|
|
48 |
* @return array
|
|
|
49 |
*/
|
|
|
50 |
protected function parseAbilityAndArguments($ability, $arguments)
|
|
|
51 |
{
|
|
|
52 |
if (is_string($ability) && ! str_contains($ability, '\\')) {
|
|
|
53 |
return [$ability, $arguments];
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
$method = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2]['function'];
|
|
|
57 |
|
|
|
58 |
return [$this->normalizeGuessedAbilityName($method), $ability];
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Normalize the ability name that has been guessed from the method name.
|
|
|
63 |
*
|
|
|
64 |
* @param string $ability
|
|
|
65 |
* @return string
|
|
|
66 |
*/
|
|
|
67 |
protected function normalizeGuessedAbilityName($ability)
|
|
|
68 |
{
|
|
|
69 |
$map = $this->resourceAbilityMap();
|
|
|
70 |
|
|
|
71 |
return $map[$ability] ?? $ability;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Authorize a resource action based on the incoming request.
|
|
|
76 |
*
|
|
|
77 |
* @param string|array $model
|
|
|
78 |
* @param string|array|null $parameter
|
|
|
79 |
* @param array $options
|
|
|
80 |
* @param \Illuminate\Http\Request|null $request
|
|
|
81 |
* @return void
|
|
|
82 |
*/
|
|
|
83 |
public function authorizeResource($model, $parameter = null, array $options = [], $request = null)
|
|
|
84 |
{
|
|
|
85 |
$model = is_array($model) ? implode(',', $model) : $model;
|
|
|
86 |
|
|
|
87 |
$parameter = is_array($parameter) ? implode(',', $parameter) : $parameter;
|
|
|
88 |
|
|
|
89 |
$parameter = $parameter ?: Str::snake(class_basename($model));
|
|
|
90 |
|
|
|
91 |
$middleware = [];
|
|
|
92 |
|
|
|
93 |
foreach ($this->resourceAbilityMap() as $method => $ability) {
|
|
|
94 |
$modelName = in_array($method, $this->resourceMethodsWithoutModels()) ? $model : $parameter;
|
|
|
95 |
|
|
|
96 |
$middleware["can:{$ability},{$modelName}"][] = $method;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
foreach ($middleware as $middlewareName => $methods) {
|
|
|
100 |
$this->middleware($middlewareName, $options)->only($methods);
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Get the map of resource methods to ability names.
|
|
|
106 |
*
|
|
|
107 |
* @return array
|
|
|
108 |
*/
|
|
|
109 |
protected function resourceAbilityMap()
|
|
|
110 |
{
|
|
|
111 |
return [
|
|
|
112 |
'index' => 'viewAny',
|
|
|
113 |
'show' => 'view',
|
|
|
114 |
'create' => 'create',
|
|
|
115 |
'store' => 'create',
|
|
|
116 |
'edit' => 'update',
|
|
|
117 |
'update' => 'update',
|
|
|
118 |
'destroy' => 'delete',
|
|
|
119 |
];
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Get the list of resource methods which do not have model parameters.
|
|
|
124 |
*
|
|
|
125 |
* @return array
|
|
|
126 |
*/
|
|
|
127 |
protected function resourceMethodsWithoutModels()
|
|
|
128 |
{
|
|
|
129 |
return ['index', 'create', 'store'];
|
|
|
130 |
}
|
|
|
131 |
}
|