2016-08-21 04:51:39 +05:30
|
|
|
|
<?php
|
|
|
|
|
namespace api\modules\authserver\models;
|
|
|
|
|
|
|
|
|
|
use api\models\authentication\LoginForm;
|
2017-05-31 05:40:22 +05:30
|
|
|
|
use api\models\base\ApiForm;
|
2016-08-21 04:51:39 +05:30
|
|
|
|
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
|
|
|
|
use api\modules\authserver\validators\RequiredValidator;
|
2016-09-01 13:01:43 +05:30
|
|
|
|
use common\helpers\Error as E;
|
2016-08-21 04:51:39 +05:30
|
|
|
|
use common\models\MinecraftAccessKey;
|
|
|
|
|
use Yii;
|
|
|
|
|
|
2017-05-31 05:40:22 +05:30
|
|
|
|
class SignoutForm extends ApiForm {
|
2016-08-21 04:51:39 +05:30
|
|
|
|
|
|
|
|
|
public $username;
|
2018-04-18 02:17:25 +05:30
|
|
|
|
|
2016-08-21 04:51:39 +05:30
|
|
|
|
public $password;
|
|
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
|
return [
|
|
|
|
|
[['username', 'password'], RequiredValidator::class],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
|
public function signout(): bool {
|
2016-08-21 04:51:39 +05:30
|
|
|
|
$this->validate();
|
|
|
|
|
|
|
|
|
|
$loginForm = new LoginForm();
|
|
|
|
|
$loginForm->login = $this->username;
|
|
|
|
|
$loginForm->password = $this->password;
|
|
|
|
|
if (!$loginForm->validate()) {
|
2016-09-01 13:01:43 +05:30
|
|
|
|
$errors = $loginForm->getFirstErrors();
|
|
|
|
|
if (isset($errors['login']) && $errors['login'] === E::ACCOUNT_BANNED) {
|
|
|
|
|
// Считаем, что заблокированный может безболезненно выйти
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 04:51:39 +05:30
|
|
|
|
// На старом сервере авторизации использовалось поле nickname, а не username, так что сохраняем эту логику
|
|
|
|
|
$attribute = $loginForm->getLoginAttribute();
|
|
|
|
|
if ($attribute === 'username') {
|
|
|
|
|
$attribute = 'nickname';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ForbiddenOperationException("Invalid credentials. Invalid {$attribute} or password.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$account = $loginForm->getAccount();
|
|
|
|
|
|
|
|
|
|
/** @noinspection SqlResolve */
|
|
|
|
|
Yii::$app->db->createCommand('
|
|
|
|
|
DELETE
|
|
|
|
|
FROM ' . MinecraftAccessKey::tableName() . '
|
|
|
|
|
WHERE account_id = :userId
|
|
|
|
|
', [
|
|
|
|
|
'userId' => $account->id,
|
|
|
|
|
])->execute();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|