mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Тесты для AuthorizationForm и кастомного RequiredValidator
В модуль Authserver добавлены хелперы для логирования Исправлена ошибка в MinecraftAccessKey Ускорено тестирование всего приложения
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace api\modules\authserver;
|
||||
|
||||
use Yii;
|
||||
use yii\base\BootstrapInterface;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
@@ -31,4 +32,12 @@ class Module extends \yii\base\Module implements BootstrapInterface {
|
||||
], false);
|
||||
}
|
||||
|
||||
public static function info($message) {
|
||||
Yii::info($message, 'legacy-authserver');
|
||||
}
|
||||
|
||||
public static function error($message) {
|
||||
Yii::info($message, 'legacy-authserver');
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -3,9 +3,11 @@ namespace api\modules\authserver\models;
|
||||
|
||||
use api\models\authentication\LoginForm;
|
||||
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
||||
use api\modules\authserver\Module as Authserver;
|
||||
use api\modules\authserver\validators\RequiredValidator;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\Account;
|
||||
use common\models\MinecraftAccessKey;
|
||||
use Yii;
|
||||
|
||||
class AuthenticationForm extends Form {
|
||||
|
||||
@@ -26,17 +28,22 @@ class AuthenticationForm extends Form {
|
||||
public function authenticate() {
|
||||
$this->validate();
|
||||
|
||||
Yii::info("Trying to authenticate user by login = '{$this->username}'.", 'legacy-authentication');
|
||||
Authserver::info("Trying to authenticate user by login = '{$this->username}'.");
|
||||
|
||||
$loginForm = new LoginForm();
|
||||
$loginForm = $this->createLoginForm();
|
||||
$loginForm->login = $this->username;
|
||||
$loginForm->password = $this->password;
|
||||
if (!$loginForm->validate()) {
|
||||
$errors = $loginForm->getFirstErrors();
|
||||
if (isset($errors['login'])) {
|
||||
Yii::error("Cannot find user by login = '{$this->username}", 'legacy-authentication');
|
||||
if ($errors['login'] === E::ACCOUNT_BANNED) {
|
||||
Authserver::error("User with login = '{$this->username}' is banned");
|
||||
throw new ForbiddenOperationException('This account has been suspended.');
|
||||
} else {
|
||||
Authserver::error("Cannot find user by login = '{$this->username}'");
|
||||
}
|
||||
} elseif (isset($errors['password'])) {
|
||||
Yii::error("User with login = '{$this->username}' passed wrong password.", 'legacy-authentication');
|
||||
Authserver::error("User with login = '{$this->username}' passed wrong password.");
|
||||
}
|
||||
|
||||
// На старом сервере авторизации использовалось поле nickname, а не username, так что сохраняем эту логику
|
||||
@@ -45,16 +52,27 @@ class AuthenticationForm extends Form {
|
||||
$attribute = 'nickname';
|
||||
}
|
||||
|
||||
// TODO: если аккаунт заблокирован, то возвращалось сообщение return "This account has been suspended."
|
||||
// TODO: эта логика дублируется с логикой в SignoutForm
|
||||
|
||||
throw new ForbiddenOperationException("Invalid credentials. Invalid {$attribute} or password.");
|
||||
}
|
||||
|
||||
$account = $loginForm->getAccount();
|
||||
$accessTokenModel = $this->createMinecraftAccessToken($account);
|
||||
$dataModel = new AuthenticateData($accessTokenModel);
|
||||
|
||||
Authserver::info("User with id = {$account->id}, username = '{$account->username}' and email = '{$account->email}' successfully logged in.");
|
||||
|
||||
return $dataModel;
|
||||
}
|
||||
|
||||
protected function createMinecraftAccessToken(Account $account) : MinecraftAccessKey {
|
||||
/** @var MinecraftAccessKey|null $accessTokenModel */
|
||||
$accessTokenModel = MinecraftAccessKey::findOne(['client_token' => $this->clientToken]);
|
||||
$accessTokenModel = MinecraftAccessKey::findOne([
|
||||
'client_token' => $this->clientToken,
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
if ($accessTokenModel === null) {
|
||||
$accessTokenModel = new MinecraftAccessKey();
|
||||
$accessTokenModel->client_token = $this->clientToken;
|
||||
@@ -64,11 +82,11 @@ class AuthenticationForm extends Form {
|
||||
$accessTokenModel->refreshPrimaryKeyValue();
|
||||
}
|
||||
|
||||
$dataModel = new AuthenticateData($accessTokenModel);
|
||||
return $accessTokenModel;
|
||||
}
|
||||
|
||||
Yii::info("User with id = {$account->id}, username = '{$account->username}' and email = '{$account->email}' successfully logged in.", 'legacy-authentication');
|
||||
|
||||
return $dataModel;
|
||||
protected function createLoginForm() : LoginForm {
|
||||
return new LoginForm();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user