From 4d906f6fc45b19ac88cb58323a9e542c8476d195 Mon Sep 17 00:00:00 2001 From: ElisDN Date: Wed, 17 Feb 2016 16:26:18 +0300 Subject: [PATCH] Added auth manager events --- modules/user/components/AuthManager.php | 31 ++++++++++++------- .../components/AuthRoleModelInterface.php | 21 ------------- .../events/RemoveAllAssignmentsEvent.php | 10 ++++++ .../user/components/events/RemoveAllEvent.php | 10 ++++++ .../components/events/RemoveRoleEvent.php | 10 ++++++ .../components/events/RenameRoleEvent.php | 11 +++++++ modules/user/models/User.php | 20 ------------ 7 files changed, 60 insertions(+), 53 deletions(-) create mode 100644 modules/user/components/events/RemoveAllAssignmentsEvent.php create mode 100644 modules/user/components/events/RemoveAllEvent.php create mode 100644 modules/user/components/events/RemoveRoleEvent.php create mode 100644 modules/user/components/events/RenameRoleEvent.php diff --git a/modules/user/components/AuthManager.php b/modules/user/components/AuthManager.php index c8e9bcd..bde4a09 100644 --- a/modules/user/components/AuthManager.php +++ b/modules/user/components/AuthManager.php @@ -2,6 +2,10 @@ namespace app\modules\user\components; +use app\modules\user\components\events\RemoveAllAssignmentsEvent; +use app\modules\user\components\events\RemoveAllEvent; +use app\modules\user\components\events\RemoveRoleEvent; +use app\modules\user\components\events\RenameRoleEvent; use yii\base\InvalidParamException; use yii\base\InvalidValueException; use yii\rbac\Assignment; @@ -10,6 +14,11 @@ class AuthManager extends PhpManager { + const EVENT_RENAME_ROLE = 'renameRole'; + const EVENT_REMOVE_ROLE = 'removeRole'; + const EVENT_REMOVE_ALL = 'removeAll'; + const EVENT_REMOVE_ALL_ASSIGNMENTS = 'removeAllAssignments'; + /** * @var string User model class name * must be instance of AuthRoleModelInterface @@ -66,9 +75,10 @@ protected function updateItem($name, $item) { if (parent::updateItem($name, $item)) { if ($item->name !== $name) { - /** @var AuthRoleModelInterface $class */ - $class = $this->modelClass; - $class::updateAuthGlobalRoleName($name, $item->name); + $this->trigger(self::EVENT_RENAME_ROLE, new RenameRoleEvent([ + 'oldRoleName' => $name, + 'newRoleName' => $item->name, + ])); } return true; } @@ -81,9 +91,9 @@ protected function updateItem($name, $item) public function removeItem($item) { if (parent::removeItem($item)) { - /** @var AuthRoleModelInterface $class */ - $class = $this->modelClass; - $class::removeAuthGlobalRoleName($item->name); + $this->trigger(self::EVENT_REMOVE_ROLE, new RemoveRoleEvent([ + 'roleName' => $item->name, + ])); return true; } return false; @@ -92,17 +102,13 @@ public function removeItem($item) public function removeAll() { parent::removeAll(); - /** @var AuthRoleModelInterface $class */ - $class = $this->modelClass; - $class::removeAuthGlobalRoleNames(); + $this->trigger(self::EVENT_REMOVE_ALL, new RemoveAllEvent()); } public function removeAllAssignments() { parent::removeAllAssignments(); - /** @var AuthRoleModelInterface $class */ - $class = $this->modelClass; - $class::removeAuthGlobalAssignments(); + $this->trigger(self::EVENT_REMOVE_ALL_ASSIGNMENTS, new RemoveAllAssignmentsEvent()); } /** @@ -154,6 +160,7 @@ public function revokeAll($userId) /** * @param integer $userId + * @throws \yii\base\InvalidValueException * @return null|AuthRoleModelInterface */ private function getUser($userId) diff --git a/modules/user/components/AuthRoleModelInterface.php b/modules/user/components/AuthRoleModelInterface.php index 4092aba..ce52968 100644 --- a/modules/user/components/AuthRoleModelInterface.php +++ b/modules/user/components/AuthRoleModelInterface.php @@ -16,27 +16,6 @@ public static function findAuthRoleIdentity($id); */ public static function findAuthIdsByRoleName($roleName); - /** - * @param string $oldRoleName - * @param string $newRoleName - */ - public static function updateAuthGlobalRoleName($oldRoleName, $newRoleName); - - /** - * @param string $roleName - */ - public static function removeAuthGlobalRoleName($roleName); - - /** - * On all roles removing - */ - public static function removeAuthGlobalRoleNames(); - - /** - * On all assignments removing - */ - public static function removeAuthGlobalAssignments(); - /** * @return array */ diff --git a/modules/user/components/events/RemoveAllAssignmentsEvent.php b/modules/user/components/events/RemoveAllAssignmentsEvent.php new file mode 100644 index 0000000..980214e --- /dev/null +++ b/modules/user/components/events/RemoveAllAssignmentsEvent.php @@ -0,0 +1,10 @@ +where(['role' => $roleName])->select(['id'])->column(); } - public static function updateAuthGlobalRoleName($oldRoleName, $newRoleName) - { - self::updateAll(['role' => $newRoleName], ['role' => $oldRoleName]); - } - - public static function removeAuthGlobalRoleName($roleName) - { - self::updateAll(['role' => null], ['role' => $roleName]); - } - - public static function removeAuthGlobalRoleNames() - { - self::updateAll(['role' => null]); - } - - public static function removeAuthGlobalAssignments() - { - self::updateAll(['role' => null]); - } - public function getAuthRoleNames() { return [$this->role];