175 lines
6.1 KiB
PHP
175 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace backend\modules\user\controllers;
|
|
|
|
use Yii;
|
|
use yii\helpers\Url;
|
|
use yii\web\Response;
|
|
use yii\web\Controller;
|
|
use common\helpers\UserHelper;
|
|
use yii\filters\AccessControl;
|
|
use yii\web\NotFoundHttpException;
|
|
use common\components\user\models\User;
|
|
use frontend\models\PasswordResetRequestForm;
|
|
use common\components\user\models\search\UserSearch;
|
|
|
|
class DefaultController extends Controller
|
|
{
|
|
public function behaviors(): array
|
|
{
|
|
return [
|
|
'access' => [
|
|
'class' => AccessControl::class,
|
|
'denyCallback' => function () {
|
|
return $this->goHome();
|
|
},
|
|
'rules' => [
|
|
[
|
|
'allow' => true,
|
|
'roles' => [UserHelper::ROLE_SUPERVISOR],
|
|
],
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function actionIndex(): string
|
|
{
|
|
$searchModel = new UserSearch();
|
|
$params = Yii::$app->request->queryParams;
|
|
$dataProvider = $searchModel->search($params);
|
|
|
|
return $this->render('index', [
|
|
'dataProvider' => $dataProvider,
|
|
'searchModel' => $searchModel,
|
|
]);
|
|
}
|
|
|
|
public function actionCreate(): string|Response
|
|
{
|
|
$auth = Yii::$app->authManager;
|
|
$model = new User();
|
|
$model->setScenario(User::SCENARIO_USER_CREATE);
|
|
|
|
if ($model->load(Yii::$app->request->post())) {
|
|
$model->generateAuthKey();
|
|
$model->setPassword(Yii::$app->security->generateRandomKey(Yii::$app->params['user.passwordMinLength']));
|
|
|
|
$model->type = match ($model->role) {
|
|
UserHelper::ROLE_AUDITOR, UserHelper::ROLE_MANAGER, UserHelper::ROLE_TECHNIC, UserHelper::ROLE_AUTHOR => UserHelper::TYPE_EMPLOYEE,
|
|
UserHelper::ROLE_ADMIN, UserHelper::ROLE_SUPERVISOR => UserHelper::TYPE_ADMIN,
|
|
default => UserHelper::TYPE_CUSTOMER,
|
|
};
|
|
|
|
if ($model->save()) {
|
|
$userRole = $auth->getRole($model->role);
|
|
$auth->assign($userRole, $model->id);
|
|
|
|
Yii::$app->session->setFlash('success', Yii::t('user', 'Создан'));
|
|
|
|
return $this->redirect(['update', 'id' => $model->id]);
|
|
} else {
|
|
if ($model->getErrors()) {
|
|
Yii::$app->session->setFlash('error', json_encode($model->getErrors(), JSON_UNESCAPED_UNICODE));
|
|
}
|
|
}
|
|
}
|
|
return $this->render('create', [
|
|
'model' => $model,
|
|
]);
|
|
}
|
|
|
|
public function actionUpdate($id): Response|string
|
|
{
|
|
$model = $this->findModel($id);
|
|
|
|
$userType = $model->typeName;
|
|
$model->$userType = $model->meta;
|
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) {
|
|
if ($model->role) {
|
|
$model->setRole($model->role);
|
|
|
|
$model->meta->load($model->$userType, '');
|
|
$model->meta->save();
|
|
|
|
Yii::$app->session->setFlash('success', Yii::t('user', 'Пользватель {email} обновлён', [
|
|
'email' => $model->email,
|
|
]));
|
|
return $this->redirect(['index']);
|
|
}
|
|
|
|
Yii::$app->session->setFlash('error', Yii::t('user', 'Роль не указана'));
|
|
return $this->render('update', [
|
|
'model' => $model,
|
|
]);
|
|
} else {
|
|
if ($model->getErrors()) Yii::$app->session->setFlash('error', json_encode($model->getErrors(), JSON_UNESCAPED_UNICODE));
|
|
return $this->render('update', [
|
|
'model' => $model,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function actionBan($id): Response
|
|
{
|
|
$model = $this->findModel($id);
|
|
$model->status = UserHelper::STATUS_BLOCKED_MANUAL;
|
|
if ($model->save()) {
|
|
Yii::$app->session->setFlash('success', Yii::t('user', '{email} заблокирован', [
|
|
'email' => $model->email,
|
|
]));
|
|
}
|
|
if ($model->getErrors()) {
|
|
Yii::$app->session->setFlash('error', json_encode($model->getErrors(), JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
return $this->redirect(['index']);
|
|
}
|
|
|
|
public function actionResetPassword($id): Response
|
|
{
|
|
$model = $this->findModel($id);
|
|
|
|
if ($model->status == UserHelper::STATUS_ACTIVE || $model->status == UserHelper::STATUS_NEW || $model->status == UserHelper::STATUS_TEST) {
|
|
$user = new PasswordResetRequestForm();
|
|
$user->email = $model->email;
|
|
|
|
if ($user->validate()) {
|
|
if ($token = $user->sendEmail()) {
|
|
$message = Yii::t('user', 'Пароль сброшен для {email}: {link}', [
|
|
'email' => $model->email,
|
|
'link' => Yii::$app->params['webUrl'] . Url::to(['/reset-password', 'token' => $token]),
|
|
]);
|
|
|
|
Yii::$app->telegram->sendMessage(Yii::$app->params['telegram']['accountExpirationChatId'], $message);
|
|
Yii::$app->session->setFlash('success', $message);
|
|
} else {
|
|
Yii::$app->session->setFlash('error', 'Нельзя отправить письмо на {email}', [
|
|
'email' => $model->email,
|
|
]);
|
|
}
|
|
}
|
|
} else {
|
|
Yii::$app->session->setFlash('error', Yii::t('user', 'Пользователь не должен быть забанен'));
|
|
}
|
|
|
|
return $this->redirect(['/user']);
|
|
}
|
|
|
|
protected function findModel($id): array|User
|
|
{
|
|
$model = User::find()
|
|
->select([User::tableName() . '.*', 'auth_assignment.item_name as role'])
|
|
->where([User::tableName() . '.id' => $id])
|
|
->leftJoin('auth_assignment', 'auth_assignment.user_id = ' . User::tableName() . '.id')
|
|
->one();
|
|
|
|
if ($model !== null) {
|
|
return $model;
|
|
} else {
|
|
throw new NotFoundHttpException(Yii::t('app/error', 'The requested page does not exist.'));
|
|
}
|
|
}
|
|
}
|