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

117 lines
3.4 KiB
PHP
Raw Normal View History

2021-07-28 18:15:38 +03:00
<?php
namespace frontend\modules\api\controllers;
2021-08-02 18:37:12 +03:00
use common\behaviors\GsCors;
2023-10-11 23:19:30 +03:00
use common\classes\Debug;
2021-07-28 18:15:38 +03:00
use common\models\User;
use frontend\modules\api\models\LoginForm;
use Yii;
use yii\filters\ContentNegotiator;
use yii\helpers\ArrayHelper;
2022-12-23 17:40:43 +03:00
use yii\rest\ActiveController;
2021-07-28 18:15:38 +03:00
use yii\web\BadRequestHttpException;
use yii\web\Response;
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-11 23:19:30 +03:00
// return ArrayHelper::merge(parent::behaviors(), [
// [
// 'class' => ContentNegotiator::class,
// 'formats' => [
// 'application/json' => Response::FORMAT_JSON,
// ],
// ],
// 'corsFilter' => [
// 'class' => GsCors::class,
// 'cors' => [
// 'Origin' => ['*'],
// //'Access-Control-Allow-Credentials' => true,
// 'Access-Control-Allow-Headers' => [
// 'Access-Control-Allow-Origin',
// 'Content-Type',
// 'Access-Control-Allow-Headers',
// 'Authorization',
// 'X-Requested-With'
// ],
// ]
// ],
// ]);
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
}
2022-12-26 15:23:46 +03:00
// protected function verbs(){
// return [
// 'login' => ['POST']
// ];
// }
2021-07-28 18:15:38 +03:00
public function actionLogin()
{
$model = new LoginForm();
if ($model->load(Yii::$app->getRequest()->getBodyParams(), '') && $model->login()) {
2023-02-21 23:08:46 +03:00
/** @var User $user */
$user = $model->getUser();
2021-07-28 18:15:38 +03:00
return [
2022-01-07 15:14:46 +03:00
'access_token' => $model->login(),
'access_token_expired_at' => $model->getUser()->getTokenExpiredAt(),
2023-02-21 23:08:46 +03:00
'id' => $user->id,
2023-10-12 10:45:39 +03:00
'status' => $user->userCard->status ?? null,
2023-02-21 23:08:46 +03:00
'card_id' => $user->userCard->id ?? null,
2021-07-28 18:15:38 +03:00
];
} else {
throw new BadRequestHttpException(json_encode($model->errors));
}
}
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-11 23:19:30 +03:00
* @return \frontend\modules\api\models\User
* @throws BadRequestHttpException
*/
public function actionMe(): \frontend\modules\api\models\User
{
$user = \frontend\modules\api\models\User::findOne(Yii::$app->user->id);
if (!$user){
throw new BadRequestHttpException("User not found");
}
return $user;
}
2021-07-28 18:15:38 +03:00
}