0

I am developing an admin panel with multiple user types. I have created an "AgencyBehaviour" for agencies who can update own data. But i need to show superadmin all the data from all agencies.

What i have done :

i am checking whether the current user is super admin, if it is superadmin then i sending null value to the AgencyBehaviour.

but this is not working while super admin try to insert or update any data from diffrent agencies.

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;
}

}

please tell me the best approach to solve this.... thank you.

VSaindane
  • 11
  • 2

1 Answers1

0

what you are looking for is a way to impersonate as another user. https://github.com/dektrium/yii2-user seems to handle impersonation (not testet).

if i had to do it, i would make a superadmin button (or dropdown) to login as another agency, store the agency-user-id in the admin's session and let the behavior you wrote return this stored agency-id, if available, instead of null.

e-frank
  • 739
  • 11
  • 21