2016-12-11 17:07:55 +05:30
|
|
|
|
<?php
|
|
|
|
|
namespace api\modules\internal\models;
|
|
|
|
|
|
|
|
|
|
use api\models\base\ApiForm;
|
2016-12-18 04:50:53 +05:30
|
|
|
|
use api\modules\internal\helpers\Error as E;
|
2016-12-11 17:07:55 +05:30
|
|
|
|
use common\helpers\Amqp;
|
|
|
|
|
use common\models\Account;
|
|
|
|
|
use common\models\amqp\AccountBanned;
|
|
|
|
|
use PhpAmqpLib\Message\AMQPMessage;
|
|
|
|
|
use Yii;
|
2016-12-16 14:02:13 +05:30
|
|
|
|
use yii\base\ErrorException;
|
2016-12-11 17:07:55 +05:30
|
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
|
class BanForm extends ApiForm {
|
2016-12-11 17:07:55 +05:30
|
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
|
public const DURATION_FOREVER = -1;
|
2016-12-11 17:07:55 +05:30
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Нереализованный функционал блокировки аккаунта на определённый период времени.
|
|
|
|
|
* Сейчас установка этого параметра ничего не даст, аккаунт будет заблокирован навечно,
|
2016-12-18 04:50:53 +05:30
|
|
|
|
* но, по задумке, здесь можно передать количество секунд, на которое будет
|
2016-12-11 17:07:55 +05:30
|
|
|
|
* заблокирован аккаунт пользователя.
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
public $duration = self::DURATION_FOREVER;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Нереализованный функционал указания причины блокировки аккаунта.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2016-12-16 14:02:13 +05:30
|
|
|
|
public $message = '';
|
2016-12-11 17:07:55 +05:30
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var Account
|
|
|
|
|
*/
|
|
|
|
|
private $account;
|
|
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
|
public function rules(): array {
|
2016-12-11 17:07:55 +05:30
|
|
|
|
return [
|
|
|
|
|
[['duration'], 'integer', 'min' => self::DURATION_FOREVER],
|
|
|
|
|
[['message'], 'string'],
|
2016-12-18 04:50:53 +05:30
|
|
|
|
[['account'], 'validateAccountActivity'],
|
2016-12-11 17:07:55 +05:30
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAccount(): Account {
|
|
|
|
|
return $this->account;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
|
public function validateAccountActivity() {
|
|
|
|
|
if ($this->account->status === Account::STATUS_BANNED) {
|
|
|
|
|
$this->addError('account', E::ACCOUNT_ALREADY_BANNED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-11 17:07:55 +05:30
|
|
|
|
public function ban(): bool {
|
2016-12-18 04:50:53 +05:30
|
|
|
|
if (!$this->validate()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-11 17:07:55 +05:30
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
|
|
|
|
|
|
$account = $this->account;
|
|
|
|
|
$account->status = Account::STATUS_BANNED;
|
2016-12-16 14:02:13 +05:30
|
|
|
|
if (!$account->save()) {
|
|
|
|
|
throw new ErrorException('Cannot ban account');
|
|
|
|
|
}
|
2016-12-11 17:07:55 +05:30
|
|
|
|
|
|
|
|
|
$this->createTask();
|
|
|
|
|
|
|
|
|
|
$transaction->commit();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
|
public function createTask(): void {
|
2016-12-11 17:07:55 +05:30
|
|
|
|
$model = new AccountBanned();
|
|
|
|
|
$model->accountId = $this->account->id;
|
|
|
|
|
$model->duration = $this->duration;
|
|
|
|
|
$model->message = $this->message;
|
|
|
|
|
|
|
|
|
|
$message = Amqp::getInstance()->prepareMessage($model, [
|
|
|
|
|
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
Amqp::sendToEventsExchange('accounts.account-banned', $message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __construct(Account $account, array $config = []) {
|
|
|
|
|
$this->account = $account;
|
|
|
|
|
parent::__construct($config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|