定制 chehine/role-manager 二次开发

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

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

chehine/role-manager

Composer 安装命令:

composer require chehine/role-manager

包简介

You can use this package whenever you want to integrate an access control system based on roles in a project with MySQL as your RDBMS. It is easy to use and can be integrated with any php framework in the same way.

README 文档

README

RoleManager is a PHP-based role and permission management library designed to facilitate role-based access control (RBAC) in applications. It provides robust functionality for defining, assigning, and querying roles and permissions for different targets, allowing developers to control access in a modular and scalable manner.

Features

  • Retrieve specific roles or permissions by ID.
  • Fetch roles and permissions associated with a target (user or entity).
  • Query permissions associated with specific classes or methods.
  • Assign or remove roles and permissions for targets.
  • Persist roles in storage and manage relationships between roles and permissions.
  • Check access permissions for specific classes and methods dynamically.

Installation

Install via composer:

composer require chehine/role-manager

Command-Line Interface (CLI)

RoleManager includes CLI commands to automate essential setup tasks. Commands must be run from the directory containing the .env file, which should contain the following database configuration keys:

DB_HOST=
DB_PORT=
DB_NAME=
DB_USER=
DB_PASS=

# Optional:
DB_TARGET_TABLE=users

Note: The key DB_TARGET_TABLE refers to the table of the entity that will have roles assigned to it. In most cases, a role is affected to one or many users.

If it is not defined, then the default value will be targets. If your table name is other than targets, then you have to adapt the parameter's value.

Available Commands

  1. vendor/bin/role-manager create-tables

    • Creates necessary database tables for managing roles and permissions.
  2. vendor/bin/role-manager fill-permissions

    • Fills the permissions table based on configuration from a PHP file.

      Options:

      • --config-file="path/to/config.php": Required. Specifies the path to the configuration file containing permission definitions. Without this file, permissions will not be inserted into the database.
      • --update: Optional. Updates any entries in the database that match entries in the configuration file but have modified values.
      • --truncate: Optional. Deletes all permissions from the database.
      • --clear-cache: Optional. Clears cached permission data to resolve conflicts that might occur between database and cache.

Note: The permissions configuration file should follow this format:

return [
    'permissions' => [
        \Namespace\Class1::class => [
            [
                'name' => 'view',
                'description' => 'View class1 details',
                'method' => 'viewDetails'
            ],
            [
                'name' => 'edit',
                'description' => 'Edit class1',
                'method' => 'editClass1'
            ]
        ],
        \Namespace\Class2::class => [
            [
                'name' => 'approval',
                'description' => 'Approve a post',
                'method' => 'approvePosts'
            ]
        ]
    ]
];

Code Usage

Configuration & Initialization

You can configure the library with a .env file and a PHP configuration file that defines database credentials and permissions.

Initialize RoleManager with paths to these files for setup.

$roleManager = new RoleManager('/path/to/config.php', '/path/to/.env'); 

Example Usages

  • Creating a new role
$role = new Role();
$role->setName('Editor');
$role->setDescription('Role with permissions to edit content');

// Persist the role to the database
$roleManager->persistRole($role);

The persistRole method will save the new role to the database, making it available for assigning permissions and associating with targets.

  • Retrieve a Role by ID
$role = $roleManager->getRoleById('role_id'); 
  • Get All Roles
$roles = $roleManager->getAllRoles(); 
  • Fetch Permissions for a Target
$permissions = $roleManager->getPermissionsByTarget('target_id'); 
  • Add Permission to Role
$permission = $roleManager->getPermissionById('permission_id'); 
$role = $roleManager->getRoleById('role_id');
$roleManager->addPermissionToRole($permission, $role); 
  • Check Permission for a Class and Method
if (RoleManager::isPermitted($targetId, MyClass::class, 'methodName')) 
{ 
    // Permission granted 
} 

Note: isPermitted can automatically detect the calling class and method. If invoked within a class-method combination defined as a permission, it will match that combination without requiring parameters.

Example:

<?php
namespace Namespace

class Class1
{
    //...
    public function viewDetails() : void
    {
        if (RoleManager::isPermitted($targetId)) 
        { 
            // Permission granted (The permission is defined in the configurations file)
        } 
    }
    //...
}
?>

License

MIT License

Copyright (c) [Chehine Ammari] [2024]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-11-06