-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
382 lines (342 loc) · 11.7 KB
/
Module.php
File metadata and controls
382 lines (342 loc) · 11.7 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* @copyright Copyright (c) 2014 Vitaliy Syrchikov
* @link http://syrchikov.name
*/
namespace maddoger\admin;
use maddoger\core\BackendModule;
use Yii;
use yii\rbac\Item;
/**
* Module
*
* @author Vitaliy Syrchikov <maddoger@gmail.com>
* @link http://syrchikov.name
* @package maddoger/yii2-admin
*/
class Module extends BackendModule
{
/**
* @var string URL to logo for admin panel
*/
public $logoUrl;
/**
* @var string logo text
*/
public $logoText;
/**
* @var string
*/
public $dashboardView;
/**
* @var array menu items for sidebar menu
*/
public $sidebarMenu;
/**
* @var bool append modules navigation to sidebar menu
*/
public $sidebarMenuUseModules = true;
/**
* @var int time in seconds for sidebar menu cache
* 0 - infinity
* false - disable caching
* Default: 60 seconds
*/
public $sidebarMenuCache = 60;
/**
* @var string view for sidebar
*/
public $sidebarView = '@maddoger/admin/views/layouts/_sidebar.php';
/**
* @var string view for main menu
*/
public $sidebarMenuView = '@maddoger/admin/views/layouts/_sidebarMenu.php';
/**
* @var string view for user menu in the header
*/
public $headerUserView = '@maddoger/admin/views/layouts/_headerUser.php';
/**
* @var string view for notifications menu in the header
*/
public $headerNotificationsView = '@maddoger/admin/views/layouts/_headerNotifications.php';
/**
* @var string superuser role name.
* It will be used as a parent for all RBAC roles loaded from modules.
*/
public $superUserRole = 'superuser';
/**
* @var int superuser id
* This user will get all RBAC roles loaded from modules.
*/
public $superUserId;
/**
* @var bool
*/
public $searchUseModulesSources = true;
/**
* @var array additional search sources
*/
public $searchSources;
/**
* @var bool
*/
public $sendSystemMessageOnServerError = true;
/**
* Init module
*/
public function init()
{
parent::init();
if (!isset(Yii::$app->i18n->translations['maddoger/admin'])) {
Yii::$app->i18n->translations['maddoger/admin'] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@maddoger/admin/messages',
'sourceLanguage' => 'en-US',
];
}
}
/**
* @inheritdoc
*/
public function getName()
{
return Yii::t('maddoger/admin', 'Admin Panel Module');
}
/**
* @inheritdoc
*/
public function getVersion()
{
return 'dev';
}
/**
* @inheritdoc
*/
public function getNavigation()
{
return [
[
'label' => Yii::t('maddoger/admin', 'Admin panel'),
'icon' => 'fa fa-gear',
'items' => [
[
'label' => Yii::t('maddoger/admin', 'Users'),
'url' => ['/' . $this->id . '/user/index'],
'activeUrl' => '/' . $this->id . '/user/*',
'icon' => 'fa fa-user',
'roles' => ['admin.user.view'],
],
[
'label' => Yii::t('maddoger/admin', 'User roles'),
'url' => ['/' . $this->id . '/role/index'],
'activeUrl' => '/' . $this->id . '/role/*',
'icon' => 'fa fa-users',
'roles' => ['admin.rbac.manageRoles'],
],
[
'label' => Yii::t('maddoger/admin', 'System messages'),
'url' => ['/' . $this->id . '/system-messages/index'],
'activeUrl' => '/' . $this->id . '/system-messages/*',
'icon' => 'fa fa-warning',
'roles' => ['admin.system-messages.viewList'],
],
]
]
];
}
/**
* @inheritdoc
*/
public function getRbacItems()
{
return [
//Users
'admin.user.dashboard' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. Access to dashboard'),
],
'admin.user.profile' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. Update own profile'),
],
'admin.user.view' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. View admins'),
],
'admin.user.create' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. Create admins'),
],
'admin.user.update' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. Update admins'),
],
'admin.user.delete' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. Delete admins'),
],
'admin.user.manager' =>
[
'type' => Item::TYPE_ROLE,
'description' => Yii::t('maddoger/admin', 'Admin. Manage admins'),
'children' => [
'admin.user.view',
'admin.user.create',
'admin.user.update',
'admin.user.delete',
],
],
//RBAC
'admin.rbac.updateFromModules' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. Update user roles from modules'),
],
'admin.rbac.manageRoles' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. Create, update and delete user roles'),
],
'admin.rbac.manager' =>
[
'type' => Item::TYPE_ROLE,
'description' => Yii::t('maddoger/admin', 'Admin. Manage user roles'),
'children' => [
'admin.rbac.manageRoles',
'admin.rbac.updateFromModules',
]
],
//Admin
'admin.system-messages.viewList' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. View system messages'),
],
'admin.system-messages.viewDetail' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. View system messages with details'),
],
'admin.system-messages.delete' =>
[
'type' => Item::TYPE_PERMISSION,
'description' => Yii::t('maddoger/admin', 'Admin. Delete system messages'),
],
'admin.base' =>
[
'type' => Item::TYPE_ROLE,
'description' => Yii::t('maddoger/admin', 'Admin. Base access to admin panel'),
'children' => [
'admin.user.dashboard',
'admin.user.profile',
'admin.system-messages.viewList',
]
],
];
}
/**
* @return array
*/
public function getSearchSources()
{
return [
[
'class' => '\maddoger\core\search\ArraySearchSource',
'data' => [
[
'label' => Yii::t('maddoger/admin', 'Users'),
'url' => ['/' . $this->id . '/user/index'],
],
[
'label' => Yii::t('maddoger/admin', 'User roles'),
'url' => ['/' . $this->id . '/role/index'],
],
],
'roles' => ['admin.user.view', 'admin.rbac.manageRoles'],
],
[
'class' => '\maddoger\core\search\ArraySearchSource',
'data' => [
[
'label' => Yii::t('maddoger/admin', 'System messages'),
'url' => ['/' . $this->id . '/system-messages/index'],
],
],
'roles' => ['admin.system-messages.viewList'],
],
[
'class' => '\maddoger\core\search\ActiveSearchSource',
'modelClass' => '\maddoger\admin\models\User',
'searchAttributes' => ['username', 'email', 'real_name'],
'url' => ['/' . $this->id . '/user/view', 'id' => null],
'label' => 'username',
'labelPrefix' => Yii::t('maddoger/admin', 'User - '),
'roles' => ['admin.user.view'],
],
];
}
/**
* @return array
*/
public function getSidebarMenu()
{
$menu = null;
$cacheKey = 'ADMIN_SIDEBAR_MENU';
if ($this->sidebarMenuCache !== false) {
$menu = Yii::$app->cache->get($cacheKey);
} else {
Yii::$app->cache->delete($cacheKey);
}
if (!$menu) {
$menu = $this->sidebarMenu ?:
[
[
'label' => Yii::t('maddoger/admin', 'Dashboard'),
'icon' => 'fa fa-dashboard',
'url' => ['/' . Module::getInstance()->id . '/site/index'],
'sort' => -1,
],
];
if ($this->sidebarMenuUseModules) {
$sortIndex = 0;
//Get navigation from modules
foreach (Yii::$app->modules as $moduleId => $module) {
if (!($module instanceof \yii\base\Module)) {
$module = Yii::$app->getModule($moduleId, true);
}
if ($module instanceof BackendModule) {
$sort = $module->sortNumber ?: (++$sortIndex)*100;
$navigation = $module->getNavigation();
foreach ($navigation as $key => $value) {
if (!isset($navigation[$key]['sort'])) {
$navigation[$key]['sort'] = $sort;
}
}
$menu = array_merge($menu, $navigation);
}
}
//Sort
usort($menu, function ($a, $b) {
$res = 0;
if ($a['sort'] != $b['sort']) {
$res = $a['sort'] > $b['sort'] ? 1 : -1;
}
/*if (!$res) {
$res = strcmp($a['label'], $b['label']);
}*/
return $res;
});
}
if ($this->sidebarMenuCache !== false) {
Yii::$app->cache->set($cacheKey, $menu, $this->sidebarMenuCache);
}
}
return $menu;
}
}