Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Improve error display
Browse files Browse the repository at this point in the history
  • Loading branch information
populov committed May 12, 2020
1 parent 4680b69 commit f519f5b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes History

1.2.1
-----
Improve error display

1.2.0
-----
Make compatible with Voyager built-in roles. See https://voyager.devdojo.com
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/en/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

return [
'not_logged_in' => 'You must login first',
'role_required' => 'Access Denied: you don\'t have any of required role ":role"',
'role_required' => 'Access Denied: you don\'t have required role :role|Access Denied: you don\'t have any of required roles ":role"',
];
17 changes: 16 additions & 1 deletion src/Middleware/VerifyRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Saritasa\Roles\IHasRole;
use Saritasa\Roles\Models\Role;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class VerifyRole
Expand Down Expand Up @@ -55,6 +57,19 @@ public function handle(Request $request, Closure $next, ...$roles)
}
}

throw new AccessDeniedHttpException(trans('roles::errors.role_required', ['role' => implode(', ', $roles)]));
$labels = [];
foreach ($roles as $role) {
if (is_int($role) || is_string($role) && preg_match('/^\d+$/', $role)) {
$labels[] = Role::find($role)->name ?: $role;
} elseif ($role instanceof Model) {
$labels[] = $role->name;
} else {
$labels[] = $role;
}
}

throw new AccessDeniedHttpException(trans_choice('roles::errors.role_required', count($labels), [
'role' => implode(', ', $labels)
]));
}
}

0 comments on commit f519f5b

Please sign in to comment.