php - 如何检查 yii2 关系是否存在 显示 Controller

标签 php yii2 access-rules

我有用户和代理模型

用户有一个代理模型,可以访问$user->agency

现在我想在关系存在时检查accessRule,如果该关系为,则显示我的 Controller ,显示警报“用户->机构为空,请选择创建机构”并且然后将用户传递给代理 Controller

在用户模型中我有这样的关系:

public function getAgency(){
    return $this->hasOne(Agency::className(),['id'=>'agency_id'])
        ->viaTable(self::MAP_TABLE,['user_id'=>'id']);
}

我有这个 accessRule 组件:

namespace common\components;

use common\models\User;

class AccessRule extends \yii\filters\AccessRule {

    /**
     * @inheritdoc
     */
    protected function matchRole($user)
    {
        if (empty($this->roles)) {
            return true;
        }
        foreach ($this->roles as $role) {

            if ($role == '?') {
                if ($user->getIsGuest()) {
                    return true;
                }
            }
            elseif (!$user->getIsGuest()) {


                $userObj = User::findOne(['id'=>$user->getId()]);

                if ($role == User::AGENCY_USER) {
                    if ( $userObj->agency_perm >= User::AGENCY_USER) {
                        return true;
                    }
                    // Check if the user is logged in, and the roles match
                } elseif ($role == User::AGENCY_MODERATOR) {
                    if ( $userObj->agency_perm >= User::AGENCY_MODERATOR) {
                        return true;
                    }
                    // Check if the user is logged in, and the roles match
                } elseif ($role == User::AGENCY_ADMIN) {
                    if ($userObj->agency_perm >= User::AGENCY_ADMIN) {
                        return true;
                    }
                    // Check if the user is logged in, and the roles match
                } elseif ($role == User::SUPER_USER) {
                    if ($userObj->super_user == User::SUPER_USER) {
                        return true;
                    }
                    // Check if the user is logged in, and the roles match
                } elseif (!$user->getIsGuest() && $role == $user->identity->role) {
                    return true;
                }

            }


        }

        return false;
    }
}

并在我的 Controller 中使用它:

        'access' => [
            'class' => AccessControl::className(),
            // We will override the default rule config with the new AccessRule class
            'ruleConfig' => [
                'class' => AccessRule::className(),
            ],
            'rules' => [
                [
                    'allow' => true,
                    'roles' => [
                        User::SUPER_USER,
                    ],
                ],

            ],
        ],

最佳答案

在 Yii 访问控制中,您可以通过以下方法检查用户访问权限:

  • 检查每个操作(不推荐)
  • Controller construct()方法
  • 模块/应用中间件

您可以在此级别检查您的自定义访问权限,然后选择授予访问权限或将其重定向到另一个页面。

第三种方式,您可以访问您的 Controller /操作名称,然后决定发生什么。

关于php - 如何检查 yii2 关系是否存在 显示 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31486555/

相关文章:

php - apc_add 和 apc_store 之间的区别?

php - 如何使 PHP 正确转义 CSV (fgetcsv)?

image - 使用验证器设置图像最大文件大小

php - Yii2 - yii\console\Application::getSession()

Java 向 PHP 服务器发送 POST 数据被 accessRules 方法阻止

php - 如何使用mysql查询将csv文件中的正常日期替换为unix日期

php - 使用 ACF 从分类术语中获取图像

php - yii2 中的 createCommand() 函数返回错误值

java - Eclipse 上禁止和不鼓励引用的访问规则