enclave-code/static-auth-manager
Composer 安装命令:
composer require enclave-code/static-auth-manager
包简介
Static manage roles and permission in Laravel
README 文档
README
Manage user permissions and roles in your Laravel application by domain driven rules.
Example
Add single role
$user->assignRole('admin'); $user->hasRole('admin'); // true
Add many roles
$user->assignRole(['admin','user']); $user->hasRole('admin'); // true $user->hasRole('user'); // true
You can define roles and permissions by code at config/permission.php.
'role' => [ 'admin' => [ 'news/*', // Allow all paths beginning with news/ ], 'editor' => [ 'news/*', ], 'user' => [ 'news/show', // Explicitly allow news/show ], ]
You can check permissions by
$admin->hasPermissionTo('news/delete'); // true $editor->hasPermissionTo('news/delete'); // false $user->hasPermissionTo('news/delete'); // false
Installation
# Add library composer require enclave-code/static-auth-manager # Public vendors php artisan vendor:publish
Older than Laravel 5.5 need a service provider registration.
// config/app.php 'providers' => [ EnclaveCode\StaticAuthManager\Providers\PermissionServiceProvider::class, ];
Usage
Add trait to model
use HasRoles;
Using roles
You can define the roles in the config/permission.php file.
// config/permission.php 'roles' => [ 'role_name' => [], 'admin' => [], ],
Assign role/roles
Add a role to a model.
$model->assignRole('admin');
Add a roles to a model.
$model->assignRole(['admin','user']);
Check role/roles
You can check the roles via:
$model->hasRole('admin'); $model->getRoles(); // return collection(['admin'])
$model->hasRole(['admin','user']); $model->getRoles(); // return collection(['admin','user']);
Detach role/roles
You can detach the roles via:
$model->assignRole(['admin','user']); $model->detachRole('admin'); $model->getRoles(); // return collection(['user'])
Using permissions
Permissions are based on the MQTT syntax. Permissions are specified as path. Thus, individual security levels can be mapped and generally released via wildcards.
Check permissions
$model->hasPermissionTo('users/show/email');
$model->hasPermissionTo(['users/show', 'users/edit']);
$model->hasAnyPermission('users/show/email');
$model->hasAnyPermission(['users/show', 'users/edit']);
Configuration
*Wildcard for everything following
You can define the role permissions in the config/permission.php file.
// config/permission.php 'roles' => [ 'role_name' => [ 'users/*' ], 'admin' => [ 'users/create', ], ],
Using Blade directives
You can use Blade directives in your views.
Role
@role('admin') Show if user is admin @endrole
@unlessrole('admin') Show if user is not admin @endunlessrole
Permission
@permission('user/edit') Show if user has rights to user/edit @endpermission
You can use several permissions too.
@permission('user/edit|user/create') Show if user has rights to user/edit AND user/create @endpermission
@anypermission('user/edit|user/create') Show if user has rights to user/edit OR user/create @endanypermission
Middleware
Add the middleware to your src/Http/Kernel.php
class Kernel extends HttpKernel { ... protected $routeMiddleware = [ ... 'permission' => \EnclaveCode\StaticAuthManager\Middleware\HasAnyPermissionMiddleware::class, 'role' => \EnclaveCode\StaticAuthManager\Middleware\HasRoleMiddleware::class ] }
And use it like
// If user has 'admin' or 'user' role Route::group(['middleware' => ['role:admin|user']], function () { // }) // If user has 'admin' role Route::group(['middleware' => ['role:admin']], function () { // }) // If user has 'user/create' Route::group(['middleware' => ['permission:create/user']], function () { // }) // If user has 'user/create' or 'user/edit' Route::group(['middleware' => ['permission:create/user|user/edit']], function () { // })
Config
Example Config
<?php // config/permission.php return [ /** * DB Column name from model */ 'column_name' => env('SAM_ROLE_COLUMN_NAME', 'role'), /** * Roles with permission as path * * - `*` Wildcard everything following * * 'admin' => [ * 'users/*', * ], * 'user' => [ * 'users/create' * ] * */ 'roles' => [], ];
Additional config in .env
# StaticAuthManager - column name in user model SAM_ROLE_COLUMN_NAME='role'
Testing
composer test # same to ./vendor/bin/phpunit
Todo
- Add new migration to user with new column with role
- Describe how roles and permissions work in readme
Credits
Primarily forked from sourceboat/laravel-static-permission.
License
The MIT License (MIT). Please see License File for more information.
enclave-code/static-auth-manager 适用场景与选型建议
enclave-code/static-auth-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「acl」 「laravel」 「permission」 「roles」 「enclave」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 enclave-code/static-auth-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 enclave-code/static-auth-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 enclave-code/static-auth-manager 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
Provide a way to secure accesses to all routes of an symfony application.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
It's a barebone security class written on PHP
This package provides a flexible way to add Role-based Permissions to Laravel
Contao CMS integrity check for some files
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-30