alexhouse/trust

Laravel5 角色和权限

维护者

详细信息

github.com/alexhouse/trust

源码

3.0.1 2015-03-10 21:01 UTC

This package is not auto-updated.

Last update: 2020-09-04 19:14:28 UTC


README

通过 traits 为 Laravel 5 实现用户角色和权限

基于 https://github.com/Zizaco/entrust 的简化想法

所需设置

composer.json 文件的 require 键中添加以下内容

"alexhouse/trust": "3.*"

运行 Composer 更新命令

$ composer update

在您的 config/app.php 中将 'Smallneat\Trust\TrustServiceProvider' 添加到 $providers 数组的末尾

'providers' => array(

    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    ...
    'Smallneat\Trust\TrustServiceProvider',
),

配置

Trust 会创建多个表,并对您的模型名称进行假设。您可以使用 Laravel 的正常配置来配置所有这些设置。首先,您需要将此包中的配置发布到您的应用程序配置中(如果默认名称与您的应用程序中现有的表冲突,您需要处理此问题)

$ php artisan vendor:publish --provider="Smallneat\Trust\TrustServiceProvider"

默认设置假设您的模型名称分别为 UserRolePermission,并且用于跟踪这些模型的表分别称为 usersrolespermissions。Trust 还使用 2 个 pivot 表来提供用户、角色和权限之间的多对多关联,这些 pivot 表分别称为 user_rolerole_permission

设置完配置(或保留默认设置)后,您就可以进行下一步了。

创建数据库迁移

在上面的 vendor:publish 命令中,数据库迁移将已创建在 /database/migrations 文件夹下。您可以修改此迁移,根据需要添加自己的列。准备好后,只需运行

$ php artisan migrate

迁移后,将出现上面描述的四张新表。

注意: 这不会为您创建 users 表。如果您还没有用户表,您需要添加一个迁移来创建一个。我们假设此表名为 users,并有一个字段 user_id

在您的模型中使用 Trust

这里我们假设您已经有了用户模型(例如,Laravel 在您构建新 Laravel 应用程序时为您创建的模型),并且它在数据库中有一个匹配的表(通常称为 users)。

UserRoleTrait trait 添加到用户模型类中(通常在 app/User.php),如下所示...

<?php

...
use Smallneat\Trust\Traits\UserRoleTrait;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

  use Authenticatable, CanResetPassword;
  use UserRoleTrait;

  ...

}

接下来,创建一个看起来像这样的 Role 模型...

<?php

use Smallneat\Trust\Traits\RoleTrait;

class Role extends Model {

  use RoleTrait;

  protected $fillable = [];
}

Role 模型有一个 name 属性,这是角色的名称(例如,'Admin'、'Editor'、'Manager'、'User')。您还可以使用 roles()permissions() 方法找到与该角色关联的用户和权限。

接下来,创建一个看起来像这样的 Permission 模型...

<?php

use Smallneat\Trust\Traits\PermissionTrait;

class Permission extends Model {

  use PermissionTrait;

	protected $fillable = [];
}

Permission 模型有一个 name 属性,这是权限的名称(例如,'CreatePost'、'EditPost'、'DeletePost'),以及一个 description 属性,用于存储权限的可读描述(例如,用于在您的管理区域中显示)。roles() 方法将为您提供访问具有此权限的所有角色的权限。

最后,别忘了导出 composer 自动加载。

$ composer dump-autoload

使用方法

每个用户可以关联任意多个角色。每个角色可以授予任意多的权限,这些权限可以用来控制对网站各个区域的访问。

角色和权限通常通过数据库种子文件创建,但我们将展示如何创建一系列权限和角色,并将它们分配给用户。最后,我们将展示如何查询用户以了解他们能做什么和不能做什么...

首先,我们将为虚构的博客创建一些权限。这些代表了您希望以某种方式保护的网站区域。

$createPost = Permission::create([
    'name' => 'CreatePost',
    'description' => 'Create new posts'
]);

$editPost = Permission::create([
    'name' => 'EditPost',
    'description' => 'Edit existing posts'
]);

$deletePost = Permission::create([
    'name' => 'DeletePost',
    'description' => 'Delete posts'
]);

$manageUsers = Permission::create([
    'name' => 'ManageUsers',
    'description' => 'Create and Delete user accounts'
]);

// plus loads more for all the areas of the site we want to control

接下来,我们将创建一些不同的角色,并将一些权限与它们关联

// Create 2 new roles for an Admin and an Editor
$admin = Role::create(['name' => 'Admin']);
$editor = Role::create(['name' => 'Editor']);

// Attach some permissions to each of the roles
// You can either pass in a Role object, and id, or an array of Roles / id's
$admin->attachPermissions([$manageUsers, $deletePosts]);
$editor->attachPermissions([$createPosts, $editPosts, $deletePosts]);

// You can access the permissions that a role has (using Eloquent) like so...
$admin->perms()

// Remove them with detachPermissions()

最后,我们可以给用户分配一些这些角色,并对它们进行查询

// Give a user a role (using a Role model or id or an array of roles / id's)
$user = Auth::user();
$user->attachRoles($editor);

// Query the user
$user->hasRole('Editor');       // true
$user->hasRole('Admin');        // false
$user->can('CreatePost');       // true
$user->can('ManageUsers');      // false

// Access all the roles the user has
$user->roles();

// remove them with detachRoles()

许可

Trust 是免费软件,遵循 MIT 许可条款分发

更多信息

如有任何问题,请在此报告