mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Implemented account deletion. Not all cases covered with tests [skip ci]
This commit is contained in:
@@ -3,8 +3,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use common\components\UserPass;
|
||||
use common\tasks\CreateWebHooksDeliveries;
|
||||
use DateInterval;
|
||||
use Webmozart\Assert\Assert;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
@@ -14,21 +17,22 @@ use const common\LATEST_RULES_VERSION;
|
||||
|
||||
/**
|
||||
* Fields:
|
||||
* @property int $id
|
||||
* @property string $uuid
|
||||
* @property string $username
|
||||
* @property string $email
|
||||
* @property string $password_hash
|
||||
* @property int $password_hash_strategy
|
||||
* @property string $lang
|
||||
* @property int $status
|
||||
* @property int $rules_agreement_version
|
||||
* @property string $registration_ip
|
||||
* @property string $otp_secret
|
||||
* @property int $is_otp_enabled
|
||||
* @property int $created_at
|
||||
* @property int $updated_at
|
||||
* @property int $password_changed_at
|
||||
* @property int $id
|
||||
* @property string $uuid
|
||||
* @property string $username
|
||||
* @property string $email
|
||||
* @property string $password_hash
|
||||
* @property int $password_hash_strategy
|
||||
* @property string $lang
|
||||
* @property int $status
|
||||
* @property int|null $rules_agreement_version
|
||||
* @property string|null $registration_ip
|
||||
* @property string|null $otp_secret
|
||||
* @property int $is_otp_enabled
|
||||
* @property int $created_at
|
||||
* @property int $updated_at
|
||||
* @property int $password_changed_at
|
||||
* @property int|null $deleted_at shows the time, when the account was marked as deleted
|
||||
*
|
||||
* Getters-setters:
|
||||
* @property-write string $password plain user's password
|
||||
@@ -55,8 +59,10 @@ class Account extends ActiveRecord {
|
||||
public const PASS_HASH_STRATEGY_OLD_ELY = 0;
|
||||
public const PASS_HASH_STRATEGY_YII2 = 1;
|
||||
|
||||
public const ACCOUNT_DELETION_DELAY = 'P7D';
|
||||
|
||||
public static function tableName(): string {
|
||||
return '{{%accounts}}';
|
||||
return 'accounts';
|
||||
}
|
||||
|
||||
public static function find(): AccountQuery {
|
||||
@@ -153,17 +159,24 @@ class Account extends ActiveRecord {
|
||||
return $this->registration_ip === null ? null : inet_ntop($this->registration_ip);
|
||||
}
|
||||
|
||||
public function afterSave($insert, $changedAttributes) {
|
||||
public function getDeleteAt(): Carbon {
|
||||
Assert::notNull($this->deleted_at, 'This method should not be called on not deleted records');
|
||||
return Carbon::createFromTimestamp($this->deleted_at)->add(new DateInterval(Account::ACCOUNT_DELETION_DELAY));
|
||||
}
|
||||
|
||||
public function afterSave($insert, $changedAttributes): void {
|
||||
parent::afterSave($insert, $changedAttributes);
|
||||
|
||||
if ($insert) {
|
||||
return;
|
||||
}
|
||||
|
||||
$meaningfulFields = ['username', 'email', 'uuid', 'status', 'lang'];
|
||||
$meaningfulChangedAttributes = array_filter($changedAttributes, function(string $key) use ($meaningfulFields) {
|
||||
return in_array($key, $meaningfulFields, true);
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
$meaningfulFields = ['username', 'email', 'uuid', 'status', 'lang', 'deleted_at'];
|
||||
$meaningfulChangedAttributes = array_filter(
|
||||
$changedAttributes,
|
||||
fn(string $key): bool => in_array($key, $meaningfulFields, true),
|
||||
ARRAY_FILTER_USE_KEY,
|
||||
);
|
||||
if (empty($meaningfulChangedAttributes)) {
|
||||
return;
|
||||
}
|
||||
@@ -171,4 +184,9 @@ class Account extends ActiveRecord {
|
||||
Yii::$app->queue->push(CreateWebHooksDeliveries::createAccountEdit($this, $meaningfulChangedAttributes));
|
||||
}
|
||||
|
||||
public function afterDelete(): void {
|
||||
parent::afterDelete();
|
||||
Yii::$app->queue->push(CreateWebHooksDeliveries::createAccountDeletion($this));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,6 +10,10 @@ use yii\db\ActiveQuery;
|
||||
*/
|
||||
class AccountQuery extends ActiveQuery {
|
||||
|
||||
public function excludeDeleted(): self {
|
||||
return $this->andWhere(['NOT', ['status' => Account::STATUS_DELETED]]);
|
||||
}
|
||||
|
||||
public function andWhereLogin(string $login): self {
|
||||
return $this->andWhere([$this->getLoginAttribute($login) => $login]);
|
||||
}
|
||||
|
||||
@@ -1,29 +1,32 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveQuery;
|
||||
use yii\db\ActiveRecord;
|
||||
|
||||
/**
|
||||
* Fields:
|
||||
* @property integer $id
|
||||
* @property string $username
|
||||
* @property integer $account_id
|
||||
* @property integer $applied_in
|
||||
* @property int $id
|
||||
* @property string $username
|
||||
* @property int|null $account_id
|
||||
* @property int $applied_in
|
||||
*
|
||||
* Relations:
|
||||
* @property Account $account
|
||||
* @property-read Account $account
|
||||
*
|
||||
* Behaviors:
|
||||
* @mixin TimestampBehavior
|
||||
*/
|
||||
class UsernameHistory extends ActiveRecord {
|
||||
|
||||
public static function tableName() {
|
||||
return '{{%usernames_history}}';
|
||||
public static function tableName(): string {
|
||||
return 'usernames_history';
|
||||
}
|
||||
|
||||
public function behaviors() {
|
||||
public function behaviors(): array {
|
||||
return [
|
||||
[
|
||||
'class' => TimestampBehavior::class,
|
||||
@@ -33,19 +36,17 @@ class UsernameHistory extends ActiveRecord {
|
||||
];
|
||||
}
|
||||
|
||||
public function rules() {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getAccount() {
|
||||
public function getAccount(): ActiveQuery {
|
||||
return $this->hasOne(Account::class, ['id' => 'account_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the username after the current of the account.
|
||||
*
|
||||
* @param int $afterTime
|
||||
* @return UsernameHistory|null
|
||||
*/
|
||||
public function findNext(int $afterTime = null): ?self {
|
||||
public function findNextOwnerUsername(int $afterTime = null): ?self {
|
||||
return self::find()
|
||||
->andWhere(['account_id' => $this->account_id])
|
||||
->andWhere(['>', 'applied_in', $afterTime ?: $this->applied_in])
|
||||
|
||||
Reference in New Issue
Block a user