laravel-acl icon indicating copy to clipboard operation
laravel-acl copied to clipboard

Added: Different guard support

Open CodeIgor opened this issue 3 years ago • 2 comments

This fixes handling of a different guard other than application default.

Fixes: #135.

  • Guard parameter on config file to get default;
  /**
   * Guard
   * Set the guard for user validations.
   */
  'guard' => config('auth.defaults.guard'),
  • Added guard on HasPermission handle;
   $this->guard = config('acl.guard');
  • Added $guard = $this->guard on hasRole, hasPermission and protectMethods;

  • Changed $request->user() to $request->user($guard) on hasRole, hasPermission and protectMethods;

  • Changed Blade directives to Blade::if method and guard added;

   // role
   Blade::if('role', function ($expression) {
        return Auth::guard(config('acl.guard'))->check() && Auth::guard(config('acl.guard'))->user()->hasRole($expression);
   });

   // permission
   Blade::if('permission', function ($expression) {
        return Auth::guard(config('acl.guard'))->check() && Auth::guard(config('acl.guard'))->user()->hasPermission($expression);
   });

CodeIgor avatar Jun 10 '22 14:06 CodeIgor

@CodeIgor could you please update the readme file as well to support this update

kodeine avatar Jun 12 '22 04:06 kodeine

@kodeine Sure, feel free to tell me if there is something else needed Update README.md

CodeIgor avatar Jun 14 '22 11:06 CodeIgor

@CodeIgor sorry for very late reply, it just slipped from my todo list.

So, endpermission and endrole was removed, can we make it backwards compatible?

kodeine avatar Mar 01 '23 17:03 kodeine

@kodeine done, i changed the used directive back to how it was but with the guard method call

CodeIgor avatar Mar 14 '23 13:03 CodeIgor

Thank you!

kodeine avatar Mar 14 '23 13:03 kodeine