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

119 lines
3.4 KiB
PHP
Raw Normal View History

2021-06-25 18:11:30 +03:00
<?php
namespace frontend\modules\api\controllers;
use common\behaviors\GsCors;
use common\classes\Debug;
2021-07-03 15:15:54 +03:00
use common\models\InterviewRequest;
2021-09-07 16:32:57 +03:00
use common\models\User;
2022-01-14 12:56:39 +03:00
use common\models\UserCard;
use common\services\ProfileService;
2021-06-25 18:11:30 +03:00
use frontend\modules\api\models\ProfileSearchForm;
use kavalar\BotNotificationTemplateProcessor;
use kavalar\TelegramBotService;
2022-01-14 12:56:39 +03:00
use Yii;
2021-08-05 18:52:07 +03:00
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
2021-08-05 14:08:00 +03:00
use yii\filters\auth\QueryParamAuth;
2021-11-29 18:21:50 +03:00
use yii\filters\ContentNegotiator;
2022-01-14 12:56:39 +03:00
use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException;
2021-11-29 18:21:50 +03:00
use yii\web\Response;
2021-06-25 18:11:30 +03:00
2022-01-14 12:56:39 +03:00
2021-11-29 18:21:50 +03:00
class ProfileController extends ApiController
2021-06-25 18:11:30 +03:00
{
public function behaviors()
{
2021-11-29 18:21:50 +03:00
$parent = parent::behaviors();
$b = [
2021-06-25 18:11:30 +03:00
[
2021-11-29 18:21:50 +03:00
'class' => ContentNegotiator::className(),
2021-06-25 18:11:30 +03:00
'formats' => [
2021-11-29 18:21:50 +03:00
'application/json' => Response::FORMAT_JSON,
2021-06-25 18:11:30 +03:00
],
],
2021-08-05 18:52:07 +03:00
'authenticator' => [
'class' => CompositeAuth::class,
'authMethods' => [
HttpBearerAuth::class,
],
]
2021-06-25 18:11:30 +03:00
];
2021-11-29 18:21:50 +03:00
return array_merge($parent, $b);
2021-06-25 18:11:30 +03:00
}
public function actionIndex($id = null)
{
$searchModel = new ProfileSearchForm();
$searchModel->attributes = \Yii::$app->request->get();
2021-07-03 15:15:54 +03:00
if ($id) {
2021-06-25 18:11:30 +03:00
return $searchModel->byId();
}
return $searchModel->byParams();
}
2022-01-14 12:56:39 +03:00
public function actionProfileWithReportPermission($id)
{
$searchModel = new ProfileSearchForm();
$searchModel->attributes = \Yii::$app->request->get();
2022-01-14 12:56:39 +03:00
$searcherUser = Yii::$app->user->getId();
$searcherProfileId = UserCard::findOne($searcherUser)->id;
if ($id && $searcherProfileId) {
if(!UserCard::find()->where(['id' => $id])->exists())
{
throw new BadRequestHttpException(json_encode('There is no user with this id'));
}
$profile = $searchModel->byId();
2022-01-14 12:56:39 +03:00
$profileService = new ProfileService($searcherProfileId, $id);
if($profileService->checkReportePermission()) {
$profile += ['report_permission' => '1'];
}
else {
$profile += ['report_permission' => '0'];
}
return $profile;
}
throw new BadRequestHttpException(json_encode('Missing required parameter'));
}
2021-07-03 15:15:54 +03:00
public function actionAddToInterview()
{
if (\Yii::$app->request->isPost) {
$attributes = \Yii::$app->request->post();
2021-07-03 15:15:54 +03:00
$model = new InterviewRequest();
$model->attributes = $attributes;
2021-07-03 15:15:54 +03:00
$model->created_at = time();
2021-08-16 14:32:54 +03:00
$model->user_id = \Yii::$app->user->id;
2021-09-07 16:32:57 +03:00
if ($model->save()) {
\Yii::$app->telegram_bot->sendRenderedMessage('interview_request', $attributes);
2021-07-03 15:15:54 +03:00
return ['status' => 'success'];
}
2021-08-16 15:59:56 +03:00
\Yii::$app->response->statusCode = 400;
return ['status' => 'error', 'errors' => $model->errors];
2021-07-03 15:15:54 +03:00
}
}
2021-09-07 16:32:57 +03:00
public function actionMe()
{
if(isset(\Yii::$app->user->id)){
$user = User::find()->with('userCard')->where(['id' => \Yii::$app->user->id])->one();
}
\Yii::$app->response->statusCode = 401;
return ['status' => 'error', 'errors' => 'No authorized'];
}
2021-06-25 18:11:30 +03:00
}