/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/forms/UserForm.php
241 строка
5 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\forms; use common\forms\base\ObjectIdForm; use common\helpers\ArrayHelper; use common\helpers\UserHelper; use common\kdf\KModelStatus; use common\models\Acl; use common\models\ClonedTask; use common\models\Email; use common\models\SubjectArea; use common\models\Task; use common\models\User; use common\models\UserSubjectArea; use common\models\UserTask; use Yii; use yii\helpers\VarDumper; /** * Форма редактирования сервиса * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2024 */ class UserForm extends ObjectIdForm { /** * Тип создания записи * @var string */ public $type = 'user'; /** * Отображаемое имя * @var string */ public $display_name = null; /** * Имя пользователя * @var string */ public $username = null; /** * E-mail * @var null */ public $email = null; /** * Пароль * @var string */ public $password = null; /** * Роль * @var int */ public $role_id = null; /** * Статус * @var int */ public $rec_status = null; /** * @inheritdoc */ public function rules() { return ArrayHelper::merge(parent::rules(), [ ['rec_status', 'default', 'value' => KModelStatus::PUBLISHED], [['email', 'display_name', 'username', 'role_id'], 'required'], [['display_name', 'email', 'username'], 'string'], [['password'], 'string'], [['role_id', 'rec_status'], 'number'], [['email'], 'email'], [ ['password'], 'string', 'length' => [6, 24], ], ]); } /** * @inheritdoc */ public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), [ 'username' => Yii::t('app', 'User name'), 'display_name' => Yii::t('app', 'Display name'), 'email' => Yii::t('app', 'E-mail'), 'role_id' => Yii::t('app', 'User role'), 'password' => Yii::t('app', 'New password'), 'rec_status' => Yii::t('app', 'User Status'), ]); } /** * @inheritdoc */ protected function doSave() { $email = Email::findOrNew(['value' => $this->email]); if (!$email->save()) { $this->addErrors($email->errors); return false; } if ($this->type == 'ldap') { $t = User::findOrNew([ 'email_id' => $email->id ]); } else { $t = User::findOrNew([ 'id' => $this->oid, ]); } $is_new = $t->isNewRecord; $t->setAttributes([ 'display_name' => $this->display_name, 'email_id' => $email->id, 'username' => $this->username, 'rec_status' => $this->rec_status ]); if (!$t->save()) { $this->addErrors($t->errors); return false; } if ($is_new) { if (!$this->cloneTask($t->id)) { return false; } if (!$this->cloneSubjectArea($t->id)) { return false; } } Yii::debug(VarDumper::dumpAsString([ 'attributes' => $this->getAttributes() ])); if ($this->role_id) { $acl = Acl::findOrNew([ 'user_id' => $t->id, ]); $acl->setAttributes([ 'role_id' => $this->role_id ]); if (!$acl->save()) { $this->addErrors($acl->errors); return false; } } if ($this->password) { $t->generatePassword($this->password); } $this->model = $t; return true; } /** * Клонируем сервисы * * @param $user_id * @return false|void * @throws \yii\base\InvalidConfigException * @throws \yii\db\Exception */ protected function cloneTask($user_id) { $task_list = Task::find() ->prepareDefault() ->andWhere(['is_research' => 0]) ->all(); foreach ($task_list as $task) { UserHelper::cloneTask($task, $user_id); } return true; } /** * Клонируем тематики * * @param $user_id * @return false|void * @throws \yii\base\InvalidConfigException * @throws \yii\db\Exception */ protected function cloneSubjectArea($user_id) { $subject_area_list = SubjectArea::find() ->prepareDefault() ->all(); foreach ($subject_area_list as $subject_area) { /** @var SubjectArea $cloned */ $cloned = $subject_area->copySelf(); if (!$cloned) { $this->addError('summary', Yii::t('app', 'Error clone subject area')); return false; } $ut = UserSubjectArea::findOrNew([ 'subject_area_id' => $cloned->id, 'user_id' => $user_id ]); if (!$ut->save()) { $this->addErrors($ut->errors); return false; } } return true; } }