guild/frontend/modules/api/controllers/RegisterController.php

198 lines
5.9 KiB
PHP
Raw Normal View History

2023-10-11 13:44:21 +03:00
<?php
namespace frontend\modules\api\controllers;
2023-10-18 13:12:02 +03:00
use common\models\email\RegistrationEmail;
2023-10-18 16:26:49 +03:00
use common\models\email\ResetPasswordEmail;
2023-10-11 13:44:21 +03:00
use common\models\User;
2023-10-18 13:12:02 +03:00
use common\services\EmailService;
2023-10-18 16:26:49 +03:00
use Exception;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
2023-10-11 13:44:21 +03:00
use frontend\models\SignupForm;
use Yii;
2023-10-18 16:26:49 +03:00
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
2023-10-11 13:44:21 +03:00
class RegisterController extends ApiController
{
public function behaviors() {
$newBehavior = parent::behaviors();
unset($newBehavior['authenticator']);
return $newBehavior;
}
2023-10-18 13:12:02 +03:00
private EmailService $emailService;
public function __construct($id, $module, EmailService $emailService, $config = [])
{
$this->emailService = $emailService;
parent::__construct($id, $module, $config);
}
2023-10-11 13:44:21 +03:00
/**
*
* @OA\Post(path="/register/sign-up",
* summary="Регистрация",
* description="Метод для регистрации",
2023-10-11 23:19:30 +03:00
* tags={"Registration"},
2023-10-11 13:44:21 +03:00
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* required={"username", "email", "password"},
* @OA\Property(
* property="username",
* type="string",
* description="Имя пользрователя",
* ),
* @OA\Property(
* property="email",
* type="string",
* description="Электронная почта пользователя",
* ),
* @OA\Property(
* property="password",
* type="string",
* description="Пароль пользователя",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает идентификатор пользователя",
* @OA\MediaType(
* mediaType="application/json",
* ),
* ),
* )
*/
public function actionSignUp()
{
$model = new SignupForm();
if ($model->load(Yii::$app->getRequest()->getBodyParams(), '')) {
/** @var User $user */
if ($user = $model->signup()) {
2023-10-18 13:12:02 +03:00
$this->emailService->sendEmail(new RegistrationEmail($user));
2023-10-11 13:44:21 +03:00
return [
'id' => $user->id,
];
}
}
return null;
}
2023-10-18 16:26:49 +03:00
/**
*
* @OA\Post(path="/register/request-password-reset",
* summary="Запросить сброс пароля",
* description="Метод метод высылает токен сброса пароля на почтовый адрес",
* tags={"Registration"},
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* required={"email"},
* @OA\Property(
* property="email",
* type="string",
* description="Электронная почта пользователя",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает true в случае успеха",
* @OA\MediaType(
* mediaType="application/json",
* ),
* ),
* )
*
* @return bool|string
*/
public function actionRequestPasswordReset()
{
$model = new PasswordResetRequestForm();
if ($model->load(Yii::$app->request->post(), '') & $model->validate()) {
/* @var $user User */
$user = User::findOne([
'status' => User::STATUS_ACTIVE,
'email' => $model->email,
]);
if (!$user) {
return false;
}
if (!User::isPasswordResetTokenValid($user->password_reset_token)) {
$user->generatePasswordResetToken();
if (!$user->save()) {
return false;
}
}
return $this->emailService->sendEmail(new ResetPasswordEmail($user));
}
return json_encode($model->getFirstErrors());
}
/**
*
* @OA\Post(path="/register/reset-password",
* summary="Cброс пароля",
* description="Метод сброса пароля",
* tags={"Registration"},
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* required={"token", "password"},
* @OA\Property(
* property="token",
* type="string",
* description="Токен сброса пароля",
* ),
* @OA\Property(
* property="password",
* type="string",
* description="Новый пароль пользователя",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает сообщение об успехе",
* @OA\MediaType(
* mediaType="application/json",
* ),
* ),
* )
*
* @return array|string
* @throws BadRequestHttpException
*/
public function actionResetPassword()
{
try {
$model = new ResetPasswordForm(Yii::$app->request->post()['token']);
} catch (Exception $e) {
throw new BadRequestHttpException($e->getMessage());
}
if ($model->load(Yii::$app->request->post(), '') & $model->validate() & $model->resetPassword()) {
return 'Success! New password saved.';
} else {
return $model->errors;
}
}
2023-10-11 13:44:21 +03:00
}