簡體   English   中英

RBAC在yii中不起作用

[英]RBAC is not working in yii

我有一個雇員表,它包含emp_id,電子郵件,密碼和角色。 我已將用戶和管理員指定為字段角色的值。 我還創建了Webuser組件,它擴展了CWebUser。 這是我的網絡用戶代碼。

class WebUser extends CWebUser
{
public function checkAccess($operation, $params=array())
{
    if (empty($this->id)) {
        // Not identified => no rights
        return false;
    }
    $role = $this->getState("roles");
    if ($role === 'admin') {
        return true; // admin role has access to everything
    }
    return ($operation === $role);
}

}

這是我的UserIdentity代碼。

class UserIdentity extends CUserIdentity
{
private $_id;

public function authenticate()
{
        $user= Employee::model()->find('LOWER(email)=?',array(strtolower($this->username)));
        if($user===null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    else if(!$user->validatePassword($this->password))
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        $this->_id=$user->emp_id;
                    $this->setState('roles',$user->roles);
        $this->username=$user->email;
        $this->errorCode=self::ERROR_NONE;
            }
    return $this->errorCode==self::ERROR_NONE;
}

}

這是我的控制器代碼。

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('index','view'),
            'users'=>array('*'),
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('create'),
            'users'=>array('@'),
        ),
        array('allow', // allow admin user to perform 'admin' and 'delete' actions
            'actions'=>array('admin','update','delete'),
            'roles'=>array('admin'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
        ),
    );
}

看來一切都很好。 但是,當我嘗試更新時,它不起作用,我已經嘗試過對具有管理員角色的人員進行此操作。 如果有錯,請糾正我。

我認為問題出在checkAccess中-您需要訪問模型Employee

class WebUser extends CWebUser
{
  private $_model = null;
  public function getModel(){
      if (!$this->isGuest && $this->_model === null) {
          $this->_model = Employee::model()->findByPk($this->id);
      }
    return $this->_model;
  }

  public function checkAccess($operation, $params=array()){
    return  $this->model->roles == 'admin';
  }
}

如果您的應用程序不復雜,則應該可以使用。 但是最好在完整的RBAC支持下使用PhpAuthManager(或DbVersion)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM