enclave-code/static-auth-manager 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

enclave-code/static-auth-manager

Composer 安装命令:

composer require enclave-code/static-auth-manager

包简介

Static manage roles and permission in Laravel

README 文档

README

Manage user permissions and roles in your Laravel application by domain driven rules.

Example

Add single role

$user->assignRole('admin');

$user->hasRole('admin'); // true

Add many roles

$user->assignRole(['admin','user']);

$user->hasRole('admin'); // true
$user->hasRole('user'); // true

You can define roles and permissions by code at config/permission.php.

'role' => [
  'admin' => [
    'news/*', // Allow all paths beginning with news/
  ],
  'editor' => [
    'news/*',
  ],
  'user' => [
    'news/show', // Explicitly allow news/show
  ],
]

You can check permissions by

$admin->hasPermissionTo('news/delete'); // true
$editor->hasPermissionTo('news/delete'); // false
$user->hasPermissionTo('news/delete'); // false

Installation

# Add library
composer require enclave-code/static-auth-manager

# Public vendors
php artisan vendor:publish

Older than Laravel 5.5 need a service provider registration.

// config/app.php

'providers' => [
  EnclaveCode\StaticAuthManager\Providers\PermissionServiceProvider::class,
];

Usage

Add trait to model

  use HasRoles;

Using roles

You can define the roles in the config/permission.php file.

// config/permission.php

'roles' => [
  'role_name' => [],
  'admin' => [],
],

Assign role/roles

Add a role to a model.

$model->assignRole('admin');

Add a roles to a model.

$model->assignRole(['admin','user']);

Check role/roles

You can check the roles via:

$model->hasRole('admin');

$model->getRoles(); // return collection(['admin'])
$model->hasRole(['admin','user']);


$model->getRoles(); // return collection(['admin','user']);

Detach role/roles

You can detach the roles via:

$model->assignRole(['admin','user']);
$model->detachRole('admin');


$model->getRoles(); // return collection(['user'])

Using permissions

Permissions are based on the MQTT syntax. Permissions are specified as path. Thus, individual security levels can be mapped and generally released via wildcards.

Check permissions

$model->hasPermissionTo('users/show/email');
$model->hasPermissionTo(['users/show', 'users/edit']);
$model->hasAnyPermission('users/show/email');
$model->hasAnyPermission(['users/show', 'users/edit']);

Configuration

  • * Wildcard for everything following

You can define the role permissions in the config/permission.php file.

// config/permission.php

'roles' => [
  'role_name' => [
    'users/*'
  ],
  'admin' => [
    'users/create',
  ],
],

Using Blade directives

You can use Blade directives in your views.

Role

@role('admin')
  Show if user is admin
@endrole
@unlessrole('admin')
  Show if user is not admin
@endunlessrole

Permission

@permission('user/edit')
  Show if user has rights to user/edit
@endpermission

You can use several permissions too.

@permission('user/edit|user/create')
  Show if user has rights to user/edit AND user/create
@endpermission
@anypermission('user/edit|user/create')
 Show if user has rights to user/edit OR user/create
@endanypermission

Middleware

Add the middleware to your src/Http/Kernel.php

class Kernel extends HttpKernel
{
... 
  protected $routeMiddleware = [
    ...
    'permission' => \EnclaveCode\StaticAuthManager\Middleware\HasAnyPermissionMiddleware::class,
    'role' => \EnclaveCode\StaticAuthManager\Middleware\HasRoleMiddleware::class

  ]

}

And use it like

// If user has 'admin' or 'user' role
Route::group(['middleware' => ['role:admin|user']], function () {
    //
})

// If user has 'admin' role
Route::group(['middleware' => ['role:admin']], function () {
    //
})

// If user has 'user/create'
Route::group(['middleware' => ['permission:create/user']], function () {
    //
})

// If user has 'user/create' or 'user/edit'
Route::group(['middleware' => ['permission:create/user|user/edit']], function () {
    //
})

Config

Example Config

<?php
// config/permission.php

return [
    /**
     * DB Column name from model
     */
    'column_name' => env('SAM_ROLE_COLUMN_NAME', 'role'),

    /**
     * Roles with permission as path
     *
     * - `*` Wildcard everything following
     *
     * 'admin' => [
     *      'users/*',
     * ],
     * 'user' => [
     *     'users/create'
     * ]
     *
     */
    'roles' => [],

];

Additional config in .env

# StaticAuthManager - column name in user model
SAM_ROLE_COLUMN_NAME='role' 

Testing

composer test
# same to
./vendor/bin/phpunit

Todo

  • Add new migration to user with new column with role
  • Describe how roles and permissions work in readme

Credits

Primarily forked from sourceboat/laravel-static-permission.

License

The MIT License (MIT). Please see License File for more information.

enclave-code/static-auth-manager 适用场景与选型建议

enclave-code/static-auth-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「security」 「acl」 「laravel」 「permission」 「roles」 「enclave」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 enclave-code/static-auth-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 enclave-code/static-auth-manager 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-30