muan/laravel-acl
Composer 安装命令:
composer require muan/laravel-acl
包简介
ACL package for Laravel
关键字:
README 文档
README
Muan Acl is a PHP package for Laravel Framework, used for manipulation of access control list. Package is providing an easier way to control roles and permissions of users on your site.
Requirements
- PHP >=7.0
Install
- Type next command in your terminal:
composer require muan/laravel-acl
- Add the service provider to your config/app.php file in section providers:
Laravel 5.5 uses Package Auto-Discovery, so does not require you to manually add the ServiceProvider.
'providers' => [ // ... Muan\Acl\AclServiceProvider::class, // ... ],
- Run the migrations:
php artisan migrate
Usage
Use the following traits on your User model:
// ... use Muan\Acl\Traits\{HasRolesTrait, HasPermissionsTrait}; class User extends Authenticatable { use HasRolesTrait, HasPermissionsTrait; // ... Your User Model Code }
Using observer
To bind the base role to the user after registration, you can specify a public property $baseRole.
For example:
class User extends Authenticatable { // ... /** * Attach base role */ public $baseRole = 'user'; // ... }
Using in code
Check role
if ($user->hasRole('admin')) { // User is admin } // or if ($user->hasRole('admin', 'writer')) { // User is admin or writer }
Attach role
$user->attachRole(10, "moderator")
The same function, detach role
$user->detachRole('moder'); // ... $user->detachRole('admin', '3', '2');
Clear all roles
$user->clearRoles();
Check permission
if ($user->hasPermission('create post')) { // User has permission "create post" }
Attach permissions
$user->attachPermission("update post");
Detach permissions
$user->detachPermission("remove post");
Clear all permissions
$user->clearPermissions();
See the code for more information... =)
Commands for manipulation
Permissions
Create new permission
php artisan permission:add "create post"
Rename permission
php artisan permission:rename "create post" create.post
Remove permission
php artisan permission:remove "create post"
Show all permissions
php artisan permission:list
Roles
Create new role
php artisan role:add admin
Rename role
php artisan role:rename admin superuser
Remove role
php artisan role:remove admin
View all roles
php artisan role:list
Attach permissions to role
php artisan role:attach admin --id=2 --id=3 --name="create post"
Detach permissions from role
php artisan role:detach admin --id=3 --name="destroy user"
Clear all attached permissions
php artisan role:clear
View information about role and show all attached permissions
php artisan role:view admin
Users
Attach roles
php artisan user:role-attach 5 --id=2 --name=moderator
Detach roles
php artisan user:role-detach 5 --id=2 --name=admin
Detached all roles from user
php artisan user:role-clear
Attach permissions
php artisan user:permission-attach 5 --id=7 --name="remove comment"
Detach permissions
php artisan user:permission-detach 5 --id=2 --name="read secret post"
Detached all permission from user
php artisan user:permission-clear
View information about user, all attached roles and permissions
php artisan user:view 5
where 5 is ID of user.
Using blade directives
You also can use directives to verify the currently logged in user has any roles or permissions.
Check roles:
@role('admin') <!-- User has role admin --> @elserole('writer') <!-- User has role writer --> <!-- ... --> @else <!-- User with other roles --> @endrole
or check more roles in one directive:
@role(['admin', 'writer']) <!-- User has next roles: admin, writer --> @endrole
Check permissions:
@can('create post') <!-- User can create post --> @elsecan('edit post') <!-- User can edit post --> @endcan
Using middlewares
You can use role middleware for check access to some routes
Route::middleware(['role:admin'])->group(function() { // Only for user with role admin Route::get('/admin', function() { // some code }); });
also you can use permission middleware
Route::middleware(['permission:create post'])->group(function() { // Only for user with permission create post Route::get('/admin/post', function() { // some code }); });
or use role and permission middleware together
Route::middleware(['role:moderator', 'permission:remove post'])->group(function() { // Only for user with role moderator and with permission create post Route::get('/admin/post/remove', function() { // some code }); });
License
Muan Laravel Acl package is licensed under the MIT License.
muan/laravel-acl 适用场景与选型建议
muan/laravel-acl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 138 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「acl」 「laravel」 「permission」 「role」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 muan/laravel-acl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 muan/laravel-acl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 muan/laravel-acl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
This package provides a flexible way to add Role-based Permissions to Laravel
Simple ASCII output of array data
AclManager plugin for CakePHP 3.x
统计信息
- 总下载量: 138
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-12-13