Skip to content

Commit

Permalink
Added auth manager events
Browse files Browse the repository at this point in the history
  • Loading branch information
ElisDN committed May 8, 2018
1 parent ef0b56f commit 4d906f6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 53 deletions.
31 changes: 19 additions & 12 deletions modules/user/components/AuthManager.php
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -154,6 +160,7 @@ public function revokeAll($userId)

/**
* @param integer $userId
* @throws \yii\base\InvalidValueException
* @return null|AuthRoleModelInterface
*/
private function getUser($userId)
Expand Down
21 changes: 0 additions & 21 deletions modules/user/components/AuthRoleModelInterface.php
Expand Up @@ -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
*/
Expand Down
10 changes: 10 additions & 0 deletions modules/user/components/events/RemoveAllAssignmentsEvent.php
@@ -0,0 +1,10 @@
<?php

namespace app\modules\user\components\events;

use yii\base\Event;

class RemoveAllAssignmentsEvent extends Event
{

}
10 changes: 10 additions & 0 deletions modules/user/components/events/RemoveAllEvent.php
@@ -0,0 +1,10 @@
<?php

namespace app\modules\user\components\events;

use yii\base\Event;

class RemoveAllEvent extends Event
{

}
10 changes: 10 additions & 0 deletions modules/user/components/events/RemoveRoleEvent.php
@@ -0,0 +1,10 @@
<?php

namespace app\modules\user\components\events;

use yii\base\Event;

class RemoveRoleEvent extends Event
{
public $roleName;
}
11 changes: 11 additions & 0 deletions modules/user/components/events/RenameRoleEvent.php
@@ -0,0 +1,11 @@
<?php

namespace app\modules\user\components\events;

use yii\base\Event;

class RenameRoleEvent extends Event
{
public $oldRoleName;
public $newRoleName;
}
20 changes: 0 additions & 20 deletions modules/user/models/User.php
Expand Up @@ -164,26 +164,6 @@ public static function findAuthIdsByRoleName($roleName)
return static::find()->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];
Expand Down

0 comments on commit 4d906f6

Please sign in to comment.