/
Module.php
137 lines (117 loc) · 3.93 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace alhimik1986\yii2_settings_module;
use Yii;
use yii\di\Container;
use yii\web\ForbiddenHttpException;
use alhimik1986\yii2_settings_module\models\SettingsModel;
class Module extends \yii\base\Module
{
public $controllerNamespace = 'alhimik1986\yii2_settings_module\controllers';
//public $defaultRoute = 'setting';
public $allowedIPs = ['127.0.0.1', '::1'];
public $userModule; // Компонент пользователя (для этого модуля)
public $password = '123'; // Пароль по умолчанию
public $password_in_settings = false; // Брать и проверять пароль в настройка (settings.json), а не в web.config-файле
public $l18n_base_path = null;
public $settings_json_file = null;
public function init()
{
parent::init();
$this->registerTranslations();
$this->userModule = new Container();
$this->userModule->set('user', [
'identityClass' => __NAMESPACE__.'\models\User',
'enableAutoLogin' => true,
'class' => 'yii\web\User',
'loginUrl' => [$this->id.'/site/login'],
]);
if ($this->settings_json_file)
SettingsModel::set_custom_settings_json_file_path($this->settings_json_file);
}
protected static function namespace_str()
{
return str_replace('\\', '/', __NAMESPACE__);
}
public function registerTranslations()
{
Yii::$app->i18n->translations[self::namespace_str().'/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => $this->l18n_base_path ? $this->l18n_base_path : __DIR__ .'/messages',
'fileMap' => [
self::namespace_str().'/app' => 'messages.php',
self::namespace_str().'/settings' => 'settings_messages.php',
],
];
}
/**
* Использование интернационализации (перевода) внутри модуля.
* Пример использования: use alhimik1986\yii2-settings-module\Module; Module::t('app', 'The data is not found!');
*/
public static function t($category, $message, $params = [], $language = null)
{
return Yii::t(self::namespace_str() .'/'. $category, $message, $params, $language);
}
/**
* @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.');
}
if ( ! $this->checkUser()) {
$this->userModule->get('user')->loginRequired();
return false;
}
$this->resetGlobalSettings();
return true;
}
/**
* Resets potentially incompatible global settings done in app config.
*/
protected function resetGlobalSettings()
{
if (Yii::$app instanceof \yii\web\Application) {
Yii::$app->assetManager->bundles = [];
}
}
/**
* @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 Gii is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
return false;
}
protected function checkUser()
{
if ( ! $this->is_password_required())
return true;
$user = $this->userModule->get('user');
$result = $user->getIsGuest();
return ! $result;
}
public function is_password_required()
{
if (Yii::$app->controller->id == 'site')
return false;
if ( ! $this->password_in_settings AND empty($this->password)) {
return false;
} else if ($this->password_in_settings) {
$pass = SettingsModel::getSetting('password');
if (empty($pass))
return false;
}
return true;
}
}