Skip to content

Commit 316d9ad

Browse files
committed
refactor: Simplify authorize and authorizeOrNotFound functions by removing message parameter
1 parent 243f317 commit 316d9ad

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

src/Helpers/authorization_helper.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,10 @@ function cannot($ability, ...$arguments)
5656
* @throws UnauthorizedPageException If the user is not authorized.
5757
*/
5858
if (! function_exists('authorize')) {
59-
function authorize($ability, $message = 'Unauthorized Action.', ...$arguments): bool
59+
function authorize($ability, ...$arguments): bool
6060
{
61-
if (! is_string($message)) {
62-
array_unshift($arguments, $message);
63-
$message = 'Unauthorized Action.';
64-
}
65-
6661
if (cannot($ability, ...$arguments)) {
67-
throw new UnauthorizedPageException($message);
62+
throw new UnauthorizedPageException('Unauthorized Action.');
6863
}
6964

7065
return true;
@@ -83,15 +78,10 @@ function authorize($ability, $message = 'Unauthorized Action.', ...$arguments):
8378
* @throws PageNotFoundException If the user is not authorized.
8479
*/
8580
if (! function_exists('authorizeOrNotFound')) {
86-
function authorizeOrNotFound($ability, $message = 'Page Not Found.', ...$arguments): bool
81+
function authorizeOrNotFound($ability, ...$arguments): bool
8782
{
88-
if (! is_string($message)) {
89-
array_unshift($arguments, $message);
90-
$message = 'Page Not Found.';
91-
}
92-
9383
if (cannot($ability, ...$arguments)) {
94-
throw PageNotFoundException::forPageNotFound($message);
84+
throw PageNotFoundException::forPageNotFound('Page Not Found.');
9585
}
9686

9787
return true;

src/Traits/AuthorizationTrait.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,10 @@ protected function cannot($ability, ...$arguments)
5353
*
5454
* @throws UnauthorizedPageException If the user is not authorized.
5555
*/
56-
protected function authorize($ability, $message = 'Unauthorized Action.', ...$arguments): bool
56+
protected function authorize($ability, ...$arguments): bool
5757
{
58-
if (! is_string($message)) {
59-
array_unshift($arguments, $message);
60-
$message = 'Unauthorized Action.';
61-
}
62-
6358
if (cannot($ability, ...$arguments)) {
64-
throw new UnauthorizedPageException($message);
59+
throw new UnauthorizedPageException('Unauthorized Action.');
6560
}
6661

6762
return true;
@@ -78,15 +73,10 @@ protected function authorize($ability, $message = 'Unauthorized Action.', ...$ar
7873
*
7974
* @throws PageNotFoundException If the user is not authorized.
8075
*/
81-
public function authorizeOrNotFound($ability, $message = 'Page Not Found.', ...$arguments): bool
76+
public function authorizeOrNotFound($ability, ...$arguments): bool
8277
{
83-
if (! is_string($message)) {
84-
array_unshift($arguments, $message);
85-
$message = 'Page Not Found.';
86-
}
87-
8878
if (cannot($ability, ...$arguments)) {
89-
throw PageNotFoundException::forPageNotFound($message);
79+
throw PageNotFoundException::forPageNotFound('Page Not Found.');
9080
}
9181

9282
return true;

0 commit comments

Comments
 (0)