fix api controllers

This commit is contained in:
andrey 2022-01-14 17:05:29 +03:00
parent a189c07c02
commit 8806be42b2
3 changed files with 24 additions and 27 deletions

View File

@ -3,7 +3,11 @@
namespace frontend\modules\api\controllers; namespace frontend\modules\api\controllers;
use common\behaviors\GsCors; use common\behaviors\GsCors;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\ContentNegotiator;
use yii\rest\Controller; use yii\rest\Controller;
use yii\web\Response;
class ApiController extends Controller class ApiController extends Controller
{ {
@ -24,6 +28,18 @@ class ApiController extends Controller
], ],
] ]
], ],
'authenticator' => [
'class' => CompositeAuth::class,
'authMethods' => [
HttpBearerAuth::class,
],
],
[
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
]; ];
} }

View File

@ -14,18 +14,8 @@ use yii\web\NotFoundHttpException;
use yii\rest\Controller; use yii\rest\Controller;
use yii\web\ServerErrorHttpException; use yii\web\ServerErrorHttpException;
class DocumentController extends Controller class DocumentController extends ApiController
{ {
public function behaviors(): array
{
$behaviors = parent::behaviors();
$behaviors['authenticator']['authMethods'] = [
HttpBearerAuth::className(),
];
return $behaviors;
}
public function verbs(): array public function verbs(): array
{ {

View File

@ -5,22 +5,14 @@ namespace frontend\modules\api\controllers;
use common\models\Document; use common\models\Document;
use common\models\Template; use common\models\Template;
use Yii; use Yii;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth; use yii\filters\auth\HttpBearerAuth;
use yii\filters\ContentNegotiator;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\rest\Controller; use yii\web\Response;
class TemplateController extends Controller class TemplateController extends ApiController
{ {
public function behaviors(): array
{
$behaviors = parent::behaviors();
$behaviors['authenticator']['authMethods'] = [
HttpBearerAuth::className(),
];
return $behaviors;
}
public function verbs(): array public function verbs(): array
{ {
@ -34,7 +26,7 @@ class TemplateController extends Controller
{ {
$template = Template::find()->asArray()->all(); $template = Template::find()->asArray()->all();
if(empty($template)) { if (empty($template)) {
throw new NotFoundHttpException('Documents are not assigned'); throw new NotFoundHttpException('Documents are not assigned');
} }
@ -44,8 +36,7 @@ class TemplateController extends Controller
public function actionGetTemplateFields(): array public function actionGetTemplateFields(): array
{ {
$template_id = Yii::$app->request->get('template_id'); $template_id = Yii::$app->request->get('template_id');
if(empty($template_id) or !is_numeric($template_id)) if (empty($template_id) or !is_numeric($template_id)) {
{
throw new NotFoundHttpException('Incorrect template ID'); throw new NotFoundHttpException('Incorrect template ID');
} }
@ -55,7 +46,7 @@ class TemplateController extends Controller
->asArray() ->asArray()
->all(); ->all();
if(empty($templates)) { if (empty($templates)) {
throw new NotFoundHttpException('Documents are not assigned'); throw new NotFoundHttpException('Documents are not assigned');
} }