While profiling my app I found out, that all AuthItems are called from Db each time a ‘checkAccess’ request is raised and the result is not cached within CWebUser.
I don’t think the items will change within one request, so I wrote a small improvement to cache these items within the CDbAuthManager object.
protected $authItems = array(); // new array
public function checkAccess($itemName,$userId,$params=array())
{
if(!empty($this->defaultRoles) && $this->checkDefaultRoles($itemName,$params))
return true;
// items are only called once from db
if(empty($this->authItems)) {
$sql="SELECT name, type, description, t1.bizrule, t1.data, t2.bizrule AS bizrule2, t2.data AS data2 FROM {$this->itemTable} t1, {$this->assignmentTable} t2 WHERE name=itemname AND userid=:userid";
$command=$this->db->createCommand($sql);
$command->bindValue(':userid',$userId);
$this->authItems = $command->queryAll();
}
// check directly assigned items
$names=array();
foreach($this->authItems as $row)
{
Yii::trace('Checking permission "'.$row['name'].'"','system.web.auth.CDbAuthManager');
if($this->executeBizRule($row['bizrule2'],$params,unserialize($row['data2']))
&& $this->executeBizRule($row['bizrule'],$params,unserialize($row['data'])))
{
if($row['name']===$itemName)
return true;
$names[]=$row['name'];
}
}
...
}
Is there a reason why they are called each time from Db which I’ve missed?
Regards.