Skip to content

Commit

Permalink
feat: added messages
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaxlexx committed Jun 14, 2021
1 parent 454a338 commit bcfa514
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions config/laratrust.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,15 @@
|
*/
'magic_can_method_case' => 'kebab_case',

/*
|--------------------------------------------------------------------------
| Display the Roles/Permissions being checked
|--------------------------------------------------------------------------
|
| If true, the roles or permissions being checked will be included
| in the abort message.
|
*/
'display_roles_permissions_being_checked' => true,
];
14 changes: 13 additions & 1 deletion src/Middleware/LaratrustMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
class LaratrustMiddleware
{
const DELIMITER = '|';
protected $roleOrPermissionBeingChecked = [];
protected $isRoleOrPermission;

/**
* Check if the request has authorization to continue.
Expand All @@ -31,6 +33,9 @@ protected function authorization($type, $rolesPermissions, $team, $options)
$rolesPermissions = explode(self::DELIMITER, $rolesPermissions);
}

$this->roleOrPermissionBeingChecked = $rolesPermissions;
$this->isRoleOrPermission = $type;

return !Auth::guard($guard)->guest()
&& Auth::guard($guard)->user()->$method($rolesPermissions, $team, $requireAll);
}
Expand All @@ -46,8 +51,15 @@ protected function unauthorized()
$handler = Config::get("laratrust.middleware.handlers.{$handling}");

if ($handling == 'abort') {
$shouldDisplayRolesPermissionsBeingChecked = Config::get("laratrust.display_roles_permissions_being_checked");

$defaultMessage = 'User does not have any of the necessary access rights.';
return App::abort($handler['code'], $handler['message'] ?? $defaultMessage);
$message = $handler['message'] ?? $defaultMessage;

if($shouldDisplayRolesPermissionsBeingChecked)
$message .= " | " . ucwords($this->isRoleOrPermission) . ": " . implode(", ", $this->roleOrPermissionBeingChecked);

return App::abort($handler['code'], $message);
}

$redirect = Redirect::to($handler['url']);
Expand Down

0 comments on commit bcfa514

Please sign in to comment.