繁体   English   中英

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

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

我四处搜寻,似乎找不到解决问题的方法。 我是菜鸟开发者,如果这很简单,请您道歉。

我想根据用户角色进行简单的重定向。 我的“用户”表中有一个“角色”行,如果它们是“用户”,我希望将它们定向到“ Index.php”页面,如果它们是“管理员”,则希望将它们定向到“仪表盘”页面。

我知道这与“ 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 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('*').
        ),
    );
} 

为了将新类与基于角色的访问一起使用,请将其作为应用程序组件添加到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文件夹中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM