diff --git a/README.md b/README.md index acd4a02..876777a 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,9 @@ composer require kodeine/laravel-acl "^1.0" $ php artisan vendor:publish --provider="Kodeine\Acl\AclServiceProvider" ``` -> **Use your own models.** -> Once you publish, it publishes the configuration file where you can define your own models which should extend to Acl models. +> **Once you publish, it publishes the configuration file where you can:** +> - **Use your own models**: Define your own models which should extend to Acl models. +> - **Use your own guard**: Define a guard other than laravel default for user recovery. 4. Add the middleware to your `app/Http/Kernel.php`. @@ -100,6 +101,9 @@ Here's the TODO list for the next release. # Change Logs +**June 14 2022** +* [x] Added support for different guard + **September 14 2019** * [x] Updated the readme to reflect new major release diff --git a/src/Kodeine/Acl/AclServiceProvider.php b/src/Kodeine/Acl/AclServiceProvider.php index d64f7d7..3cd8c72 100644 --- a/src/Kodeine/Acl/AclServiceProvider.php +++ b/src/Kodeine/Acl/AclServiceProvider.php @@ -50,9 +50,8 @@ public function publishConfig() public function registerBladeDirectives() { - // role Blade::directive('role', function ($expression) { - return "hasRole({$expression})): ?>"; + return "check() && Auth::guard(config('acl.guard'))->user()->hasRole({$expression})): ?>"; }); Blade::directive('endrole', function () { @@ -61,7 +60,7 @@ public function registerBladeDirectives() // permission Blade::directive('permission', function ($expression) { - return "hasPermission({$expression})): ?>"; + return "check() && Auth::guard(config('acl.guard'))->user()->hasPermission({$expression})): ?>"; }); Blade::directive('endpermission', function () { diff --git a/src/Kodeine/Acl/Middleware/HasPermission.php b/src/Kodeine/Acl/Middleware/HasPermission.php index 0593885..9656ef0 100644 --- a/src/Kodeine/Acl/Middleware/HasPermission.php +++ b/src/Kodeine/Acl/Middleware/HasPermission.php @@ -39,6 +39,7 @@ class HasPermission public function handle($request, Closure $next) { $this->request = $request; + $this->guard = config('acl.guard'); // override crud resources via config $this->crudConfigOverride(); @@ -73,8 +74,9 @@ protected function hasRole() { $request = $this->request; $role = $this->getAction('is'); + $guard = $this->guard; - return ! $this->forbiddenRoute() && $request->user()->hasRole($role); + return ! $this->forbiddenRoute() && $request->user($guard)->hasRole($role); } /** @@ -86,8 +88,9 @@ protected function hasPermission() { $request = $this->request; $do = $this->getAction('can'); + $guard = $this->guard; - return ! $this->forbiddenRoute() && $request->user()->hasPermission($do); + return ! $this->forbiddenRoute() && $request->user($guard)->hasPermission($do); } /** @@ -105,6 +108,9 @@ protected function protectMethods() // protection methods being passed in a route. $methods = $this->getAction('protect_methods'); + // guard from config + $guard = $this->guard; + // get method being called on controller. $caller = $this->parseMethod(); @@ -133,7 +139,7 @@ protected function protectMethods() return $e . '.' . $this->parseAlias(); }, array_keys($crud))); - return ! $this->forbiddenRoute() && $request->user()->hasPermission($permission); + return ! $this->forbiddenRoute() && $request->user($guard)->hasPermission($permission); } private function filterMethods($methods, $callback) { diff --git a/src/config/acl.php b/src/config/acl.php index e296c1b..015d77b 100644 --- a/src/config/acl.php +++ b/src/config/acl.php @@ -44,4 +44,11 @@ */ 'cacheMinutes' => 1, + + /** + * Guard + * Set the guard for user validations. + */ + + 'guard' => config('auth.defaults.guard'), ];