定制 bfg/permission 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bfg/permission

Composer 安装命令:

composer require bfg/permission

包简介

The ability to create and monitor the rules for the Laravel gates

README 文档

README

Install

  1. Install as dependency:
composer require bfg/permission
  1. Set up for mode Permission trait:
<?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
    
    // ...
}
  1. 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_id and id user-friendly gate. These fields are configured in the settings user_eq_field and model_eq_field. Or you can write a class verification rule by adding the gateCheck(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 我们能提供哪些服务?
定制开发 / 二次开发

基于 bfg/permission 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-09