rs/laravel-doorman
Composer 安装命令:
composer require rs/laravel-doorman
包简介
Will the doorman let you in? Only if you've got the right set of permissions.
README 文档
README
This package provides traits to use to quickly scaffold role based permissions to your laravel project.
Once installed we can do the following:
// Give a permission to a role $role->givePermissionTo('view users'); // Assign a user to a role $user->assignRole($role); // Check if a user has permission to $user->can('view users');
Permissions are registered with Laravel's gate and so it is possible to check using any laravel gate methods.
Installation
composer require rs/laravel-doorman
The service provider will be automatically registered.
Configuration file can be published using the artisan command.
php artisan vendor:publish --tag="doorman-config"
Configuration
Configuration allows you to specify which models you would like to use for the Role and Permission. By default they use models defined by the package. If extra functionality is required or you would like to use your own models then they can be updated here.
Migrations
php artisan migrate
This will run the default migrations needed. If you like to change the default migrations you can publish the migration files.
php artisan vendor:publish --tag="doorman-migrations"
If you dont want any migrations to run then you can disable the migrations in the config file.
Basic usage
User
Add the HasPermissionsViaRoles trait to your User model.
use Illuminate\Foundation\Auth\User as Authenticatable; use Redsnapper\LaravelDoorman\Models\Traits\HasPermissionsViaRoles; class User extends Authenticatable { use HasPermissionsViaRoles; // ... }
A role can be assigned to any user:
$user->assignRole('writer'); // You can also assign multiple roles at once $user->assignRole('editor', 'admin'); // or as an array $user->assignRole(['editor', 'admin']);
A role can be removed from a user:
$user->removeRole('editor');
Roles can also be synced:
// All current roles will be removed from the user and replaced by the array given $user->syncRoles(['editor', 'admin']);
You can determine if a user has a certain role:
$user->hasRole($role);
Permissions and roles can be accessed from the user using the HasPermissionsViaRoles trait.
// permissions relaitionship $permissions = $user->permissions; // roles relationship $roles = $user->roles
You can check if a user has a permission:
$user->hasPermissionTo('edit users'); // Name of permission $user->hasPermissionTo($somePermission->id); // Id of permission $user->hasPermissionTo($somePermission); // Permission Model
Permissions and roles
A permission can be assigned to a role using 1 of these methods:
$role->givePermissionTo($permission); $permission->assignRole($role);
Multiple permissions can be synced to a role using 1 of these methods:
$role->syncPermissions($permissions); $permission->syncRoles($roles);
A permission can be removed from a role using 1 of these methods:
$role->revokePermissionTo($permission); $permission->removeRole($role);
Permission and role customization
If you would like to setup your own permission and role models then you can update the configuration to use your own models.
The existing doorman models can be extended or the models can use the existing traits.
When implementing your own models the models must fulfil the Role and Permission Contracts.
An example of a role model.
namespace App; use Illuminate\Database\Eloquent\Model; use Redsnapper\LaravelDoorman\Models\Contracts\Role as RoleContract; use Redsnapper\LaravelDoorman\Models\Traits\HasPermissions; class Role extends Model implements RoleContract { use HasPermissions; }
An example of the permission model.
namespace App; use Illuminate\Database\Eloquent\Model; use Redsnapper\LaravelDoorman\Models\Contracts\Permission as PermissionContract; use Redsnapper\LaravelDoorman\Models\Traits\HasRoles; use Redsnapper\LaravelDoorman\Models\Traits\PermissionIsFindable; class Permission extends Model implements PermissionContract { use HasRoles, PermissionIsFindable; }
rs/laravel-doorman 适用场景与选型建议
rs/laravel-doorman 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.78k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rs/laravel-doorman 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rs/laravel-doorman 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: mit
- 更新时间: 2020-09-07