|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Port\Request\Abstracts; |
|
4
|
|
|
|
|
5
|
|
|
use App\Containers\User\Models\User; |
|
6
|
|
|
use App\Port\Exception\Exceptions\IncorrectIdException; |
|
7
|
|
|
use App\Port\Exception\Exceptions\ValidationFailedException; |
|
8
|
|
|
use App\Port\HashId\Traits\HashIdTrait; |
|
9
|
|
|
use Illuminate\Contracts\Validation\Validator; |
|
10
|
|
|
use Illuminate\Foundation\Http\FormRequest as LaravelFrameworkRequest; |
|
11
|
|
|
use Illuminate\Support\Facades\Config; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Request |
|
15
|
|
|
* |
|
16
|
|
|
* A.K.A (app/Http/Requests/Request.php) |
|
17
|
|
|
* |
|
18
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class Request extends LaravelFrameworkRequest |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
use HashIdTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This function will be called from the Requests (authorize) to check if a user |
|
27
|
|
|
* has permission to perform an action. |
|
28
|
|
|
* User can set multiple permissions (separated with "|") and if the user has |
|
29
|
|
|
* any of the permissions, he will be authorize to proceed with this action. |
|
30
|
|
|
* |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public function hasAccess(User $user = null) |
|
34
|
|
|
{ |
|
35
|
|
|
if(!$user){ |
|
36
|
|
|
$user = $this->user(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// $this->access is optionally set on the Request |
|
40
|
|
|
|
|
41
|
|
|
// allow access when the access is not defined |
|
42
|
|
|
// allow access when nothing no roles or permissions are declared |
|
43
|
|
|
if (!isset($this->access) || !isset($this->access['permission'])) { |
|
|
|
|
|
|
44
|
|
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// allow access if has permission set but is empty or null |
|
48
|
|
|
if (!$this->access['permission']) { |
|
|
|
|
|
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$permissions = explode('|', $this->access['permission']); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$hasAccess = array_map(function ($permission) use ($user){ |
|
55
|
|
|
return $user->hasPermissionTo($permission); |
|
56
|
|
|
}, $permissions); |
|
57
|
|
|
|
|
58
|
|
|
// allow access if user has access to any of the defined permissions. |
|
59
|
|
|
return in_array(true, $hasAccess); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Overriding this function to modify the any user input before |
|
64
|
|
|
* applying the validation rules. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public function all() |
|
69
|
|
|
{ |
|
70
|
|
|
$requestData = parent::all(); |
|
71
|
|
|
|
|
72
|
|
|
// the hash ID feature must be enabled to use this decoder feature. |
|
73
|
|
|
if (Config::get('hello.hash-id') && isset($this->decode) && !empty($this->decode)) { |
|
|
|
|
|
|
74
|
|
|
$requestData = $this->decodeHashedIdsBeforeValidatingThem($requestData); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (isset($this->applyRulesOn) && !empty($this->applyRulesOn)) { |
|
|
|
|
|
|
78
|
|
|
$requestData = $this->applyValidationRulesToUrlParams($requestData); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $requestData; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* without decoding the encoded ID's you won't be able to use |
|
86
|
|
|
* validation features like `exists:table,id` |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $requestData |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
private function decodeHashedIdsBeforeValidatingThem(Array $requestData) |
|
93
|
|
|
{ |
|
94
|
|
|
foreach ($this->decode as $id) { |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
if (isset($requestData[$id])) { |
|
97
|
|
|
// validate the user is not trying to pass real ID |
|
98
|
|
|
if (is_int($requestData[$id])) { |
|
99
|
|
|
throw new IncorrectIdException('Only Hashed ID\'s allowed to be passed.'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// perform the decoding |
|
103
|
|
|
$requestData[$id] = $this->decodeThisId($requestData[$id]); |
|
104
|
|
|
} // do nothing if the input is incorrect, because what if it's not required! |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $requestData; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* apply validation rules to the ID's in the URL, since Laravel |
|
112
|
|
|
* doesn't validate them by default! |
|
113
|
|
|
* |
|
114
|
|
|
* Now you can use validation riles like this: `'id' => 'required|integer|exists:items,id'` |
|
115
|
|
|
* |
|
116
|
|
|
* @param array $requestData |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
|
|
private function applyValidationRulesToUrlParams(Array $requestData) |
|
121
|
|
|
{ |
|
122
|
|
|
foreach ($this->applyRulesOn as $param) { |
|
|
|
|
|
|
123
|
|
|
$requestData[$param] = $this->route($param); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $requestData; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Overriding this function to throw a custom |
|
131
|
|
|
* exception instead of the default Laravel exception. |
|
132
|
|
|
* |
|
133
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
|
134
|
|
|
* |
|
135
|
|
|
* @return mixed|void |
|
136
|
|
|
*/ |
|
137
|
|
|
public function failedValidation(Validator $validator) |
|
138
|
|
|
{ |
|
139
|
|
|
throw new ValidationFailedException($validator->getMessageBag()); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.