mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Добавлен action для принятия правил + тесты для него
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace api\controllers;
|
||||
|
||||
use api\models\profile\AcceptRulesForm;
|
||||
use api\models\profile\ChangeEmail\ConfirmNewEmailForm;
|
||||
use api\models\profile\ChangeEmail\InitStateForm;
|
||||
use api\models\profile\ChangeEmail\NewEmailForm;
|
||||
@@ -21,7 +22,7 @@ class AccountsController extends Controller {
|
||||
'class' => AccessControl::class,
|
||||
'rules' => [
|
||||
[
|
||||
'actions' => ['current'],
|
||||
'actions' => ['current', 'accept-rules'],
|
||||
'allow' => true,
|
||||
'roles' => ['@'],
|
||||
],
|
||||
@@ -57,6 +58,7 @@ class AccountsController extends Controller {
|
||||
'change-email-submit-new-email' => ['POST'],
|
||||
'change-email-confirm-new-email' => ['POST'],
|
||||
'change-lang' => ['POST'],
|
||||
'accept-rules' => ['POST'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -185,4 +187,20 @@ class AccountsController extends Controller {
|
||||
];
|
||||
}
|
||||
|
||||
public function actionAcceptRules() {
|
||||
$account = Yii::$app->user->identity;
|
||||
$model = new AcceptRulesForm($account);
|
||||
$model->load(Yii::$app->request->post());
|
||||
if (!$model->agreeWithLatestRules()) {
|
||||
return [
|
||||
'success' => false,
|
||||
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
35
api/models/profile/AcceptRulesForm.php
Normal file
35
api/models/profile/AcceptRulesForm.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace api\models\profile;
|
||||
|
||||
use api\models\base\ApiForm;
|
||||
use common\models\Account;
|
||||
use yii\base\ErrorException;
|
||||
use const \common\LATEST_RULES_VERSION;
|
||||
|
||||
class AcceptRulesForm extends ApiForm {
|
||||
|
||||
/**
|
||||
* @var Account
|
||||
*/
|
||||
private $account;
|
||||
|
||||
public function __construct(Account $account, array $config = []) {
|
||||
$this->account = $account;
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
public function agreeWithLatestRules() : bool {
|
||||
$account = $this->getAccount();
|
||||
$account->rules_agreement_version = LATEST_RULES_VERSION;
|
||||
if (!$account->save()) {
|
||||
throw new ErrorException('Cannot set user rules version');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getAccount() : Account {
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user