|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Ship\Parents\Requests; |
|
4
|
|
|
|
|
5
|
|
|
use App\Ship\Features\Exceptions\ValidationFailedException; |
|
6
|
|
|
use Illuminate\Contracts\Validation\Validator; |
|
7
|
|
|
use Dingo\Api\Http\Request as DingoRequest; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class RequestTrait |
|
11
|
|
|
* |
|
12
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
trait RequestTrait |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Overriding this function to modify the any user input before |
|
19
|
|
|
* applying the validation rules. |
|
20
|
|
|
* |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
public function all() |
|
24
|
|
|
{ |
|
25
|
|
|
$requestData = parent::all(); |
|
26
|
|
|
|
|
27
|
|
|
$requestData = $this->mergeUrlParametersWithRequestData($requestData); |
|
28
|
|
|
|
|
29
|
|
|
$requestData = $this->decodeHashedIdsBeforeValidation($requestData); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
return $requestData; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Overriding this function to throw a custom |
|
36
|
|
|
* exception instead of the default Laravel exception. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed|void |
|
41
|
|
|
*/ |
|
42
|
|
|
public function failedValidation(Validator $validator) |
|
43
|
|
|
{ |
|
44
|
|
|
if ($this->container['request'] instanceof DingoRequest) { |
|
|
|
|
|
|
45
|
|
|
throw new ValidationFailedException($validator->getMessageBag()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
parent::failedValidation($validator); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Used from the `authorize` function if the Request class. |
|
54
|
|
|
* To call functions and compare their bool responses to determine |
|
55
|
|
|
* if the user can proceed with the request or not. |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $functions |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function check(array $functions) |
|
62
|
|
|
{ |
|
63
|
|
|
$orIndicator = '|'; |
|
64
|
|
|
$returns = []; |
|
65
|
|
|
|
|
66
|
|
|
// iterate all functions in the array |
|
67
|
|
|
foreach ($functions as $function) { |
|
68
|
|
|
|
|
69
|
|
|
// in case the value doesn't contains a separator (single function per key) |
|
70
|
|
|
if (!strpos($function, $orIndicator)) { |
|
71
|
|
|
// simply call the single function and store the response. |
|
72
|
|
|
$returns[] = $this->{$function}(); |
|
73
|
|
|
} else { |
|
74
|
|
|
// in case the value contains a separator (multiple functions per key) |
|
75
|
|
|
$orReturns = []; |
|
76
|
|
|
|
|
77
|
|
|
// iterate over each function in the key |
|
78
|
|
|
foreach (explode($orIndicator, $function) as $orFunction) { |
|
79
|
|
|
// dynamically call each function |
|
80
|
|
|
$orReturns[] = $this->{$orFunction}(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// if in_array returned `true` means at least one function returned `true` thus return `true` to allow access. |
|
84
|
|
|
// if in_array returned `false` means no function returned `true` thus return `false` to prevent access. |
|
85
|
|
|
// return single boolean for all the functions found inside the same key. |
|
86
|
|
|
$returns[] = in_array(true, $orReturns) ? true : false; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// if in_array returned `true` means a function returned `false` thus return `false` to prevent access. |
|
91
|
|
|
// if in_array returned `false` means all functions returned `true` thus return `true` to allow access. |
|
92
|
|
|
// return the final boolean |
|
93
|
|
|
return in_array(false, $returns) ? false : true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* apply validation rules to the ID's in the URL, since Laravel |
|
98
|
|
|
* doesn't validate them by default! |
|
99
|
|
|
* |
|
100
|
|
|
* Now you can use validation riles like this: `'id' => 'required|integer|exists:items,id'` |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $requestData |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
private function mergeUrlParametersWithRequestData(Array $requestData) |
|
107
|
|
|
{ |
|
108
|
|
|
if (isset($this->urlParameters) && !empty($this->urlParameters)) { |
|
109
|
|
|
foreach ($this->urlParameters as $param) { |
|
|
|
|
|
|
110
|
|
|
$requestData[$param] = $this->route($param); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $requestData; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $user |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
private function hasAnyPermissionAccess($user) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
if (!array_key_exists('permissions', $this->access) || !$this->access['permissions']) { |
|
|
|
|
|
|
125
|
|
|
return []; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$permissions = explode('|', $this->access['permissions']); |
|
129
|
|
|
|
|
130
|
|
|
$hasAccess = array_map(function ($permission) use ($user) { |
|
131
|
|
|
// Note: internal return |
|
132
|
|
|
return $user->hasPermissionTo($permission); |
|
133
|
|
|
}, $permissions); |
|
134
|
|
|
|
|
135
|
|
|
return $hasAccess; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param $user |
|
140
|
|
|
* |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
View Code Duplication |
private function hasAnyRoleAccess($user) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
if (!array_key_exists('roles', $this->access) || !$this->access['roles']) { |
|
146
|
|
|
return []; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$roles = explode('|', $this->access['roles']); |
|
150
|
|
|
|
|
151
|
|
|
$hasAccess = array_map(function ($role) use ($user) { |
|
152
|
|
|
// Note: internal return |
|
153
|
|
|
return $user->hasRole($role); |
|
154
|
|
|
}, $roles); |
|
155
|
|
|
|
|
156
|
|
|
return $hasAccess; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.