Package Data | |
---|---|
Maintainer Username: | sixbyte |
Maintainer Contact: | liu.sixbyte@gmail.com (sixbyte) |
Package Create Date: | 2015-08-24 |
Package Last Update: | 2015-12-04 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 03:06:30 |
Package Statistics | |
---|---|
Total Downloads: | 56 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 3 |
Total Watchers: | 3 |
Total Forks: | 1 |
Total Open Issues: | 0 |
基于 laravel5.* 的权限管理包
参考了 artesaos/Defender 和其他权限管理包的一些特性,但也有一些跟他们不一样的地方
composer require "sixbyte/perchecker:0.2.*"
在 app/config/app.php
的 $providers
和 $aliases
数组下分别添加
providers
'Sixbyte\Perchecker\PercheckerServiceProvider',
aliases
'Perchecker' => 'Sixbyte\Perchecker\PercheckerFacade',
设置资源
php artisan vendor:publish
数据迁移
php artisan migrate
路由入库
php artisan perchecker:routesync
Trait 在 app/User.php 添加
use Sixbyte\Perchecker\HasPermissionTrait;
use ... HasPermissionTrait
配置完成
$routeMiddleware
数组 注册中间件'perchecker' => 'Sixbyte\Perchecker\PercheckerMiddleware',
// 用户登录验证是前提
Route::get('/test', ['middleware' => ['auth', 'perchecker'], 'as' => 'test', function () {
echo "i have permission";
}]);
1
绑定新角色# 创建新角色
$role_model = Perchecker::getRoleModel();
$role_id = $role_model->insert(['name'=>'admin','readable_name'=>'超级管理员']);
# 用户绑定新角色 多对多的关系
$user = User::find(1);
$user->roles()->attach($role_id);
1
绑定权限# 创建权限
$permission_model = Perchecker::getPermissionModel();
$permission_id = $permission_model->insert(['name'=>'user.create']);
# 权限绑定角色 多对多关系
$role_model = Perchecker::getRoleModel();
$role = $role_model->find(1);
$role->permissions()->attach($permission_id);
1
绑定权限 同上
/**
* Perchecker - Laravel 5.1 Package
* Author: liu.sixbyte@gmail.com.
*/
return [
'role_model' => 'Sixbyte\Perchecker\Models\Role', // 5.0 style
'permission_model' => \Sixbyte\Perchecker\Models\Permission::class,
'route_model' => \Sixbyte\Perchecker\Models\Route::class,
/*
* Forbidden callback
*/
'forbidden_callback' => function () {
header('HTTP/1.0 403 You don\'t have permission to do it!');
exit('You don\'t have permission to do it!');
},
/**
* route filter function
*/
'filter_route' => function ($route) {
if (in_array('perchecker', $route['middleware'])) {
return $route;
}
return null;
},
/*
* Use template helpers
*/
'template_helpers' => true,
/*
* Super User role name
*/
'superuser_role' => 'superuser',
];
: hasPermission($p) $p 权限的id属性值或者名字属性值
$user = Auth::user();
$user->hasPermission(1);
$user->hasPermission('user.create');
权限的验证方式:
true
, 不存在 false
: hasRole($r) $p 角色的id属性值或者名字属性值
$user = Auth::user();
$user->hasRole(1);
$user->hasRole('admin');
用户
$user = Auth::user();
$user->getPermissions();
角色
$role_model = Perchecker::getRoleModel();
$role = $role_model->find(1);
$role->permissions()->attach($permission_id);
$role->getPermissions();
继承 Model
类,修改配置文件
重新编写 中间件, ServiceProvider, 修改注册文件
替换HasPermissionTrait.php