baklysystems/laravel-permission

Laravel 5.1 的权限处理

1.3.4 2016-02-27 13:07 UTC

This package is auto-updated.

Last update: 2024-09-23 04:46:45 UTC


README

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score Total Downloads

此包允许将权限和角色保存到数据库中。它基于 Laravel 的授权功能,该功能在版本 5.1.11 中引入。

安装后,您可以进行以下操作

//adding permissions to a user
$user->givePermissionTo('edit articles');

//adding permissions via a role
$user->assignRole('writer');
$user2->assignRole('writer');

$role->givePermissionTo('edit articles');

您可以使用 Laravel 的默认 can 函数测试用户是否有权限。

$user->can('edit articles');

如果您想要一个检查权限的中间件,请查看我们的 authorize 包:https://github.com/spatie/laravel-authorize

Spatie 是一家位于比利时的安特卫普网络设计公司。您可以在我们的网站上找到我们所有开源项目的概述:https://spatie.be/opensource

安装

您可以通过 composer 安装此包

$ composer require spatie/laravel-permission

必须安装此服务提供程序。

// config/app.php
'providers' => [
    ...
    Spatie\Permission\PermissionServiceProvider::class,
];

您可以使用以下命令发布迁移

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations"

此包假定您的用户表名为 "users"。如果情况不是这样,您应该手动编辑已发布的迁移以使用您自定义的表名。

迁移发布后,您可以通过运行迁移来创建角色和权限表

php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"

这是已发布配置文件的内容

return [

    /*
    |--------------------------------------------------------------------------
    | Authorization Models
    |--------------------------------------------------------------------------
    */

    'models' => [

        /*
        |--------------------------------------------------------------------------
        | Permission Model
        |--------------------------------------------------------------------------
        |
        | When using the "HasRoles" 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,

        /*
        |--------------------------------------------------------------------------
        | Role Model
        |--------------------------------------------------------------------------
        |
        | 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,
    ],

    /*
    |--------------------------------------------------------------------------
    | Authorization Tables
    |--------------------------------------------------------------------------
    */

    'table_names' => [

        /*
        |--------------------------------------------------------------------------
        | Roles Table
        |--------------------------------------------------------------------------
        |
        | 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',

        /*
        |--------------------------------------------------------------------------
        | Permissions Table
        |--------------------------------------------------------------------------
        |
        | When using the "HasRoles" 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',

        /*
        |--------------------------------------------------------------------------
        | User Permissions Table
        |--------------------------------------------------------------------------
        |
        | When using the "HasRoles" trait from this package, we need to know which
        | table should be used to retrieve your users permissions. We have chosen a
        | basic default value but you may easily change it to any table you like.
        |
        */

        'user_has_permissions' => 'user_has_permissions',

        /*
        |--------------------------------------------------------------------------
        | User Roles Table
        |--------------------------------------------------------------------------
        |
        | When using the "HasRoles" trait from this package, we need to know which
        | table should be used to retrieve your users roles. We have chosen a
        | basic default value but you may easily change it to any table you like.
        |
        */

        'user_has_roles' => 'user_has_roles',

        /*
        |--------------------------------------------------------------------------
        | Role Permissions Table
        |--------------------------------------------------------------------------
        |
        | 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',

    ],

];

用法

首先将 Spatie\Permission\Traits\HasRoles 特性添加到您的 User 模型中。

use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;
    
    // ...
}

此包允许用户与角色关联。权限可以与角色关联。Role 和 Permission 是常规 Eloquent 模型。它们可以有名称,可以像这样创建

use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

$role = Role::create(['name' => 'writer']);
$permission = Permission::create(['name' => 'edit articles']);

HasRoles 为您的模型添加了 eloquent 关系,可以直接访问或用作基础查询。

$permissions = $user->permissions;
$roles = $user->roles()->pluck('name');

### 使用权限 您可以将权限授予用户

$user->givePermissionTo('edit articles');

您可以从用户撤销权限

$user->revokePermissionTo('edit articles');

您可以测试用户是否有权限

$user->hasPermissionTo('edit articles');

保存的权限将注册到 Illuminate\Auth\Access\Gate 类。因此,您可以使用 Laravel 的默认 can 函数测试用户是否有权限。

$user->can('edit articles');

### 使用角色和权限 您可以将角色分配给用户

$user->assignRole('writer');

您可以从用户移除角色

$user->removeRole('writer');

您可以确定用户是否有特定的角色

$user->hasRole('writer');

您还可以确定用户是否有给定列表中的任何角色

$user->hasAnyRole(Role::all());

您还可以确定用户是否有给定列表中的所有角色

$user->hasAllRoles(Role::all());

assignRolehasRolehasAnyRolehasAllRolesremoveRole 函数可以接受字符串、Spatie\Permission\Models\Role 对象或 \Illuminate\Support\Collection 对象。

您可以将权限授予角色

$role->givePermissionTo('edit articles');

您可以从角色撤销权限

$role->revokePermissionTo('edit articles');

givePermissionTorevokePermissionTo 函数可以接受字符串或 Spatie\Permission\Models\Permission 对象。

保存的权限和角色也注册到 Illuminate\Auth\Access\Gate 类。

$user->can('edit articles');

### 使用 blade 指令 此包还添加了 Blade 指令以验证当前登录用户是否具有给定列表中的所有或任何角色。

@role('writer')
I'm a writer!
@else
I'm not a writer...
@endrole
@hasrole('writer')
I'm a writer!
@else
I'm not a writer...
@endhasrole
@hasanyrole(Role::all())
I have one or more of these roles!
@else
I have none of these roles...
@endhasanyrole
@hasallroles(Role::all())
I have all of these roles!
@else
I don't have all of these roles...
@endhasallroles

您可以使用 Laravel 的本地 @can 指令检查用户是否有特定的权限。

扩展

如果您需要扩展或替换现有的 RolePermission 模型,您只需注意以下事项:

  • 您的 Role 模型需要实现 Spatie\Permission\Contracts\Role 接口。
  • 您的 Permission 模型需要实现 Spatie\Permission\Contracts\Permission 接口。
  • 您必须使用以下命令发布配置: php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config" 并更新 models.rolemodels.permission 的值。

变更日志

有关最近更改的详细信息,请参阅 CHANGELOG

测试

$ composer test

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过电子邮件 freek@spatie.be 联系,而不是使用问题跟踪器。

鸣谢

本包大量借鉴了 Jeffrey Way 的出色 Laracasts -课程 角色和权限。他的原始代码可在 GitHub 上的此仓库中找到。

替代方案

关于 Spatie

Spatie 是一家位于比利时的安特卫普网络设计公司。您可以在我们的网站上找到我们所有开源项目的概述:https://spatie.be/opensource

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件