2021-07-28 18:15:38 +03:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
|
|
|
|
|
use common\models\User;
|
2023-10-25 14:37:29 +03:00
|
|
|
|
use frontend\modules\api\models\profile\forms\ProfileChangeEmailForm;
|
|
|
|
|
use frontend\modules\api\models\profile\forms\ProfileChangePersonalDataForm;
|
2023-10-20 15:02:59 +03:00
|
|
|
|
use frontend\modules\api\services\UserService;
|
2021-07-28 18:15:38 +03:00
|
|
|
|
use Yii;
|
2023-10-20 15:02:59 +03:00
|
|
|
|
use yii\base\InvalidConfigException;
|
2021-07-28 18:15:38 +03:00
|
|
|
|
use yii\web\BadRequestHttpException;
|
|
|
|
|
|
2023-10-11 23:19:30 +03:00
|
|
|
|
class UserController extends ApiController
|
2021-07-28 18:15:38 +03:00
|
|
|
|
{
|
|
|
|
|
public $modelClass = User::class;
|
|
|
|
|
|
2023-10-12 10:45:39 +03:00
|
|
|
|
public function behaviors()
|
|
|
|
|
{
|
|
|
|
|
$behaviors = parent::behaviors();
|
|
|
|
|
if($this->action->id == "login"){
|
|
|
|
|
unset($behaviors['authenticator']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $behaviors;
|
2023-10-20 15:02:59 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UserService $userService;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
$id,
|
|
|
|
|
$module,
|
|
|
|
|
UserService $userService,
|
|
|
|
|
$config = []
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$this->userService = $userService;
|
|
|
|
|
parent::__construct($id, $module, $config);
|
2023-10-12 10:45:39 +03:00
|
|
|
|
}
|
2021-07-28 18:15:38 +03:00
|
|
|
|
|
2022-12-26 15:23:46 +03:00
|
|
|
|
public function actions()
|
2021-07-28 18:15:38 +03:00
|
|
|
|
{
|
2022-12-23 17:40:43 +03:00
|
|
|
|
$actions = parent::actions();
|
|
|
|
|
unset($actions['index']);
|
|
|
|
|
unset($actions['create']);
|
|
|
|
|
unset($actions['update']);
|
|
|
|
|
unset($actions['delete']);
|
2021-07-28 18:15:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-20 15:02:59 +03:00
|
|
|
|
public function verbs(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'change-personalData' => ['put', 'patch'],
|
|
|
|
|
'change-email' => ['put', 'patch'],
|
|
|
|
|
'change-password' => ['put', 'patch'],
|
|
|
|
|
];
|
|
|
|
|
}
|
2021-07-28 18:15:38 +03:00
|
|
|
|
|
2023-10-20 15:02:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws BadRequestHttpException
|
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
|
*/
|
|
|
|
|
public function actionLogin(): array
|
2021-07-28 18:15:38 +03:00
|
|
|
|
{
|
2023-10-20 15:02:59 +03:00
|
|
|
|
return $this->userService->login(Yii::$app->getRequest()->getBodyParams());
|
2021-07-28 18:15:38 +03:00
|
|
|
|
}
|
2023-10-11 23:19:30 +03:00
|
|
|
|
|
|
|
|
|
/**
|
2023-10-11 23:32:30 +03:00
|
|
|
|
*
|
|
|
|
|
* @OA\Get(path="/user/me",
|
|
|
|
|
* summary="Получить данные пользователя",
|
|
|
|
|
* description="Метод для получения данныех пользователя",
|
|
|
|
|
* security={
|
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
|
* },
|
|
|
|
|
* tags={"User"},
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response=200,
|
|
|
|
|
* description="Возвращает данные пользователя",
|
|
|
|
|
* @OA\MediaType(
|
|
|
|
|
* mediaType="application/json",
|
|
|
|
|
* ),
|
|
|
|
|
* ),
|
|
|
|
|
* )
|
|
|
|
|
*
|
2023-10-20 15:02:59 +03:00
|
|
|
|
* @return \frontend\modules\api\models\profile\User
|
2023-10-11 23:19:30 +03:00
|
|
|
|
* @throws BadRequestHttpException
|
|
|
|
|
*/
|
2023-10-20 15:02:59 +03:00
|
|
|
|
public function actionMe(): \frontend\modules\api\models\profile\User
|
2023-10-11 23:19:30 +03:00
|
|
|
|
{
|
2023-10-20 15:02:59 +03:00
|
|
|
|
return $this->userService->findCurrentUser();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @OA\Put(path="/user/change-email",
|
|
|
|
|
* summary="Изменить email адрес",
|
|
|
|
|
* description="Метод для изменения email адреса пользователя",
|
|
|
|
|
* security={
|
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
|
* },
|
|
|
|
|
* tags={"User"},
|
|
|
|
|
*
|
|
|
|
|
* @OA\RequestBody(
|
|
|
|
|
* @OA\MediaType(
|
|
|
|
|
* mediaType="application/x-www-form-urlencoded",
|
|
|
|
|
* @OA\Schema(
|
|
|
|
|
* required={"newEmail"},
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="newEmail",
|
|
|
|
|
* type="string",
|
|
|
|
|
* description="Новый email адрес",
|
|
|
|
|
* ),
|
|
|
|
|
* ),
|
|
|
|
|
* ),
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response=200,
|
|
|
|
|
* description="Возвращает сообщение об успехе",
|
|
|
|
|
* ),
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*
|
|
|
|
|
* @return ProfileChangeEmailForm|string[]
|
|
|
|
|
*/
|
|
|
|
|
public function actionChangeEmail()
|
|
|
|
|
{
|
|
|
|
|
return $this->userService->changeEmail(Yii::$app->request->post());
|
|
|
|
|
}
|
2023-10-11 23:19:30 +03:00
|
|
|
|
|
2023-10-20 15:02:59 +03:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @OA\Put(path="/user/change-password",
|
|
|
|
|
* summary="Изменить пароль",
|
|
|
|
|
* description="Метод для изменения пароля пользователя",
|
|
|
|
|
* security={
|
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
|
* },
|
|
|
|
|
* tags={"User"},
|
|
|
|
|
*
|
|
|
|
|
* @OA\RequestBody(
|
|
|
|
|
* @OA\MediaType(
|
|
|
|
|
* mediaType="application/x-www-form-urlencoded",
|
|
|
|
|
* @OA\Schema(
|
2023-10-20 15:07:01 +03:00
|
|
|
|
* required={"password", "newPassword"},
|
2023-10-20 15:02:59 +03:00
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="password",
|
|
|
|
|
* type="string",
|
|
|
|
|
* description="Старый пароль",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="newPassword",
|
|
|
|
|
* type="string",
|
|
|
|
|
* description="Новый пароль",
|
|
|
|
|
* ),
|
|
|
|
|
* ),
|
|
|
|
|
* ),
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response=200,
|
|
|
|
|
* description="Возвращает сообщение об успехе",
|
|
|
|
|
* ),
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*
|
|
|
|
|
* @return ProfileChangeEmailForm|string[]
|
|
|
|
|
*/
|
|
|
|
|
public function actionChangePassword()
|
|
|
|
|
{
|
|
|
|
|
return $this->userService->changePassword(Yii::$app->request->post());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @OA\Put(path="/user/change-personal-data",
|
|
|
|
|
* summary="Изменить логин",
|
|
|
|
|
* description="Метод для изменения логина пользователя",
|
|
|
|
|
* security={
|
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
|
* },
|
|
|
|
|
* tags={"User"},
|
|
|
|
|
*
|
|
|
|
|
* @OA\RequestBody(
|
|
|
|
|
* @OA\MediaType(
|
|
|
|
|
* mediaType="application/x-www-form-urlencoded",
|
|
|
|
|
* @OA\Schema(
|
|
|
|
|
* required={"newUsername"},
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="newUsername",
|
|
|
|
|
* type="string",
|
|
|
|
|
* description="Новый логин",
|
|
|
|
|
* ),
|
|
|
|
|
* ),
|
|
|
|
|
* ),
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response=200,
|
|
|
|
|
* description="Возвращает сообщение об успехе",
|
|
|
|
|
* ),
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*
|
|
|
|
|
* @return ProfileChangePersonalDataForm|string[]
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function actionChangePersonalData()
|
|
|
|
|
{
|
|
|
|
|
return $this->userService->changeChangePersonalData(Yii::$app->request->post());
|
2023-10-11 23:19:30 +03:00
|
|
|
|
}
|
2021-07-28 18:15:38 +03:00
|
|
|
|
}
|