rinvex / tmp-josephsilber-bouncer
优雅的Eloquent角色和权限。
Requires
- php: ^8.1.0
- illuminate/auth: ^10.0.0 || ^11.0.0
- illuminate/cache: ^10.0.0 || ^11.0.0
- illuminate/container: ^10.0.0 || ^11.0.0
- illuminate/contracts: ^10.0.0 || ^11.0.0
- illuminate/database: ^10.0.0 || ^11.0.0
Requires (Dev)
- illuminate/console: ^10.0.0 || ^11.0.0
- illuminate/events: ^10.0.0 || ^11.0.0
- larapack/dd: ^1.1
- mockery/mockery: ^1.6.0
- phpunit/phpunit: ^10.1.0
Suggests
- illuminate/console: Allows running the bouncer:clean artisan command
- illuminate/events: Required for multi-tenancy support
- dev-master
- v1.1.1
- v1.1.0
- 1.0.x-dev
- v1.0.1
- v1.0.0
- v1.0.0-rc.15
- v1.0.0-rc.14
- v1.0.0-rc.13
- v1.0.0-rc.12
- v1.0.0-rc.11
- v1.0.0-rc.10
- v1.0.0-rc.9
- v1.0.0-rc.8
- v1.0.0-rc.7
- v1.0.0-rc.6
- v1.0.0-rc.5
- v1.0.0-rc.4
- v1.0.0-rc.3
- v1.0.0-rc.2
- v1.0.0-rc.1
- v1.0.0-beta.5
- v1.0.0-beta.4
- v1.0.0-beta.3
- v1.0.0-beta.2
- v1.0.0-beta.1
- v1.0.0-alpha.3
- v1.0.0-alpha.2
- v1.0.0-alpha.1
- 0.1.x-dev
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.26
- v0.0.25
- v0.0.24
- v0.0.23
- v0.0.22
- v0.0.21
- v0.0.20
- v0.0.19
- v0.0.18
- v0.0.17
- v0.0.16
- v0.0.15
- v0.0.14
- v0.0.13
- v0.0.12
- v0.0.11
- v0.0.10
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-develop
- dev-imgbot
This package is auto-updated.
Last update: 2024-07-31 00:19:59 UTC
README
Bouncer
Bouncer是一种优雅且与框架无关的方法,用于管理任何使用Eloquent模型的应用程序的角色和权限。
目录
简介
Bouncer是一种优雅且与框架无关的方法,用于管理任何使用Eloquent模型的应用程序的角色和权限。它具有表达性和流畅的语法,尽可能少地打扰你:当你需要时使用它,不需要时忽略它。
要快速查看Bouncer的功能列表,请查看速查表。
Bouncer可以很好地与其他你自己在应用程序中硬编码的权限一起工作。你的代码始终具有优先权:如果你的代码允许执行操作,Bouncer不会干预。
安装后,你可以简单地告诉守门员你想要允许什么
// Give a user the ability to create posts Bouncer::allow($user)->to('create', Post::class); // Alternatively, do it through a role Bouncer::allow('admin')->to('create', Post::class); Bouncer::assign('admin')->to($user); // You can also grant an ability only to a specific model Bouncer::allow($user)->to('edit', $post);
当你检查Laravel守门员的权限时,Bouncer会自动被咨询。如果Bouncer看到一个已授予当前用户(无论直接还是通过角色)的权限,它将授权检查。
安装
注意:Bouncer需要PHP 7.2+和Laravel/Eloquent 6.0+
如果你没有更新,请使用Bouncer RC6。它支持回退到PHP 5.5 & Laravel 5.1,并且没有已知的错误。
在Laravel应用程序中安装Bouncer
-
使用composer安装Bouncer
composer require silber/bouncer
-
将Bouncer的特质添加到你的用户模型中
use Silber\Bouncer\Database\HasRolesAndAbilities; class User extends Model { use HasRolesAndAbilities; }
-
现在,要运行Bouncer的迁移。首先通过运行以下命令将迁移发布到应用程序的
migrations
目录php artisan vendor:publish --tag="bouncer.migrations"
-
最后,运行迁移
php artisan migrate
外观
每次你在代码中使用Bouncer
外观时,请记住在文件顶部添加以下行到命名空间导入中
use Bouncer;
有关 Laravel Facades 的更多信息,请参阅 Laravel 文档。
在非Laravel应用程序中安装Bouncer
-
使用composer安装Bouncer
composer require silber/bouncer
-
使用 Eloquent Capsule 组件 设置数据库。
use Illuminate\Database\Capsule\Manager as Capsule; $capsule = new Capsule; $capsule->addConnection([/* connection config */]); $capsule->setAsGlobal();
有关详细信息,请参阅 Eloquent Capsule 文档。
-
可以通过以下任一方法运行迁移
-
将Bouncer的特质添加到你的用户模型中
use Illuminate\Database\Eloquent\Model; use Silber\Bouncer\Database\HasRolesAndAbilities; class User extends Model { use HasRolesAndAbilities; }
-
创建 Bouncer 实例
use Silber\Bouncer\Bouncer; $bouncer = Bouncer::create(); // If you are in a request with a current user // that you'd wish to check permissions for, // pass that user to the "create" method: $bouncer = Bouncer::create($user);
如果您在应用程序中使用依赖注入,可以在容器中将
Bouncer
实例注册为单例。use Silber\Bouncer\Bouncer; use Illuminate\Container\Container; Container::getInstance()->singleton(Bouncer::class, function () { return Bouncer::create(); });
现在您可以将
Bouncer
注入任何需要它的类。create
方法使用合理的默认值创建Bouncer
实例。要完全自定义,请使用make
方法获取工厂实例。在工厂上调用create()
以创建Bouncer
实例。use Silber\Bouncer\Bouncer; $bouncer = Bouncer::make() ->withCache($customCacheInstance) ->create();
查看
Factory
类 以查看所有可用的自定义选项。
启用缓存
默认情况下,Bouncer 的查询将缓存到当前请求。为了更好的性能,您可能想要 启用跨请求缓存。
使用方法
将角色和权限添加到用户非常简单。您无需事先创建角色或权限。只需传递角色/权限的名称,如果不存在,Bouncer 将创建它。
注意:以下所有示例都使用了
Bouncer
Facade。如果您不使用 Facade,则可以将Silber\Bouncer\Bouncer
实例注入到您的类中。
创建角色和权限
让我们创建一个名为 admin
的角色,并给它添加从我们网站上 ban-users
的权限
Bouncer::allow('admin')->to('ban-users');
就是这样。在幕后,Bouncer 将为您创建 Role
模型和 Ability
模型。
如果您想向角色/权限添加其他属性,例如可读性强的标题,您可以使用 Bouncer
类上的 role
和 ability
方法手动创建它们。
$admin = Bouncer::role()->firstOrCreate([ 'name' => 'admin', 'title' => 'Administrator', ]); $ban = Bouncer::ability()->firstOrCreate([ 'name' => 'ban-users', 'title' => 'Ban users', ]); Bouncer::allow($admin)->to($ban);
将角色分配给用户
现在,要给用户分配 admin
角色,只需告诉 Bouncer 给定的用户应分配管理员角色
Bouncer::assign('admin')->to($user);
或者,您可以直接在用户上调用 assign
方法
$user->assign('admin');
直接赋予用户权限
有时您可能想直接给用户分配权限,而不使用角色
Bouncer::allow($user)->to('ban-users');
同样,您可以直接从用户处完成此操作
$user->allow('ban-users');
限制模型上的权限
有时您可能想将权限限制为特定模型类型。只需传递模型名称作为第二个参数
Bouncer::allow($user)->to('edit', Post::class);
如果您想将权限限制为特定的模型实例,请传递实际的模型
Bouncer::allow($user)->to('edit', $post);
允许用户或角色“拥有”模型
使用 toOwn
方法允许用户管理 他们自己的 模型
Bouncer::allow($user)->toOwn(Post::class);
现在,当在网关上检查用户是否可以执行对给定帖子的操作时,帖子的 user_id
将与登录用户的 id
(这可以自定义)进行比较。如果它们匹配,网关将允许操作。
上面的操作将授予用户“拥有”的模型上的所有权限。您可以通过调用 to
方法来限制权限
Bouncer::allow($user)->toOwn(Post::class)->to('view'); // Or pass it an array of abilities: Bouncer::allow($user)->toOwn(Post::class)->to(['view', 'update']);
您还可以允许用户拥有应用程序中所有 类型 的模型
Bouncer::allow($user)->toOwnEverything(); // And to restrict ownership to a given ability Bouncer::allow($user)->toOwnEverything()->to('view');
从用户撤回角色
守门人还可以从用户撤回之前分配的角色
Bouncer::retract('admin')->from($user);
或者直接在用户上操作
$user->retract('admin');
移除权限
守门人还可以从用户撤回之前授予的能力
Bouncer::disallow($user)->to('ban-users');
或者直接在用户上操作
$user->disallow('ban-users');
注意:如果用户拥有允许其
封禁用户
的角色,他们仍然会拥有那个能力。要禁止它,要么从角色中移除该能力,要么从用户撤回该角色。
如果该能力是通过角色授予的,告诉守门人从角色中移除该能力
Bouncer::disallow('admin')->to('ban-users');
要移除特定模型类型的能力,传递其名称作为第二个参数
Bouncer::disallow($user)->to('delete', Post::class);
警告:如果用户拥有删除特定
$post
实例的能力,上面的代码将不会移除该能力。您必须单独移除该能力 - 通过传递实际的$post
作为第二个参数,如下所示。
要移除特定模型实例的能力,传递实际的模型
Bouncer::disallow($user)->to('delete', $post);
注意:
disallow
方法仅移除先前授予此用户/角色的能力。如果您想禁止更一般能力所允许的子集,请使用forbid
方法。
禁止权限
守门人还允许您禁止
给定能力,以便进行更细致的控制。有时您可能希望授予用户/角色一个涵盖广泛操作的能力,但随后限制其中一小部分操作。
以下是一些示例
-
您可能允许用户查看所有文档,但有一个特定的机密文档他们不应被允许查看
Bouncer::allow($user)->to('view', Document::class); Bouncer::forbid($user)->to('view', $classifiedDocument);
-
您可能希望允许您的
superadmin
在您的应用程序中做任何事情,包括添加/删除用户。然后您可能有一个可以执行所有操作除了管理用户的admin
角色Bouncer::allow('superadmin')->everything(); Bouncer::allow('admin')->everything(); Bouncer::forbid('admin')->toManage(User::class);
-
您可能希望偶尔封禁用户,移除他们所有能力的权限。但是,实际上移除所有角色和能力意味着当封禁被移除时,我们必须找出他们原来的角色和能力。
使用禁止的能力意味着他们可以保留所有现有的角色和能力,但仍无法获得授权。我们可以通过创建一个特殊的
banned
角色来实现这一点,我们将禁止所有能力Bouncer::forbid('banned')->everything();
然后,每次我们想封禁一个用户时,我们将为他们分配
banned
角色Bouncer::assign('banned')->to($user);
要解除封禁,我们只需从用户撤回该角色
Bouncer::retract('banned')->from($user);
如您所见,守门人的禁止能力为您提供了对应用程序权限的很多细致控制。
解除禁止权限
要移除禁止的能力,使用unforbid
方法
Bouncer::unforbid($user)->to('view', $classifiedDocument);
注意:这将移除任何先前禁止的能力。它不会自动允许该能力,如果它不是由授予此用户/角色的不同常规能力允许。
检查用户的角色
注意:一般来说,您不需要直接检查角色。更好的做法是允许角色一定的能力,然后检查那些能力。如果您需要的是非常一般的,您可以创建非常广泛的能力。例如,
access-dashboard
能力总是比直接检查admin
或editor
角色更好。如果您非常罕见地需要检查角色,该功能在这里可用。
守门人可以检查用户是否具有特定的角色
Bouncer::is($user)->a('moderator');
如果您要检查的角色以元音字母开头,您可能想使用an
别名方法
Bouncer::is($user)->an('admin');
对于相反的情况,您还可以检查用户没有特定的角色
Bouncer::is($user)->notA('moderator'); Bouncer::is($user)->notAn('admin');
您可以检查用户是否具有许多角色之一
Bouncer::is($user)->a('moderator', 'editor');
您还可以检查用户是否具有所有给定的角色
Bouncer::is($user)->all('editor', 'moderator');
您还可以检查用户是否没有任何给定的角色
Bouncer::is($user)->notAn('editor', 'moderator');
这些检查也可以直接在用户上执行
$user->isAn('admin'); $user->isA('subscriber'); $user->isNotAn('admin'); $user->isNotA('subscriber'); $user->isAll('editor', 'moderator');
按角色查询用户
您可以通过用户是否具有给定角色来查询您的用户
$users = User::whereIs('admin')->get();
您还可以传递多个角色,以查询具有 任何 给定角色的用户
$users = User::whereIs('superadmin', 'admin')->get();
要查询具有 所有 给定角色的用户,请使用 whereIsAll
方法
$users = User::whereIsAll('sales', 'marketing')->get();
获取用户的所有角色
您可以直接从用户模型中获取用户的全部角色
$roles = $user->getRoles();
获取用户的所有权限
您可以直接从用户模型中获取用户的全部能力
$abilities = $user->getAbilities();
这将返回一个包含用户允许的能力的集合,包括通过其角色授予给用户的能力。
您还可以获取一个被 明确 禁止的能力列表
$forbiddenAbilities = $user->getForbiddenAbilities();
授权用户
用户授权直接在 Laravel 的 Gate
上处理,或在用户模型上($user->can($ability)
)。
为了方便,Bouncer
类提供了这些透传方法
Bouncer::can($ability); Bouncer::can($ability, $model); Bouncer::canAny($abilities); Bouncer::canAny($abilities, $model); Bouncer::cannot($ability); Bouncer::cannot($ability, $model); Bouncer::authorize($ability); Bouncer::authorize($ability, $model);
这些方法直接调用 Gate
类上的等效方法。
Blade指令
Bouncer 不添加自己的 blade 指令。由于 Bouncer 直接与 Laravel 的 gate 一起工作,因此只需使用其 @can
指令来检查当前用户的权限。
@can ('update', $post) <a href="{{ route('post.update', $post) }}">Edit Post</a> @endcan
由于直接检查角色通常 不推荐,Bouncer 不提供单独的指令来执行该操作。如果您仍然坚持检查角色,可以使用通用的 @if
指令来执行。
@if ($user->isAn('admin')) // @endif
刷新缓存
Bouncer 对当前请求执行的查询都将进行缓存。如果您启用 跨请求缓存,则缓存将跨不同的请求持久化。
您可以在需要时完全刷新 bouncer 的缓存
Bouncer::refresh();
注意:如果存在,完全刷新所有用户的缓存将使用 缓存标签。并非所有缓存驱动程序都支持此功能。请参考 Laravel 的文档 以查看您的驱动程序是否支持缓存标签。如果您的驱动程序不支持缓存标签,调用
refresh
可能会稍微慢一些,具体取决于您系统中用户数量。
或者,您可以选择只刷新特定用户的缓存
Bouncer::refreshFor($user);
多租户
Bouncer 完全支持多租户应用程序,允许您无缝地将 Bouncer 的角色和能力集成到同一应用程序中的所有租户。
作用域中间件
要开始,首先将 作用域中间件发布到您的应用程序中
php artisan vendor:publish --tag="bouncer.middleware"
中间件现在将发布到 app/Http/Middleware/ScopeBouncer.php
。此中间件是您告诉 Bouncer 当前请求要使用哪个租户的地方。例如,假设您的用户都具有 account_id
属性,则中间件可能如下所示
public function handle($request, Closure $next) { $tenantId = $request->user()->account_id; Bouncer::scope()->to($tenantId); return $next($request); }
当然,您可以自由修改此中间件以适应您应用程序的需求,例如从子域等位置获取租户信息。
现在,在中间件就位后,请确保在您的 HTTP 核心文件 中注册它
protected $middlewareGroups = [ 'web' => [ // Keep the existing middleware here, and add this: \App\Http\Middleware\ScopeBouncer::class, ] ];
现在,Bouncer 的所有查询都将针对给定的租户进行作用域。
自定义Bouncer的作用域
根据您的应用设置,您可能并不希望所有查询都限制在当前租户范围内。例如,您可能有一组固定的角色/能力,对所有租户都相同,并且只允许用户控制哪些用户被分配哪些角色,以及哪些角色具有哪些能力。为了实现这一点,您可以告诉Bouncer的限定范围只限定Bouncer模型之间的关系,但不限定模型本身。
Bouncer::scope()->to($tenantId)->onlyRelations();
此外,您的应用甚至可能不允许用户控制给定角色具有哪些能力。在这种情况下,告诉Bouncer的限定范围排除角色能力,使这些关系在所有租户中保持全局性。
Bouncer::scope()->to($tenantId)->onlyRelations()->dontScopeRoleAbilities();
如果您的需求比上面所述更加专业,您可以创建自己的Scope
,并添加您需要的任何自定义逻辑。
use Silber\Bouncer\Contracts\Scope; class MyScope implements Scope { // Whatever custom logic your app needs }
然后,在一个服务提供商中注册您的自定义限定范围。
Bouncer::scope(new MyScope);
Bouncer将在其执行过程中的多个点上调用Scope
接口的方法。您可以根据您的具体需求自由处理它们。
配置
Bouncer提供了合理的默认设置,所以大多数情况下不需要任何配置。为了更细粒度的控制,可以通过调用Bouncer
类的各种配置方法来自定义Bouncer。
如果您只使用其中一个或两个配置选项,可以将它们放入主AppServiceProvider
的boot
方法中。如果它们开始增长,您可以在您的app/Providers
目录中创建一个单独的BouncerServiceProvider
类(记得在providers
配置数组中注册它)。
缓存
默认情况下,Bouncer会对当前请求中执行的查询进行缓存。为了更好的性能,您可能希望使用跨请求缓存。
Bouncer::cache();
警告:如果您启用跨请求缓存,您负责在更改用户的角色/能力时刷新缓存。有关如何刷新缓存的信息,请参阅刷新缓存。
相反,有时您可能希望即使在同一请求中,也完全禁用缓存。
Bouncer::dontCache();
这在单元测试中特别有用,当您想要对刚刚授予的角色/能力进行断言时。
自定义模型
您可以轻松扩展Bouncer内建的Role
和Ability
模型。
use Silber\Bouncer\Database\Ability; class MyAbility extends Ability { // custom code }
use Silber\Bouncer\Database\Role; class MyRole extends Role { // custom code }
或者,您可以使用Bouncer的IsAbility
和IsRole
特质,而不必扩展Bouncer的任何模型。
use Illuminate\Database\Eloquent\Model; use Silber\Bouncer\Database\Concerns\IsAbility; class MyAbility extends Model { use IsAbility; // custom code }
use Illuminate\Database\Eloquent\Model; use Silber\Bouncer\Database\Concerns\IsRole; class MyRole extends Model { use IsRole; // custom code }
所有权
Bouncer中使用所有权的概念,以允许用户对其“拥有”的模型执行操作。
默认情况下,Bouncer会检查模型的user_id
与当前用户的主键是否匹配。如果需要,这可以设置为一个不同的属性。
Bouncer::ownedVia('userId');
如果不同的模型使用不同的列来表示所有权,您可以单独注册它们。
Bouncer::ownedVia(Post::class, 'created_by'); Bouncer::ownedVia(Order::class, 'entered_by');
为了有更大的控制权,您可以通过闭包传递您的自定义逻辑。
Bouncer::ownedVia(Game::class, function ($game, $user) { return $game->team_id == $user->team_id; });
常见问题解答
在Bouncer中,有一些概念人们经常询问,所以这里是一个关于这些主题的简要列表。
我在哪里设置应用程序的角色和权限?
可以在常规的Laravel种子类中初始化初始角色和能力。首先为Bouncer创建一个特定的种子文件。
php artisan make:seeder BouncerSeeder
将所有您的种子角色 & 能力代码放置在种子类的run
方法中。以下是一个示例:
use Bouncer; use Illuminate\Database\Seeder; class BouncerSeeder extends Seeder { public function run() { Bouncer::allow('superadmin')->everything(); Bouncer::allow('admin')->everything(); Bouncer::forbid('admin')->toManage(User::class); Bouncer::allow('editor')->to('create', Post::class); Bouncer::allow('editor')->toOwn(Post::class); // etc. } }
要实际运行它,请将播种器的类名传递给db:seed
命令的class
选项
php artisan db:seed --class=BouncerSeeder
我可以在网站的不同部分使用不同的角色和权限集,分别是公共区域和仪表板吗?
Bouncer的scope
可以用来隔离网站的不同部分,为每个部分创建一个带有自己角色和能力的隔离区
-
创建一个接受
$identifier
并将其设置为当前范围的ScopeBouncer
中间件use Bouncer, Closure; class ScopeBouncer { public function handle($request, Closure $next, $identifier) { Bouncer::scope()->to($identifier); return $next($request); } }
-
将此新中间件注册为路由中间件,在你的HTTP内核类中
protected $routeMiddleware = [ // Keep the other route middleware, and add this: 'scope-bouncer' => \App\Http\Middleware\ScopeBouncer::class, ];
-
在你的路由服务提供者中,分别对公开路由和仪表板路由应用此中间件,使用不同的标识符
Route::middleware(['web', 'scope-bouncer:1']) ->namespace($this->namespace) ->group(base_path('routes/public.php')); Route::middleware(['web', 'scope-bouncer:2']) ->namespace($this->namespace) ->group(base_path('routes/dashboard.php'));
就是这样。现在,你的网站每个部分的所有角色和能力都将被单独隔离。要微调隔离范围,请参阅自定义Bouncer的范围。
我尝试运行迁移,但得到一个SQL错误,表明“指定的键太长”
从Laravel 5.4开始,默认的数据库字符集现在是utf8mb4
。如果你正在使用Laravel 5.4+的某些旧版数据库(MySQL低于5.7.7,或MariaDB低于10.2.2),当你尝试在字符串列上创建索引时,你会得到一个SQL错误。为了解决这个问题,请更改你的AppServiceProvider
中的Laravel默认字符串长度
use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); }
你可以在这篇文章中了解更多信息。
我尝试运行迁移,但得到一个SQL错误,表明存在“语法错误或访问违规:1064 ... 在 near 'json not null)' 时出错”
JSON列是MySQL(5.7.8)和MariaDB(10.2.7)相对较新的功能。如果你使用的是这些数据库的旧版本,你不能使用JSON列。
最好的解决方案是升级你的数据库。如果目前无法升级,你可以更改你已发布的迁移文件,以使用text
列代替
- $table->json('options')->nullable(); + $table->text('options')->nullable();
控制台命令
bouncer:clean
bouncer:clean
命令删除未使用的权限。运行此命令将删除2种类型的未使用权限
-
未分配的权限 - 没有人分配的权限。例如
Bouncer::allow($user)->to('view', Plan::class); Bouncer::disallow($user)->to('view', Plan::class);
此时,“查看计划”的权限没有分配给任何人,所以它将被删除。
注意:根据你应用程序的上下文,你可能不希望删除这些。如果你允许用户在你的应用程序UI中管理权限,你可能不希望删除未分配的权限。请参阅下面的内容。
-
孤儿权限 - 模型权限,其模型已被删除
Bouncer::allow($user)->to('delete', $plan); $plan->delete();
由于该计划不再存在,该权限不再有任何用处,所以它将被删除。
如果你想只删除一种未使用的权限,请使用以下标志之一运行它
php artisan bouncer:clean --unassigned
php artisan bouncer:clean --orphaned
如果你不传递任何标志,它将删除两种类型的未使用权限。
要定期自动运行此命令,请将其添加到你的控制台内核的计划
$schedule->command('bouncer:clean')->weekly();
速查表
// Adding abilities for users Bouncer::allow($user)->to('ban-users'); Bouncer::allow($user)->to('edit', Post::class); Bouncer::allow($user)->to('delete', $post); Bouncer::allow($user)->everything(); Bouncer::allow($user)->toManage(Post::class); Bouncer::allow($user)->toManage($post); Bouncer::allow($user)->to('view')->everything(); Bouncer::allow($user)->toOwn(Post::class); Bouncer::allow($user)->toOwnEverything(); // Removing abilities uses the same syntax, e.g. Bouncer::disallow($user)->to('delete', $post); Bouncer::disallow($user)->toManage(Post::class); Bouncer::disallow($user)->toOwn(Post::class); // Adding & removing abilities for roles Bouncer::allow('admin')->to('ban-users'); Bouncer::disallow('admin')->to('ban-users'); // You can also forbid specific abilities with the same syntax... Bouncer::forbid($user)->to('delete', $post); // And also remove a forbidden ability with the same syntax... Bouncer::unforbid($user)->to('delete', $post); // Re-syncing a user's abilities Bouncer::sync($user)->abilities($abilities); // Assigning & retracting roles from users Bouncer::assign('admin')->to($user); Bouncer::retract('admin')->from($user); // Re-syncing a user's roles Bouncer::sync($user)->roles($roles); // Checking the current user's abilities $boolean = Bouncer::can('ban-users'); $boolean = Bouncer::can('edit', Post::class); $boolean = Bouncer::can('delete', $post); $boolean = Bouncer::cannot('ban-users'); $boolean = Bouncer::cannot('edit', Post::class); $boolean = Bouncer::cannot('delete', $post); // Checking a user's roles $boolean = Bouncer::is($user)->a('subscriber'); $boolean = Bouncer::is($user)->an('admin'); $boolean = Bouncer::is($user)->notA('subscriber'); $boolean = Bouncer::is($user)->notAn('admin'); $boolean = Bouncer::is($user)->a('moderator', 'editor'); $boolean = Bouncer::is($user)->all('moderator', 'editor'); Bouncer::cache(); Bouncer::dontCache(); Bouncer::refresh(); Bouncer::refreshFor($user);
其中一些功能也直接在用户模型上可用
$user->allow('ban-users'); $user->allow('edit', Post::class); $user->allow('delete', $post); $user->disallow('ban-users'); $user->disallow('edit', Post::class); $user->disallow('delete', $post); $user->assign('admin'); $user->retract('admin'); $boolean = $user->isAn('admin'); $boolean = $user->isAn('editor', 'moderator'); $boolean = $user->isAll('moderator', 'editor'); $boolean = $user->isNotAn('admin', 'moderator'); // Querying users by their roles $users = User::whereIs('superadmin')->get(); $users = User::whereIs('superadmin', 'admin')->get(); $users = User::whereIsAll('sales', 'marketing')->get(); $abilities = $user->getAbilities(); $forbidden = $user->getForbiddenAbilities();
替代
在Spatie慷慨赠予社区的大量包中,你会发现优秀的laravel-permission包。像Bouncer一样,它与Laravel内置的门和权限检查很好地集成,但在语法、数据库结构和功能方面有不同的设计选择。
许可证
跳闸(Bouncer)是开源软件,许可协议为MIT许可证