0
我寫了下面的代碼,以驗證方法的角色分配給用戶返回falseYii的checkAccess方法在登錄請求
$auth=Yii::app()->authManager;
if(!$auth->isAssigned($user->type,$user->id))
{
if($auth->assign($user->type,$user->id))
{
Yii::app()->authManager->save();
var_dump(Yii::app()->authManager->getRoles($user->id));
var_dump(Yii::app()->user->checkAccess($user->type));
}
}
上面的代碼返回
array(1) { 'USER' => class CAuthItem#77 (8) { private $_auth => class CPhpAuthManager#76 (10) { public $authFile => string(25) "protected/config/auth.php" private $_items => array(6) { ... } private $_children => array(2) { ... } private $_assignments => array(2) { ... } public $showErrors => bool(false) public $defaultRoles => array(0) { ... } public $behaviors => array(0) { ... } private $_initialized => bool(true) private $_e => NULL private $_m => NULL } private $_type => int(2) private $_name => string(4) "USER" private $_description => string(14) "User Role Type" private $_bizRule => string(0) "" private $_data => string(0) "" private $_e => NULL private $_m => NULL } }
bool(false)
,但如果我在接下來的請求中使用checkAccess
它將返回true。像save
這樣的方法異步工作,並不保存規則,因爲它調用。
這是我的身份驗證配置:
'authManager'=>array(
'class' => 'CPhpAuthManager',
'authFile' => 'protected/config/auth.php',
),
我跟着this wiki
返回相同的結果,我貼 – 2014-09-03 16:13:10
嘗試'的Yii ::應用程序() - > authManager-> load();'保存後,你也可以直接調用checkAccess'Yii :: app() - > authManager-> checkAccess($ user-> type,$ user-> id);'。但是這不能保證成功,因爲保存使用不是原子的file_put_contents()函數。這意味着您可以在其他進程正在保存時讀取文件。最好的做法是使用CDbAuthManager,並將角色放在數據庫中,該數據庫固有地針對併發進行了優化。 – 2014-09-04 10:00:14