krubio/perfect-rbac
Composer 安装命令:
composer require krubio/perfect-rbac
包简介
RBAC - Role Based Access
关键字:
README 文档
README
RBAC Authorization Service Documentation
The RBAC (Role-Based Access Control) Authorization Service provides a simple and effective way to implement role-based access control in your PHP application. This documentation will guide you on how to use the RBAC classes and their methods to perform user role authorization and permission checks.
Prerequisites
Before using the RBAC Authorization Service, make sure you have the following:
- PHP installed on your system
- PDO extension enabled (for database connectivity)
- A database with the required tables (user_roles, roles, role_permissions, and permissions) populated with relevant data
Class Overview
The RBAC Authorization Service consists of the following classes:
AuthorizationService: Provides methods to check if a user role is authorized based on a set of allowed roles.PermissionsRepository: Handles the retrieval of permissions for user roles from the database.UserRolesRepository: Handles the retrieval of user roles from the database.
Getting Started
To start using the RBAC Authorization Service, follow these steps:
- Include the necessary RBAC classes in your PHP file:
use PerfectApp\RBAC\AuthorizationService; use PerfectApp\RBAC\PermissionsRepository; use PerfectApp\RBAC\UserRolesRepository;
- Create an instance of the
PDOclass with the appropriate database connection details.
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
- Create instances of the
PermissionsRepositoryandUserRolesRepositoryclasses, passing thePDOinstance to their constructors.
$permissionsRepository = new PermissionsRepository($pdo); $userRolesRepository = new UserRolesRepository($pdo);
- Create an instance of the
AuthorizationServiceclass, passing theUserRolesRepositoryinstance to its constructor.
$authorizationService = new AuthorizationService($userRolesRepository);
- You are now ready to use the RBAC Authorization Service in your application.
Checking User Role Authorization
The AuthorizationService class provides the isUserRoleAuthorized method to check if a user role is authorized based on a set of allowed roles.
/** * @param int $userId * @param array<mixed> $allowedRoles * @return bool */ public function isUserRoleAuthorized(int $userId, array $allowedRoles): bool
Parameters
$userId(integer): The ID of the user whose role needs to be checked.$allowedRoles(array): An array of allowed roles. The user's role will be compared against these roles.
Return Value
trueif the user's role matches any of the allowed roles, indicating authorization.falseif the user's role does not match any of the allowed roles or an error occurs.
Example Usage
$userId = 1; $allowedRoles = ['admin', 'editor']; if ($authorizationService->isUserRoleAuthorized($userId, $allowedRoles)) { echo "User is authorized."; } else { echo "User is not authorized."; }
Checking User Role Permissions
The PermissionsRepository class provides the userRoleHasPermission method to check if a user role has a specific permission.
/** * @param int $userId * @param string $requiredPermission * @return bool */ public function userRoleHasPermission(int $userId, string $requiredPermission): bool
Parameters
$userId(integer): The ID of the user whose role's permission needs to be checked.$requiredPermission(string): The name of the required permission.
Return Value
- `
true` if the user's role has the required permission.
falseif the user's role does not have the required permission or an error occurs.
Example Usage
$userId = 1; $requiredPermission = 'edit_post'; if ($permissionsRepository->userRoleHasPermission($userId, $requiredPermission)) { echo "User has the required permission."; } else { echo "User does not have the required permission."; }
Conclusion
You have now learned how to use the RBAC Authorization Service to perform user role authorization and permission checks in your PHP application. By following the provided instructions and examples, you can easily integrate RBAC functionality into your application to control access based on user roles and permissions.
krubio/perfect-rbac 适用场景与选型建议
krubio/perfect-rbac 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validaton」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 krubio/perfect-rbac 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 krubio/perfect-rbac 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 krubio/perfect-rbac 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 35
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-27