2016-12-11 17:07:55 +05:30
|
|
|
<?php
|
|
|
|
namespace api\modules\internal\controllers;
|
|
|
|
|
|
|
|
use api\components\ApiUser\AccessControl;
|
|
|
|
use api\controllers\Controller;
|
2016-12-18 04:50:53 +05:30
|
|
|
use api\modules\internal\models\BanForm;
|
2017-01-05 03:27:04 +05:30
|
|
|
use api\modules\internal\models\PardonForm;
|
2016-12-16 14:06:21 +05:30
|
|
|
use common\models\Account;
|
2016-12-11 17:07:55 +05:30
|
|
|
use common\models\OauthScope as S;
|
2016-12-16 14:06:21 +05:30
|
|
|
use Yii;
|
2016-12-11 17:07:55 +05:30
|
|
|
use yii\helpers\ArrayHelper;
|
2017-04-03 17:24:33 +05:30
|
|
|
use yii\web\BadRequestHttpException;
|
2016-12-16 14:06:21 +05:30
|
|
|
use yii\web\NotFoundHttpException;
|
2016-12-11 17:07:55 +05:30
|
|
|
|
|
|
|
class AccountsController extends Controller {
|
|
|
|
|
|
|
|
public function behaviors() {
|
|
|
|
return ArrayHelper::merge(parent::behaviors(), [
|
2016-12-18 04:50:53 +05:30
|
|
|
'authenticator' => [
|
|
|
|
'user' => Yii::$app->apiUser,
|
|
|
|
],
|
2016-12-11 17:07:55 +05:30
|
|
|
'access' => [
|
|
|
|
'class' => AccessControl::class,
|
|
|
|
'rules' => [
|
|
|
|
[
|
2016-12-18 04:50:53 +05:30
|
|
|
'actions' => ['ban'],
|
2016-12-11 17:07:55 +05:30
|
|
|
'allow' => true,
|
|
|
|
'roles' => [S::ACCOUNT_BLOCK],
|
|
|
|
],
|
2017-04-03 17:24:33 +05:30
|
|
|
[
|
|
|
|
'actions' => ['info'],
|
|
|
|
'allow' => true,
|
|
|
|
'roles' => [S::INTERNAL_ACCOUNT_INFO],
|
|
|
|
],
|
2016-12-11 17:07:55 +05:30
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-01-05 03:27:04 +05:30
|
|
|
public function verbs() {
|
|
|
|
return [
|
|
|
|
'ban' => ['POST', 'DELETE'],
|
2017-04-03 17:24:33 +05:30
|
|
|
'info' => ['GET'],
|
2017-01-05 03:27:04 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-18 04:50:53 +05:30
|
|
|
public function actionBan(int $accountId) {
|
2016-12-16 14:06:21 +05:30
|
|
|
$account = $this->findAccount($accountId);
|
2017-01-05 03:27:04 +05:30
|
|
|
if (Yii::$app->request->isPost) {
|
|
|
|
return $this->banAccount($account);
|
|
|
|
} else {
|
|
|
|
return $this->pardonAccount($account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 17:24:33 +05:30
|
|
|
public function actionInfo(int $id = null, string $username = null, string $uuid = null) {
|
|
|
|
if ($id !== null) {
|
|
|
|
$account = Account::findOne($id);
|
|
|
|
} elseif ($username !== null) {
|
|
|
|
$account = Account::findOne(['username' => $username]);
|
|
|
|
} elseif ($uuid !== null) {
|
|
|
|
$account = Account::findOne(['uuid' => $uuid]);
|
|
|
|
} else {
|
|
|
|
throw new BadRequestHttpException('One of the required get params must be presented.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($account === null) {
|
|
|
|
throw new NotFoundHttpException('User by provided param not found.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => $account->id,
|
|
|
|
'uuid' => $account->uuid,
|
|
|
|
'email' => $account->email,
|
|
|
|
'username' => $account->username,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-01-05 03:27:04 +05:30
|
|
|
private function banAccount(Account $account) {
|
2016-12-18 04:50:53 +05:30
|
|
|
$model = new BanForm($account);
|
2016-12-16 14:06:21 +05:30
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (!$model->ban()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $model->getFirstErrors(),
|
|
|
|
];
|
|
|
|
}
|
2016-12-11 17:07:55 +05:30
|
|
|
|
2016-12-16 14:06:21 +05:30
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-01-05 03:27:04 +05:30
|
|
|
private function pardonAccount(Account $account) {
|
|
|
|
$model = new PardonForm($account);
|
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (!$model->pardon()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $model->getFirstErrors(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:06:21 +05:30
|
|
|
private function findAccount(int $accountId): Account {
|
|
|
|
$account = Account::findOne($accountId);
|
|
|
|
if ($account === null) {
|
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $account;
|
2016-12-11 17:07:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|