mirror of
https://github.com/elyby/accounts.git
synced 2024-11-30 02:32:26 +05:30
Базовая реализация API для блокировки аккаунта
This commit is contained in:
parent
213782ff62
commit
28b06d51ce
30
api/modules/internal/controllers/AccountsController.php
Normal file
30
api/modules/internal/controllers/AccountsController.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace api\modules\internal\controllers;
|
||||
|
||||
use api\components\ApiUser\AccessControl;
|
||||
use api\controllers\Controller;
|
||||
use common\models\OauthScope as S;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class AccountsController extends Controller {
|
||||
|
||||
public function behaviors() {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'access' => [
|
||||
'class' => AccessControl::class,
|
||||
'rules' => [
|
||||
[
|
||||
'actions' => ['block'],
|
||||
'allow' => true,
|
||||
'roles' => [S::ACCOUNT_BLOCK],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionBlock(int $accountId) {
|
||||
|
||||
}
|
||||
|
||||
}
|
80
api/modules/internal/models/BlockForm.php
Normal file
80
api/modules/internal/models/BlockForm.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
namespace api\modules\internal\models;
|
||||
|
||||
use api\models\base\ApiForm;
|
||||
use common\helpers\Amqp;
|
||||
use common\models\Account;
|
||||
use common\models\amqp\AccountBanned;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
use Yii;
|
||||
|
||||
class BlockForm extends ApiForm {
|
||||
|
||||
const DURATION_FOREVER = -1;
|
||||
|
||||
/**
|
||||
* Нереализованный функционал блокировки аккаунта на определённый период времени.
|
||||
* Сейчас установка этого параметра ничего не даст, аккаунт будет заблокирован навечно,
|
||||
* но по задумке, здесь необходимо передать количество секунд, на которое будет
|
||||
* заблокирован аккаунт пользователя.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $duration = self::DURATION_FOREVER;
|
||||
|
||||
/**
|
||||
* Нереализованный функционал указания причины блокировки аккаунта.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $message;
|
||||
|
||||
/**
|
||||
* @var Account
|
||||
*/
|
||||
private $account;
|
||||
|
||||
public function rules() {
|
||||
return [
|
||||
[['duration'], 'integer', 'min' => self::DURATION_FOREVER],
|
||||
[['message'], 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
public function getAccount(): Account {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
public function ban(): bool {
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
|
||||
$account = $this->account;
|
||||
$account->status = Account::STATUS_BANNED;
|
||||
$account->save();
|
||||
|
||||
$this->createTask();
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function createTask() {
|
||||
$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);
|
||||
}
|
||||
|
||||
}
|
14
common/models/amqp/AccountBanned.php
Normal file
14
common/models/amqp/AccountBanned.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace common\models\amqp;
|
||||
|
||||
use yii\base\Object;
|
||||
|
||||
class AccountBanned extends Object {
|
||||
|
||||
public $accountId;
|
||||
|
||||
public $duration = -1;
|
||||
|
||||
public $message = '';
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user