Php Yii:获取登录用户的角色并根据角色显示内容

Php Yii:获取登录用户的角色并根据角色显示内容,php,yii,Php,Yii,我想获得注册用户的角色,并根据他们的角色向注册用户显示内容。 我现在有两个用户 管理员 用户(已验证) 我试图做的事情是,当管理员通过“webapp/user/login”登录时,我已经制作的sidebarwidget应该在登录时显示,当用户(已验证)登录时,用户(已验证)应该只能看到index.php页面 我正在使用Yii用户和权限。我环顾四周,找到了这段代码,用于获取登录用户的角色,但我不知道将这段代码放置在何处以获取输出。 下面是两段代码,请告诉我哪一段更有用 if($user = Us

我想获得注册用户的角色,并根据他们的角色向注册用户显示内容。 我现在有两个用户

  • 管理员
  • 用户(已验证)
  • 我试图做的事情是,当管理员通过“webapp/user/login”登录时,我已经制作的sidebarwidget应该在登录时显示,当用户(已验证)登录时,用户(已验证)应该只能看到index.php页面

    我正在使用Yii用户权限。我环顾四周,找到了这段代码,用于获取登录用户的角色,但我不知道将这段代码放置在何处以获取输出。 下面是两段代码,请告诉我哪一段更有用

     if($user = Users::model()->findAll()) {
        foreach($user as $id => $user) {
        if(!$user->checkAccess('Authenticated')) {
            unset($user[$id]);
            }
        }
        $users = array_values($user); // to reset indices (optional)
    }
    
    这是我发现的另一段代码

    $command = Yii::app()->db->createCommand("SELECT * FROM `authassignment` WHERE userid={$user->id}");
    $results = $command->queryAll();
    $roles = array();
    foreach ($results as $result)
    {
      $roles[] = $result['itemname'];
    }
    $this->setState('roles', $roles);
    

    根据我在以下教程中所做的工作,这里有一个建议

    身份验证可以在文件protected/components/UserIdentity.php中进行:

    public function authenticate($native=false){
        $record=User::model()->findByAttributes(array('username'=>$this->username));
        //can provide function "same" if needed - found it here:
        //http://codereview.stackexchange.com/questions/13512
        if($record!==null&&$this->same($record->password,crypt($this->password,$record->password)){
            $authRoleName=Role::model()->findByAttributes(array('id'=>$record->role_id))->name;
            $this->setState('role_name', $authRoleName);
            $this->errorCode = self::ERROR_NONE;
        }else{
            $this->errorCode=self::ERROR_UNKNOWN_IDENTITY;
        }
        return !$this->errorCode;
    }
    
    在这种情况下,多个角色(管理员、移动用户、用户等)存储在db(表角色)中,每个用户模型都有一个角色id

    我假设SiteController进行登录(文件protected/controllers/SiteController.php):

    文件保护/models/LoginForm.php:

    class LoginForm extends CFormModel
    public $username;
    public $password;
    public $rememberMe;
    
    private $_identity;
    
    public function authenticate($attribute,$params)
    {
        if(!$this->hasErrors())
        {
            $this->_identity=new UserIdentity($this->username,$this->password);
            if(!$this->_identity->authenticate())
                $this->addError('password','False username or password.');
        }
    }
    
    public function login()
    {
        if($this->_identity===null)
        {
            $this->_identity=new UserIdentity($this->username,$this->password);
            $this->_identity->authenticate();
        }
        if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
        {
            $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
            Yii::app()->user->login($this->_identity, duration);
            return true;
        }
        else
            return false;
    }
    
    在视图中,您可以执行基于角色的决策,如以下文件protected/views/site/index.php中的示例:

    <?php
    $userModel =User::model()->findByAttributes(array('id'=>Yii::app()->user->getId()));
    if($userModel){
        if(Yii::app()->user->getState('role_name') == 'admin'){
            $this->renderPartial(
            //...
            );
         }else{
             //...
         }
    }    
    
    此外,在这种情况下,您可以通过发出角色而不是用户名,在控制器的函数accessRules()中设置整个操作的权限:

    public function accessRules()
    {
        return array{
            array('allow',
                'actions'=>array('index', 'index2', 'view','create','update','getRecordDetails', 'getTotalCount'),
                'roles'=>array('admin'),
            ),
        );
    }
    
    if(Yii::app()->user->checkAccess('admin')){
        //staff for admins
    }
    
    public function accessRules()
    {
        return array{
            array('allow',
                'actions'=>array('index', 'index2', 'view','create','update','getRecordDetails', 'getTotalCount'),
                'roles'=>array('admin'),
            ),
        );
    }