-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMrbacModule.php
181 lines (157 loc) · 5.09 KB
/
MrbacModule.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace mrbac;
use Yii;
use mrbac\components\Configs;
use yii\helpers\Inflector;
use yii\web\ForbiddenHttpException;
class MrbacModule extends \yii\base\Module
{
public $controllerNamespace = 'mrbac\controllers';
/**
* @inheritdoc
*/
public $defaultRoute = 'authitem';
// public $defaultRoute = 'assignment';
/**
* @var array
* @see [[items]]
*/
private $_menus = [];
/**
* @var array
* @see [[items]]
*/
private $_coreItems = [
'authitem/manager' => '授权项列表',
'authitem/auto' => '自动建立授权项',
'authitem/assign' => '分配授权项',
'authitem/edit-allowed' => '编辑总是允许',
];
/**
* @var array
* @see [[items]]
*/
private $_normalizeMenus;
/**
* Nav bar items
* @var array
*/
public $navbar;
/**
* @var string Main layout using for module. Default to layout of parent module.
* Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
*/
// public $mainLayout = '@mrbac/views/layouts/main.php';
public $userClassName;
public $idField = 'id';
public $usernameField = 'username';
public $searchClass;
public $debug=false;
public $layout = '@mrbac/views/layouts/left-menu.php';
public $alwaysAllows = array();
public $allowedIPs = ['127.0.0.1', '::1'];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (!isset(Yii::$app->i18n->translations['rbac-admin'])) {
Yii::$app->i18n->translations['rbac-admin'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@mrbac/messages'
];
}
//user did not define the Navbar?
if ($this->navbar === null) {
$this->navbar = [
// ['label' => Yii::t('rbac-admin', 'Help'), 'url' => 'https://github.com/mdmsoft/yii2-admin/blob/master/docs/guide/basic-usage.md'],
['label' => 'Application', 'url' => Yii::$app->homeUrl]
];
}
}
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (!parent::beforeAction($action)) {
return false;
}
$this->allowIps();
return true;
}
public function allowIps(){
if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
throw new ForbiddenHttpException('You are not allowed to access this page.');
}
}
/**
* @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;
}
/**
* Get avalible menu.
* @return array
*/
public function getMenus()
{
if ($this->_normalizeMenus === null) {
$mid = '/' . $this->getUniqueId() . '/';
// resolve core menus
$this->_normalizeMenus = [];
$config = Configs::instance();
foreach ($this->_coreItems as $id => $lable) {
if ($id !== 'menu' || ($config->db !== null && $config->db->schema->getTableSchema($config->menuTable) !== null)) {
$this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', $lable), 'url' => [$mid . $id]];
}
}
foreach (array_keys($this->controllerMap) as $id) {
$this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', Inflector::humanize($id)), 'url' => [$mid . $id]];
}
// user configure menus
foreach ($this->_menus as $id => $value) {
if (empty($value)) {
unset($this->_normalizeMenus[$id]);
} else {
if (is_string($value)) {
$value = [
'label' => $value,
];
}
$this->_normalizeMenus[$id] = isset($this->_normalizeMenus[$id]) ? array_merge($this->_normalizeMenus[$id], $value) : $value;
if (!isset($this->_normalizeMenus[$id]['url'])) {
$this->_normalizeMenus[$id]['url'] = [$mid . $id];
}
}
}
}
return $this->_normalizeMenus;
}
/**
* Set or add avalible menu.
* @param array $menus
*/
public function setMenus($menus)
{
$this->_menus = array_merge($this->_menus, $menus);
$this->_normalizeMenus = null;
}
/**
* @return array 总是允许的权限
*/
public function getAlwaysAllowed(){
return $this->alwaysAllows;
}
}