Package Data | |
---|---|
Maintainer Username: | zjien |
Maintainer Contact: | reyen12@gmail.com (zjien) |
Package Create Date: | 2016-11-05 |
Package Last Update: | 2016-11-13 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-11 15:08:32 |
Package Statistics | |
---|---|
Total Downloads: | 12 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 3 |
Total Watchers: | 2 |
Total Forks: | 1 |
Total Open Issues: | 1 |
Quantum是个基于RESTful API的RBAC Laravel扩展包
在终端输入下面命令:
composer require zjien/quantum
然后稍等几分钟。
当composer下载并安装完成后。编辑 config/app.php
文件,添加下面代码到 providers
数组里:
Zjien\Quantum\Providers\QuantumServiceProvider::class,
然后添加下列代码到 aliases
数组里:
'Quantum' => Zjien\Quantum\Facades\QuantumFacade::class,
php artisan vendor:publish
php artisan quantum:migration
该命令会生成Quantum的迁移文件到你项目的迁移目录中。
config/quantum.php
配置文件设置相应的配置信息。创建你自己的 Role 模型(或者直接使用Quantum的),并继承 Quantum 的 Role 模型:
<?php namespace App;
use Zjien\Quantum\Models\Role as QuantumRole;
class Role extends QuantumRole
{
//your code goes here
}
创建你自己的 Permission 模型(或者直接使用Quantum的),并继承 Quantum 的 Permission 模型:
<?php namespace App;
use Zjien\Quantum\Models\Permission as QuantumPermission;
class Permission extends QuantumPermission
{
//your code goes here
}
创建你自己的 User 模型(或者直接使用Quantum的),并继承 Quantum 的 User 模型:
<?php namespace App;
use Zjien\Quantum\Models\User as QuantumUser;
class User extends QuantumUser
{
//your code goes here
}
接着运行以下命令:
composer dump-autoload
$role['name'] = 'admin';
$role['display_name'] = 'Platform Admin';
$role['description'] = 'the platform admin';
$role = (new Role())->create($role);
$perm['name'] = 'create-admin';
$perm['verb'] = 'POST';
$perm['uri'] = '/admins';
$perm['display_name'] = 'Create Amin';
$perm['description'] = 'the permission to create a admin account';
$perm = (new Permission())->create($perm);
$role->attachPermission($perm);
$role->attachPermission([1,2,3]);//1,2,3为权限对应的id
$role->attachPermission(1);//添加权限id为1的权限
$user->attachRole($role);
$user->attachRole([1,2,3]);//1,2,3为角色对应的id
$user->attachRole(1);//添加角色id为1的角色
$role = Role::find(1); $user->is($role);//判断用户是否拥有某个角色 $perm = Permission::find(2); $user->can($perm);//判断用户是否有某个权限
is
和 can
都可以传入一个对象数组
$role = Role::find([1,2]);
$user->is($role, true);//要求用户拥有所有角色才有效
$perm = Permission::find([1,2]);
$user->can($perm);//判断用户是否有多个权限中的某个
使用中间验证请求资源的用户是否具有访问该资源的权限
在 app/Http/Kernel.php
的 routeMiddleware
数组中添加如下代码:
'permission_check' => \Zjien\Quantum\Middleware\QuantumAccess::class,