mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Добавлен контроллер для блокировки аккаунта
Добавлен client_credentials grant для oAuth Рефакторинг структуры OauthScopes чтобы можно было разделить владельца прав на пользовательские и общие (машинные) Исправлена стилистика кода, внедряются фишки PHP 7.1
This commit is contained in:
19
api/modules/internal/Module.php
Normal file
19
api/modules/internal/Module.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace api\modules\internal;
|
||||
|
||||
use yii\base\BootstrapInterface;
|
||||
|
||||
class Module extends \yii\base\Module implements BootstrapInterface {
|
||||
|
||||
public $id = 'internal';
|
||||
|
||||
/**
|
||||
* @param \yii\base\Application $app the application currently running
|
||||
*/
|
||||
public function bootstrap($app) {
|
||||
$app->getUrlManager()->addRules([
|
||||
'/internal/<controller>/<accountId>/<action>' => "{$this->id}/<controller>/<action>",
|
||||
], false);
|
||||
}
|
||||
|
||||
}
|
@@ -3,7 +3,7 @@ namespace api\modules\internal\controllers;
|
||||
|
||||
use api\components\ApiUser\AccessControl;
|
||||
use api\controllers\Controller;
|
||||
use api\modules\internal\models\BlockForm;
|
||||
use api\modules\internal\models\BanForm;
|
||||
use common\models\Account;
|
||||
use common\models\OauthScope as S;
|
||||
use Yii;
|
||||
@@ -14,11 +14,14 @@ class AccountsController extends Controller {
|
||||
|
||||
public function behaviors() {
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
'authenticator' => [
|
||||
'user' => Yii::$app->apiUser,
|
||||
],
|
||||
'access' => [
|
||||
'class' => AccessControl::class,
|
||||
'rules' => [
|
||||
[
|
||||
'actions' => ['block'],
|
||||
'actions' => ['ban'],
|
||||
'allow' => true,
|
||||
'roles' => [S::ACCOUNT_BLOCK],
|
||||
],
|
||||
@@ -27,9 +30,9 @@ class AccountsController extends Controller {
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionBlock(int $accountId) {
|
||||
public function actionBan(int $accountId) {
|
||||
$account = $this->findAccount($accountId);
|
||||
$model = new BlockForm($account);
|
||||
$model = new BanForm($account);
|
||||
$model->load(Yii::$app->request->post());
|
||||
if (!$model->ban()) {
|
||||
return [
|
||||
|
8
api/modules/internal/helpers/Error.php
Normal file
8
api/modules/internal/helpers/Error.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace api\modules\internal\helpers;
|
||||
|
||||
final class Error {
|
||||
|
||||
public const ACCOUNT_ALREADY_BANNED = 'error.account_already_banned';
|
||||
|
||||
}
|
@@ -2,6 +2,7 @@
|
||||
namespace api\modules\internal\models;
|
||||
|
||||
use api\models\base\ApiForm;
|
||||
use api\modules\internal\helpers\Error as E;
|
||||
use common\helpers\Amqp;
|
||||
use common\models\Account;
|
||||
use common\models\amqp\AccountBanned;
|
||||
@@ -9,14 +10,14 @@ use PhpAmqpLib\Message\AMQPMessage;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class BlockForm extends ApiForm {
|
||||
class BanForm extends ApiForm {
|
||||
|
||||
const DURATION_FOREVER = -1;
|
||||
public const DURATION_FOREVER = -1;
|
||||
|
||||
/**
|
||||
* Нереализованный функционал блокировки аккаунта на определённый период времени.
|
||||
* Сейчас установка этого параметра ничего не даст, аккаунт будет заблокирован навечно,
|
||||
* но по задумке, здесь необходимо передать количество секунд, на которое будет
|
||||
* но, по задумке, здесь можно передать количество секунд, на которое будет
|
||||
* заблокирован аккаунт пользователя.
|
||||
*
|
||||
* @var int
|
||||
@@ -35,10 +36,11 @@ class BlockForm extends ApiForm {
|
||||
*/
|
||||
private $account;
|
||||
|
||||
public function rules() {
|
||||
public function rules(): array {
|
||||
return [
|
||||
[['duration'], 'integer', 'min' => self::DURATION_FOREVER],
|
||||
[['message'], 'string'],
|
||||
[['account'], 'validateAccountActivity'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -46,7 +48,17 @@ class BlockForm extends ApiForm {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
public function validateAccountActivity() {
|
||||
if ($this->account->status === Account::STATUS_BANNED) {
|
||||
$this->addError('account', E::ACCOUNT_ALREADY_BANNED);
|
||||
}
|
||||
}
|
||||
|
||||
public function ban(): bool {
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transaction = Yii::$app->db->beginTransaction();
|
||||
|
||||
$account = $this->account;
|
||||
@@ -62,7 +74,7 @@ class BlockForm extends ApiForm {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function createTask() {
|
||||
public function createTask(): void {
|
||||
$model = new AccountBanned();
|
||||
$model->accountId = $this->account->id;
|
||||
$model->duration = $this->duration;
|
Reference in New Issue
Block a user