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

77 lines
2.2 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;
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;
class UserController extends ActiveController
{
public $modelClass = User::class;
public function behaviors()
{
return ArrayHelper::merge(parent::behaviors(), [
[
'class' => ContentNegotiator::class,
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
2021-11-29 18:21:50 +03:00
'corsFilter' => [
'class' => GsCors::class,
'cors' => [
'Origin' => ['*'],
//'Access-Control-Allow-Credentials' => true,
'Access-Control-Allow-Headers' => [
'Access-Control-Allow-Origin',
2021-11-29 18:21:50 +03:00
'Content-Type',
'Access-Control-Allow-Headers',
'Authorization',
'X-Requested-With'
],
]
2022-03-03 17:24:45 +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()) {
return [
2022-01-07 15:14:46 +03:00
'access_token' => $model->login(),
'access_token_expired_at' => $model->getUser()->getTokenExpiredAt(),
'id' => $model->getUser()->id,
2023-02-21 21:20:13 +03:00
'status' => $model->getUser()->status,
2022-12-23 17:40:43 +03:00
'card_id' => $model->getUser()->userCard->id ?? null,
2021-07-28 18:15:38 +03:00
];
} else {
throw new BadRequestHttpException(json_encode($model->errors));
}
}
}