elisdn / yii2-hybrid-authmanager
为 Yii2 框架提供的混合 RBAC AuthManager。
1.0.2
2021-08-01 13:17 UTC
Requires
- yiisoft/yii2: ~2.0
Requires (Dev)
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2024-09-23 03:41:51 UTC
README
该扩展扩展了 yii\rbac\PhpManager
并允许将用户分配存储在任何存储中,而不是 app/rbac/assignments.php
文件。
安装
使用 composer 安装
composer require elisdn/yii2-hybrid-authmanager
或者添加
"elisdn/yii2-hybrid-authmanager": "*"
到你的 composer.json
文件的 require 部分。
在配置中设置 authManager
组件
'components' => [ ... 'authManager' => [ 'class' => 'elisdn\hybrid\AuthManager', 'modelClass' => 'app\modules\User', ], ],
并在你的模型类中实现 AuthRoleModelInterface
。
使用示例
在 user
表的 role
字段中存储单个角色
namespace app\models; use elisdn\hybrid\AuthRoleModelInterface; class User extends ActiveRecord implements IdentityInterface, AuthRoleModelInterface { ... public static function findAuthRoleIdentity($id) { return static::findOne($id); } public static function findAuthIdsByRoleName($roleName) { return static::find()->where(['role' => $roleName])->select('id')->column(); } public function getAuthRoleNames() { return (array)$this->role; } public function addAuthRoleName($roleName) { $this->updateAttributes(['role' => $this->role = $roleName]); } public function removeAuthRoleName($roleName) { $this->updateAttributes(['role' => $this->role = null]); } public function clearAuthRoleNames() { $this->updateAttributes(['role' => $this->role = null]); } ... }
存储多个角色
class User extends ActiveRecord implements IdentityInterface, AuthRoleModelInterface { ... public function getAuthRoleNames() { return $this->roles; } public function addAuthRoleName($roleName) { $this->updateAttributes(['roles' => $this->roles = array_merge($this->roles, [$roleName])]); } public function removeAuthRoleName($roleName) { $this->updateAttributes(['roles' => $this->roles = array_diff($this->roles, [$roleName])]); } public function clearAuthRoleNames() { $this->updateAttributes(['roles' => $this->roles = []]); } ... }
或者使用 JSON 序列化
class User extends ActiveRecord implements IdentityInterface, AuthRoleModelInterface { ... public function getAuthRoleNames() { return (array)Json::decode($this->roles); } public function addAuthRoleName($roleName) { $roles = (array)Json::decode($this->roles); $this->roles[] = $roleName; $this->updateAttributes(['role' => $this->roles = Json::encode($this->roles)]); } public function removeAuthRoleName($roleName) { $roles = (array)Json::decode($this->roles); $this->roles = array_diff($this->roles, [$roleName]) $this->updateAttributes(['role' => $this->roles = Json::encode($this->roles)]); } public function clearAuthRoleNames() { $this->updateAttributes(['role' => $this->roles = Json::encode([])]); } ... }
处理角色事件(可选)
如果你想在系统事件中更新你的存储,只需添加你的事件处理程序
class User extends ActiveRecord implements IdentityInterface, AuthRoleModelInterface { ... public static function onRenameRole(RenameRoleEvent $event) { self::updateAll(['role' => $event->newRoleName], ['role' => $event->oldRoleName]); } public static function onRemoveRole(RemoveRoleEvent $event) { self::updateAll(['role' => null], ['role' => $event->roleName]); } public static function onRemoveAll(RemoveAllEvent $event) { self::updateAll(['role' => null]); } public static function onRemoveAllAssignments(RemoveAllAssignmentsEvent $event) { self::updateAll(['role' => null]); } }
并注册处理程序
'authManager' => [ 'class' => 'elisdn\hybrid\AuthManager', 'modelClass' => 'app\models\User', 'on renameRole' => ['app\models\User', 'onRenameRole'], 'on removeRole' => ['app\models\User', 'onRemoveRole'], 'on removeAll' => ['app\models\User', 'onRemoveAll'], 'on removeAllAssignments' => ['app\models\User', 'onRemoveAllAssignments'], ],