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;
|
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-01-05 03:27:04 +05:30
|
|
|
public function verbs() {
|
|
|
|
return [
|
|
|
|
'ban' => ['POST', 'DELETE'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|