All hello. Do how i can filter access IP on modules config.php?
'modules' => [
'cms' => [
'class' => 'app\modules\cms\cms',
],
],
But i use
'identityClass' => 'app\modules\cms\models\fl\UserMy',
All hello. Do how i can filter access IP on modules config.php?
'modules' => [
'cms' => [
'class' => 'app\modules\cms\cms',
],
],
But i use
'identityClass' => 'app\modules\cms\models\fl\UserMy',
Hi Semiromid,
You can do it by implementing the BootstrapInterface to your module.
Step: 1 Add your custom public array property from your module
'modules' => [
'jay' => [
'class' => 'app\modules\jay\JayModule',
'allowedIPs' => ['58.182.49.223'],
],
],
Copy and paste the following code and customise to your module config.
/**
* @author Arockia Johnson<johnson@arojohnson.tk>
*/
namespace app\modules\jay;
use Yii;
use yii\base\BootstrapInterface;
use yii\web\ForbiddenHttpException;
/**
* jay module definition class
*/
class JayModule extends \yii\base\Module implements BootstrapInterface {
/**
* @inheritdoc
*/
public $controllerNamespace = 'app\modules\jay\controllers';
/**
*
* @var Array list of IP Addresses to allow
*/
public $allowedIPs = [];
/**
*
* @param \yii\web\Application $app
* @override BootstrapInterface -> bootstrap
*/
public function bootstrap($app) {
if ($app instanceof \yii\web\Application) {
//Write your rules if needed!
}
}
/**
* @inheritdoc
*/
public function init() {
parent::init();
// custom initialization code goes here
}
/**
* @inheritdoc
*/
public function beforeAction($action) {
if (!parent::beforeAction($action)) {
return false;
}
if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
throw new ForbiddenHttpException('You are not allowed to access this page.');
}
foreach (array_merge($this->coreGenerators(), $this->generators) as $id => $config) {
if (is_object($config)) {
$this->generators[$id] = $config;
} else {
$this->generators[$id] = Yii::createObject($config);
}
}
$this->resetGlobalSettings();
return true;
}
/**
* @return boolean whether the module can be accessed by the current user
*/
protected function checkAccess() {
$ip = Yii::$app->getRequest()->getUserIP();
foreach ($this->allowedIPs as $filter) {
if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
return true;
}
}
Yii::warning('Access to <YOUR_MODULE> is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
return false;
}
}
In before action you can check the list of IP Addresses to allow.