geeky / laravel-permission
这是 Laravel 5.4 及以上版本对 Spatia 权限处理的分支
Requires
- php: >=7.0
- illuminate/auth: ~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/container: ~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/contracts: ~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/database: ~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0
Requires (Dev)
- orchestra/testbench: ~3.4.2|~3.5.0|~3.6.0|~3.7.0
- phpunit/phpunit: ^5.7|6.2|^7.0
- predis/predis: ^1.1
- dev-master
- v3.x-dev
- 3.0.0
- 2.37.0
- 2.36.1
- 2.36.0
- 2.35.0
- 2.34.0
- 2.33.0
- 2.32.0
- 2.31.0
- 2.30.0
- 2.29.0
- 2.28.2
- 2.28.1
- 2.28.0
- 2.27.0
- 2.26.2
- 2.26.1
- 2.26.0
- 2.25.0
- 2.24.0
- 2.23.0
- 2.22.1
- 2.22.0
- 2.21.0
- 2.20.0
- 2.19.2
- 2.19.1
- 2.19.0
- 2.18.0
- 2.17.1
- 2.17.0
- 2.16.0
- 2.15.0
- 2.14.0
- 2.13.0
- 2.12.2
- 2.12.1
- 2.12.0
- 2.11.0
- 2.10.0
- 2.9.2
- 2.9.1
- 2.9.0
- 2.8.2
- 2.8.1
- 2.8.0
- 2.7.9
- 2.7.8
- 2.7.7
- 2.7.6
- 2.7.5
- 2.7.4
- 2.7.3
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.0
- 2.5.4
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.0.2
- 2.0.1
- 2.0.0
- v1.x-dev
- 1.18.0
- 1.17.0
- 1.16.1
- 1.16.0
- 1.15.0
- 1.14.1
- 1.14.0
- 1.13.0
- 1.12.0
- 1.11.1
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.1
- dev-analysis-8QQ4Jo
- dev-fix-guard-checks
This package is auto-updated.
Last update: 2024-09-29 05:02:38 UTC
README
赞助商
此包允许您在数据库中管理用户权限和角色。
安装后,您可以执行如下操作:
// Adding permissions to a user $user->givePermissionTo('edit articles'); // Adding permissions via a role $user->assignRole('writer'); $role->givePermissionTo('edit articles');
如果您使用多个守卫,我们也有相应的支持。每个守卫都有自己的权限和角色集,可以分配给守卫的用户。有关详细信息,请参阅README中的使用多个守卫部分。
由于所有权限都将注册在Laravel 的 gate上,您可以使用 Laravel 的默认can
函数检查用户是否有权限。
$user->can('edit articles');
Spatie 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述这里。
安装
Laravel
此包适用于 Laravel 5.4 或更高版本。如果您正在使用 Laravel 的旧版本,请参阅此包的 v1 分支。
您可以通过 composer 安装此包
composer require geeky/laravel-permission
服务提供程序将自动注册。或者,您可以在您的config/app.php
文件中手动添加服务提供程序
'providers' => [ // ... Spatie\Permission\PermissionServiceProvider::class, ];
您可以使用以下命令发布create_permission_tables.php
迁移:
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations"
如果您正在使用User
模型的 UUID 或 GUID,您可以更新create_permission_tables.php
迁移,将$table->unsignedBigInteger($columnNames['model_morph_key'])
替换为$table->uuid($columnNames['model_morph_key'])
。为了保持一致性,您还可以更新包配置文件,使用model_uuid
列名而不是默认的model_id
列名。
迁移发布后,您可以通过运行迁移来创建角色和权限表:
php artisan migrate
您可以使用以下命令发布配置文件:
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"
发布后,config/permission.php配置文件包含以下内容:
return [ 'models' => [ /* * When using the "HasPermissions" trait from this package, we need to know which * Eloquent model should be used to retrieve your permissions. Of course, it * is often just the "Permission" model but you may use whatever you like. * * The model you want to use as a Permission model needs to implement the * `Spatie\Permission\Contracts\Permission` contract. */ 'permission' => Spatie\Permission\Models\Permission::class, /* * When using the "HasRoles" trait from this package, we need to know which * Eloquent model should be used to retrieve your roles. Of course, it * is often just the "Role" model but you may use whatever you like. * * The model you want to use as a Role model needs to implement the * `Spatie\Permission\Contracts\Role` contract. */ 'role' => Spatie\Permission\Models\Role::class, ], 'table_names' => [ /* * When using the "HasRoles" trait from this package, we need to know which * table should be used to retrieve your roles. We have chosen a basic * default value but you may easily change it to any table you like. */ 'roles' => 'roles', /* * When using the "HasPermissions" trait from this package, we need to know which * table should be used to retrieve your permissions. We have chosen a basic * default value but you may easily change it to any table you like. */ 'permissions' => 'permissions', /* * When using the "HasPermissions" trait from this package, we need to know which * table should be used to retrieve your models permissions. We have chosen a * basic default value but you may easily change it to any table you like. */ 'model_has_permissions' => 'model_has_permissions', /* * When using the "HasRoles" trait from this package, we need to know which * table should be used to retrieve your models roles. We have chosen a * basic default value but you may easily change it to any table you like. */ 'model_has_roles' => 'model_has_roles', /* * When using the "HasRoles" trait from this package, we need to know which * table should be used to retrieve your roles permissions. We have chosen a * basic default value but you may easily change it to any table you like. */ 'role_has_permissions' => 'role_has_permissions', ], 'column_names' => [ /* * Change this if you want to name the related model primary key other than * `model_id`. * * For example, this would be nice if your primary keys are all UUIDs. In * that case, name this `model_uuid`. */ 'model_morph_key' => 'model_id', ], /* * When set to true, the required permission/role names are added to the exception * message. This could be considered an information leak in some contexts, so * the default setting is false here for optimum safety. */ 'display_permission_in_exception' => false, 'cache' => [ /* * By default all permissions are cached for 24 hours to speed up performance. * When permissions or roles are updated the cache is flushed automatically. */ 'expiration_time' => \DateInterval::createFromDateString('24 hours'), /* * The cache key used to store all permissions. */ 'key' => 'spatie.permission.cache', /* * When checking for a permission against a model by passing a Permission * instance to the check, this key determines what attribute on the * Permissions model is used to cache against. * * Ideally, this should match your preferred way of checking permissions, eg: * `$user->can('view-posts')` would be 'name'. */ 'model_key' => 'name', /* * You may optionally indicate a specific cache driver to use for permission and * role caching using any of the `store` drivers listed in the cache.php config * file. Using 'default' here means to use the `default` set in cache.php. */ 'store' => 'default', ], ];
Lumen
您可以通过 Composer 安装此包
composer require spatie/laravel-permission
复制所需的文件
mkdir -p config cp vendor/spatie/laravel-permission/config/permission.php config/permission.php cp vendor/spatie/laravel-permission/database/migrations/create_permission_tables.php.stub database/migrations/2018_01_01_000000_create_permission_tables.php
您还需要在config/auth.php
中创建另一个配置文件。您可以从 Laravel 仓库获取它,或者只需运行以下命令:
curl -Ls https://raw.githubusercontent.com/laravel/lumen-framework/5.7/config/auth.php -o config/auth.php
然后,在bootstrap/app.php
中注册中间件
$app->routeMiddleware([ 'auth' => App\Http\Middleware\Authenticate::class, 'permission' => Spatie\Permission\Middlewares\PermissionMiddleware::class, 'role' => Spatie\Permission\Middlewares\RoleMiddleware::class, ]);
以及配置文件、服务提供程序和缓存别名
$app->configure('permission'); $app->alias('cache', \Illuminate\Cache\CacheManager::class); // if you don't have this already $app->register(Spatie\Permission\PermissionServiceProvider::class);
现在,运行您的迁移
php artisan migrate
使用
首先,将Spatie\Permission\Traits\HasRoles
特质添加到您的User
模型中
use Illuminate\Foundation\Auth\User as Authenticatable; use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { use HasRoles; // ... }
- 请注意,如果您需要将
HasRoles
特性与其他模型(例如Page
)一起使用,您还需要为该模型添加protected $guard_name = 'web';
,否则您将遇到错误。use Illuminate\Database\Eloquent\Model; use Spatie\Permission\Traits\HasRoles; class Page extends Model { use HasRoles; protected $guard_name = 'web'; // or whatever guard you want to use // ... }
此包允许用户与权限和角色相关联。每个角色都与多个权限相关联。一个 Role
和一个 Permission
都是常规的 Eloquent 模型。它们需要一个 name
,可以按照这种方式创建。
use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Permission; $role = Role::create(['name' => 'writer']); $permission = Permission::create(['name' => 'edit articles']);
可以使用以下方法之一将权限分配给角色。
$role->givePermissionTo($permission); $permission->assignRole($role);
可以使用以下方法之一将多个权限同步到角色。
$role->syncPermissions($permissions); $permission->syncRoles($roles);
可以使用以下方法之一从角色中移除权限。
$role->revokePermissionTo($permission); $permission->removeRole($role);
如果您使用多个守卫,则还需要设置 guard_name
属性。在 README 中的 使用多个守卫 部分中了解更多信息。
HasRoles
特性为您模型添加 Eloquent 关系,您可以直接访问它们或用作基础查询。
// get a list of all permissions directly assigned to the user $permissionNames = $user->getPermissionNames(); // collection of name strings $permissions = $user->permissions; // collection of permission objects // get all permissions for the user, either directly, or from roles, or from both $permissions = $user->getDirectPermissions(); $permissions = $user->getPermissionsViaRoles(); $permissions = $user->getAllPermissions(); // get the names of the user's roles $roles = $user->getRoleNames(); // Returns a collection
HasRoles
特性还为您模型添加了一个 role
范围,以将查询范围到某些角色或权限。
$users = User::role('writer')->get(); // Returns only users with the role 'writer'
role
范围可以接受一个字符串,一个 \Spatie\Permission\Models\Role
对象或一个 \Illuminate\Support\Collection
对象。
相同的特性还添加了一个范围,仅获取具有特定权限的用户。
$users = User::permission('edit articles')->get(); // Returns only users with the permission 'edit articles' (inherited or directly)
该范围可以接受一个字符串,一个 \Spatie\Permission\Models\Permission
对象或一个 \Illuminate\Support\Collection
对象。
使用“直接”权限(见下文以同时使用角色和权限)
权限可以被赋予任何用户。
$user->givePermissionTo('edit articles'); // You can also give multiple permission at once $user->givePermissionTo('edit articles', 'delete articles'); // You may also pass an array $user->givePermissionTo(['edit articles', 'delete articles']);
权限可以被从用户中撤销。
$user->revokePermissionTo('edit articles');
或者一次性撤销和添加新的权限。
$user->syncPermissions(['edit articles', 'delete articles']);
您可以检查用户是否具有权限。
$user->hasPermissionTo('edit articles');
或者您可以通过整数传递表示权限 ID。
$user->hasPermissionTo('1'); $user->hasPermissionTo(Permission::find(1)->id); $user->hasPermissionTo($somePermission->id);
您可以检查用户是否具有数组中任何权限。
$user->hasAnyPermission(['edit articles', 'publish articles', 'unpublish articles']);
...或者如果用户具有数组中所有权限
$user->hasAllPermissions(['edit articles', 'publish articles', 'unpublish articles']);
您也可以传递整数以通过权限 ID 进行查找。
$user->hasAnyPermission(['edit articles', 1, 5]);
保存的权限将与默认守卫的 Illuminate\Auth\Access\Gate
类注册。因此,您可以使用 Laravel 的默认 can
函数检查用户是否具有权限。
$user->can('edit articles');
通过角色使用权限
角色可以被分配给任何用户。
$user->assignRole('writer'); // You can also assign multiple roles at once $user->assignRole('writer', 'admin'); // or as an array $user->assignRole(['writer', 'admin']);
角色可以被从用户中移除。
$user->removeRole('writer');
角色也可以被同步。
// All current roles will be removed from the user and replaced by the array given $user->syncRoles(['writer', 'admin']);
您可以确定用户是否具有特定角色。
$user->hasRole('writer');
您还可以确定用户是否具有给定的角色列表中的任何角色。
$user->hasAnyRole(Role::all());
您还可以确定用户是否具有给定的角色列表中的所有角色。
$user->hasAllRoles(Role::all());
assignRole
、hasRole
、hasAnyRole
、hasAllRoles
和 removeRole
函数可以接受一个字符串,一个 \Spatie\Permission\Models\Role
对象或一个 \Illuminate\Support\Collection
对象。
权限可以被赋予角色。
$role->givePermissionTo('edit articles');
您可以确定角色是否具有特定权限。
$role->hasPermissionTo('edit articles');
权限可以被从角色中撤销。
$role->revokePermissionTo('edit articles');
givePermissionTo
和 revokePermissionTo
函数可以接受一个字符串或一个 Spatie\Permission\Models\Permission
对象。
权限会自动从角色继承。此外,还可以为用户分配单个权限。例如
$role = Role::findByName('writer'); $role->givePermissionTo('edit articles'); $user->assignRole('writer'); $user->givePermissionTo('delete articles');
在上面的示例中,一个角色被赋予编辑文章的权限,并且这个角色被分配给了一个用户。现在这个用户可以编辑文章,并且还可以删除文章。'删除文章' 的权限是用户的直接权限,因为它直接分配给了他们。当我们调用 $user->hasDirectPermission('delete articles')
时,它返回 true
,但 false
对于 $user->hasDirectPermission('edit articles')
。
此方法在构建用于设置应用程序中角色和用户权限的表单时非常有用,并且想要限制或更改用户的角色继承权限,即只允许更改用户的直接权限。
您可以列出所有这些权限。
// Direct permissions $user->getDirectPermissions() // Or $user->permissions; // Permissions inherited from the user's roles $user->getPermissionsViaRoles(); // All permissions which apply on the user (inherited and direct) $user->getAllPermissions();
所有这些响应都是 Spatie\Permission\Models\Permission
对象的集合。
如果我们遵循前面的示例,第一个响应将包含具有 删除文章
权限的集合,第二个将包含具有 编辑文章
权限的集合,第三个将包含两者。
使用 Blade 指令
此包还添加了 Blade 指令,以验证当前登录用户是否具有给定的角色列表中的所有或任何角色。
可选地,您可以将要执行检查的 guard
作为第二个参数传递。
Blade 和角色
检查特定角色
@role('writer') I am a writer! @else I am not a writer... @endrole
等同于
@hasrole('writer') I am a writer! @else I am not a writer... @endhasrole
检查列表中的任何角色
@hasanyrole($collectionOfRoles) I have one or more of these roles! @else I have none of these roles... @endhasanyrole // or @hasanyrole('writer|admin') I am either a writer or an admin or both! @else I have none of these roles... @endhasanyrole
检查所有角色
@hasallroles($collectionOfRoles) I have all of these roles! @else I do not have all of these roles... @endhasallroles // or @hasallroles('writer|admin') I am both a writer and an admin! @else I do not have all of these roles... @endhasallroles
或者,@unlessrole
为检查单个角色提供了相反的功能,如下所示
@unlessrole('does not have this role') I do not have the role @else I do have the role @endunlessrole
Blade 和权限
此包不会添加任何特定的权限 Blade 指令。相反,使用 Laravel 的原生 @can
指令来检查用户是否具有特定的权限。
@can('edit articles') // @endcan
或者
@if(auth()->user()->can('edit articles') && $some_other_condition) // @endif
定义超级管理员
我们强烈建议通过设置全局 Gate::before
规则来处理超级管理员,该规则检查所需的角色。
然后您可以在整个应用程序中主要使用基于权限的控制,而无需始终检查“这是否是超级管理员”。
有关在您的应用程序中定义全局“管理员”门规则的更多信息,请参阅此维基文章:定义超级管理员门规则。
最佳实践 -- 角色 vs 权限
通常最好围绕 permissions
来编码您的应用程序。这样,您就可以在应用程序的每个地方始终使用原生的 Laravel @can
和 can()
指令。
角色仍然可以用来分组权限,以便于分配,如果确实需要,您仍然可以使用基于角色的辅助方法。但大多数与应用程序相关的逻辑通常最好使用 can
方法来控制,这允许 Laravel 的门层做所有繁重的工作。
最佳实践 -- 使用策略
为应用程序功能访问控制的最佳方式是使用模型策略。这样,您可以将应用程序逻辑与权限规则相结合,使您的实现更简单。您可以在以下示例应用程序中找到使用此 Laravel Permissions 包实现模型策略的示例:[链接](https://github.com/drbyte/spatie-permissions-demo/blob/master/app/Policies/PostPolicy.php)
使用多个守卫
当使用默认的 Laravel 身份验证配置时,上述所有方法都将自动工作,无需额外配置。
然而,当使用多个守卫时,它们将像命名空间一样作用于您的权限和角色。这意味着每个守卫都有自己的权限和角色集合,可以分配给其用户模型。
使用多个守卫的权限和角色
在创建新的权限和角色时,如果没有指定守卫,则将使用 auth.guards
配置数组中定义的第一个守卫。当为特定守卫创建权限和角色时,您必须在模型上指定其 guard_name
。
// Create a superadmin role for the admin users $role = Role::create(['guard_name' => 'admin', 'name' => 'superadmin']); // Define a `publish articles` permission for the admin users belonging to the admin guard $permission = Permission::create(['guard_name' => 'admin', 'name' => 'publish articles']); // Define a *different* `publish articles` permission for the regular users belonging to the web guard $permission = Permission::create(['guard_name' => 'web', 'name' => 'publish articles']);
检查用户是否具有特定守卫的权限
$user->hasPermissionTo('publish articles', 'admin');
注意:在确定给定模型上的角色/权限是否有效时,它将按照以下顺序选择守卫:首先模型的
$guard_name
属性;然后配置中的守卫(通过提供者);然后auth.guards
配置数组中定义的第一个守卫;然后auth.defaults.guard
配置。
注意:当使用除默认
web
守卫之外的其他守卫时,您需要声明每个模型希望使用的guard_name
,通过在模型中设置$guard_name
属性。每个模型一个是最简单的。
注意:如果你的应用只使用了一个守卫,但不是
web
,那么请将你的config/auth.php
中列出的守卫的顺序更改,将主要的守卫设置为默认并作为定义守卫列表中的第一个。
为守卫用户分配权限和角色
你可以使用与上述 通过角色使用权限 描述的相同方法来为用户分配权限和角色。只需确保权限或角色上的 guard_name
与用户的守卫匹配,否则将抛出 GuardDoesNotMatch
异常。
使用多个守卫的 blade 指令
你可以使用 使用 blade 指令 中列出的所有 blade 指令,只需将你希望使用的守卫作为指令的第二个参数传递即可。
@role('super-admin', 'admin') I am a super-admin! @else I am not a super-admin... @endrole
使用中间件
此包包含 RoleMiddleware
、PermissionMiddleware
和 RoleOrPermissionMiddleware
中间件。你可以在 app/Http/Kernel.php
文件中添加它们。
protected $routeMiddleware = [ // ... 'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class, 'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class, 'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class, ];
然后你可以使用中间件规则来保护你的路由。
Route::group(['middleware' => ['role:super-admin']], function () { // }); Route::group(['middleware' => ['permission:publish articles']], function () { // }); Route::group(['middleware' => ['role:super-admin','permission:publish articles']], function () { // }); Route::group(['middleware' => ['role_or_permission:super-admin']], function () { // }); Route::group(['middleware' => ['role_or_permission:publish articles']], function () { // });
或者,你可以使用 |
(管道)字符将多个角色或权限分开。
Route::group(['middleware' => ['role:super-admin|writer']], function () { // }); Route::group(['middleware' => ['permission:publish articles|edit articles']], function () { // }); Route::group(['middleware' => ['role_or_permission:super-admin|edit articles']], function () { // });
你还可以通过在构造函数中设置所需的中间件来类似地保护你的控制器。
public function __construct() { $this->middleware(['role:super-admin','permission:publish articles|edit articles']); }
public function __construct() { $this->middleware(['role_or_permission:super-admin|edit articles']); }
捕获角色和权限失败
如果你想覆盖默认的 403
响应,你可以使用你的应用的异常处理器捕获 UnauthorizedException
。
public function render($request, Exception $exception) { if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) { // Code here ... } return parent::render($request, $exception); }
使用 artisan 命令
你可以使用 artisan 命令从控制台创建角色或权限。
php artisan permission:create-role writer
php artisan permission:create-permission "edit articles"
当你为特定的守卫创建权限/角色时,可以指定守卫名称作为第二个参数。
php artisan permission:create-role writer web
php artisan permission:create-permission "edit articles" web
创建角色时,你还可以同时创建和链接权限。
php artisan permission:create-role writer web "create articles|edit articles"
单元测试
在你的应用的测试中,如果你没有在测试的 setUp()
部分作为种子分配角色和权限,那么你可能会遇到一个“先有鸡还是先有蛋”的情况,即角色和权限没有在网关(因为你的测试是在网关注册之后创建它们)注册。解决这个问题的方法很简单:在你的测试中简单地添加一个 setUp()
指令来重新注册权限,如下所示
public function setUp() { // first include all the normal setUp operations parent::setUp(); // now re-register all the roles and permissions $this->app->make(\Spatie\Permission\PermissionRegistrar::class)->registerPermissions(); }
数据库填充
你可能会发现,在播种之前清除此包的缓存是最佳选择,以避免缓存冲突错误。这可以在种子类中直接完成。以下是一个示例种子,它首先清除缓存,然后创建权限,最后将权限分配给角色(这些步骤的顺序是有意的)
use Illuminate\Database\Seeder; use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Permission; class RolesAndPermissionsSeeder extends Seeder { public function run() { // Reset cached roles and permissions app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions(); // create permissions Permission::create(['name' => 'edit articles']); Permission::create(['name' => 'delete articles']); Permission::create(['name' => 'publish articles']); Permission::create(['name' => 'unpublish articles']); // create roles and assign created permissions // this can be done as separate statements $role = Role::create(['name' => 'writer']); $role->givePermissionTo('edit articles'); // or may be done by chaining $role = Role::create(['name' => 'moderator']) ->givePermissionTo(['publish articles', 'unpublish articles']); $role = Role::create(['name' => 'super-admin']); $role->givePermissionTo(Permission::all()); } }
扩展
如果你需要扩展现有的 Role
或 Permission
模型,请注意
- 你的
Role
模型需要扩展Spatie\Permission\Models\Role
模型 - 你的
Permission
模型需要扩展Spatie\Permission\Models\Permission
模型
如果你需要替换现有的 Role
或 Permission
模型,你需要注意以下事项
- 你的
Role
模型需要实现Spatie\Permission\Contracts\Role
协议 - 你的
Permission
模型需要实现Spatie\Permission\Contracts\Permission
协议
在两种情况下,无论是扩展还是替换,你都需要在配置中指定你的新模型。为此,你必须发布配置后更新配置文件中的 models.role
和 models.permission
值
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"
缓存
角色和权限数据被缓存以加快性能。
虽然我们建议不要更改缓存 "key" 名称,但如果你想更改过期时间,你可以在 config/permission.php
文件中的 cache
数组中这样做。请注意,从 v2.26.0 版本开始,这里的 cache
条目现在是一个数组,而 expiration_time
是一个子数组条目。
当你使用用于操作角色和权限的内置函数时,缓存会自动为你重置,并且关系会自动重新加载当前模型记录。
$user->assignRole('writer'); $user->removeRole('writer'); $user->syncRoles(params); $role->givePermissionTo('edit articles'); $role->revokePermissionTo('edit articles'); $role->syncPermissions(params); $permission->assignRole('writer'); $permission->removeRole('writer'); $permission->syncRoles(params);
但是,如果你直接在数据库中操作权限/角色数据而不是调用提供的方法,那么除非你手动重置缓存,否则你将看不到应用中反映出的更改。
手动缓存重置
要手动重置此包的缓存,您可以在您的应用代码中运行以下命令:
app()->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
或者您可以使用 Artisan 命令
php artisan permission:cache-reset
缓存标识符
提示:如果您正在使用像 redis
或 memcached
这样的缓存服务,并且您的服务器上运行着其他网站,您可能会遇到应用程序之间的缓存冲突。明智的做法是在 Laravel 的 /config/cache.php
中设置自己的缓存 prefix
,以确保每个应用程序都是唯一的。这将防止其他应用程序意外使用/更改您的缓存数据。
需要用户界面吗?
此包不包括任何屏幕,您需要自己构建。以下是一些启动选项:
测试
composer test
升级
如果您要从 v1 升级到 v2,@fabricecw 准备了 一个gist,这可能使数据迁移更容易。您还需要删除旧的 laravel-permission.php
配置文件并发布新的 permission.php
,并进行相应编辑。
变更日志
请参阅 CHANGELOG 了解最近的变化。
贡献
请参阅 CONTRIBUTING 了解详情。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 freek@spatie.be 而不是使用问题跟踪器。
明信片软件
您可以使用此包,但如果它进入您的生产环境,我们非常欢迎您从家乡给我们寄一张明信片,说明您正在使用我们哪些包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
我们将所有收到的明信片 发布在我们的公司网站上。
致谢
此包在很大程度上基于 Jeffrey Way 在 Laracasts 上关于 权限和角色 的精彩课程。他的原始代码可以在 GitHub 上的 此存储库中找到。
特别感谢 Alex Vanderbist 在 v2
中的大力帮助,以及感谢 Chris Brown 长期支持我们维护此包。
替代方案
- Povilas Korop 在 Laravel News 的文章中列出了替代方案 。 在同一篇文章中,他比较了 laravel-permission 和 Joseph Silber 的 Bouncer,我们认为这也是一个优秀的包。
- ultraware/roles 在其功能方面采用了一种略有不同的方法。
支持我们
Spatie 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到所有开源项目的概述 。
您的业务是否依赖我们的贡献?请通过 Patreon 支持我们。所有承诺都将用于分配人力维护和新酷炫的功能。
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 获取更多信息。