0%

Laravel 5.2 ACL

Ref:
https://www.youtube.com/watch?v=Kas2w2DBuFg
https://github.com/mschwarzmueller/laravel-playground
My:https://github.com/lovenery/laravel-simple-acl


Great!
shinobi
https://github.com/caffeinated/shinobi
GUI
https://github.com/SmarchSoftware/watchtower


Entrust
Ref:https://github.com/Zizaco/entrust

Install

照著Readme做,但
composer require zizaco/entrust:dev-master
5.2.x-dev有不能用file cache的問題QQ

以下也是5.2.x-dev的問題

php artisan entrust:migration之前就要

1
2
3
4
5
6
7
8
9
10
11
12
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
'table' => 'users', // add me
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],

不然你就要
$table->foreign('user_id')->references('id')->on('')
to
$table->foreign('user_id')->references('id')->on('users')

才能php artisan migrate

不能php artisan migrate:reset的時後記得
composer dump-autoload

Configuration

php artisan make:auth

php artisan make:model Role

1
2
3
4
5
6
7
8
9
<?php
namespace App;
use Zizaco\Entrust\EntrustRole;
class Role extends EntrustRole
{
}

php artisan make:model Permission

1
2
3
4
5
6
7
8
9
<?php
namespace App;
use Zizaco\Entrust\EntrustPermission;
class Permission extends EntrustPermission
{
}
1
2
3
4
5
6
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Eloquent
{
use EntrustUserTrait; // add this trait to your user model
...
}

This will enable the relation with Role and add the following methods roles(), hasRole($name), can($permission), and ability($roles, $permissions, $options) within your User model.

記得composer dump-autoload

Play

php artisan make:seeder RolesTableSeeder

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
44
45
$user = new User();
$user->name = 'hsu';
$user->email = 'q@q.q';
$user->password = bcrypt('qqqqqq');
$user->save();
$owner = new Role();
$owner->name = 'owner';
$owner->display_name = 'Project Owner'; // optional
$owner->description = 'User is the owner of a given project'; // optional
$owner->save();
$admin = new Role();
$admin->name = 'admin';
$admin->display_name = 'User Administrator'; // optional
$admin->description = 'User is allowed to manage and edit other users'; // optional
$admin->save();
$createPost = new Permission();
$createPost->name = 'create-post';
$createPost->display_name = 'Create Posts'; // optional
// Allow a user to...
$createPost->description = 'create new blog posts'; // optional
$createPost->save();
$editUser = new Permission();
$editUser->name = 'edit-user';
$editUser->display_name = 'Edit Users'; // optional
// Allow a user to...
$editUser->description = 'edit existing users'; // optional
$editUser->save();
//-----------------------------------------------
$user = User::where('name', '=', 'hsu')->first();
// role attach alias
$user->attachRole($admin); // parameter can be an Role object, array, or id
// or eloquent's original technique
//$user->roles()->attach($admin->id); // id only
//----------------------------------------------
$admin->attachPermission($createPost);
// equivalent to $admin->perms()->sync(array($createPost->id));
$owner->attachPermissions(array($createPost, $editUser));
// equivalent to $owner->perms()->sync(array($createPost->id, $editUser->id));

$this->call(RolesTableSeeder::class);