【问题标题】:Yii2: Is it possible to apply an RBAC rule to a guest?Yii2:是否可以将 RBAC 规则应用于访客?
【发布时间】:2017-03-10 03:18:03
【问题描述】:

是否可以将 RBAC yii\rbac\Rule 应用于未经身份验证的用户 (Yii::$app->user->isGuest == true)?如果有,怎么做?

我的规则也用于经过身份验证的用户,将所有逻辑保存在一个地方会很好,也很干燥,如下所示:

class UserAccesslevelRule extends Rule {
    public $name = 'userAccesslevel';

    public function execute($userID, $item, $params) {
        if (Yii::$app->user->isGuest && $someotherlogic == true) {
            return true;
        } else {
            if ($somelogic == true) {
                return true;
            }
        }
        return false;
    }
}

【问题讨论】:

  • 你想在哪里为访客用户应用规则,在控制器中?
  • 您将如何准备系统来区分客人?
  • @Mohan 在控制器或模型中的某些逻辑中。
  • @Bizley 不明白你的意思。
  • 由于来宾之间没有区别(无需额外实现),每个来宾都将获得相同的权限,因此没有必要为他们准备 RBAC 规则。

标签: php yii2 rbac


【解决方案1】:

您可以使用 ACF(访问控制过滤器),在控制器中您可以为访客分配允许的操作

来自 Yii2 指南

  use yii\web\Controller;
  use yii\filters\AccessControl;

  class SiteController extends Controller
  {
      public function behaviors()
      {
          return [
              'access' => [
                  'class' => AccessControl::className(),
                  'only' => ['login', 'logout', 'signup'],
                  'rules' => [
                      [
                          'allow' => true,
                          'actions' => ['login', 'signup'],
                          'roles' => ['?'],  // <----- guest 
                      ],
                      [
                          'allow' => true,
                          'actions' => ['logout'],
                          'roles' => ['@'],
                      ],
                  ],
              ],
          ];
      }
      // ...
  }

http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

使用 RBAC,您还可以定义新规则,请参阅此新规则 http://www.yiiframework.com/doc-2.0/guide-security-authorization.html#using-rules 但似乎您想重新定义 isguest 的行为 .. isGuest 属性和 getIsGuest() 公共方法已定义 http://www.yiiframework.com/doc-2.0/yii-web-user.html 可能是为此,您应该扩展此类并重新定义 isGuest 函数..

【讨论】:

  • 我需要在规则中添加一些逻辑,所以不会这样做。我的问题中的规则是指我创建的 app\rbac\Rule 类。关于您在此处描述的方法还有其他 SO 答案。
  • 我已经用一些关于您的评论的建议更新了答案,希望这很有用..
【解决方案2】:

在Controller中,你可以这样做

    use yii\filters\AccessControl;

    class controllerName extends Controller {

    public function behaviors() {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'only' => ['index','Other_action_names'],
                    'rules' => [
                        [
                            'allow' => true,
                            'actions' => ['index','Other_action_names'],
                            'roles' => (Yii::$app->user->isGuest) ? ["@"] : [],   // Your roles
                        ],
                    ],
                ],
            ];
        }

   public function actionIndex() {


            $searchModel = new SearchModel();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

            return $this->render('index', [
                        'searchModel' => $searchModel,
                        'dataProvider' => $dataProvider,
            ]);

    }

    }

希望这对你有用。

【讨论】:

    猜你喜欢
    • 2016-06-09
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 2018-06-01
    • 2021-11-21
    相关资源
    最近更新 更多