bfg/permission
Composer 安装命令:
composer require bfg/permission
包简介
The ability to create and monitor the rules for the Laravel gates
README 文档
README
Install
- Install as dependency:
composer require bfg/permission
- Set up for mode
Permissiontrait:
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use App\Models\Traits\User\UserHasRole; use Illuminate\Database\Eloquent\Factories\HasFactory; use Bfg\Permission\Traits\Permissions; /** * User Class * @package App\Models */ class User extends Authenticatable { use Notifiable, UserHasRole, HasFactory, Permissions; // Like this // ... }
- Install resources and tables:
php artisan install bfg/permission
About the concept.
The ability to create and monitor the rules for the
Laravel gates with
the distribution on the role and the ability to control it conveniently.
Also comes with a user role model and a list of ready-made roles,
such as: Root, Administrator, Moderator, User, Guest.
Description
The package has a hierarchy of roles and can distribute access between
them, using the Laravel gates
system. The warehouse for the gate is simply "PHP" file with an array in
the "Storage" folder.
Where can I use it?
Always when you want to use the authorization control system "Laravel" through the gate, but do not want to cheat a whole bunch of rumbled rows with a gate or create a bunch of files with politicians if you need to control all your gates and disable or switch them between the roles of users.
Publish
Publish configs
php artisan vendor:publish --tag=permission-config
Publish migrations
php artisan vendor:publish --tag=permission-migrations
Commands
Commands for managing access and distribution of them between roles.
Permission list
In order to display a full list of rules for the gate.
Usage: permissions [<find>] Arguments: find Find word
But it happens so that the rules becomes too much and on this can be used by searching, specifying the search word immediately after the command, for example:
php artisan permissions message
Output:
+---------------------+--------+---------------+-----------+------+-------+ | Name | Global | Administrator | Moderator | User | Guest | +---------------------+--------+---------------+-----------+------+-------+ | viewAny-message | Yes | No | Yes | Yes | Yes | | view-message | Yes | No | Yes | Yes | Yes | | create-message | Yes | No | Yes | Yes | Yes | | update-message | Yes | No | Yes | Yes | Yes | | delete-message | Yes | No | Yes | Yes | Yes | | restore-message | Yes | No | Yes | Yes | Yes | | forceDelete-message | Yes | No | Yes | Yes | Yes | +---------------------+--------+---------------+-----------+------+-------+
Allow or add permission
To add immediately with open access or open access to the rule, you must use this command.
Usage: allow [options] [--] <name> [<role_or_user_id>] Arguments: name The name of permission role_or_user_id Role slug or user id in system Options: -r, --resource Resource permission
If you create a rule as a resource:
php artisan allow message -r
You will be created 7 rules with the name you indicated, namely:
viewAny-message, view-message, create-message, update-message,
delete-message, restore-message, forceDelete-message
In order to manage access for a role or for a user, you can add the following parameter that calls for the user ID or role name:
php artisan allow message guest -r
Opens access to all communication resources for the guest.
Disallow or add permission
To immediately add with closed access or close access to the rule, you must use this command.
Usage: disallow [options] [--] <name> [<role_or_user_id>] Arguments: name The name of permission role_or_user_id Role slug or user id in system Options: -r, --resource Resource permission
All the logic of the team is identical to the opening team.
php artisan disallow message guest -r
Delete permission
To remove the rules of the gate from the general list.
Usage: permission:delete [options] [--] <name> Arguments: name The name of permission Options: -r, --resource Make resource permission
php artisan permission:delete view-message
You can delete one rule or immediately all its resources using the resource flag:
php artisan permission:delete message -r
How to use?
All rules created by you automatically fall into the system of the gate
of "Laravel" and in this can be used as before you used the system
of the Laravel gates.
<?php namespace App\Http\Controllers; use App\Models\Message; use Illuminate\Http\Request; use Illuminate\Support\Facades\Gate; class MessageController extends Controller { /** * Update the given message. * * @param \Illuminate\Http\Request $request * @param \App\Models\Message $message * @return \Illuminate\Http\Response */ public function update(Request $request, Message $message) { if (! Gate::allows('update-message', $message)) { abort(403); } // Update the message... } }
if (Gate::forUser($user)->allows('update-message', $message)) { // The user can update the message... } if (Gate::forUser($user)->denies('update-message', $message)) { // The user can't update the message... }
if (Gate::any(['update-post', 'delete-message'], $message)) { // The user can update or delete the message... } if (Gate::none(['update-post', 'delete-message'], $message)) { // The user can't update or delete the message... }
Gate::authorize('update-message', $message);
Important! If you transmit as a parameter to the gate model that is recovered, the rules will check the field
user_idandiduser-friendly gate. These fields are configured in the settingsuser_eq_fieldandmodel_eq_field. Or you can write a class verification rule by adding thegateCheck(string $rule, Model $user, Model $model)method into it.
bfg/permission 适用场景与选型建议
bfg/permission 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「extension」 「laravel」 「permission」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bfg/permission 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bfg/permission 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bfg/permission 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
Adds more BBCode
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-09