Yii 1.1 登录重定向取决于用户角色(基于角色的访问控制)

Yii 1.1 Login redirect depending on user role (Role based access control)

我四处寻找,似乎找不到解决问题的办法。我是一名菜鸟开发人员,如果这很简单,我深表歉意。

我想根据用户角色进行简单的重定向。我的 "Users" table 中有一个 "role" 行,如果它们是 "user",我希望将它们定向到 "Index.php" 页面,并且 "Dashboard" 页面,如果它们是 "administrator".

我知道它与 "SiteController" 有关,我只是不确定确切的代码。作为参考,我目前在 "ActionLogin" 函数下有以下内容 -

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.
来实现您的用户 class 类似于:

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_SUPERADMINUser::LEVEL_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('*').
        ),
    );
} 

为了使用基于角色访问的新 class,请将其作为应用程序组件添加到 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!';
}

您必须为用户管理数据库table,并且可能添加字段来存储用户角色常量。关于 Role Base Access 的进一步阅读是:

要继续阅读答案中提供的代码,请转到 here

更新

为了执行您提到的重定向,请尝试:

// 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 文件夹中。