Commit c3f4e005 by Qiang Xue

Refactored `yii\rbac\PhpManager`

Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()`
parent 12c3b59b
......@@ -7,7 +7,7 @@ Yii Framework 2 Change Log
- Bug #3091: Fixed inconsistent treatment of `Widget::run()` when a widget is used as a container and as a self-contained object (qiangxue)
- Enh #3103: debugger panel is now not displayed when printing a page (githubjeka)
- Enh #3108: Added `yii\debug\Module::enableDebugLogs` to disable logging debug logs by default (qiangxue)
- no changes in this release.
- Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue)
2.0.0-beta April 13, 2014
......
......@@ -7,3 +7,10 @@ The following upgrading instructions are cumulative. That is,
if you want to upgrade from version A to version C and there is
version B between A and C, you need to following the instructions
for both A and B.
Upgrade from Yii 2.0 Beta
-------------------------
* If you used `clearAll()` or `clearAllAssignments()` of `yii\rbac\DbManager`, you should replace
them with `removeAll()` and `removeAllAssignments()` respectively.
......@@ -551,6 +551,17 @@ class DbManager extends BaseManager
/**
* @inheritdoc
*/
public function hasChild($parent, $child)
{
return (new Query)
->from($this->itemChildTable)
->where(['parent' => $parent->name, 'child' => $child->$name])
->one($this->db) !== false;
}
/**
* @inheritdoc
*/
public function getChildren($name)
{
$query = (new Query)
......@@ -627,20 +638,78 @@ class DbManager extends BaseManager
}
/**
* Removes all authorization data.
* @inheritdoc
*/
public function clearAll()
public function removeAll()
{
$this->clearAssignments();
$this->removeAllAssignments();
$this->db->createCommand()->delete($this->itemChildTable)->execute();
$this->db->createCommand()->delete($this->itemTable)->execute();
$this->db->createCommand()->delete($this->ruleTable)->execute();
}
/**
* Removes all authorization assignments.
* @inheritdoc
*/
public function removeAllPermissions()
{
$this->removeAllItems(Item::TYPE_PERMISSION);
}
/**
* @inheritdoc
*/
public function removeAllRoles()
{
$this->removeAllItems(Item::TYPE_ROLE);
}
/**
* Removes all auth items of the specified type.
* @param integer $type the auth item type (either Item::TYPE_PERMISSION or Item::TYPE_ROLE)
*/
protected function removeAllItems($type)
{
if (!$this->supportsCascadeUpdate()) {
$names = (new Query)
->select(['name'])
->from($this->itemTable)
->where(['type' => $type])
->column($this->db);
if (empty($names)) {
return;
}
$key = $type == Item::TYPE_PERMISSION ? 'child' : 'parent';
$this->db->createCommand()
->delete($this->itemChildTable, [$key => $names])
->execute();
$this->db->createCommand()
->delete($this->assignmentTable, ['item_name' => $names])
->execute();
}
$this->db->createCommand()
->delete($this->itemTable, ['type' => $type])
->execute();
}
/**
* @inheritdoc
*/
public function removeAllRules()
{
if (!$this->supportsCascadeUpdate()) {
$this->db->createCommand()
->update($this->itemTable, ['ruleName' => null])
->execute();
}
$this->db->createCommand()->delete($this->ruleTable)->execute();
}
/**
* @inheritdoc
*/
public function clearAssignments()
public function removeAllAssignments()
{
$this->db->createCommand()->delete($this->assignmentTable)->execute();
}
......
......@@ -146,6 +146,14 @@ interface ManagerInterface
public function removeChild($parent, $child);
/**
* Returns a value indicating whether the child already exists for the parent.
* @param Item $parent
* @param Item $child
* @return boolean whether `$child` is already a child of `$parent`
*/
public function hasChild($parent, $child);
/**
* Returns the child permissions and/or roles.
* @param string $name the parent name
* @return Item[] the child permissions and/or roles
......@@ -198,12 +206,30 @@ interface ManagerInterface
public function getAssignments($userId);
/**
* Removes all authorization data.
* Removes all authorization data, including roles, permissions, rules, and assignments.
*/
public function removeAll();
/**
* Removes all permissions.
* All parent child relations will be adjusted accordingly.
*/
public function removeAllPermissions();
/**
* Removes all roles.
* All parent child relations will be adjusted accordingly.
*/
public function removeAllRoles();
/**
* Removes all rules.
* All roles and permissions which have rules will be adjusted accordingly.
*/
public function clearAll();
public function removeAllRules();
/**
* Removes all authorization assignments.
* Removes all role assignments.
*/
public function clearAssignments();
public function removeAllAssignments();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment