首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >yii2检查用户在行为中的角色并给予访问

yii2检查用户在行为中的角色并给予访问
EN

Stack Overflow用户
提问于 2017-04-17 13:04:17
回答 1查看 883关注 0票数 0

我正在开发一个多用户类型的管理面板。我为能够更新自己数据的机构创建了一个"AgencyBehaviour“。但我需要向超级管理员展示所有机构的数据。

我所做的:

我正在检查当前用户是否是超级管理员,如果它是超级管理员,那么我将向AgencyBehaviour发送空值。

但这是行不通的,而超级管理员试图插入或更新任何数据从不同的机构。

AgencyBehavior.php

代码语言:javascript
复制
<?php

namespace app\components;

use Yii;
use yii\behaviors\AttributeBehavior;
use yii\db\BaseActiveRecord;

class AgencyBehavior extends AttributeBehavior {

private $agency_id;
public $value;

/**
 * @inheritdoc
 */
public function init() {
    parent::init();

    if (empty($this->attributes)) {
        $this->attributes = [
            BaseActiveRecord::EVENT_BEFORE_VALIDATE => 'agency_id',
        ];
    }
}

// return value of thsi method is set to the attribute attched to the event in the init. we can as well define the event handler
protected function getValue($event) {
    if ($this->value === null) {
        $user = Yii::$app->get('user', false);
        //Check for super admin user role
        $superAdminId = \app\models\Parameters::getValue('SYSTEM_USER_ID');

        if (yii::$app->user->identity->role_id == $superAdminId) {
             return null;
        }

        return ($user && !$user->isGuest) ? yii::$app->user->identity->agency_id : null;
    }

    return parent::getValue($event);
}

public function getAgency_id() {
    if ($this->agency_id === null) {
        $user = Yii::$app->get('user', false);
        $this->agency_id = $user && !$user->isGuest ? $user->agency_id : null;
        return $this->agency_id;
    }

    return parent::getValue($event);
}

public function setAgency_id($value) {
    $this->agency_id = $value;
}

}

请告诉我解决这个问题的最佳方法.谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-04-18 08:50:57

您要寻找的是一种冒充另一个用户的方法。https://github.com/dektrium/yii2-user似乎处理模拟(不是testet)。

如果必须这样做,我将创建一个超级管理按钮(或下拉列表)作为另一个代理登录,将代理用户id存储在管理员的会话中,并让您编写的行为返回存储的代理id(如果可用的话),而不是null。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43452075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档