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