diff --git a/api/controllers/AccountsController.php b/api/controllers/AccountsController.php index 2861d3c..e349d18 100644 --- a/api/controllers/AccountsController.php +++ b/api/controllers/AccountsController.php @@ -1,6 +1,7 @@ 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, + ]; + } + } diff --git a/api/models/profile/AcceptRulesForm.php b/api/models/profile/AcceptRulesForm.php new file mode 100644 index 0000000..05785f0 --- /dev/null +++ b/api/models/profile/AcceptRulesForm.php @@ -0,0 +1,35 @@ +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; + } + +} diff --git a/tests/codeception/api/_pages/AccountsRoute.php b/tests/codeception/api/_pages/AccountsRoute.php index 7b4938b..5ff4a3b 100644 --- a/tests/codeception/api/_pages/AccountsRoute.php +++ b/tests/codeception/api/_pages/AccountsRoute.php @@ -59,4 +59,9 @@ class AccountsRoute extends BasePage { ]); } + public function acceptRules() { + $this->route = ['accounts/accept-rules']; + $this->actor->sendPOST($this->getUrl()); + } + } diff --git a/tests/codeception/api/functional/AccountsAcceptRulesCest.php b/tests/codeception/api/functional/AccountsAcceptRulesCest.php new file mode 100644 index 0000000..4f6fee7 --- /dev/null +++ b/tests/codeception/api/functional/AccountsAcceptRulesCest.php @@ -0,0 +1,28 @@ +route = new AccountsRoute($I); + } + + public function testCurrent(FunctionalTester $I) { + $I->loggedInAsActiveAccount('Veleyaba', 'password_0'); + $this->route->acceptRules(); + $I->canSeeResponseCodeIs(200); + $I->canSeeResponseIsJson(); + $I->canSeeResponseContainsJson([ + 'success' => true, + ]); + } + +} diff --git a/tests/codeception/api/unit/models/profile/AcceptRulesFormTest.php b/tests/codeception/api/unit/models/profile/AcceptRulesFormTest.php new file mode 100644 index 0000000..2105a40 --- /dev/null +++ b/tests/codeception/api/unit/models/profile/AcceptRulesFormTest.php @@ -0,0 +1,33 @@ + AccountFixture::class, + ]; + } + + public function testApplyLanguage() { + $this->specify('rules version bumped to latest', function() { + /** @var Account $account */ + $account = Account::findOne($this->accounts['account-with-old-rules-version']); + $model = new AcceptRulesForm($account); + expect($model->agreeWithLatestRules())->true(); + expect($account->rules_agreement_version)->equals(LATEST_RULES_VERSION); + }); + } + +} diff --git a/tests/codeception/common/fixtures/data/accounts.php b/tests/codeception/common/fixtures/data/accounts.php index b4f66f9..cde12ff 100644 --- a/tests/codeception/common/fixtures/data/accounts.php +++ b/tests/codeception/common/fixtures/data/accounts.php @@ -107,4 +107,17 @@ return [ 'created_at' => 1463349615, 'updated_at' => 1463349615, ], + 'account-with-old-rules-version' => [ + 'id' => 9, + 'uuid' => '410462d3-8e71-47cc-bac6-64f77f88cf80', + 'username' => 'Veleyaba', + 'email' => 'veleyaba@gmail.com', + 'password_hash' => '$2y$13$2rYkap5T6jG8z/mMK8a3Ou6aZxJcmAaTha6FEuujvHEmybSHRzW5e', # password_0 + 'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2, + 'lang' => 'en', + 'status' => \common\models\Account::STATUS_ACTIVE, + 'rules_agreement_version' => null, + 'created_at' => 1470499952, + 'updated_at' => 1470499952, + ], ];