Merge remote-tracking branch 'origin/master'

This commit is contained in:
iIronside 2023-10-12 11:34:58 +03:00
commit add34538f1
4 changed files with 170 additions and 24 deletions

View File

@ -68,6 +68,14 @@ class ProfileService
return $searchModel->byParams(); return $searchModel->byParams();
} }
public static function getProfileById($id): ?array
{
$searchModel = new ProfileSearchForm();
$searchModel->id = $id;
return $searchModel->byId();
}
/** /**
* @throws ServerErrorHttpException * @throws ServerErrorHttpException
*/ */

View File

@ -0,0 +1,71 @@
<?php
namespace frontend\modules\api\controllers;
use common\classes\Debug;
use common\models\User;
use frontend\models\SignupForm;
use Yii;
class RegisterController extends ApiController
{
public function behaviors() {
$newBehavior = parent::behaviors();
unset($newBehavior['authenticator']);
return $newBehavior;
}
/**
*
* @OA\Post(path="/register/sign-up",
* summary="Регистрация",
* description="Метод для регистрации",
* tags={"Registration"},
* @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()) {
return [
'id' => $user->id,
];
}
}
return null;
}
}

View File

@ -4,6 +4,7 @@
namespace frontend\modules\api\controllers; namespace frontend\modules\api\controllers;
use common\behaviors\GsCors; use common\behaviors\GsCors;
use common\classes\Debug;
use common\models\User; use common\models\User;
use frontend\modules\api\models\LoginForm; use frontend\modules\api\models\LoginForm;
use Yii; use Yii;
@ -13,34 +14,40 @@ use yii\rest\ActiveController;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\Response; use yii\web\Response;
class UserController extends ActiveController class UserController extends ApiController
{ {
public $modelClass = User::class; public $modelClass = User::class;
public function behaviors() public function behaviors()
{ {
return ArrayHelper::merge(parent::behaviors(), [ $behaviors = parent::behaviors();
[ if($this->action->id == "login"){
'class' => ContentNegotiator::class, unset($behaviors['authenticator']);
'formats' => [ }
'application/json' => Response::FORMAT_JSON,
], return $behaviors;
], // return ArrayHelper::merge(parent::behaviors(), [
'corsFilter' => [ // [
'class' => GsCors::class, // 'class' => ContentNegotiator::class,
'cors' => [ // 'formats' => [
'Origin' => ['*'], // 'application/json' => Response::FORMAT_JSON,
//'Access-Control-Allow-Credentials' => true, // ],
'Access-Control-Allow-Headers' => [ // ],
'Access-Control-Allow-Origin', // 'corsFilter' => [
'Content-Type', // 'class' => GsCors::class,
'Access-Control-Allow-Headers', // 'cors' => [
'Authorization', // 'Origin' => ['*'],
'X-Requested-With' // //'Access-Control-Allow-Credentials' => true,
], // 'Access-Control-Allow-Headers' => [
] // 'Access-Control-Allow-Origin',
], // 'Content-Type',
]); // 'Access-Control-Allow-Headers',
// 'Authorization',
// 'X-Requested-With'
// ],
// ]
// ],
// ]);
} }
public function actions() public function actions()
@ -68,11 +75,42 @@ class UserController extends ActiveController
'access_token' => $model->login(), 'access_token' => $model->login(),
'access_token_expired_at' => $model->getUser()->getTokenExpiredAt(), 'access_token_expired_at' => $model->getUser()->getTokenExpiredAt(),
'id' => $user->id, 'id' => $user->id,
'status' => $user->userCard->status, 'status' => $user->userCard->status ?? null,
'card_id' => $user->userCard->id ?? null, 'card_id' => $user->userCard->id ?? null,
]; ];
} else { } else {
throw new BadRequestHttpException(json_encode($model->errors)); throw new BadRequestHttpException(json_encode($model->errors));
} }
} }
/**
*
* @OA\Get(path="/user/me",
* summary="Получить данные пользователя",
* description="Метод для получения данныех пользователя",
* security={
* {"bearerAuth": {}}
* },
* tags={"User"},
* @OA\Response(
* response=200,
* description="Возвращает данные пользователя",
* @OA\MediaType(
* mediaType="application/json",
* ),
* ),
* )
*
* @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;
}
} }

View File

@ -0,0 +1,29 @@
<?php
namespace frontend\modules\api\models;
use backend\modules\card\models\UserCardSearch;
use common\services\ProfileService;
class User extends \common\models\User
{
/**
* @return string[]
*/
public function fields(): array
{
return [
'email',
'username',
'userCard' => function () {
if(isset($this->userCard->id)){
return ProfileService::getProfileById($this->userCard->id);
}
return null;
}
];
}
}