Skip to content

Commit

Permalink
feat(request)!: remove check method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Feb 7, 2025
1 parent 9f3e1f2 commit 7d80dec
Show file tree
Hide file tree
Showing 12 changed files with 11 additions and 72 deletions.
39 changes: 0 additions & 39 deletions src/Abstract/Requests/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,45 +145,6 @@ protected function hasAnyRoleAccess($user): array
return array_map(static fn ($role) => $user->hasRole($role), $roles);
}

/**
* Used from the `authorize` function if the Request class.
* To call functions and compare their bool responses to determine
* if the user can proceed with the request or not.
*/
protected function check(array $functions): bool
{
$orIndicator = '|';
$returns = [];

// iterate all functions in the array
foreach ($functions as $function) {
// in case the value doesn't contain a separator (single function per key)
if (!strpos((string) $function, $orIndicator)) {
// simply call the single function and store the response.
$returns[] = $this->{$function}();
} else {
// in case the value contains a separator (multiple functions per key)
$orReturns = [];

// iterate over each function in the key
foreach (explode($orIndicator, (string) $function) as $orFunction) {
// dynamically call each function
$orReturns[] = $this->{$orFunction}();
}

// if in_array returned `true` means at least one function returned `true` thus return `true` to allow access.
// if in_array returned `false` means no function returned `true` thus return `false` to prevent access.
// return single boolean for all the functions found inside the same key.
$returns[] = in_array(true, $orReturns, true);
}
}

// if in_array returned `true` means a function returned `false` thus return `false` to prevent access.
// if in_array returned `false` means all functions returned `true` thus return `true` to allow access.
// return the final boolean
return !in_array(false, $returns, true);
}

public function route($param = null, $default = null)
{
if (in_array($param, $this->decode, true) && config('apiato.hash-id')) {
Expand Down
4 changes: 1 addition & 3 deletions src/Generator/Stubs/requests/create.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class {{class-name}} extends ParentRequest

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}
4 changes: 1 addition & 3 deletions src/Generator/Stubs/requests/delete.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class {{class-name}} extends ParentRequest

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}
4 changes: 1 addition & 3 deletions src/Generator/Stubs/requests/edit.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class {{class-name}} extends ParentRequest

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}
4 changes: 1 addition & 3 deletions src/Generator/Stubs/requests/find.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class {{class-name}} extends ParentRequest

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}
4 changes: 1 addition & 3 deletions src/Generator/Stubs/requests/generic.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class {{class-name}} extends ParentRequest

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}
4 changes: 1 addition & 3 deletions src/Generator/Stubs/requests/list.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class {{class-name}} extends ParentRequest

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}
4 changes: 1 addition & 3 deletions src/Generator/Stubs/requests/store.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class {{class-name}} extends ParentRequest

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}
4 changes: 1 addition & 3 deletions src/Generator/Stubs/requests/update.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class {{class-name}} extends ParentRequest

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public function rules(): array

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public function rules(): array

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public function rules(): array

public function authorize(): bool
{
return $this->check([
'hasAccess',
]);
return $this->hasAccess();
}
}

0 comments on commit 7d80dec

Please sign in to comment.