Php Yii 1.1登录重定向取决于用户角色(基于角色的访问控制)
我到处寻找,似乎找不到解决这个问题的办法。我是一个新手开发人员,如果这是直截了当的话,我道歉 我想根据用户角色进行简单的重新定向。我在“用户”表中有一个“角色”行,如果他们是“用户”,我希望他们被引导到“Index.php”页面,如果他们是“管理员”,我希望他们被引导到“Dashboard”页面 我知道它与“SiteController”有关,只是不确定确切的代码。作为参考,我目前在“ActionLogin”函数下有以下内容-Php Yii 1.1登录重定向取决于用户角色(基于角色的访问控制),php,yii,rbac,Php,Yii,Rbac,我到处寻找,似乎找不到解决这个问题的办法。我是一个新手开发人员,如果这是直截了当的话,我道歉 我想根据用户角色进行简单的重新定向。我在“用户”表中有一个“角色”行,如果他们是“用户”,我希望他们被引导到“Index.php”页面,如果他们是“管理员”,我希望他们被引导到“Dashboard”页面 我知道它与“SiteController”有关,只是不确定确切的代码。作为参考,我目前在“ActionLogin”函数下有以下内容- public function actionLogin() { $m
public function actionLogin()
{
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(array("Site/Dashboard"));
}
// display the login form
$this->render('login',array('model'=>$model));
}
有人知道怎么做吗
非常感谢,我正在慢慢学习 为了实现基于角色的访问,您必须扩展Yii的默认实现,该实现仅与用户身份验证一起提供(用户已登录或用户为来宾) 为了从基于角色的访问开始,我建议您首先通过扩展Yii CWebUser类来实现用户类。
比如:
class WebUser extends CWebUser {
/**
* cache for the logged in User active record
* @return User
*/
private $_user;
/**
* is the user a superadmin ?
* @return boolean
*/
function getIsSuperAdmin(){
return ( $this->user && $this->user->accessLevel == User::LEVEL_SUPERADMIN );
}
/**
* is the user an administrator ?
* @return boolean
*/
function getIsAdmin(){
return ( $this->user && $this->user->accessLevel >= User::LEVEL_ADMIN );
}
/**
* get the logged user
* @return User|null the user active record or null if user is guest
*/
function getUser(){
if( $this->isGuest )
return null;
if( $this->_user === null ){
$this->_user = User::model()->findByPk( $this->id );
}
return $this->_user;
}
}
如您所见,User::LEVEL\u SUPERADMIN
和User::LEVEL\u ADMIN
由CWebUser提供。然后在站点控制器accessRules()中放入如下内容:
// Get the current user
$user = Yii::app()->user;
function accessRules(){
return array(
//only accessable by admins
array('allow',
'expression'=>'$user->isAdmin',
),
//deny all other users
array('deny',
'users'=>array('*').
),
);
}
要将新类用于基于角色的访问,请将其作为应用程序组件添加到config/main.php文件中:
'components'=>array(
'user'=>array(
//tell the application to use your WebUser class
'class'=>'WebUser'
),
),
在您的视图中,您可以通过以下方式查看其工作原理:
if(Yii::app()->user->isAdmin){
echo 'Administrator!';
}
if(Yii::app()->user->isSuperAdmin){
echo 'SuperAdmin!';
}
您必须为用户管理数据库表,并可能添加字段来存储用户角色常量。有关角色库访问的更多阅读资料如下:
// collect user input data
if(isset($_POST['LoginForm'])) {
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
// If you just want to run the view
$this->render('dashboard',array('model'=>$model));
// If you want to reander the action inside the controller
// $this->redirect( array("site/dashboard") );
}
// display the login form
$this->render('login',array('model'=>$model));
}
注意dashboard.php文件必须放在
/protected/views/site
文件夹中。你好,卡洛斯,谢谢你抽出时间与我联系。我应该解释得更清楚一点。我目前有一个有效的RBAC授权系统,因此可以识别用户类型并根据每个用户限制/授予访问权限。我想做的是有一个IF语句,在登录时会说“是的,他是管理员,把他发送到”Dashboard.php“,或者“哦,他是一个”用户“,把他发送到”Index.php“,现在我假设这将在SiteController中的ActionLogin函数下进行。你知道怎么做吗?在登录时非常感谢,而不是重定向语句do:$this->render('dashboard',array('model'=>$model))代码>并在/views/site文件夹中创建dashboard.php文件。如果您想呈现控制器操作,请按照答案更新。嗨,Karlos,注意,它正在工作!非常感谢你花时间和精力帮助我,我真的很感激。