From f54246d17fbd0b82c3c5475eac140d97c5137725 Mon Sep 17 00:00:00 2001 From: Andrii Vasyliev Date: Fri, 23 Dec 2016 17:18:01 +0000 Subject: [PATCH] added `ValidateMfaBehavior` --- src/Module.php | 14 ++--------- src/behaviors/ValidateMfaBehavior.php | 35 +++++++++++++++++++++++++++ src/config/hisite.php | 2 +- 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 src/behaviors/ValidateMfaBehavior.php diff --git a/src/Module.php b/src/Module.php index c27144b..ad6f99b 100644 --- a/src/Module.php +++ b/src/Module.php @@ -13,7 +13,6 @@ use hiqdev\yii2\mfa\base\Totp; use Yii; -use yii\base\Event; use yii\di\Instance; use yii\helpers\StringHelper; use yii\validators\IpValidator; @@ -75,16 +74,7 @@ public function removeHalfUser() $this->sessionRemove('halfUser'); } - public static function onBeforeLogin(Event $event) - { - $module = Yii::$app->getModule('mfa'); - $identity = $event->identity; - $module->setHalfUser($identity); - $module->validateIps($identity); - $module->validateTotp($identity); - } - - protected function validateIps(IdentityInterface $identity) + public function validateIps(IdentityInterface $identity) { if (empty($identity->allowed_ips)) { return; @@ -102,7 +92,7 @@ protected function validateIps(IdentityInterface $identity) Yii::$app->end(); } - protected function validateTotp(IdentityInterface $identity) + public function validateTotp(IdentityInterface $identity) { if (empty($identity->totp_secret)) { return; diff --git a/src/behaviors/ValidateMfaBehavior.php b/src/behaviors/ValidateMfaBehavior.php new file mode 100644 index 0000000..70ac06a --- /dev/null +++ b/src/behaviors/ValidateMfaBehavior.php @@ -0,0 +1,35 @@ + 'beforeLogin', + ]; + } + + public function beforeLogin(Event $event) + { + $module = Yii::$app->getModule('mfa'); + $identity = $event->identity; + $module->setHalfUser($identity); + $module->validateIps($identity); + $module->validateTotp($identity); + } +} diff --git a/src/config/hisite.php b/src/config/hisite.php index 16dba18..9ce1520 100644 --- a/src/config/hisite.php +++ b/src/config/hisite.php @@ -20,7 +20,7 @@ ], ], 'user' => [ - 'on beforeLogin' => [\hiqdev\yii2\mfa\Module::class, 'onBeforeLogin'], + 'as validateMfa' => \hiqdev\yii2\mfa\behaviors\ValidateMfaBehavior::class, ], 'themeManager' => [ 'pathMap' => [