Skip to content

Commit

Permalink
Merge pull request #267 from CodeIgor/master
Browse files Browse the repository at this point in the history
Added:  Different guard support
  • Loading branch information
kodeine authored Mar 14, 2023
2 parents 90c32b9 + 0db6ae4 commit d65a171
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down Expand Up @@ -100,6 +101,9 @@ Here's the TODO list for the next release.

# <a name="change-logs"></a>Change Logs

**June 14 2022**
* [x] Added support for different guard

**September 14 2019**
* [x] Updated the readme to reflect new major release

Expand Down
5 changes: 2 additions & 3 deletions src/Kodeine/Acl/AclServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ public function publishConfig()

public function registerBladeDirectives()
{
// role
Blade::directive('role', function ($expression) {
return "<?php if (Auth::check() && Auth::user()->hasRole({$expression})): ?>";
return "<?php if (Auth::guard(config('acl.guard'))->check() && Auth::guard(config('acl.guard'))->user()->hasRole({$expression})): ?>";
});

Blade::directive('endrole', function () {
Expand All @@ -61,7 +60,7 @@ public function registerBladeDirectives()

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

Blade::directive('endpermission', function () {
Expand Down
12 changes: 9 additions & 3 deletions src/Kodeine/Acl/Middleware/HasPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions src/config/acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@
*/

'cacheMinutes' => 1,

/**
* Guard
* Set the guard for user validations.
*/

'guard' => config('auth.defaults.guard'),
];

0 comments on commit d65a171

Please sign in to comment.