Теперь при передаче запроса как json, закодированный в теле, он автоматически парсится

This commit is contained in:
ErickSkrauch
2017-05-31 03:10:22 +03:00
parent 30fedc51ef
commit 400f0e87b9
14 changed files with 94 additions and 57 deletions

View File

@@ -3,6 +3,7 @@ namespace api\modules\authserver\controllers;
use api\controllers\Controller;
use api\modules\authserver\models;
use Yii;
class AuthenticationController extends Controller {
@@ -25,21 +26,21 @@ class AuthenticationController extends Controller {
public function actionAuthenticate() {
$model = new models\AuthenticationForm();
$model->loadByPost();
$model->load(Yii::$app->request->post());
return $model->authenticate()->getResponseData(true);
}
public function actionRefresh() {
$model = new models\RefreshTokenForm();
$model->loadByPost();
$model->load(Yii::$app->request->post());
return $model->refresh()->getResponseData(false);
}
public function actionValidate() {
$model = new models\ValidateForm();
$model->loadByPost();
$model->load(Yii::$app->request->post());
$model->validateToken();
// В случае успеха ожидается пустой ответ. В случае ошибки же бросается исключение,
// которое обработает ErrorHandler
@@ -47,7 +48,7 @@ class AuthenticationController extends Controller {
public function actionSignout() {
$model = new models\SignoutForm();
$model->loadByPost();
$model->load(Yii::$app->request->post());
$model->signout();
// В случае успеха ожидается пустой ответ. В случае ошибки же бросается исключение,
// которое обработает ErrorHandler
@@ -55,7 +56,7 @@ class AuthenticationController extends Controller {
public function actionInvalidate() {
$model = new models\InvalidateForm();
$model->loadByPost();
$model->load(Yii::$app->request->post());
$model->invalidateToken();
// В случае успеха ожидается пустой ответ. В случае ошибки же бросается исключение,
// которое обработает ErrorHandler

View File

@@ -2,6 +2,7 @@
namespace api\modules\authserver\models;
use api\models\authentication\LoginForm;
use api\models\base\ApiForm;
use api\modules\authserver\exceptions\ForbiddenOperationException;
use api\modules\authserver\Module as Authserver;
use api\modules\authserver\validators\RequiredValidator;
@@ -9,7 +10,7 @@ use common\helpers\Error as E;
use common\models\Account;
use common\models\MinecraftAccessKey;
class AuthenticationForm extends Form {
class AuthenticationForm extends ApiForm {
public $username;
public $password;

View File

@@ -1,27 +0,0 @@
<?php
namespace api\modules\authserver\models;
use Yii;
use yii\base\Model;
abstract class Form extends Model {
public function formName() {
return '';
}
public function loadByGet() {
return $this->load(Yii::$app->request->get());
}
public function loadByPost() {
$data = Yii::$app->request->post();
if (empty($data)) {
// TODO: помнится у Yii2 есть механизм парсинга данных входящего запроса. Лучше будет сделать это там
$data = json_decode(Yii::$app->request->getRawBody(), true);
}
return $this->load($data);
}
}

View File

@@ -1,10 +1,11 @@
<?php
namespace api\modules\authserver\models;
use api\models\base\ApiForm;
use api\modules\authserver\validators\RequiredValidator;
use common\models\MinecraftAccessKey;
class InvalidateForm extends Form {
class InvalidateForm extends ApiForm {
public $accessToken;
public $clientToken;

View File

@@ -1,12 +1,13 @@
<?php
namespace api\modules\authserver\models;
use api\models\base\ApiForm;
use api\modules\authserver\exceptions\ForbiddenOperationException;
use api\modules\authserver\validators\RequiredValidator;
use common\models\Account;
use common\models\MinecraftAccessKey;
class RefreshTokenForm extends Form {
class RefreshTokenForm extends ApiForm {
public $accessToken;
public $clientToken;

View File

@@ -2,13 +2,14 @@
namespace api\modules\authserver\models;
use api\models\authentication\LoginForm;
use api\models\base\ApiForm;
use api\modules\authserver\exceptions\ForbiddenOperationException;
use api\modules\authserver\validators\RequiredValidator;
use common\helpers\Error as E;
use common\models\MinecraftAccessKey;
use Yii;
class SignoutForm extends Form {
class SignoutForm extends ApiForm {
public $username;
public $password;

View File

@@ -1,11 +1,12 @@
<?php
namespace api\modules\authserver\models;
use api\models\base\ApiForm;
use api\modules\authserver\exceptions\ForbiddenOperationException;
use api\modules\authserver\validators\RequiredValidator;
use common\models\MinecraftAccessKey;
class ValidateForm extends Form {
class ValidateForm extends ApiForm {
public $accessToken;