Skip to content

Commit

Permalink
Fixes yiisoft#3168: Improved the performance of `yii\rbac\DbManager::…
Browse files Browse the repository at this point in the history
…checkAccess()` by caching mechanism
  • Loading branch information
qiangxue committed Feb 13, 2015
1 parent 937dd63 commit d188dd1
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 6 deletions.
3 changes: 2 additions & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Yii Framework 2 Change Log
- Bug #7055: composite IN condition was not generated correctly for certain DBMS (nineinchnick)
- Bug #7074: `yii\data\ArrayDataProvider` did not correctly handle the case `Pagination::pageSize = 0` (kirsenn, qiangxue)
- Bug #7218: `yii\captcha\CaptchaAction` should send response in JSON format (InteLigent, qiangxue)
- Bug #7226: `yii\web\Request::getEtag()` should strip off `-gzip` which may be added by Apache (mcd-php)
- Bug #7226: `yii\web\Request::getEtag()` should strip off `-gzip` which may be added by Apache (mcd-php)
- Enh #3168: Improved the performance of `yii\rbac\DbManager::checkAccess()` by caching mechanism (qiangxue)
- Enh #4710: Added `yii\web\AssetManager::appendTimestamp` to support cache busting for assets (qiangxue)
- Enh #5663: Added support for using `data-params` to specify additional form data to be submitted via the `data-method` approach (usualdesigner, qiangxue)
- Enh #6106: Added ability to specify `encode` for each item of `yii\widgets\Breadcrumbs` (samdark, aleksanderd)
Expand Down
180 changes: 176 additions & 4 deletions framework/rbac/DbManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace yii\rbac;

use Yii;
use yii\caching\Cache;
use yii\db\Connection;
use yii\db\Query;
use yii\db\Expression;
Expand Down Expand Up @@ -58,7 +59,44 @@ class DbManager extends BaseManager
* @var string the name of the table storing rules. Defaults to "auth_rule".
*/
public $ruleTable = '{{%auth_rule}}';

/**
* @var Cache|array|string the cache used to improve RBAC performance. This can be one of the followings:
*
* - an application component ID (e.g. `cache`)
* - a configuration array
* - a [[yii\caching\Cache]] object
*
* When this is not set, it means caching is not enabled.
*
* Note that by enabling RBAC cache, all auth items, rules and auth item parent-child relationships will
* be cached and loaded into memory. This will improve the performance of RBAC permission check. However,
* it does require extra memory and as a result may not be appropriate if your RBAC system contains too many
* auth items. You should seek other RBAC implementations (e.g. RBAC based on Redis storage) in this case.
*
* Also note that if you modify RBAC items, rules or parent-child relationships from outside of this component,
* you have to manually call [[invalidateCache()]] to ensure data consistency.
*
* @since 2.0.3
*/
public $cache;
/**
* @var string the key used to store RBAC data in cache
* @see cache
* @since 2.0.3
*/
public $cacheKey = 'rbac';
/**
* @var Item[] all auth items (name => Item)
*/
protected $items;
/**
* @var Rule[] all auth rules (name => Rule)
*/
protected $rules;
/**
* @var array auth item parent-child relationships (childName => list of parents)
*/
protected $parents;

/**
* Initializes the application component.
Expand All @@ -68,6 +106,9 @@ public function init()
{
parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
if ($this->cache !== null) {
$this->cache = Instance::ensure($this->cache, Cache::className());
}
}

/**
Expand All @@ -76,7 +117,54 @@ public function init()
public function checkAccess($userId, $permissionName, $params = [])
{
$assignments = $this->getAssignments($userId);
return $this->checkAccessRecursive($userId, $permissionName, $params, $assignments);
$this->loadFromCache();
if ($this->items !== null) {
return $this->checkAccessFromCache($userId, $permissionName, $params, $assignments);
} else {
return $this->checkAccessRecursive($userId, $permissionName, $params, $assignments);
}
}

/**
* Performs access check for the specified user based on the data loaded from cache.
* This method is internally called by [[checkAccess()]] when [[cache]] is enabled.
* @param string|integer $user the user ID. This should can be either an integer or a string representing
* the unique identifier of a user. See [[\yii\web\User::id]].
* @param string $itemName the name of the operation that need access check
* @param array $params name-value pairs that would be passed to rules associated
* with the tasks and roles assigned to the user. A param with name 'user' is added to this array,
* which holds the value of `$userId`.
* @param Assignment[] $assignments the assignments to the specified user
* @return boolean whether the operations can be performed by the user.
* @since 2.0.3
*/
protected function checkAccessFromCache($user, $itemName, $params, $assignments)
{
if (!isset($this->items[$itemName])) {
return false;
}

$item = $this->items[$itemName];

Yii::trace($item instanceof Role ? "Checking role: $itemName" : "Checking permission: $itemName", __METHOD__);

if (!$this->executeRule($user, $item, $params)) {
return false;
}

if (isset($assignments[$itemName]) || in_array($itemName, $this->defaultRoles)) {
return true;
}

if (!empty($this->parents[$itemName])) {
foreach ($this->parents[$itemName] as $parent) {
if ($this->checkAccessRecursive($user, $parent, $params, $assignments)) {
return true;
}
}
}

return false;
}

/**
Expand Down Expand Up @@ -126,6 +214,10 @@ protected function checkAccessRecursive($user, $itemName, $params, $assignments)
*/
protected function getItem($name)
{
if (!empty($this->items[$name])) {
return $this->items[$name];
}

$row = (new Query)->from($this->itemTable)
->where(['name' => $name])
->one($this->db);
Expand Down Expand Up @@ -174,6 +266,8 @@ protected function addItem($item)
'updated_at' => $item->updatedAt,
])->execute();

$this->invalidateCache();

return true;
}

Expand All @@ -195,6 +289,8 @@ protected function removeItem($item)
->delete($this->itemTable, ['name' => $item->name])
->execute();

$this->invalidateCache();

return true;
}

Expand Down Expand Up @@ -228,6 +324,8 @@ protected function updateItem($name, $item)
'name' => $name,
])->execute();

$this->invalidateCache();

return true;
}

Expand All @@ -251,6 +349,8 @@ protected function addRule($rule)
'updated_at' => $rule->updatedAt,
])->execute();

$this->invalidateCache();

return true;
}

Expand All @@ -276,6 +376,8 @@ protected function updateRule($name, $rule)
'name' => $name,
])->execute();

$this->invalidateCache();

return true;
}

Expand All @@ -294,6 +396,8 @@ protected function removeRule($rule)
->delete($this->ruleTable, ['name' => $rule->name])
->execute();

$this->invalidateCache();

return true;
}

Expand Down Expand Up @@ -451,6 +555,10 @@ protected function getChildrenRecursive($name, $childrenList, &$result)
*/
public function getRule($name)
{
if ($this->rules !== null) {
return isset($this->rules[$name]) ? $this->rules[$name] : null;
}

$row = (new Query)->select(['data'])
->from($this->ruleTable)
->where(['name' => $name])
Expand All @@ -463,6 +571,10 @@ public function getRule($name)
*/
public function getRules()
{
if ($this->rules !== null) {
return $this->rules;
}

$query = (new Query)->from($this->ruleTable);

$rules = [];
Expand Down Expand Up @@ -543,6 +655,8 @@ public function addChild($parent, $child)
->insert($this->itemChildTable, ['parent' => $parent->name, 'child' => $child->name])
->execute();

$this->invalidateCache();

return true;
}

Expand All @@ -551,19 +665,27 @@ public function addChild($parent, $child)
*/
public function removeChild($parent, $child)
{
return $this->db->createCommand()
$result = $this->db->createCommand()
->delete($this->itemChildTable, ['parent' => $parent->name, 'child' => $child->name])
->execute() > 0;

$this->invalidateCache();

return $result;
}

/**
* @inheritdoc
*/
public function removeChildren($parent)
{
return $this->db->createCommand()
$result = $this->db->createCommand()
->delete($this->itemChildTable, ['parent' => $parent->name])
->execute() > 0;

$this->invalidateCache();

return $result;
}

/**
Expand Down Expand Up @@ -672,6 +794,7 @@ public function removeAll()
$this->db->createCommand()->delete($this->itemChildTable)->execute();
$this->db->createCommand()->delete($this->itemTable)->execute();
$this->db->createCommand()->delete($this->ruleTable)->execute();
$this->invalidateCache();
}

/**
Expand Down Expand Up @@ -716,6 +839,8 @@ protected function removeAllItems($type)
$this->db->createCommand()
->delete($this->itemTable, ['type' => $type])
->execute();

$this->invalidateCache();
}

/**
Expand All @@ -730,6 +855,8 @@ public function removeAllRules()
}

$this->db->createCommand()->delete($this->ruleTable)->execute();

$this->invalidateCache();
}

/**
Expand All @@ -739,4 +866,49 @@ public function removeAllAssignments()
{
$this->db->createCommand()->delete($this->assignmentTable)->execute();
}

public function invalidateCache()
{
if ($this->cache !== null) {
$this->cache->delete($this->cacheKey);
$this->items = null;
$this->rules = null;
$this->parents = null;
}
}

public function loadFromCache()
{
if ($this->items !== null || !$this->cache instanceof Cache) {
return;
}

$data = $this->cache->get($this->cacheKey);
if (is_array($data) && isset($data[0], $data[1], $data[2])) {
list ($this->items, $this->rules, $this->parents) = unserialize($data);
return;
}

$query = (new Query)->from($this->itemTable);
$this->items = [];
foreach ($query->all($this->db) as $row) {
$this->items[$row['name']] = $this->populateItem($row);
}

$query = (new Query)->from($this->ruleTable);
$this->rules = [];
foreach ($query->all($this->db) as $row) {
$this->rules[$row['name']] = unserialize($row['data']);
}

$query = (new Query)->from($this->itemChildTable);
$this->parents = [];
foreach ($query->all($this->db) as $row) {
if (isset($this->items[$row['child']])) {
$this->parents[$row['child']][] = $row['parent'];
}
}

$this->cache->set($this->cacheKey, [$this->items, $this->rules, $this->parents]);
}
}
2 changes: 1 addition & 1 deletion tests/unit/framework/rbac/DbManagerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected static function runConsoleAction($route, $params = [])
}

ob_start();
$result = Yii::$app->runAction('migrate/up', ['migrationPath' => '@yii/rbac/migrations/', 'interactive' => false]);
$result = Yii::$app->runAction($route, $params);
echo "Result is ".$result;
if ($result !== Controller::EXIT_CODE_NORMAL) {
ob_end_flush();
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/framework/rbac/MySQLManagerCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace yiiunit\framework\rbac;

use yii\caching\FileCache;
use yii\rbac\DbManager;

/**
* MySQLManagerCacheTest
* @group db
* @group rbac
*/
class MySQLManagerCacheTest extends MySQLManagerTest
{
/**
* @return \yii\rbac\ManagerInterface
*/
protected function createManager()
{
return new DbManager([
'db' => $this->getConnection(),
'cache' => new FileCache(['cachePath' => '@yiiunit/runtime/cache']),
]);
}
}

0 comments on commit d188dd1

Please sign in to comment.