zet-lib 1.2.14 → 1.2.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/zRole.js +228 -189
- package/lib/zRoute.js +2 -1
- package/package.json +1 -1
package/lib/zRole.js
CHANGED
|
@@ -1,245 +1,284 @@
|
|
|
1
|
-
const Util = require('./Util')
|
|
2
|
-
const fs = require(
|
|
3
|
-
const myCache = require(
|
|
4
|
-
const zRoute = require('./zRoute')
|
|
1
|
+
const Util = require('./Util')
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
const myCache = require('./cache')
|
|
4
|
+
const zRoute = require('./zRoute')
|
|
5
5
|
|
|
6
|
-
const a = {}
|
|
6
|
+
const a = {}
|
|
7
7
|
|
|
8
8
|
/*
|
|
9
9
|
Please add your routes here
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
const routes = zRoute.ROUTES()
|
|
13
|
-
const cacheRoutes = myCache.get(
|
|
14
|
-
const cacheRoles = myCache.get(
|
|
12
|
+
const routes = zRoute.ROUTES()
|
|
13
|
+
const cacheRoutes = myCache.get('ROUTES')
|
|
14
|
+
const cacheRoles = myCache.get('ROLES') || {}
|
|
15
15
|
|
|
16
|
-
if(cacheRoutes && cacheRoutes.length) {
|
|
17
|
-
|
|
16
|
+
if (cacheRoutes && cacheRoutes.length) {
|
|
17
|
+
a.routes = process.env.NODE_ENV == 'production' ? Util.arrayDeletes(cacheRoutes, ['auth', 'test']) : Util.arrayDeletes(cacheRoutes, ['generator', 'auth', 'test'])
|
|
18
18
|
} else {
|
|
19
|
-
|
|
19
|
+
a.routes = process.env.NODE_ENV == 'production' ? Util.arrayDeletes(routes, ['auth', 'test']) : Util.arrayDeletes(routes, ['generator', 'auth', 'test'])
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/*
|
|
23
23
|
Default actions
|
|
24
24
|
you can additional here
|
|
25
25
|
*/
|
|
26
|
-
a.actions = ['index', 'create', 'update', 'delete', 'view', 'import', 'export','approval']
|
|
26
|
+
a.actions = ['index', 'create', 'update', 'delete', 'view', 'import', 'export', 'approval']
|
|
27
27
|
|
|
28
28
|
/*
|
|
29
29
|
all in table roles
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
|
-
a.params =
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
32
|
+
a.params = function (roleId) {
|
|
33
|
+
let cacheRoles = myCache.get('ROLES')
|
|
34
|
+
if (cacheRoles && cacheRoles.hasOwnProperty(roleId)) {
|
|
35
|
+
return roleId ? cacheRoles[roleId].params : {}
|
|
36
|
+
}
|
|
37
|
+
return {}
|
|
38
|
+
}
|
|
39
39
|
|
|
40
40
|
a.rules = function (roleId) {
|
|
41
|
-
|
|
42
|
-
}
|
|
41
|
+
return a.params(roleId)
|
|
42
|
+
}
|
|
43
43
|
|
|
44
|
-
a.list =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
44
|
+
a.list = (roleId, route) => {
|
|
45
|
+
let params = a.params(roleId)
|
|
46
|
+
return a.levels(route, params)
|
|
47
|
+
}
|
|
48
48
|
|
|
49
49
|
a.levels = (route, params) => {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
} else {
|
|
60
|
-
obj[a.actions[i]] = false;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
} else {
|
|
64
|
-
for (let i = 0; i < a.actions.length; i++) {
|
|
65
|
-
obj[a.actions[i]] = true;
|
|
50
|
+
let obj = {}
|
|
51
|
+
if (a.routes.indexOf(route) > -1) {
|
|
52
|
+
for (let i = 0; i < a.actions.length; i++) {
|
|
53
|
+
if (params.hasOwnProperty(route)) {
|
|
54
|
+
if (params[route].indexOf(a.actions[i]) > -1) {
|
|
55
|
+
obj[a.actions[i]] = true
|
|
56
|
+
} else {
|
|
57
|
+
obj[a.actions[i]] = false
|
|
66
58
|
}
|
|
59
|
+
} else {
|
|
60
|
+
obj[a.actions[i]] = false
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
for (let i = 0; i < a.actions.length; i++) {
|
|
65
|
+
obj[a.actions[i]] = true
|
|
67
66
|
}
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
}
|
|
68
|
+
return obj
|
|
69
|
+
}
|
|
70
70
|
|
|
71
71
|
a.myLevel = (req, res, table) => {
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
const levels = a.levels(table, a.routes.includes(table) ? a.rules(res.locals.roleId) : {})
|
|
73
|
+
return levels
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
a.menuAccess = (res, menu) => {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
77
|
+
if (Array.isArray(menu)) {
|
|
78
|
+
let isTrue = false
|
|
79
|
+
for (let i = 0; i < menu.length; i++) {
|
|
80
|
+
let r = a.menuAccess(res, menu[i])
|
|
81
|
+
if (r == true) {
|
|
82
|
+
return true
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
if (Util.in_array(menu, a.routes)) {
|
|
87
|
+
let params = a.params(res.locals.roleId)
|
|
88
|
+
let arr = Object.keys(params) || []
|
|
89
|
+
if (Util.in_array(menu, arr)) {
|
|
90
|
+
return true
|
|
91
|
+
} else {
|
|
92
|
+
return false
|
|
93
|
+
}
|
|
85
94
|
} else {
|
|
86
|
-
|
|
87
|
-
let params = a.params(res.locals.roleId);
|
|
88
|
-
let arr = Object.keys(params) || [];
|
|
89
|
-
if(Util.in_array(menu, arr)){
|
|
90
|
-
return true;
|
|
91
|
-
} else {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
} else {
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
95
|
+
return true
|
|
97
96
|
}
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
}
|
|
98
|
+
return false
|
|
99
|
+
}
|
|
100
100
|
|
|
101
101
|
a.isAccess = (roleId, route, action) => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
if(a.actions.includes(action)){
|
|
108
|
-
if(params[route].includes(action)){
|
|
109
|
-
return true;
|
|
110
|
-
} else {
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
} else {
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
return false;
|
|
102
|
+
let params = a.params(roleId)
|
|
103
|
+
if (a.routes.includes(route)) {
|
|
104
|
+
if (!params[route]) {
|
|
105
|
+
return false
|
|
117
106
|
}
|
|
118
|
-
|
|
119
|
-
|
|
107
|
+
if (a.actions.includes(action)) {
|
|
108
|
+
if (params[route].includes(action)) {
|
|
109
|
+
return true
|
|
110
|
+
} else {
|
|
111
|
+
return false
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
return true
|
|
115
|
+
}
|
|
116
|
+
return false
|
|
117
|
+
}
|
|
118
|
+
return true
|
|
119
|
+
}
|
|
120
120
|
|
|
121
121
|
//get access page after login
|
|
122
122
|
a.access = (req, res, next) => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
123
|
+
if (req.session.user === null || typeof req.session.user === 'undefined') {
|
|
124
|
+
req.session.sessionFlash = Util.flashError(LANGUAGE.login_first)
|
|
125
|
+
res.redirect(`${process.env.APP_AFTER_LOGOUT}`)
|
|
126
|
+
} else {
|
|
127
|
+
const isAccess = a.isAccess(res.locals.roleId, req.route, req.action)
|
|
128
|
+
if (isAccess) {
|
|
129
|
+
next()
|
|
126
130
|
} else {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
131
|
+
if (a.isAccess(res.locals.roleId, 'zrole', 'index')) {
|
|
132
|
+
req.session.sessionFlash = Util.flashError(LANGUAGE.no_access)
|
|
133
|
+
res.redirect(`${process.env.APP_AFTER_LOGIN}?setup=role`)
|
|
134
|
+
} else {
|
|
135
|
+
res.redirect(`${process.env.APP_AFTER_LOGIN}`)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
a.menuValidasi = (res, me, obj) => {
|
|
142
|
+
let jsonArray = []
|
|
143
|
+
obj.children = a.childrenMenu(res, me.children)
|
|
144
|
+
if (obj.children.length) {
|
|
145
|
+
//console.log('children : ',obj.children);
|
|
146
|
+
let count = obj.children.length
|
|
147
|
+
for (let i = 0; i < count; i++) {
|
|
148
|
+
if (obj.children[i].hasOwnProperty('children')) {
|
|
149
|
+
if (obj.children[i].hasOwnProperty('children') > 0) {
|
|
130
150
|
} else {
|
|
131
|
-
|
|
132
|
-
req.session.sessionFlash = Util.flashError(LANGUAGE.no_access);
|
|
133
|
-
res.redirect(`${process.env.APP_AFTER_LOGIN}?setup=role`)
|
|
134
|
-
} else {
|
|
135
|
-
res.redirect(`${process.env.APP_AFTER_LOGIN}`)
|
|
136
|
-
}
|
|
151
|
+
jsonArray.push(obj.children[i])
|
|
137
152
|
}
|
|
153
|
+
} else {
|
|
154
|
+
jsonArray.push(obj.children[i])
|
|
155
|
+
}
|
|
138
156
|
}
|
|
139
|
-
}
|
|
157
|
+
}
|
|
158
|
+
return jsonArray
|
|
159
|
+
}
|
|
140
160
|
|
|
141
161
|
a.menu = (req, res) => {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
return jsonArray;
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
a.menuSystems = (req,res) => {
|
|
169
|
-
let arr = [];
|
|
170
|
-
let menus = myCache.get("MENU_SYSTEMS");
|
|
171
|
-
let children = [];
|
|
172
|
-
menus["users"].forEach(function (item) {
|
|
173
|
-
if (a.menuAccess(res, item.href)) {
|
|
174
|
-
children.push(item);
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
if(children.length == 0) {
|
|
178
|
-
delete menus.users;
|
|
162
|
+
let jsonArray = []
|
|
163
|
+
if (!res.locals.isLogin) return jsonArray
|
|
164
|
+
let companyId = res.locals.companyId
|
|
165
|
+
let userId = res.locals.userId
|
|
166
|
+
let jsonMenu = myCache.get('MENU')
|
|
167
|
+
let arr = []
|
|
168
|
+
if (jsonMenu && jsonMenu.hasOwnProperty(companyId)) {
|
|
169
|
+
arr = jsonMenu[companyId] || []
|
|
170
|
+
}
|
|
171
|
+
//console.log(JSON.stringify(arr))
|
|
172
|
+
let count = arr.length
|
|
173
|
+
for (let i = 0; i < count; i++) {
|
|
174
|
+
let me = arr[i]
|
|
175
|
+
let obj = a.addItemMenu(me)
|
|
176
|
+
if (me.hasOwnProperty('children')) {
|
|
177
|
+
let menuchild = a.menuValidasi(res, me, obj)
|
|
178
|
+
if (menuchild.length) {
|
|
179
|
+
me.children = menuchild
|
|
180
|
+
jsonArray.push(me)
|
|
181
|
+
}
|
|
179
182
|
} else {
|
|
180
|
-
|
|
181
|
-
text: "LANGUAGE.users",
|
|
182
|
-
icon: "tabler-users-plus",
|
|
183
|
-
children: children
|
|
184
|
-
};
|
|
185
|
-
arr.push(userManagament);
|
|
183
|
+
jsonArray.push(obj)
|
|
186
184
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
185
|
+
}
|
|
186
|
+
/* arr.map((me) => {
|
|
187
|
+
if (a.menuAccess(res, me.href)) {
|
|
188
|
+
let obj = a.addItemMenu(me);
|
|
189
|
+
if (me.hasOwnProperty("children")) {
|
|
190
|
+
obj.children = a.childrenMenu(res, me.children);
|
|
191
|
+
if(obj.children.length) {
|
|
192
|
+
jsonArray.push(obj);
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
jsonArray.push(obj);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
});*/
|
|
199
|
+
|
|
200
|
+
//console.log(JSON.stringify(jsonArray))
|
|
201
|
+
|
|
202
|
+
return jsonArray
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
a.menuSystems = (req, res) => {
|
|
206
|
+
let arr = []
|
|
207
|
+
let menus = myCache.get('MENU_SYSTEMS')
|
|
208
|
+
let children = []
|
|
209
|
+
menus['users'].forEach(function (item) {
|
|
210
|
+
if (a.menuAccess(res, item.href)) {
|
|
211
|
+
children.push(item)
|
|
202
212
|
}
|
|
203
|
-
|
|
204
|
-
|
|
213
|
+
})
|
|
214
|
+
if (children.length == 0) {
|
|
215
|
+
delete menus.users
|
|
216
|
+
} else {
|
|
217
|
+
let userManagament = {
|
|
218
|
+
text: 'LANGUAGE.users',
|
|
219
|
+
icon: 'tabler-users-plus',
|
|
220
|
+
children: children,
|
|
221
|
+
}
|
|
222
|
+
arr.push(userManagament)
|
|
223
|
+
}
|
|
224
|
+
children = []
|
|
225
|
+
menus['systems'].forEach(function (item) {
|
|
226
|
+
if (a.menuAccess(res, item.href)) {
|
|
227
|
+
children.push(item)
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
if (children.length == 0) {
|
|
231
|
+
delete menus.systems
|
|
232
|
+
} else {
|
|
233
|
+
let settings = {
|
|
234
|
+
text: 'LANGUAGE.systems',
|
|
235
|
+
icon: 'tabler-settings-check',
|
|
236
|
+
children: children,
|
|
237
|
+
}
|
|
238
|
+
arr.push(settings)
|
|
239
|
+
}
|
|
240
|
+
return arr
|
|
241
|
+
}
|
|
205
242
|
|
|
206
243
|
a.menus = (req, res) => {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
244
|
+
let arr = a.menu(req, res)
|
|
245
|
+
let menus = [...arr, ...a.menuSystems(req, res)]
|
|
246
|
+
//console.log(JSON.stringify(menus));
|
|
247
|
+
return menus
|
|
248
|
+
}
|
|
210
249
|
|
|
211
250
|
a.addItemMenu = (obj) => {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
251
|
+
let newObj = {}
|
|
252
|
+
for (const key in obj) {
|
|
253
|
+
if (key != 'children') {
|
|
254
|
+
newObj[key] = obj[key]
|
|
217
255
|
}
|
|
218
|
-
|
|
219
|
-
|
|
256
|
+
}
|
|
257
|
+
return newObj
|
|
258
|
+
}
|
|
220
259
|
|
|
221
260
|
a.childrenMenu = (res, arr) => {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
261
|
+
let myArr = []
|
|
262
|
+
let nArr = arr.reduce((result, item) => [...result, item.href], [])
|
|
263
|
+
let isAccess = a.menuAccess(res, nArr)
|
|
264
|
+
if (isAccess) {
|
|
265
|
+
//stupid way
|
|
266
|
+
arr.map((item) => {
|
|
267
|
+
let obj = {}
|
|
268
|
+
if (a.isAccess(res.locals.roleId, item.href, 'index')) {
|
|
269
|
+
obj = a.addItemMenu(item)
|
|
270
|
+
if (item.hasOwnProperty('children') && item.children.length) {
|
|
271
|
+
let child = []
|
|
272
|
+
child.push(a.childrenMenu(res, item.children))
|
|
273
|
+
if (child[0]) {
|
|
274
|
+
obj.children = child[0]
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
myArr.push(obj)
|
|
278
|
+
}
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
return myArr
|
|
282
|
+
}
|
|
244
283
|
|
|
245
|
-
module.exports = a
|
|
284
|
+
module.exports = a
|
package/lib/zRoute.js
CHANGED
|
@@ -3482,10 +3482,11 @@ zRoute.tabAccess = async (req, res) => {
|
|
|
3482
3482
|
let table = req.body.table
|
|
3483
3483
|
let roles = myCache.get('ROLES')
|
|
3484
3484
|
let roleId = res.locals.roleId
|
|
3485
|
-
let tabs = roles[roleId].tabs[table]
|
|
3485
|
+
let tabs = roles[roleId] && roles[roleId].tabs ? roles[roleId].tabs[table] : []
|
|
3486
3486
|
res.json(tabs)
|
|
3487
3487
|
} catch (e) {
|
|
3488
3488
|
console.log(e)
|
|
3489
|
+
io.to(res.locals.token).emit('error', e + '')
|
|
3489
3490
|
}
|
|
3490
3491
|
}
|
|
3491
3492
|
|