zet-lib 1.2.15 → 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.
Files changed (3) hide show
  1. package/lib/zRole.js +228 -189
  2. package/lib/zRoute.js +1 -1
  3. package/package.json +1 -1
package/lib/zRole.js CHANGED
@@ -1,245 +1,284 @@
1
- const Util = require('./Util');
2
- const fs = require("fs");
3
- const myCache = require("./cache");
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("ROUTES");
14
- const cacheRoles = myCache.get("ROLES") || {};
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
- a.routes = process.env.NODE_ENV == "production" ? Util.arrayDeletes(cacheRoutes,["auth","test"]) : Util.arrayDeletes(cacheRoutes,["generator","auth","test"]);
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
- a.routes = process.env.NODE_ENV == "production" ? Util.arrayDeletes(routes,["auth","test"]) : Util.arrayDeletes(routes,["generator","auth","test"]);
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 = function (roleId) {
33
- let cacheRoles = myCache.get("ROLES");
34
- if(cacheRoles && cacheRoles.hasOwnProperty(roleId)) {
35
- return roleId ? cacheRoles[roleId].params : {};
36
- }
37
- return {}
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
- return a.params(roleId);
42
- };
41
+ return a.params(roleId)
42
+ }
43
43
 
44
- a.list = (roleId, route) => {
45
- let params = a.params(roleId);
46
- return a.levels(route, params)
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
- 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;
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
- return obj;
69
- };
67
+ }
68
+ return obj
69
+ }
70
70
 
71
71
  a.myLevel = (req, res, table) => {
72
- const levels = a.levels(table, a.routes.includes(table) ? a.rules(res.locals.roleId) : {});
73
- return levels;
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
- 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
- }
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
- 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
- }
94
- } else {
95
- return true;
96
- }
95
+ return true
97
96
  }
98
- return false;
99
- };
97
+ }
98
+ return false
99
+ }
100
100
 
101
101
  a.isAccess = (roleId, route, action) => {
102
- let params = a.params(roleId);
103
- if(a.routes.includes(route)) {
104
- if(!params[route]){
105
- return false;
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
- return true;
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
- 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}`);
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
- const isAccess = a.isAccess(res.locals.roleId, req.route, req.action);
128
- if (isAccess) {
129
- next();
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
- 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
- }
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
- let jsonArray = [];
143
- if (!res.locals.isLogin) return jsonArray;
144
- let companyId = res.locals.companyId;
145
- let userId = res.locals.userId;
146
- let jsonMenu = myCache.get("MENU");
147
- let arr = [];
148
- if (jsonMenu && jsonMenu.hasOwnProperty(companyId)) {
149
- arr = jsonMenu[companyId] || [];
150
- }
151
- arr.map((me) => {
152
- if (a.menuAccess(res, me.href)) {
153
- let obj = a.addItemMenu(me);
154
- if (me.hasOwnProperty("children")) {
155
- obj.children = a.childrenMenu(res, me.children);
156
- if(obj.children.length) {
157
- jsonArray.push(obj);
158
- }
159
- } else {
160
- jsonArray.push(obj);
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
- let userManagament = {
181
- text: "LANGUAGE.users",
182
- icon: "tabler-users-plus",
183
- children: children
184
- };
185
- arr.push(userManagament);
183
+ jsonArray.push(obj)
186
184
  }
187
- children = [];
188
- menus["systems"].forEach(function (item) {
189
- if (a.menuAccess(res, item.href)) {
190
- children.push(item);
191
- }
192
- });
193
- if(children.length == 0) {
194
- delete menus.systems;
195
- } else {
196
- let settings = {
197
- text: "LANGUAGE.systems",
198
- icon: "tabler-settings-check",
199
- children: children
200
- };
201
- arr.push(settings);
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
- return arr;
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
- let arr = a.menu(req, res);
208
- return [...arr, ...a.menuSystems(req, res)];
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
- let newObj = {};
213
- for (const key in obj) {
214
- if (key != "children") {
215
- newObj[key] = obj[key];
216
- }
251
+ let newObj = {}
252
+ for (const key in obj) {
253
+ if (key != 'children') {
254
+ newObj[key] = obj[key]
217
255
  }
218
- return newObj;
219
- };
256
+ }
257
+ return newObj
258
+ }
220
259
 
221
260
  a.childrenMenu = (res, arr) => {
222
- let myArr = [];
223
- let nArr = arr.reduce((result, item) => [...result, item.href],[]);
224
- let isAccess = a.menuAccess(res, nArr);
225
- if (isAccess) {
226
- //stupid way
227
- arr.map((item) => {
228
- let obj = {};
229
- if(a.isAccess(res.locals.roleId,item.href,"index")) {
230
- obj = a.addItemMenu(item);
231
- if (item.hasOwnProperty("children") && item.children.length) {
232
- let child = [];
233
- child.push(a.childrenMenu(res, item.children));
234
- if (child[0]) {
235
- obj.children = child[0];
236
- }
237
- }
238
- myArr.push(obj)
239
- }
240
- });
241
- }
242
- return myArr;
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,7 +3482,7 @@ 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] && roles.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)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.2.15",
3
+ "version": "1.2.16",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"