mirror of
https://github.com/elyby/accounts.git
synced 2024-12-26 23:20:18 +05:30
Merge branch 'develop'
This commit is contained in:
commit
c4bd4a994a
@ -2,56 +2,53 @@
|
||||
namespace api\models\authentication;
|
||||
|
||||
use api\models\AccountIdentity;
|
||||
use api\models\base\KeyConfirmationForm;
|
||||
use api\models\base\ApiForm;
|
||||
use api\models\profile\ChangeUsernameForm;
|
||||
use api\validators\EmailActivationKeyValidator;
|
||||
use common\models\Account;
|
||||
use common\models\EmailActivation;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class ConfirmEmailForm extends KeyConfirmationForm {
|
||||
class ConfirmEmailForm extends ApiForm {
|
||||
|
||||
public $key;
|
||||
|
||||
public function rules() {
|
||||
return [
|
||||
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \api\components\User\LoginResult|bool
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function confirm() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$confirmModel = $this->getActivationCodeModel();
|
||||
if ($confirmModel->type !== EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION) {
|
||||
$confirmModel->delete();
|
||||
// TODO: вот где-то здесь нужно ещё попутно сгенерировать соответствующую ошибку
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$account = $confirmModel->account;
|
||||
$account->status = Account::STATUS_ACTIVE;
|
||||
if (!$confirmModel->delete()) {
|
||||
throw new ErrorException('Unable remove activation key.');
|
||||
}
|
||||
|
||||
if (!$account->save()) {
|
||||
throw new ErrorException('Unable activate user account.');
|
||||
}
|
||||
|
||||
$changeUsernameForm = new ChangeUsernameForm();
|
||||
$changeUsernameForm->createEventTask($account->id, $account->username, null);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (ErrorException $e) {
|
||||
$transaction->rollBack();
|
||||
if (YII_DEBUG) {
|
||||
throw $e;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
/** @var \common\models\confirmations\RegistrationConfirmation $confirmModel */
|
||||
$confirmModel = $this->key;
|
||||
$account = $confirmModel->account;
|
||||
$account->status = Account::STATUS_ACTIVE;
|
||||
if (!$confirmModel->delete()) {
|
||||
throw new ErrorException('Unable remove activation key.');
|
||||
}
|
||||
|
||||
/** @var \api\components\User\Component $component */
|
||||
$component = Yii::$app->user;
|
||||
if (!$account->save()) {
|
||||
throw new ErrorException('Unable activate user account.');
|
||||
}
|
||||
|
||||
return $component->login(new AccountIdentity($account->attributes), true);
|
||||
$changeUsernameForm = new ChangeUsernameForm();
|
||||
$changeUsernameForm->createEventTask($account->id, $account->username, null);
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return Yii::$app->user->login(new AccountIdentity($account->attributes), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,26 +2,30 @@
|
||||
namespace api\models\authentication;
|
||||
|
||||
use api\models\AccountIdentity;
|
||||
use api\models\base\KeyConfirmationForm;
|
||||
use api\models\base\ApiForm;
|
||||
use api\validators\EmailActivationKeyValidator;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\EmailActivation;
|
||||
use common\validators\PasswordValidator;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class RecoverPasswordForm extends KeyConfirmationForm {
|
||||
class RecoverPasswordForm extends ApiForm {
|
||||
|
||||
public $key;
|
||||
|
||||
public $newPassword;
|
||||
|
||||
public $newRePassword;
|
||||
|
||||
public function rules() {
|
||||
return array_merge(parent::rules(), [
|
||||
return [
|
||||
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_FORGOT_PASSWORD_KEY],
|
||||
['newPassword', 'required', 'message' => E::NEW_PASSWORD_REQUIRED],
|
||||
['newRePassword', 'required', 'message' => E::NEW_RE_PASSWORD_REQUIRED],
|
||||
['newPassword', PasswordValidator::class],
|
||||
['newRePassword', 'validatePasswordAndRePasswordMatch'],
|
||||
]);
|
||||
];
|
||||
}
|
||||
|
||||
public function validatePasswordAndRePasswordMatch($attribute) {
|
||||
@ -32,46 +36,32 @@ class RecoverPasswordForm extends KeyConfirmationForm {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \api\components\User\LoginResult|bool
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function recoverPassword() {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$confirmModel = $this->getActivationCodeModel();
|
||||
if ($confirmModel->type !== EmailActivation::TYPE_FORGOT_PASSWORD_KEY) {
|
||||
$confirmModel->delete();
|
||||
// TODO: вот где-то здесь нужно ещё попутно сгенерировать соответствующую ошибку
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$account = $confirmModel->account;
|
||||
$account->password = $this->newPassword;
|
||||
if (!$confirmModel->delete()) {
|
||||
throw new ErrorException('Unable remove activation key.');
|
||||
}
|
||||
|
||||
if (!$account->save(false)) {
|
||||
throw new ErrorException('Unable activate user account.');
|
||||
}
|
||||
|
||||
$transaction->commit();
|
||||
} catch (ErrorException $e) {
|
||||
$transaction->rollBack();
|
||||
if (YII_DEBUG) {
|
||||
throw $e;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
/** @var \common\models\confirmations\ForgotPassword $confirmModel */
|
||||
$confirmModel = $this->key;
|
||||
$account = $confirmModel->account;
|
||||
$account->password = $this->newPassword;
|
||||
if (!$confirmModel->delete()) {
|
||||
throw new ErrorException('Unable remove activation key.');
|
||||
}
|
||||
|
||||
// TODO: ещё было бы неплохо уведомить пользователя о том, что его пароль изменился
|
||||
if (!$account->save(false)) {
|
||||
throw new ErrorException('Unable activate user account.');
|
||||
}
|
||||
|
||||
/** @var \api\components\User\Component $component */
|
||||
$component = Yii::$app->user;
|
||||
$transaction->commit();
|
||||
|
||||
return $component->login(new AccountIdentity($account->attributes), false);
|
||||
return Yii::$app->user->login(new AccountIdentity($account->attributes), false);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
namespace api\models\base;
|
||||
|
||||
use common\helpers\Error as E;
|
||||
use api\validators\EmailActivationKeyValidator;
|
||||
use common\models\EmailActivation;
|
||||
|
||||
class KeyConfirmationForm extends ApiForm {
|
||||
|
||||
public $key;
|
||||
|
||||
private $model;
|
||||
|
||||
public function rules() {
|
||||
return [
|
||||
// TODO: нужно провалидировать количество попыток ввода кода для определённого IP адреса и в случае чего запросить капчу
|
||||
['key', 'required', 'message' => E::KEY_REQUIRED],
|
||||
['key', EmailActivationKeyValidator::class],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EmailActivation|null
|
||||
*/
|
||||
public function getActivationCodeModel() {
|
||||
if ($this->model === null) {
|
||||
$this->model = EmailActivation::findOne($this->key);
|
||||
}
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
}
|
@ -1,60 +1,60 @@
|
||||
<?php
|
||||
namespace api\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\base\KeyConfirmationForm;
|
||||
use api\models\base\ApiForm;
|
||||
use api\validators\EmailActivationKeyValidator;
|
||||
use common\helpers\Amqp;
|
||||
use common\models\Account;
|
||||
use common\models\amqp\EmailChanged;
|
||||
use Exception;
|
||||
use common\models\EmailActivation;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class ConfirmNewEmailForm extends KeyConfirmationForm {
|
||||
class ConfirmNewEmailForm extends ApiForm {
|
||||
|
||||
public $key;
|
||||
|
||||
/**
|
||||
* @var Account
|
||||
*/
|
||||
private $account;
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
public function rules() {
|
||||
return [
|
||||
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
public function getAccount() : Account {
|
||||
public function getAccount(): Account {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
public function changeEmail() : bool {
|
||||
public function changeEmail(): bool {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
/** @var \common\models\confirmations\NewEmailConfirmation $activation */
|
||||
$activation = $this->getActivationCodeModel();
|
||||
$activation->delete();
|
||||
|
||||
$account = $this->getAccount();
|
||||
$oldEmail = $account->email;
|
||||
$account->email = $activation->newEmail;
|
||||
if (!$account->save()) {
|
||||
throw new ErrorException('Cannot save new account email value');
|
||||
}
|
||||
/** @var \common\models\confirmations\NewEmailConfirmation $activation */
|
||||
$activation = $this->key;
|
||||
$activation->delete();
|
||||
|
||||
$this->createTask($account->id, $account->email, $oldEmail);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
$account = $this->getAccount();
|
||||
$oldEmail = $account->email;
|
||||
$account->email = $activation->newEmail;
|
||||
if (!$account->save()) {
|
||||
throw new ErrorException('Cannot save new account email value');
|
||||
}
|
||||
|
||||
$this->createTask($account->id, $account->email, $oldEmail);
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -77,4 +77,9 @@ class ConfirmNewEmailForm extends KeyConfirmationForm {
|
||||
Amqp::sendToEventsExchange('accounts.email-changed', $message);
|
||||
}
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,19 @@
|
||||
<?php
|
||||
namespace api\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\base\KeyConfirmationForm;
|
||||
use api\models\base\ApiForm;
|
||||
use api\validators\EmailActivationKeyValidator;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
use common\models\EmailActivation;
|
||||
use common\validators\EmailValidator;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
use yii\base\Exception;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
class NewEmailForm extends KeyConfirmationForm {
|
||||
class NewEmailForm extends ApiForm {
|
||||
|
||||
public $key;
|
||||
|
||||
public $email;
|
||||
|
||||
@ -20,39 +22,32 @@ class NewEmailForm extends KeyConfirmationForm {
|
||||
*/
|
||||
private $account;
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
public function rules() {
|
||||
return array_merge(parent::rules(), [
|
||||
return [
|
||||
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION],
|
||||
['email', EmailValidator::class],
|
||||
]);
|
||||
];
|
||||
}
|
||||
|
||||
public function getAccount() : Account {
|
||||
public function getAccount(): Account {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
public function sendNewEmailConfirmation() {
|
||||
public function sendNewEmailConfirmation(): bool {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$previousActivation = $this->getActivationCodeModel();
|
||||
$previousActivation->delete();
|
||||
|
||||
$activation = $this->createCode();
|
||||
$this->sendCode($activation);
|
||||
/** @var \common\models\confirmations\CurrentEmailConfirmation $previousActivation */
|
||||
$previousActivation = $this->key;
|
||||
$previousActivation->delete();
|
||||
|
||||
$transaction->commit();
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
$activation = $this->createCode();
|
||||
$this->sendCode($activation);
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -98,4 +93,9 @@ class NewEmailForm extends KeyConfirmationForm {
|
||||
}
|
||||
}
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,30 +5,54 @@ use common\helpers\Error as E;
|
||||
use common\models\EmailActivation;
|
||||
use yii\validators\Validator;
|
||||
|
||||
/**
|
||||
* Валидатор для проверки полученного от пользователя кода активации.
|
||||
* В случае успешной валидации подменяет значение поля на актуальную модель
|
||||
*/
|
||||
class EmailActivationKeyValidator extends Validator {
|
||||
|
||||
/**
|
||||
* @var int тип ключа. Если не указан, то валидирует по всем ключам.
|
||||
*/
|
||||
public $type;
|
||||
|
||||
public $keyRequired = E::KEY_REQUIRED;
|
||||
|
||||
public $notExist = E::KEY_NOT_EXISTS;
|
||||
|
||||
public $expired = E::KEY_EXPIRE;
|
||||
|
||||
public function validateValue($value) {
|
||||
if (($model = $this->findEmailActivationModel($value)) === null) {
|
||||
return [$this->notExist, []];
|
||||
public $skipOnEmpty = false;
|
||||
|
||||
public function validateAttribute($model, $attribute) {
|
||||
$value = $model->$attribute;
|
||||
if (empty($value)) {
|
||||
$this->addError($model, $attribute, $this->keyRequired);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($model->isExpired()) {
|
||||
return [$this->expired, []];
|
||||
$activation = $this->findEmailActivationModel($value, $this->type);
|
||||
if ($activation === null) {
|
||||
$this->addError($model, $attribute, $this->notExist);
|
||||
return;
|
||||
}
|
||||
|
||||
return null;
|
||||
if ($activation->isExpired()) {
|
||||
$this->addError($model, $attribute, $this->expired);
|
||||
return;
|
||||
}
|
||||
|
||||
$model->$attribute = $activation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return null|EmailActivation
|
||||
*/
|
||||
protected function findEmailActivationModel($key) {
|
||||
return EmailActivation::findOne($key);
|
||||
protected function findEmailActivationModel(string $key, int $type = null): ?EmailActivation {
|
||||
$query = EmailActivation::find();
|
||||
$query->andWhere(['key' => $key]);
|
||||
if ($type !== null) {
|
||||
$query->andWhere(['type' => $type]);
|
||||
}
|
||||
|
||||
return $query->one();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,21 +8,26 @@ class Api {
|
||||
|
||||
const BASE_DOMAIN = 'http://skinsystem.ely.by';
|
||||
|
||||
public function textures($username) : array {
|
||||
/**
|
||||
* @param string $username
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function textures(string $username): array {
|
||||
$response = $this->getClient()->get($this->getBuildUrl('/textures/' . $username));
|
||||
$textures = json_decode($response->getBody(), true);
|
||||
|
||||
return $textures;
|
||||
return json_decode($response->getBody(), true);
|
||||
}
|
||||
|
||||
protected function getBuildUrl(string $url) : string {
|
||||
protected function getBuildUrl(string $url): string {
|
||||
return self::BASE_DOMAIN . $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return GuzzleClient
|
||||
*/
|
||||
protected function getClient() : GuzzleClient {
|
||||
protected function getClient(): GuzzleClient {
|
||||
return Yii::$app->guzzle;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
return [
|
||||
'version' => '1.1.3',
|
||||
'version' => '1.1.4',
|
||||
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
|
||||
'components' => [
|
||||
'cache' => [
|
||||
|
@ -1,48 +0,0 @@
|
||||
<?php
|
||||
namespace common\models;
|
||||
|
||||
use common\components\Redis\Set;
|
||||
use yii\db\ActiveRecord;
|
||||
|
||||
/**
|
||||
* Поля:
|
||||
* @property string $access_token
|
||||
* @property string $session_id
|
||||
* @property integer $expire_time
|
||||
*
|
||||
* Геттеры:
|
||||
* @property Set $scopes
|
||||
*
|
||||
* Отношения:
|
||||
* @property OauthSession $session
|
||||
* @deprecated
|
||||
*/
|
||||
class OauthAccessToken extends ActiveRecord {
|
||||
|
||||
public static function tableName() {
|
||||
return '{{%oauth_access_tokens}}';
|
||||
}
|
||||
|
||||
public function getSession() {
|
||||
return $this->hasOne(OauthSession::class, ['id' => 'session_id']);
|
||||
}
|
||||
|
||||
public function getScopes() {
|
||||
return new Set(static::getDb()->getSchema()->getRawTableName(static::tableName()), $this->access_token, 'scopes');
|
||||
}
|
||||
|
||||
public function beforeDelete() {
|
||||
if (!$result = parent::beforeDelete()) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$this->getScopes()->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isExpired() : bool {
|
||||
return time() > $this->expire_time;
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,8 @@ namespace common\models;
|
||||
use common\components\SkinSystem\Api as SkinSystemApi;
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Yii;
|
||||
|
||||
class Textures {
|
||||
|
||||
@ -57,9 +59,20 @@ class Textures {
|
||||
}
|
||||
}
|
||||
|
||||
public function getTextures() {
|
||||
$api = new SkinSystemApi();
|
||||
return $api->textures($this->account->username);
|
||||
public function getTextures(): array {
|
||||
try {
|
||||
$textures = $this->getSkinsystemApi()->textures($this->account->username);
|
||||
} catch (RequestException $e) {
|
||||
Yii::warning('Cannot get textures from skinsystem.ely.by. Exception message is ' . $e->getMessage());
|
||||
$textures = [
|
||||
'SKIN' => [
|
||||
'url' => 'http://skins.minecraft.net/MinecraftSkins/' . $this->account->username . '.png',
|
||||
'hash' => md5(uniqid('random, please', true)),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
return $textures;
|
||||
}
|
||||
|
||||
public static function encrypt(array $data) {
|
||||
@ -70,4 +83,8 @@ class Textures {
|
||||
return json_decode(base64_decode($string), $assoc);
|
||||
}
|
||||
|
||||
protected function getSkinsystemApi(): SkinSystemApi {
|
||||
return new SkinSystemApi();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
},
|
||||
"minimum-stability": "stable",
|
||||
"require": {
|
||||
"php": "^7.0.6",
|
||||
"php": "^7.1",
|
||||
"yiisoft/yii2": "2.0.10",
|
||||
"yiisoft/yii2-swiftmailer": "*",
|
||||
"ramsey/uuid": "^3.5.0",
|
||||
@ -25,7 +25,7 @@
|
||||
"ely/yii2-tempmail-validator": "~1.0.0",
|
||||
"emarref/jwt": "~1.0.3",
|
||||
"ely/amqp-controller": "dev-master#d7f8cdbc66c45e477c9c7d5d509bc0c1b11fd3ec",
|
||||
"ely/email-renderer": "dev-master#38a148cd5081147acc31125ddc49966b149f65cf",
|
||||
"ely/email-renderer": "dev-master#ef1cb3f7a13196524b97ca5aa0a2d5867f2d9207",
|
||||
"predis/predis": "^1.0",
|
||||
"mito/yii2-sentry": "dev-fix_init#27f00805cb906f73b2c6f8181c1c655decb9be70"
|
||||
},
|
||||
|
@ -2,13 +2,18 @@
|
||||
namespace console\controllers;
|
||||
|
||||
use Ely\Amqp\ControllerTrait;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
use Yii;
|
||||
use yii\console\Controller;
|
||||
use yii\db\Exception as YiiDbException;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\helpers\Inflector;
|
||||
use yii\helpers\StringHelper;
|
||||
|
||||
abstract class AmqpController extends Controller {
|
||||
use ControllerTrait;
|
||||
use ControllerTrait {
|
||||
callback as _callback;
|
||||
}
|
||||
|
||||
public final function actionIndex() {
|
||||
$this->start();
|
||||
@ -18,6 +23,26 @@ abstract class AmqpController extends Controller {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Переопределяем метод callback, чтобы избержать логгирования в консоль ошибок,
|
||||
* связанных с обвалом того или иного соединения. Это нормально, PHP рождён умирать,
|
||||
* а не работать 24/7 в качестве демона.
|
||||
*
|
||||
* @param AMQPMessage $msg
|
||||
* @throws YiiDbException
|
||||
*/
|
||||
public function callback(AMQPMessage $msg) {
|
||||
try {
|
||||
$this->_callback($msg);
|
||||
} catch (YiiDbException $e) {
|
||||
if (StringHelper::startsWith($e->getMessage(), 'Error while sending QUERY packet')) {
|
||||
exit(self::EXIT_CODE_ERROR);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
|
@ -1,22 +1,8 @@
|
||||
<?php
|
||||
namespace console\controllers;
|
||||
|
||||
use common\models\OauthAccessToken;
|
||||
use yii\console\Controller;
|
||||
|
||||
class CleanupController extends Controller {
|
||||
|
||||
public function actionAccessTokens() {
|
||||
$accessTokens = OauthAccessToken::find()
|
||||
->andWhere(['<', 'expire_time', time()])
|
||||
->each(1000);
|
||||
|
||||
foreach($accessTokens as $token) {
|
||||
/** @var OauthAccessToken $token */
|
||||
$token->delete();
|
||||
}
|
||||
|
||||
return self::EXIT_CODE_NORMAL;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use console\db\Migration;
|
||||
|
||||
class m161222_222520_remove_oauth_access_tokens extends Migration {
|
||||
|
||||
public function safeUp() {
|
||||
$this->dropForeignKey('FK_oauth_access_toke_to_oauth_session', '{{%oauth_access_tokens}}');
|
||||
$this->dropTable('{{%oauth_access_tokens}}');
|
||||
}
|
||||
|
||||
public function safeDown() {
|
||||
$this->createTable('{{%oauth_access_tokens}}', [
|
||||
'access_token' => $this->string(64),
|
||||
'session_id' => $this->getDb()->getTableSchema('{{%oauth_sessions}}')->getColumn('id')->dbType,
|
||||
'expire_time' => $this->integer()->notNull(),
|
||||
$this->primary('access_token'),
|
||||
], $this->tableOptions);
|
||||
|
||||
$this->addForeignKey(
|
||||
'FK_oauth_access_toke_to_oauth_session',
|
||||
'{{%oauth_access_tokens}}',
|
||||
'session_id',
|
||||
'{{%oauth_sessions}}',
|
||||
'id',
|
||||
'CASCADE',
|
||||
'SET NULL'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\models\base;
|
||||
|
||||
use api\models\base\KeyConfirmationForm;
|
||||
use Codeception\Specify;
|
||||
use common\models\EmailActivation;
|
||||
use tests\codeception\api\unit\TestCase;
|
||||
use tests\codeception\common\fixtures\EmailActivationFixture;
|
||||
|
||||
class KeyConfirmationFormTest extends TestCase {
|
||||
use Specify;
|
||||
|
||||
public function _fixtures() {
|
||||
return [
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testGetActivationCodeModel() {
|
||||
$model = new KeyConfirmationForm();
|
||||
$model->key = $this->tester->grabFixture('emailActivations', 'freshRegistrationConfirmation')['key'];
|
||||
$this->assertInstanceOf(EmailActivation::class, $model->getActivationCodeModel());
|
||||
|
||||
$model = new KeyConfirmationForm();
|
||||
$model->key = 'this-is-invalid-key';
|
||||
$this->assertNull($model->getActivationCodeModel());
|
||||
}
|
||||
|
||||
}
|
@ -3,71 +3,80 @@ namespace codeception\api\unit\validators;
|
||||
|
||||
use api\validators\EmailActivationKeyValidator;
|
||||
use Codeception\Specify;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\confirmations\ForgotPassword;
|
||||
use common\models\EmailActivation;
|
||||
use tests\codeception\api\unit\TestCase;
|
||||
use tests\codeception\common\_support\ProtectedCaller;
|
||||
use tests\codeception\common\fixtures\EmailActivationFixture;
|
||||
use yii\base\Model;
|
||||
|
||||
class EmailActivationKeyValidatorTest extends TestCase {
|
||||
use Specify;
|
||||
use ProtectedCaller;
|
||||
|
||||
public function _fixtures() {
|
||||
return [
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
public function testValidateAttribute() {
|
||||
/** @var Model $model */
|
||||
$model = new class extends Model {
|
||||
public $key;
|
||||
};
|
||||
|
||||
/** @var EmailActivationKeyValidator|\PHPUnit_Framework_MockObject_MockObject $validator */
|
||||
$validator = $this->getMockBuilder(EmailActivationKeyValidator::class)
|
||||
->setMethods(['findEmailActivationModel'])
|
||||
->getMock();
|
||||
|
||||
$expiredActivation = new ForgotPassword();
|
||||
$expiredActivation->created_at = time() - $expiredActivation->expirationTimeout - 10;
|
||||
|
||||
$validActivation = new EmailActivation();
|
||||
|
||||
$validator->expects($this->exactly(3))
|
||||
->method('findEmailActivationModel')
|
||||
->willReturnOnConsecutiveCalls(null, $expiredActivation, $validActivation);
|
||||
|
||||
$validator->validateAttribute($model, 'key');
|
||||
$this->assertEquals([E::KEY_REQUIRED], $model->getErrors('key'));
|
||||
$this->assertNull($model->key);
|
||||
|
||||
$model->clearErrors();
|
||||
$model->key = 'original value';
|
||||
$validator->validateAttribute($model, 'key');
|
||||
$this->assertEquals([E::KEY_NOT_EXISTS], $model->getErrors('key'));
|
||||
$this->assertEquals('original value', $model->key);
|
||||
|
||||
$model->clearErrors();
|
||||
$validator->validateAttribute($model, 'key');
|
||||
$this->assertEquals([E::KEY_EXPIRE], $model->getErrors('key'));
|
||||
$this->assertEquals('original value', $model->key);
|
||||
|
||||
$model->clearErrors();
|
||||
$validator->validateAttribute($model, 'key');
|
||||
$this->assertEmpty($model->getErrors('key'));
|
||||
$this->assertEquals($validActivation, $model->key);
|
||||
}
|
||||
|
||||
public function testFindEmailActivationModel() {
|
||||
$this->specify('get EmailActivation model for exists key', function() {
|
||||
$key = $this->tester->grabFixture('emailActivations', 'freshRegistrationConfirmation')['key'];
|
||||
$model = new EmailActivationKeyValidator();
|
||||
/** @var EmailActivation $result */
|
||||
$result = $this->callProtected($model, 'findEmailActivationModel', $key);
|
||||
expect($result)->isInstanceOf(EmailActivation::class);
|
||||
expect($result->key)->equals($key);
|
||||
});
|
||||
$this->tester->haveFixtures(['emailActivations' => EmailActivationFixture::class]);
|
||||
|
||||
$this->specify('get null model for exists key', function() {
|
||||
$model = new EmailActivationKeyValidator();
|
||||
expect($this->callProtected($model, 'findEmailActivationModel', 'invalid-key'))->null();
|
||||
});
|
||||
}
|
||||
$key = $this->tester->grabFixture('emailActivations', 'freshRegistrationConfirmation')['key'];
|
||||
$model = new EmailActivationKeyValidator();
|
||||
/** @var EmailActivation $result */
|
||||
$result = $this->callProtected($model, 'findEmailActivationModel', $key);
|
||||
$this->assertInstanceOf(EmailActivation::class, $result, 'valid key without specifying type must return model');
|
||||
$this->assertEquals($key, $result->key);
|
||||
|
||||
public function testValidateValue() {
|
||||
$this->specify('get error.key_not_exists with validation wrong key', function () {
|
||||
/** @var EmailActivationKeyValidator $model */
|
||||
$model = new class extends EmailActivationKeyValidator {
|
||||
public function findEmailActivationModel($key) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
expect($this->callProtected($model, 'validateValue', null))->equals([$model->notExist, []]);
|
||||
});
|
||||
/** @var EmailActivation $result */
|
||||
$result = $this->callProtected($model, 'findEmailActivationModel', $key, 0);
|
||||
$this->assertInstanceOf(EmailActivation::class, $result, 'valid key with valid type must return model');
|
||||
|
||||
$this->specify('get error.key_expire if we use old key', function () {
|
||||
/** @var EmailActivationKeyValidator $model */
|
||||
$model = new class extends EmailActivationKeyValidator {
|
||||
public function findEmailActivationModel($key) {
|
||||
$codeModel = new ForgotPassword();
|
||||
$codeModel->created_at = time() - $codeModel->expirationTimeout - 10;
|
||||
/** @var EmailActivation $result */
|
||||
$result = $this->callProtected($model, 'findEmailActivationModel', $key, 1);
|
||||
$this->assertNull($result, 'valid key, but invalid type must return null');
|
||||
|
||||
return $codeModel;
|
||||
}
|
||||
};
|
||||
expect($this->callProtected($model, 'validateValue', null))->equals([$model->expired, []]);
|
||||
});
|
||||
|
||||
$this->specify('no errors, if model exists and not expired', function () {
|
||||
/** @var EmailActivationKeyValidator $model */
|
||||
$model = new class extends EmailActivationKeyValidator {
|
||||
public function findEmailActivationModel($key) {
|
||||
return new EmailActivation();
|
||||
}
|
||||
};
|
||||
expect($this->callProtected($model, 'validateValue', null))->null();
|
||||
});
|
||||
$model = new EmailActivationKeyValidator();
|
||||
$result = $this->callProtected($model, 'findEmailActivationModel', 'invalid-key');
|
||||
$this->assertNull($result, 'invalid key must return null');
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\fixtures;
|
||||
|
||||
use common\models\OauthAccessToken;
|
||||
use yii\test\ActiveFixture;
|
||||
|
||||
class OauthAccessTokenFixture extends ActiveFixture {
|
||||
|
||||
public $modelClass = OauthAccessToken::class;
|
||||
|
||||
public $dataFile = '@tests/codeception/common/fixtures/data/oauth-access-tokens.php';
|
||||
|
||||
public $depends = [
|
||||
OauthSessionFixture::class,
|
||||
];
|
||||
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
<?php
|
||||
return [
|
||||
'admin-test1' => [
|
||||
'access_token' => '07541285-831e-1e47-e314-b950309a6fca',
|
||||
'session_id' => 1,
|
||||
'expire_time' => time() + 3600,
|
||||
],
|
||||
'admin-test1-expired' => [
|
||||
'access_token' => '2977ec21-3022-96f8-544db-2e1df936908',
|
||||
'session_id' => 1,
|
||||
'expire_time' => time() - 3600,
|
||||
],
|
||||
];
|
@ -1,31 +1,8 @@
|
||||
<?php
|
||||
namespace codeception\console\unit\controllers;
|
||||
|
||||
use common\models\OauthAccessToken;
|
||||
use console\controllers\CleanupController;
|
||||
use tests\codeception\common\fixtures\OauthAccessTokenFixture;
|
||||
use tests\codeception\console\unit\TestCase;
|
||||
use Yii;
|
||||
|
||||
class CleanupControllerTest extends TestCase {
|
||||
|
||||
public function _fixtures() {
|
||||
return [
|
||||
'accessTokens' => OauthAccessTokenFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testActionAccessTokens() {
|
||||
/** @var OauthAccessToken $validAccessToken */
|
||||
$validAccessToken = $this->tester->grabFixture('accessTokens', 'admin-test1');
|
||||
/** @var OauthAccessToken $expiredAccessToken */
|
||||
$expiredAccessToken = $this->tester->grabFixture('accessTokens', 'admin-test1-expired');
|
||||
|
||||
$controller = new CleanupController('cleanup', Yii::$app);
|
||||
$this->assertEquals(0, $controller->actionAccessTokens());
|
||||
|
||||
$this->tester->canSeeRecord(OauthAccessToken::class, ['access_token' => $validAccessToken->access_token]);
|
||||
$this->tester->cantSeeRecord(OauthAccessToken::class, ['access_token' => $expiredAccessToken->access_token]);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user