Skip to content

Instantly share code, notes, and snippets.

@crisu83
Created December 30, 2012 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisu83/4416007 to your computer and use it in GitHub Desktop.
Save crisu83/4416007 to your computer and use it in GitHub Desktop.
A cached database authorization manager for Yii.
<?php
class CachedDbAuthManager extends CDbAuthManager
{
const CACHE_KEY_PREFIX = 'Auth.CachedDbAuthManager.';
/**
* @var integer the time in seconds that the messages can remain valid in cache.
* Defaults to 0, meaning the caching is disabled.
*/
public $cachingDuration = 0;
/**
* @var string the ID of the cache application component that is used to cache the messages.
* Defaults to 'cache' which refers to the primary cache application component.
* Set this property to false if you want to disable caching the permissions.
*/
public $cacheID = 'cache';
/**
* Performs access check for the specified user.
* @param string $itemName the name of the operation that need access check.
* @param integer $userId the user id.
* @param array $params name-value pairs that would be passed to biz rules associated
* with the tasks and roles assigned to the user.
* @param boolean $allowCaching whether to allow caching the result of access check.
* @return boolean whether the operations can be performed by the user.
*/
public function checkAccess($itemName, $userId, $params = array(), $allowCaching = true)
{
$key = $this->resolveCacheKey($itemName, $userId, $params);
/* @var $cache CCache */
if ($allowCaching && $this->cachingDuration > 0 && $this->cacheID !== false
&& ($cache = Yii::app()->getComponent($this->cacheID)) !== null)
{
if (($data = $cache->get($key)) !== false)
return unserialize($data);
}
$result = parent::checkAccess($itemName, $userId, $params);;
if (isset($cache))
$cache->set($key, serialize($result), $this->cachingDuration);
return $result;
}
/**
* Flushes the access cache for the specified user.
* @param string $itemName the name of the operation that need access check.
* @param mixed $userId the user id.
* @param array $params name-value pairs that would be passed to biz rules associated
* with the tasks and roles assigned to the user.
*/
public function flushAccess($itemName, $userId, $params = array())
{
/* @var $cache CCache */
if ($this->cachingDuration > 0 && $this->cacheID !== false
&& ($cache = Yii::app()->getComponent($this->cacheID)) !== null)
{
$key = $this->resolveCacheKey($itemName, $userId, $params);
$cache->delete($key);
}
}
/**
* Returns the key to use when caching.
* @param string $itemName the name of the operation that need access check.
* @param integer $userId the user id.
* @param array $params name-value pairs that would be passed to biz rules associated
* with the tasks and roles assigned to the user.
* @return string the key.
*/
protected function resolveCacheKey($itemName, $userId, $params)
{
return self::CACHE_KEY_PREFIX . '.' . $itemName . '.' . $userId . '.' . serialize($params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment