1
|
|
|
<?php namespace Pz\LaravelDoctrine\Rest; |
2
|
|
|
|
3
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
4
|
|
|
use Illuminate\Contracts\Auth\Access\Gate; |
5
|
|
|
use Illuminate\Contracts\Validation\Validator; |
6
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
7
|
|
|
|
8
|
|
|
use Illuminate\Validation\ValidationException; |
9
|
|
|
use Pz\Doctrine\Rest\RestRequestInterface; |
10
|
|
|
|
11
|
|
|
abstract class RestRequest extends FormRequest implements RestRequestInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Authorization gateway ability. |
15
|
|
|
* Return `false` for pass auth. |
16
|
|
|
* |
17
|
|
|
* @return null|string |
18
|
|
|
*/ |
19
|
|
|
abstract public function ability(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function getFields() |
25
|
|
|
{ |
26
|
|
|
return $this->get('fields', []); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function isJsonApi() |
33
|
|
|
{ |
34
|
|
|
if (in_array(RestRequestInterface::JSON_API_CONTENT_TYPE, $this->getAcceptableContentTypes())) { |
|
|
|
|
35
|
|
|
return true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->getContentType() === static::JSON_API_CONTENT_TYPE; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
|
public function http() |
45
|
|
|
{ |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
2 |
|
public function rules() |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
2 |
|
'fields' => 'array' |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param object|string $entity |
61
|
|
|
* @throws AuthorizationException |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
4 |
|
public function authorize($entity) |
65
|
|
|
{ |
66
|
4 |
|
$this->gate()->authorize($this->ability(), $entity); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Validator $validator |
71
|
|
|
* |
72
|
|
|
* @throws ValidationException |
73
|
|
|
*/ |
74
|
1 |
|
protected function failedValidation(Validator $validator) |
75
|
|
|
{ |
76
|
1 |
|
throw (new ValidationException($validator))->errorBag($this->errorBag); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Laravel authorization gate. |
81
|
|
|
* |
82
|
|
|
* @return Gate |
83
|
|
|
*/ |
84
|
4 |
|
public function gate() |
85
|
|
|
{ |
86
|
4 |
|
return $this->container->make(Gate::class); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Pass default request authorization. |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
4 |
|
protected function passesAuthorization() |
95
|
|
|
{ |
96
|
4 |
|
return true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|