Created
April 24, 2024 13:00
-
-
Save mortenscheel/c23700b68e6c31c68732a6116d9465b4 to your computer and use it in GitHub Desktop.
Bouncer ACL with fallback to relation's abilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\CustomClipboard; | |
use Illuminate\Cache\ArrayStore; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
public function boot(): void | |
{ | |
// Make bouncer use the custom clipboard | |
\Bouncer::setClipboard(new CustomClipboard(new ArrayStore)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use App\Models\User; | |
use Illuminate\Database\Eloquent\Model; | |
use Silber\Bouncer\CachedClipboard; | |
class CustomClipboard extends CachedClipboard | |
{ | |
public function checkGetId(Model $authority, $ability, $model = null) | |
{ | |
$found = parent::checkGetId($authority, $ability, $model); | |
// If neither permitted or forbidden, fallback to the Customer's abilities | |
if ($found === null && $authority instanceof User) { | |
$found = $this->checkGetId($authority->customer, $ability, $model); | |
} | |
return $found; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment