简体   繁体   English

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

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

I've searched around and can't seem to find a solution to the problem. 我四处搜寻,似乎找不到解决问题的方法。 I'm a rookie developer, so apologies if this is straight forward. 我是菜鸟开发者,如果这很简单,请您道歉。

I'm wanting to have a simple re-direct depending on the user role. 我想根据用户角色进行简单的重定向。 I have a "role" row within my "Users" table, and I want them to be directed to the "Index.php" page if they are a "user", and the "Dashboard" page if they are an "administrator". 我的“用户”表中有一个“角色”行,如果它们是“用户”,我希望将它们定向到“ Index.php”页面,如果它们是“管理员”,则希望将它们定向到“仪表盘”页面。

I understand that it has something to do with the "SiteController", I'm just not sure of the exact code. 我知道这与“ SiteController”有关,但我不确定确切的代码。 For a reference, I currently have the following under the "ActionLogin" function - 作为参考,我目前在“ 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));

}

Does anybody know how to do this? 有人知道怎么做这个吗?

Thanks a lot, I'm slowly learning! 非常感谢,我正在慢慢学习!

In order to implement role base access you have to exted the default implementation of Yii, which comes only with user authentication (user is logged or user is guest). 为了实现基于角色的访问,您必须使用Yii的默认实现,该默认实现仅与用户身份验证(用户已登录或用户为访客)一起提供。

In order to start with role based access, I recommend you to start by implementing your user class by extending the Yii CWebUser class. 为了从基于角色的访问开始,我建议您首先通过扩展Yii CWebUser类来实现用户类。
Something like: 就像是:

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;
    }
}  

As you can see User::LEVEL_SUPERADMIN and User::LEVEL_ADMIN are provided by CWebUser. 如您所见, User::LEVEL_SUPERADMINUser::LEVEL_ADMIN由CWebUser提供。 Then in your site controller accessRules() put something like: 然后在您的站点控制器中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('*').
        ),
    );
} 

In order to use your new class with role based access, add it in the config/main.php file as an application component: 为了将新类与基于角色的访问一起使用,请将其作为应用程序组件添加到config / main.php文件中:

'components'=>array(
    'user'=>array(
        //tell the application to use your WebUser class 
        'class'=>'WebUser'            
    ),
),

In your views, you can see how it works by using: 在您的视图中,可以使用以下命令查看其工作方式:

if(Yii::app()->user->isAdmin){
   echo 'Administrator!';
}
if(Yii::app()->user->isSuperAdmin){
   echo 'SuperAdmin!';
}

You have to manage the database table for users, and maybe add fields to store the user role constant. 您必须为用户管理数据库表,并可能添加字段以存储用户角色常量。 Further readings on Role Base Access are: 有关“角色库访问”的更多信息是:

To continue reading about the code provided in answer, go here . 要继续阅读答案中提供的代码,请转到此处

Update 更新资料

In order to perform the redirect as you mention, try: 为了执行您提到的重定向,请尝试:

// 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));
}

Note that dashboard.php file must be placed inside /protected/views/site folder. 请注意dashboard.php文件必须放在/protected/views/site文件夹中。

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

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