From 426fa7dc6e3b9487cab2d4fe1e9ce28b0b175233 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sun, 7 Aug 2016 03:08:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D1=80=D0=B0=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82=D1=80=20ActiveUse?= =?UTF-8?q?rRule=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D0=BA=D0=B8=20=D0=B0=D0=BA=D1=82=D0=B8=D0=B2=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B8=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D1=8C=D1=81=D0=BA=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=B0=D0=BA=D0=BA=D0=B0=D1=83=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/controllers/AccountsController.php | 10 +--- api/controllers/OauthController.php | 4 +- api/filters/ActiveUserRule.php | 31 ++++++++++ .../api/unit/filters/ActiveUserRuleTest.php | 60 +++++++++++++++++++ 4 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 api/filters/ActiveUserRule.php create mode 100644 tests/codeception/api/unit/filters/ActiveUserRuleTest.php diff --git a/api/controllers/AccountsController.php b/api/controllers/AccountsController.php index e349d18..1f12be4 100644 --- a/api/controllers/AccountsController.php +++ b/api/controllers/AccountsController.php @@ -1,6 +1,7 @@ ['@'], ], [ + 'class' => ActiveUserRule::class, 'actions' => [ 'change-password', 'change-username', @@ -35,14 +37,6 @@ class AccountsController extends Controller { 'change-email-confirm-new-email', 'change-lang', ], - 'allow' => true, - 'roles' => ['@'], - 'matchCallback' => function() { - $account = Yii::$app->user->identity; - - return $account->status > Account::STATUS_REGISTERED - && $account->isAgreedWithActualRules(); - }, ], ], ], diff --git a/api/controllers/OauthController.php b/api/controllers/OauthController.php index 0a0328f..bcf224b 100644 --- a/api/controllers/OauthController.php +++ b/api/controllers/OauthController.php @@ -1,6 +1,7 @@ ['?'], ], [ + 'class' => ActiveUserRule::class, 'actions' => ['complete'], - 'allow' => true, - 'roles' => ['@'], ], ], ], diff --git a/api/filters/ActiveUserRule.php b/api/filters/ActiveUserRule.php new file mode 100644 index 0000000..4b803bd --- /dev/null +++ b/api/filters/ActiveUserRule.php @@ -0,0 +1,31 @@ +getIdentity(); + + return $account->status > Account::STATUS_REGISTERED + && $account->isAgreedWithActualRules(); + } + + /** + * @return \api\models\AccountIdentity|null + */ + protected function getIdentity() { + return Yii::$app->getUser()->getIdentity(); + } + +} diff --git a/tests/codeception/api/unit/filters/ActiveUserRuleTest.php b/tests/codeception/api/unit/filters/ActiveUserRuleTest.php new file mode 100644 index 0000000..d48f29a --- /dev/null +++ b/tests/codeception/api/unit/filters/ActiveUserRuleTest.php @@ -0,0 +1,60 @@ +specify('get false if user not finished registration', function() use (&$account) { + $account->status = 0; + $filter = $this->getFilterMock($account); + expect($this->callProtected($filter, 'matchCustom', new Action(null, null)))->false(); + }); + + $this->specify('get false if user have old EULA agreement', function() use (&$account) { + $account->status = Account::STATUS_ACTIVE; + $account->rules_agreement_version = null; + $filter = $this->getFilterMock($account); + expect($this->callProtected($filter, 'matchCustom', new Action(null, null)))->false(); + }); + + $this->specify('get true if user fully active', function() use (&$account) { + $account->status = Account::STATUS_ACTIVE; + $account->rules_agreement_version = LATEST_RULES_VERSION; + $filter = $this->getFilterMock($account); + expect($this->callProtected($filter, 'matchCustom', new Action(null, null)))->true(); + }); + } + + /** + * @param AccountIdentity $returnIdentity + * @return ActiveUserRule|\PHPUnit_Framework_MockObject_MockObject + */ + private function getFilterMock(AccountIdentity $returnIdentity) { + /** @var ActiveUserRule|\PHPUnit_Framework_MockObject_MockObject $filter */ + $filter = $this + ->getMockBuilder(ActiveUserRule::class) + ->setMethods(['getIdentity']) + ->getMock(); + + $filter + ->expects($this->any()) + ->method('getIdentity') + ->will($this->returnValue($returnIdentity)); + + return $filter; + } + +}