ahrengot/laravel-roles-and-permissions
Composer 安装命令:
composer require ahrengot/laravel-roles-and-permissions
包简介
User roles and permissions for Laravel
README 文档
README
Roles and permissions for laravel
Installation
Install the package via composer
composer require ahrengot/laravel-roles-and-permissions
Run the install command to publish the migration, stubs, config file and a basic test
php artisan roles-and-permissions:install
Run the migration to add a role column to your users table. Feel free to modify this migration as needed.
php artisan migrate
Configuring your User model
Add the HasPermissions trait to your user model and an enum cast for the role column. Optionally you can add a default value for the role using the built-in $attributes property.
use \Ahrengot\RolesAndPermissions\Traits\HasPermissions; use App\Enums\UserRole; class User extends Authenticatable { use HasPermissions; protected $casts = [ 'role' => UserRole::class, ]; // Optional default role protected $attributes = [ 'role' => UserRole::User, ] }
Configuring roles and permissions
Your permissions are configured in config/permissions.php.
User roles are defined in App\Enums\UserRole.php. Update these roles to fit your application needs.
Permissions are just simple strings, but this package provides a helper class in App/Permissions/Permission.php that declare each permission as a constant. This provides better editor support and helps prevent typos.
The config file contains an example of defining various permissions for each user role:
return [ 'roles' => [ UserRole::Admin->value => [ Permission::AccessAdminPanel, Permission::CreateApiTokens, ], ], ];
Usage
In blade
<nav> @can(Permission::AccessAdminPanel) <a href="...">Admin panel</a> @endcan <a href="...">Other link</a> </nav>
In policies
public function create(User $user) { return $user->can(Permission::CreatePosts); }
Comparing user roles
The UserRole enum has two comparison methods
$user->role->is(UserRole::Admin); $user->role->isNot(UserRole::Admin);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 2.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-02