2016-04-04

使用 Laravel 构建内容管理框架(六)

完成Entrust的配置

安装zizaco/entrust Package


zizaco/entrust是Laravel下基于角色的权限管理方案。

1.修改文件composer.json

1
2
3
4
5
6
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"gregwar/captcha": "1.*",
"zizaco/entrust": "dev-laravel-5"
},

2.在终端运行命令

1
2
3
4
5
composer install

// or

composer update

3.在文件config/app.phpproviders数组和aliase数组分别添加

1
2
3
4
5
// providers
Zizaco\Entrust\EntrustServiceProvider::class

// aliase
'Entrust' => Zizaco\Entrust\EntrustFacade::class,

4.在文件app/Http/Kernel.php$routeMiddleware添加

1
2
3
'role' => Zizaco\Entrust\Middleware\EntrustRole::class,
'permission' => Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability' => Zizaco\Entrust\Middleware\EntrustAbility::class,

5.Entrust会根据文件config/auth.phpmodel来选择用户模型,如下:

1
'model' => App\Models\User::class

执行以下命令生成配置文件config/entrust.php

1
php artisan vendor:publish

修改配置文件entrust.php如下:

1
2
'role'=>'App\Models\Role'
'permission'=>'App\Models\Permission'

6.生成数据库迁移文件并执行

1
2
php artisan entrust:migration
php artisan migrate

7.生成自动加载

1
composer dump-autoload

管理模型


为了使用Entrust,我们需要新建两个模型,在终端运行以下命令:

1
2
3
4
5
// 角色模型
php artisan make:model Models/Role

// 权限模型
php artisan make:model Models/Permission

Role.php

1
2
3
4
5
6
7
8
9
<?php
namespace App\Models;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{
protected $guarded = [];
}

Permission.php

1
2
3
4
5
6
7
8
9
<?php
namespace App\Models;

use Zizaco\Entrust\EntrustPermission;

class Permission extends EntrustPermission
{
protected $guarded = [];
}

为了配合Entrust的使用,需要修改User.php的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, EntrustUserTrait{
Authorizable::can insteadof EntrustUserTrait;
EntrustUserTrait::can as hasPermission;
}

/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
}

完成上述所有操作之后,Entrust的配置就完成了。