我正在开发一个多用户类型的管理面板。我为能够更新自己数据的机构创建了一个"AgencyBehaviour“。但我需要向超级管理员展示所有机构的数据。
我所做的:
我正在检查当前用户是否是超级管理员,如果它是超级管理员,那么我将向AgencyBehaviour发送空值。
但这是行不通的,而超级管理员试图插入或更新任何数据从不同的机构。
AgencyBehavior.php
<?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;
}
}请告诉我解决这个问题的最佳方法.谢谢。
发布于 2017-04-18 08:50:57
您要寻找的是一种冒充另一个用户的方法。https://github.com/dektrium/yii2-user似乎处理模拟(不是testet)。
如果必须这样做,我将创建一个超级管理按钮(或下拉列表)作为另一个代理登录,将代理用户id存储在管理员的会话中,并让您编写的行为返回存储的代理id(如果可用的话),而不是null。
https://stackoverflow.com/questions/43452075
复制相似问题