diff --git a/CHANGES.md b/CHANGES.md index 18cf36d..c6c1ec4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php index c7a06b6..4931352 100644 --- a/resources/lang/en/errors.php +++ b/resources/lang/en/errors.php @@ -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"', ]; diff --git a/src/Middleware/VerifyRole.php b/src/Middleware/VerifyRole.php index 6febd36..fe7558d 100644 --- a/src/Middleware/VerifyRole.php +++ b/src/Middleware/VerifyRole.php @@ -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 @@ -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) + ])); } }