Skip to content

Instantly share code, notes, and snippets.

@mortenscheel
Created April 24, 2024 13:00
Show Gist options
  • Save mortenscheel/c23700b68e6c31c68732a6116d9465b4 to your computer and use it in GitHub Desktop.
Save mortenscheel/c23700b68e6c31c68732a6116d9465b4 to your computer and use it in GitHub Desktop.
Bouncer ACL with fallback to relation's abilities
<?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));
}
}
<?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