2016-08-21 04:51:39 +05:30
|
|
|
<?php
|
2019-12-04 23:40:15 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-21 04:51:39 +05:30
|
|
|
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
|
|
|
|
2017-05-31 05:40:22 +05:30
|
|
|
class SignoutForm extends ApiForm {
|
2016-08-21 04:51:39 +05:30
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-21 04:51:39 +05:30
|
|
|
public $username;
|
2018-04-18 02:17:25 +05:30
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-08-21 04:51:39 +05:30
|
|
|
public $password;
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
public function rules(): array {
|
2016-08-21 04:51:39 +05:30
|
|
|
return [
|
|
|
|
[['username', 'password'], RequiredValidator::class],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @throws ForbiddenOperationException
|
|
|
|
* @throws \api\modules\authserver\exceptions\IllegalArgumentException
|
|
|
|
*/
|
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) {
|
2019-07-15 04:29:56 +05:30
|
|
|
// We believe that a blocked one can get out painlessly
|
2016-09-01 13:01:43 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
// The previous authorization server implementation used the nickname field instead of username,
|
|
|
|
// so we keep such behavior
|
2016-08-21 04:51:39 +05:30
|
|
|
$attribute = $loginForm->getLoginAttribute();
|
|
|
|
if ($attribute === 'username') {
|
|
|
|
$attribute = 'nickname';
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ForbiddenOperationException("Invalid credentials. Invalid {$attribute} or password.");
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
// We're unable to invalidate access tokens because they aren't stored in our database
|
2016-08-21 04:51:39 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|