【问题标题】:Yii framework not checking roles in accessRulesYii 框架未检查 accessRules 中的角色
【发布时间】:2012-07-06 15:22:24
【问题描述】:

我正在 Yii 框架中创建某种管理面板,并且我在登录时设置状态是这样的

public function authenticate()
{

    $record=AdminTbl::model()->findByAttributes(array('usr'=>$this->username));
    if($record===null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    else if($record->pwd!==$this->password)
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        $this->_id=$record->id;
        $this->setState('roles','main');
        $this->errorCode=self::ERROR_NONE;
    }
    return !$this->errorCode;
}

我检查了状态是否真的设置,并在视图中回显。后来我把这个角色放在 accessrules() 中

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

我无法在此用户登录的情况下访问这些页面。问题是什么?

【问题讨论】:

    标签: yii setstate


    【解决方案1】:

    看看这里,http://www.yiiframework.com/doc/guide/1.1/en/topics.auth

    您需要创建 main 角色并通过用户 ID 分配它。

    【讨论】:

    • 在文档中对此没有解释。
    【解决方案2】:

    如果您需要简单的基于角色的访问控制而不需要冗长的 RBAC 流程,那么本文就是为您准备的

    http://www.yiiframework.com/wiki/328/simple-rbac/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 2012-02-09
      • 2023-03-24
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多