我正在为 RBAC 使用 CPHPAuthmanager。我不知道我的代码有什么问题?为什么每当有人登录时它不分配角色,除了正常?
//protected/config/app.php
<?php
return array(
'userTypes' => array(
'normal' => 0,
'admin' => 1,
'student' => 2,
'teacher' => 3
),
'userTypesArray' => array(
0 => 'normal',
1 => 'admin',
2 => 'student',
3 => 'teacher'
),
);
?>
//受保护的/数据/auth.php
<?php
return array(
'admin' => array(
'type' => 2,
'description' => '',
'bizRule' => NULL,
'data' => NULL
),
'student' => array(
'type' => 2,
'description' => '',
'bizRule' => NULL,
'data' => NULL
),
'teacher' => array(
'type' => 2,
'description' => '',
'bizRule' => NULL,
'data' => NULL
),
'normal' => array(
'type' => 2,
'description' => '',
'bizRule' => NULL,
'data' => NULL
)
);
?>
//protected/components/UserIdentity.php
if(count($users) == 0)
$this->errorCode=self::ERROR_USERNAME_INVALID;
elseif ($users[0]->password!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
Yii::app()->authManager->clearAuthAssignments();
$this->errorCode=self::ERROR_NONE;
$this->setState('rol', $users[0]->type);
if($users[0]->type == Yii::app()->params['app']['userTypes']['admin'])
Yii::app()->authManager->assign('admin', $users[0]->UId);
elseif($users[0]->type == Yii::app()->params['app']['userTypes']['student'])
Yii::app()->authManager->assign('student', $users[0]->UId);
elseif($users[0]->type == Yii::app()->params['app']['userTypes']['teacher'])
Yii::app()->authManager->assign('teacher', $users[0]->UId);
else
{
Yii::app()->authManager->assign('normal', $users[0]->UId);
}
Yii::app()->authManager->save();
}
return !$this->errorCode;
//配置/主
$auth=Yii::app()->authManager;
$bizRule='return !Yii::app()->user->isGuest;';
$auth->createRole('normal', 'Normal User', $bizRule);
$bizRule='return (isset(Yii::app()->user->rol))? (Yii::app()->user->rol === "admin") : false;';
$auth->createRole('admin', 'System Admin', $bizRule);
$bizRule='return (isset(Yii::app()->user->rol))? (Yii::app()->user->rol === "student") : false;';
$auth->createRole('student', 'Student', $bizRule);
$bizRule='return (isset(Yii::app()->user->rol))? (Yii::app()->user->rol === "teacher") : false;';
$auth->createRole('teacher', 'Teacher', $bizRule);
//控制器/一些控制器
public function accessRules()
{
return array(
array('allow',
'actions'=>array('index', 'view', 'create', 'update', 'admin','delete'),
'roles'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
请帮助我,在此先感谢。:)