bhhaskin/laravel-roles-permissions
Composer 安装命令:
composer require bhhaskin/laravel-roles-permissions
包简介
Roles and permissions support for Laravel applications.
README 文档
README
Roles and permissions management for Laravel applications.
Installation
composer require bhhaskin/laravel-roles-permissions
Optionally publish the configuration and migration stubs:
php artisan vendor:publish --provider="Bhhaskin\RolesPermissions\RolesPermissionsServiceProvider" --tag="laravel-roles-permissions-config" php artisan vendor:publish --provider="Bhhaskin\RolesPermissions\RolesPermissionsServiceProvider" --tag="laravel-roles-permissions-migrations"
Run the migrations:
php artisan migrate
Setup
Apply the trait to any authenticatable model that should handle roles and permissions:
use Bhhaskin\RolesPermissions\Models\Permission; use Bhhaskin\RolesPermissions\Models\Role; use Bhhaskin\RolesPermissions\Traits\HasRoles; class User extends Authenticatable { use HasRoles; }
By default the package ships with Role and Permission Eloquent models. You can swap these (and the underlying table names) by publishing the config file.
Usage
$admin = Role::create(['name' => 'Administrator', 'slug' => 'admin']); $editor = Role::create(['name' => 'Editor', 'slug' => 'editor']); $publishPosts = Permission::create(['name' => 'Publish posts', 'slug' => 'publish-posts']); $user->assignRole($admin); $user->givePermission($publishPosts); $user->hasRole('admin'); // true $user->hasPermission('publish-posts'); // true $admin->uuid; // UUIDs are generated automatically for frontend consumers
Roles automatically inherit all attached permissions, so hasPermission will return true when a user has a role that grants the ability.
Roles and permissions include a generated uuid column, and the default route key is switched to use it so API responses can safely expose the identifier without leaking incremental IDs.
Laravel's class-based factory discovery is supported out of the box; pull in the package and you can call Role::factory() or Permission::factory() from your tests or seeders. A convenience database seeder (Bhhaskin\RolesPermissions\Database\Seeders\RolesPermissionsSeeder) is also provided—run it with php artisan db:seed --class="Bhhaskin\RolesPermissions\Database\Seeders\RolesPermissionsSeeder" to quickly populate sample data. If you prefer to customize them, publish the assets:
php artisan vendor:publish --provider="Bhhaskin\RolesPermissions\RolesPermissionsServiceProvider" --tag="laravel-roles-permissions-factories" php artisan vendor:publish --provider="Bhhaskin\RolesPermissions\RolesPermissionsServiceProvider" --tag="laravel-roles-permissions-seeders"
Configuration
Publish the config to tweak model classes, table names, or caching preferences. The cache section is reserved for future enhancements; for now it stays disabled by default.
Enable object-level assignments by setting object_permissions.enabled to true in the published config. When enabled you can scope roles and permissions to individual models:
config(['roles-permissions.object_permissions.enabled' => true]); $editor = Role::create(['name' => 'Project Editor', 'slug' => 'project-editor']); $approve = Permission::create(['name' => 'Approve Post', 'slug' => 'approve-post']); $editor->permissions()->attach($approve); $user->assignRole($editor, $post); // scoped to this $post instance $user->givePermission($approve, $post); // direct permission for this post $user->hasRole('project-editor', $post); // true $user->hasPermission('approve-post', $post); // true $user->hasPermission('approve-post'); // false, only scoped to the post
If you need different role catalogs for different models (for example teams vs organizations) define role_scopes in the config:
'role_scopes' => [ 'team' => App\\Models\\Team::class, 'organization' => App\\Models\\Organization::class, ],
Roles created with a matching scope value (or created through Role::factory()->forScope('team')) can then only be assigned when that context model is supplied, while standalone roles continue to work globally. You can query the catalog by scope using the provided helpers:
Role::forScope('team')->get(); // only team roles Permission::forScope('organization')->get(); // permissions scoped to organizations
Scoped permission creation
Scoped permissions work the same way as scoped roles. You can generate them via the factories and attach them to matching roles:
$permission = Permission::factory() ->forScope('organization') ->create([ 'name' => 'View Organization Metrics', 'slug' => 'view-org-metrics', ]); Role::factory() ->forScope('organization') ->create(['name' => 'Organization Analyst', 'slug' => 'org-analyst']) ->permissions() ->attach($permission);
To list abilities for a given tenant type you can chain the helpers:
$orgPermissionSlugs = Permission::forScope('organization')->pluck('slug');
bhhaskin/laravel-roles-permissions 适用场景与选型建议
bhhaskin/laravel-roles-permissions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「laravel」 「roles」 「permissions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bhhaskin/laravel-roles-permissions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bhhaskin/laravel-roles-permissions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bhhaskin/laravel-roles-permissions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
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
Laravel JWT auth service package
统计信息
- 总下载量: 70
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-01