1 | <?php |
||
15 | class Request |
||
16 | { |
||
17 | /** @var array */ |
||
18 | protected $json; |
||
19 | |||
20 | /** @var IlluminateRequest */ |
||
21 | protected $request; |
||
22 | |||
23 | /** @var Validator */ |
||
24 | protected $validator; |
||
25 | |||
26 | 24 | public function __construct(IlluminateRequest $request) |
|
30 | |||
31 | /** |
||
32 | * @throws RequestFailedValidation |
||
33 | */ |
||
34 | 3 | public function validate() |
|
43 | |||
44 | /** |
||
45 | * Ensure that a requested operation is authorized. |
||
46 | * If not, throw an exception. |
||
47 | * |
||
48 | * This requires a registered Policy. |
||
49 | * If no policy is defined, |
||
50 | * the framework will throw InvalidArgumentException. |
||
51 | * |
||
52 | * See also: |
||
53 | * https://laravel.com/docs/master/authorization |
||
54 | * http://jsonapi.org/format/#errors |
||
55 | * |
||
56 | * @param string $action Desired action; must match a policy method name. |
||
57 | * @param mixed $object Target object; class must match a policy. |
||
58 | * @param array $source Reference to source of error in request. |
||
59 | * |
||
60 | * @return bool True on success; throws exception on failure. |
||
61 | * |
||
62 | * @throws RequestFailedAuthorization |
||
63 | * |
||
64 | * TODO: use a UUID for the source? |
||
65 | */ |
||
66 | 9 | public function authorize( |
|
67 | string $action, |
||
68 | $object, |
||
69 | array $source = null |
||
70 | ) { |
||
71 | 9 | if ($this->request()->user()->cant($action, $object)) { |
|
72 | 6 | throw new RequestFailedAuthorization( |
|
73 | 6 | new Error( |
|
74 | 6 | $id = null, |
|
75 | 6 | $link = new Link('https://tools.ietf.org/html/rfc7231#section-6.5.3'), |
|
76 | 6 | $status = '403', |
|
77 | 6 | $code = null, |
|
78 | 6 | $title = 'Forbidden', |
|
79 | 6 | $desc = 'Access is denied for one or more of the specified resources', |
|
80 | 6 | $source, |
|
81 | 6 | $meta = null |
|
82 | ) |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | 3 | return true; |
|
87 | } |
||
88 | |||
89 | 6 | public function json(): array |
|
108 | |||
109 | 6 | public function validator() : ValidatesRequests |
|
113 | |||
114 | 6 | public function setValidator(ValidatesRequests $validator) |
|
118 | |||
119 | 18 | public function request(): IlluminateRequest |
|
123 | } |
||
124 |