From e3d9b024915711c809bde14855703faf25ae45b7 Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Wed, 18 Sep 2024 18:11:35 +0300 Subject: [PATCH] =?utf8?q?[ERP-190]=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?utf8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?utf8?q?=D0=BE=D1=81=D1=82=D1=8C=20=D0=B1=D1=8B=D1=81=D1=82=D1=80=D0=BE?= =?utf8?q?=D0=B3=D0=BE=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BA=D0=BB=D1=8E=D1=87?= =?utf8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE?= =?utf8?q?=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/config/params.php | 3 +- erp24/controllers/MainController.php | 86 ++++++++++++++++++++++++++++ erp24/views/layouts/header.php | 64 ++++++++++++++++++++- 3 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 erp24/controllers/MainController.php diff --git a/erp24/config/params.php b/erp24/config/params.php index ff2b5815..772d5bee 100644 --- a/erp24/config/params.php +++ b/erp24/config/params.php @@ -10,5 +10,6 @@ return [ ['Login' => 'lazava-d8-136', 'Password' => '5gp3znn0'], ['Login' => 'alieah-4d-136', 'Password' => 'qh7bxq5p'], ['Login' => 'ipbelo-n2-ci', 'Password' => '8h09h42q38'], - ] + ], + 'SWITCH_USER_COOKIE_PASSWORD' => '123pass@WORD', ]; diff --git a/erp24/controllers/MainController.php b/erp24/controllers/MainController.php new file mode 100644 index 00000000..75f60277 --- /dev/null +++ b/erp24/controllers/MainController.php @@ -0,0 +1,86 @@ +response->format = \yii\web\Response::FORMAT_JSON; + + if (Admin::findOne(Yii::$app->user->id)->group_id == AdminGroup::GROUP_IT) { + $this->setPermission(); + } else { + if (!$this->checkPermission()) { + throw new \Exception('Нет прав на смену пользователя'); + } + } + + $adminArr = []; + foreach (\yii_app\records\Admin::find()->with('adminGroup')->all() as $admin) { + if ($admin->group_id > 0) { + $adminArr[] = ['id' => $admin->id, 'name' => $admin->name, 'groupName' => $admin->adminGroup->name ?? "Другие"]; + } + } + $admins = ArrayHelper::map($adminArr, 'id', 'name', 'groupName'); + + return $admins; + } + + public function actionAjaxSwitchUser() { + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + + if (!$this->checkPermission()) { + throw new \Exception('Нет прав на смену пользователя'); + } + + $adminId = Yii::$app->request->post('adminId'); + + Yii::$app->user->logout(); + + $user = Admin::findOne($adminId); + + if ($user) { + $user->legacyFill(); + + Yii::$app->user->login($user, 3600 * 24 * 30); + + return ['response' => true]; + } + + return ['response' => false]; + } + + private function setPermission() { + $cookies = Yii::$app->response->cookies; + $cookies->add(new \yii\web\Cookie([ + 'name' => self::SWITCH_USER_COOKIE, + 'value' => self::md5Algo(Yii::$app->params['SWITCH_USER_COOKIE_PASSWORD']), + 'expire' => strtotime('+5 minutes', time()) + ])); + } + + private function checkPermission() { + $cookies = Yii::$app->request->cookies; + $cookie = $cookies->get(self::SWITCH_USER_COOKIE); + if ($cookie) { + $md5 = $cookie->value; + return $md5 == self::md5Algo(Yii::$app->params['SWITCH_USER_COOKIE_PASSWORD']); + } + return false; + } + + private static function md5Algo($md5) { + foreach (range(1, date('d')) as $ind) { + $md5 = md5($md5); + } + return $md5; + } +} \ No newline at end of file diff --git a/erp24/views/layouts/header.php b/erp24/views/layouts/header.php index 84d68d2b..88173b76 100755 --- a/erp24/views/layouts/header.php +++ b/erp24/views/layouts/header.php @@ -169,6 +169,10 @@ $this->registerCssFile('/css/timetable/style.css');
Привязать устройство
+ + +
Переключить пользователя
+
Выйти
@@ -186,4 +190,62 @@ $this->registerCssFile('/css/timetable/style.css'); - \ No newline at end of file + + + \ No newline at end of file -- 2.39.5