1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RealPage\JsonApi\Requests; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\Validator; |
6
|
|
|
use Illuminate\Http\Request as IlluminateRequest; |
7
|
|
|
use Neomerx\JsonApi\Exceptions\JsonApiException; |
8
|
|
|
use RealPage\JsonApi\Authorization\RequestFailedAuthorization; |
9
|
|
|
use RealPage\JsonApi\ErrorFactory; |
10
|
|
|
use RealPage\JsonApi\Validation\RequestFailedValidation; |
11
|
|
|
use RealPage\JsonApi\Validation\ValidatesRequests; |
12
|
|
|
|
13
|
|
|
class Request |
14
|
|
|
{ |
15
|
|
|
/** @var array */ |
16
|
|
|
protected $json; |
17
|
|
|
|
18
|
|
|
/** @var IlluminateRequest */ |
19
|
|
|
protected $request; |
20
|
|
|
|
21
|
|
|
/** @var Validator */ |
22
|
|
|
protected $validator; |
23
|
|
|
|
24
|
|
|
public function __construct(IlluminateRequest $request) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this->request = $request; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @throws RequestFailedValidation |
31
|
|
|
*/ |
32
|
|
|
public function validate() |
33
|
|
|
{ |
34
|
|
|
/** @var ValidatesRequests $validator */ |
35
|
|
|
$validator = $this->validator(); |
36
|
|
|
|
37
|
|
|
if ($validator->isValid($this) === false) { |
38
|
|
|
throw new RequestFailedValidation($validator->errors()); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Ensure that a requested operation is authorized. |
44
|
|
|
* If not, throw an exception. |
45
|
|
|
* |
46
|
|
|
* This requires a registered Policy. |
47
|
|
|
* If no policy is defined, |
48
|
|
|
* the framework will throw InvalidArgumentException. |
49
|
|
|
* |
50
|
|
|
* See also: |
51
|
|
|
* https://laravel.com/docs/master/authorization |
52
|
|
|
* http://jsonapi.org/format/#errors |
53
|
|
|
* |
54
|
|
|
* @param string $action Desired action; must match a policy method name. |
55
|
|
|
* @param mixed $object Target object; class must match a policy. |
56
|
|
|
* @param array $source Reference to source of error in request. |
57
|
|
|
* |
58
|
|
|
* @return bool True on success; throws exception on failure. |
59
|
|
|
* |
60
|
|
|
* @throws RequestFailedAuthorization |
61
|
|
|
* |
62
|
|
|
* TODO: use a UUID for the source? |
63
|
|
|
*/ |
64
|
|
|
public function authorize( |
65
|
|
|
string $action, |
66
|
|
|
$object, |
67
|
|
|
array $source = null |
68
|
|
|
) { |
69
|
|
|
if ($this->request()->user()->cant($action, $object)) { |
70
|
|
|
throw new RequestFailedAuthorization( |
71
|
|
|
ErrorFactory::buildUnauthorized( |
72
|
|
|
$id = null, |
73
|
|
|
$aboutLink = null, |
74
|
|
|
$code = null, |
75
|
|
|
$source, |
76
|
|
|
$meta = null |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function json(): array |
85
|
|
|
{ |
86
|
|
|
if (!isset($this->json)) { |
87
|
|
|
$json = json_decode( |
88
|
|
|
$this->request()->getContent(), |
89
|
|
|
true, // associative array |
90
|
|
|
512, // depth |
91
|
|
|
JSON_UNESCAPED_SLASHES |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
95
|
|
|
throw new JsonApiException(new MalformedRequest()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->json = $json; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->json; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function validator() : ValidatesRequests |
105
|
|
|
{ |
106
|
|
|
return $this->validator; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function setValidator(ValidatesRequests $validator) |
110
|
|
|
{ |
111
|
|
|
$this->validator = $validator; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function request(): IlluminateRequest |
115
|
|
|
{ |
116
|
|
|
return $this->request; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|