riteshjha/laravel-permission
Composer 安装命令:
composer require riteshjha/laravel-permission
包简介
Laravel role and user based permission
README 文档
README
This package allows you to manage user permissions in a database using role and direct user based permission with Account (Organization) level and owner level permission check. It handle 2 types of abilities (route ability and field ability).
Installation
-
Require it with Composer
composer require riteshjha/laravel-permission -
You can publish the migrations, views, assets and the config file with:
php artisan vendor:publish --provider="Rkj\Permission\PermissionServiceProvider" -
Add the service provider in your config/app.php file:
'providers' => [ // ... Rkj\Permission\PermissionServiceProvider::class, ]; 'aliases' => [ //... 'Permission' => Rkj\Permission\Facades\Permission::class ]; -
This package publishes a
config/permission.phpfile. Check this file and change accourding to your needs for different (Ability, Role and User) model name and other config options. -
Run migrations:
php artisan migrate
Configuration
-
First configure models namesapce in
config/permission.php -
Add
UserHasRoletrait to your User model andRoleHasAbilityto Role model. -
All model that you want to include in permission should implements
Permissionableinterface and addHasPermissiontrait.
Ability
Package handle 2 types of ability (route ability and field ability). Package parse Auth route (route which has auth middleware) and store route name as a Route Ability. For field ability, you have to define it in your model like this:
class Project extends Model
{
protected $fieldAbilities = ['cost', 'estimated_cost'] ; //list projects table fields on which you want to apply permission
}
You can use Project::allowFieldAbilities(['cost']) in your seeder to alow default field ability. For details check PermissionSeeder in tests.
Role and Ability Group
Role and Ability is divided in 2 groups (SYSTEM and ACCOUNT). All admin users that mange admin tasks will under SYSTEM group role.
All users that signup or login as front end user will under ACCOUNT group. Similarly All ability (route name) which is used for admin
interface will be under SYSTEM group and all ability which is used for front-end will be under ACCOUNT group. To disable it change disableAbilityGroup to true in config.
Sync Ability
php artisan ability:record // if need fresh then add --fresh
Admin Interface
There is an admin interface with routes and views for handling ability and permissions. You have to add package routes in your admin route group.
Route::group(['middleware' => ['auth'], 'prefix' => 'admin'], function () {
Permission::routes();
});
Now you can access permission interface via admin/permission/roles routes
Note: If you use admin routes prefix other than 'admin' then change adminRoutePrefix value in config/permission.php
Usage
Package use laravel gate, so you can use can('project.create') in view and $this->authorize('project.create') in controller for route ability. And can('projects::cost') for field ability in view. Here projects is a table name.
For Details check Tests
Create/Update
When creating or updating record in model then filter data using filterFieldAccess method like :
$data = Project::filterFieldAccess($data)
riteshjha/laravel-permission 适用场景与选型建议
riteshjha/laravel-permission 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 796 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 riteshjha/laravel-permission 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 riteshjha/laravel-permission 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 796
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-18