Skip to content

Commit 0fc6e2f

Browse files
committed
introduce $allowedIpConfiguration
Ip configuration that determines if an administrator can log in. Defaults to null which means that no ip check is being performed.
1 parent 7fe5766 commit 0fc6e2f

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

Module.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ class Module extends BaseModule
8484
/** @var array Model map */
8585
public $modelMap = [];
8686

87+
/** @var array Ip configuration that determines if an administrator can log in. Defaults to null which means that no
88+
* ip check is being performed.
89+
* It uses the yii\validators\IpValidator for validation:
90+
* @see http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#ip
91+
* @see http://www.yiiframework.com/doc-2.0/yii-validators-ipvalidator.html
92+
* @example
93+
'ranges' => [
94+
'192.168.10.128'
95+
'!192.168.10.0/24',
96+
'any' // allows any other IP addresses
97+
]
98+
* In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the 192.168.10.0/24 subnet.
99+
* IPv4 address 192.168.10.128 is also allowed, because it is listed before the restriction.
100+
*/
101+
public $allowedIpConfiguration = null;
102+
87103
/**
88104
* @var string The prefix for user module URL.
89105
*

models/LoginForm.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
use dektrium\user\Finder;
1515
use dektrium\user\helpers\Password;
1616
use dektrium\user\traits\ModuleTrait;
17+
use yii\base\DynamicModel;
1718
use yii\helpers\ArrayHelper;
1819
use yii\helpers\Html;
1920
use Yii;
2021
use yii\base\Model;
22+
use yii\validators\IpValidator;
2123

2224
/**
2325
* LoginForm get user's login and password, validates them and logs the user in. If user has been blocked, it adds
@@ -53,7 +55,7 @@ public function __construct(Finder $finder, $config = [])
5355
$this->finder = $finder;
5456
parent::__construct($config);
5557
}
56-
58+
5759
/**
5860
* Gets all users to generate the dropdown list when in debug mode.
5961
*
@@ -145,6 +147,30 @@ public function login()
145147
}
146148

147149

150+
/**
151+
* Validates if an administrator is coming from the correct IP adress.
152+
*
153+
* @see Module $allowedIpConfiguration
154+
* @see http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#ip
155+
* @see http://www.yiiframework.com/doc-2.0/yii-validators-ipvalidator.html
156+
* @return mixed true if the check is correct, an array with the errors otherwise
157+
*/
158+
public function checkValidIpAddresses()
159+
{
160+
$config = $this->module->allowedIpConfiguration;
161+
162+
if (!$this->user->isAdmin || !$config) {
163+
return true;
164+
}
165+
166+
$rules = ArrayHelper::merge(['login', 'ip'], $config);
167+
168+
$model = DynamicModel::validateData(['login' => Yii::$app->request->userIP], [$rules]);
169+
170+
return $model->hasErrors() ? $model->getErrors() : true;
171+
}
172+
173+
148174
/** @inheritdoc */
149175
public function formName()
150176
{
@@ -157,6 +183,14 @@ public function beforeValidate()
157183
if (parent::beforeValidate()) {
158184
$this->user = $this->finder->findUserByUsernameOrEmail(trim($this->login));
159185

186+
$valid = $this->checkValidIpAddresses();
187+
188+
if($valid !== true) {
189+
foreach($valid as $error) {
190+
$this->addError('login', $error);
191+
}
192+
}
193+
160194
return true;
161195
} else {
162196
return false;

0 commit comments

Comments
 (0)