zet-lib 1.0.54 → 1.0.56

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 (2) hide show
  1. package/lib/zRoleRouter.js +281 -144
  2. package/package.json +1 -1
@@ -1,160 +1,265 @@
1
- const express = require('express');
2
- const router = express.Router();
1
+ const express = require('express')
2
+ const router = express.Router()
3
3
  // setup route middlewares
4
- const csrf = require('csurf');
5
- const bodyParser = require('body-parser');
6
- const path = require('path');
7
- const parseForm = bodyParser.urlencoded({extended: true});
8
- const csrfProtection = csrf({cookie: true});
9
- const pm2 = require('pm2');
10
- const env = process.env.NODE_ENV || 'development';
11
- const ejs = require('ejs');
12
- const Util = require('./Util');
13
- const access = require('./access');
14
- const connection = require('./connection');
15
- const zCache = require('./zCache');
16
- const zRole = require('./zRole');
17
- const moduleLib = require('./moduleLib');
4
+ const csrf = require('csurf')
5
+ const bodyParser = require('body-parser')
6
+ const path = require('path')
7
+ const parseForm = bodyParser.urlencoded({ extended: true })
8
+ const csrfProtection = csrf({ cookie: true })
9
+ const pm2 = require('pm2')
10
+ const env = process.env.NODE_ENV || 'development'
11
+ const ejs = require('ejs')
12
+ const Util = require('./Util')
13
+ const access = require('./access')
14
+ const connection = require('./connection')
15
+ const zCache = require('./zCache')
16
+ const zRole = require('./zRole')
17
+ const moduleLib = require('./moduleLib')
18
18
 
19
19
  router.get('/', csrfProtection, async function (req, res, next) {
20
- let dirname = path.resolve(__dirname);
21
- let id = req.query.id;
20
+ let dirname = path.resolve(__dirname)
21
+ let id = req.query.id
22
22
  if (id == undefined) {
23
- id = 1;
23
+ id = 1
24
24
  }
25
25
  const model = await connection.results({
26
- table : "zrole",
27
- where : {
28
- id : id
26
+ table: 'zrole',
27
+ where: {
28
+ id: id,
29
+ },
30
+ })
31
+ //find all table has tabs
32
+ let zfields = await connection.results({ table: 'zfields' })
33
+ let tabs = []
34
+ zfields.map((item) => {
35
+ let itemTabs = item.tabs || []
36
+ if (itemTabs.length) {
37
+ tabs.push(item.table)
29
38
  }
30
- });
31
- const json = model[0].params ;
32
- const routes = zRole.routes;
33
- const results = await connection.results({table:"zrole"});
34
- const myLevel = zRole.myLevel(req, res, 'zrole');
39
+ })
40
+ const json = model[0].params
41
+ const routes = zRole.routes
42
+ const results = await connection.results({ table: 'zrole' })
43
+ const myLevel = zRole.myLevel(req, res, 'zrole')
35
44
  //inject to end body
36
45
  let datas = {
37
46
  model: model,
38
- table: "zrole",
47
+ tabs: tabs,
48
+ table: 'zrole',
39
49
  id: id,
40
50
  actions: zRole.actions,
41
51
  routes: routes,
42
- levels:myLevel,
52
+ levels: myLevel,
43
53
  json: json,
44
54
  results: results,
45
- csrfToken: req.csrfToken()
55
+ csrfToken: req.csrfToken(),
46
56
  }
47
- const bodyHTML = ejs.render(body,datas);
48
- const endBody = ejs.render(js, datas);
49
- datas.bodyHTML = bodyHTML;
50
- moduleLib.addModule(req,res,endBody);
51
- res.render("layouts/"+layout, datas);
52
- });
57
+ const bodyHTML = ejs.render(body, datas)
58
+ const endBody = ejs.render(js, datas)
59
+ datas.bodyHTML = bodyHTML
60
+ moduleLib.addModule(req, res, endBody)
61
+ res.render('layouts/' + layout, datas)
62
+ })
53
63
 
54
- router.post('/update/:id',async function (req, res, next) {
55
- const data = {};
56
- const name = req.body.name;
57
- const params = req.body.params;
58
- const newKey = {};
59
- Object.keys(params).map( (key) => {
60
- const arr = [];
64
+ router.post('/update/:id', async function (req, res, next) {
65
+ const data = {}
66
+ const name = req.body.name
67
+ const params = req.body.params
68
+ const newKey = {}
69
+ Object.keys(params).map((key) => {
70
+ const arr = []
61
71
  for (const k in params[key]) {
62
- arr.push(k);
72
+ arr.push(k)
63
73
  }
64
- newKey[key] = arr;
65
- });
74
+ newKey[key] = arr
75
+ })
66
76
 
67
- const json = {};
68
- json.params = JSON.stringify(newKey);
77
+ const json = {}
78
+ json.params = JSON.stringify(newKey)
69
79
  try {
70
80
  await connection.update({
71
- table : "zrole",
72
- data : {
73
- params : json.params
81
+ table: 'zrole',
82
+ data: {
83
+ params: json.params,
74
84
  },
75
- where : {
76
- id : req.params.id
77
- }
78
- });
79
- data.status = 1;
80
- data.data = 1;
81
- await zCache.ROLES();
85
+ where: {
86
+ id: req.params.id,
87
+ },
88
+ })
89
+ data.status = 1
90
+ data.data = 1
91
+ await zCache.ROLES()
82
92
 
83
- if(env == "production") {
93
+ if (env == 'production') {
84
94
  pm2.connect(function (err) {
85
95
  if (err) {
86
96
  //console.log(err.toString());
87
97
  }
88
98
  pm2.restart(process.env.PM2_NAME, (err, proc) => {
89
99
  //io.to(room).emit("message","Restart done")
90
- });
91
- });
100
+ })
101
+ })
92
102
  }
93
- res.json(data);
103
+ res.json(data)
94
104
  } catch (error) {
95
- data.status = 0;
96
- data.data = error;
97
- res.json(data);
105
+ data.status = 0
106
+ data.data = error
107
+ res.json(data)
98
108
  }
99
- });
109
+ })
100
110
 
101
- router.post('/rename/:id', async (req,res) => {
102
- let json = Util.jsonSuccess();
111
+ router.post('/rename/:id', async (req, res) => {
112
+ let json = Util.jsonSuccess()
103
113
  try {
104
- const id = req.params.id;
105
- const rename = req.body.rename;
114
+ const id = req.params.id
115
+ const rename = req.body.rename
106
116
  await connection.update({
107
- table : "zrole",
108
- where : {
109
- id : id
117
+ table: 'zrole',
118
+ where: {
119
+ id: id,
110
120
  },
111
- data : {name : rename}
121
+ data: { name: rename },
112
122
  })
113
123
  } catch (e) {
114
124
  json = Util.flashError(e.toString())
115
125
  }
116
- res.json(json);
117
- });
126
+ res.json(json)
127
+ })
118
128
 
119
- router.post('/create', async (req,res) => {
120
- let json = Util.jsonSuccess();
129
+ router.post('/create', async (req, res) => {
130
+ let json = Util.jsonSuccess()
121
131
  try {
122
- const name = req.body.name;
132
+ const name = req.body.name
123
133
  await connection.insert({
124
- table : "zrole",
125
- data : {
126
- name : name
127
- }
128
- });
129
- await zCache.ROLES();
134
+ table: 'zrole',
135
+ data: {
136
+ name: name,
137
+ },
138
+ })
139
+ await zCache.ROLES()
130
140
  } catch (e) {
131
141
  json = Util.flashError(e.toString())
132
142
  }
133
- res.json(json);
134
- });
143
+ res.json(json)
144
+ })
135
145
 
136
- router.delete('/delete/:id', async (req,res) => {
137
- let json = Util.jsonSuccess();
138
- let id = parseInt(req.params.id);
139
- if(id > 3){
146
+ router.post('/tab-access', async (req, res) => {
147
+ let json = Util.jsonSuccess()
148
+ let html = ''
149
+ const checkedFn = (obj, name, index) => {
150
+ return obj.hasOwnProperty(name) && obj[name][index] ? 'checked' : ''
151
+ }
152
+ try {
153
+ const table = req.body.table
154
+ const id = req.body.id
155
+ let tabRole = await connection.result({
156
+ table: 'zrole',
157
+ where: {
158
+ id: id,
159
+ },
160
+ })
161
+ let mytabs = tabRole.tabs || {}
162
+ let mytabsRole = mytabs[table] || {}
163
+ let result = await connection.result({
164
+ table: 'zfields',
165
+ where: {
166
+ table: table,
167
+ },
168
+ })
169
+ html += `<form id="tabform">`
170
+ html += `<table class="table table-hover"><thead><tr><th>Tab</th><th>View <input type="checkbox" onclick="tabChecks(this,'viewtab')" id="viewtabs" /></th><th>Create <input type="checkbox" id="createtabs" onclick="tabChecks(this,'createtab')" /></th><th>Edit <input type="checkbox" id="edittabs" onclick="tabChecks(this,'edittab')" /></th><th>Delete <input type="checkbox" onclick="tabChecks(this,'deletetab')" id="deletetabs" /></th></tr></thead><tbody></tbody>`
171
+ result.tabs.map((item, index) => {
172
+ html += `<tr>
173
+ <td>${item}</td>
174
+ <td><input type="checkbox" class="viewtab" name="view___${index}" ${checkedFn(mytabsRole, 'view', index)} /></td>
175
+ <td><input type="checkbox" class="createtab" name="create___${index}" ${checkedFn(mytabsRole, 'create', index)} /></td>
176
+ <td><input type="checkbox" class="edittab" name="edit___${index}" ${checkedFn(mytabsRole, 'edit', index)} /></td>
177
+ <td><input type="checkbox" class="deletetab" name="delete___${index}" ${checkedFn(mytabsRole, 'delete', index)} /></td>
178
+ </tr>`
179
+ })
180
+ html += `</tbody></table>`
181
+ html += `</form>`
182
+ } catch (e) {
183
+ json = Util.flashError(e.toString())
184
+ }
185
+ res.send(html)
186
+ })
187
+
188
+ router.post('/post-access', async (req, res) => {
189
+ let json = Util.jsonSuccess()
190
+ let table = req.body.table || ''
191
+ if (table == '') {
192
+ res.json(Util.flashError('Table is empty!'))
193
+ return false
194
+ }
195
+ let tabs = req.body.tabs || []
196
+ let id = req.body.id
197
+ let obj = tabs.reduce((acc, item) => {
198
+ acc[item.name] = item.value
199
+ return acc
200
+ }, {})
201
+ let mytabs = {}
202
+ let l = req.body.l
203
+ let roles = {}
204
+ let arr = ['view', 'create', 'edit', 'delete']
205
+ for (var i = 0; i < l; i++) {
206
+ arr.map((item) => {
207
+ if (!roles.hasOwnProperty(item)) {
208
+ roles[item] = []
209
+ }
210
+ if (obj.hasOwnProperty(`${item}___${i}`)) {
211
+ roles[item].push(true)
212
+ } else {
213
+ roles[item].push(false)
214
+ }
215
+ })
216
+ }
217
+
218
+ try {
219
+ let tabRole = await connection.result({
220
+ table: 'zrole',
221
+ where: {
222
+ id: id,
223
+ },
224
+ })
225
+ mytabs = tabRole.tabs || {}
226
+ mytabs[table] = roles
227
+ await connection.update({
228
+ table: 'zrole',
229
+ data: {
230
+ tabs: JSON.stringify(mytabs),
231
+ },
232
+ where: {
233
+ id: id,
234
+ },
235
+ })
236
+ } catch (err) {
237
+ json = Util.jsonError(err + '')
238
+ }
239
+ res.json(json)
240
+ })
241
+
242
+ router.delete('/delete/:id', async (req, res) => {
243
+ let json = Util.jsonSuccess()
244
+ let id = parseInt(req.params.id)
245
+ if (id > 3) {
140
246
  try {
141
- const name = req.body.name;
247
+ const name = req.body.name
142
248
  await connection.delete({
143
- table : "zrole",
144
- where : {
145
- id : id
146
- }
147
- });
148
- await zCache.ROLES();
249
+ table: 'zrole',
250
+ where: {
251
+ id: id,
252
+ },
253
+ })
254
+ await zCache.ROLES()
149
255
  } catch (e) {
150
256
  json = Util.flashError(e.toString())
151
257
  }
152
258
  } else {
153
- json = Util.flashError('Delete error, not allowed');
259
+ json = Util.flashError('Delete error, not allowed')
154
260
  }
155
- res.json(json);
156
- });
157
-
261
+ res.json(json)
262
+ })
158
263
 
159
264
  const body = `<div class="">
160
265
  <div class="page-header"><h1>Roles</h1></div>
@@ -163,23 +268,14 @@ const body = `<div class="">
163
268
  <div class="float-end">
164
269
  <div class="summary">
165
270
  <% if(levels.delete) {%>
166
- <span
167
- class="icon-small icons-danger" title="Delete role" onclick="deleterole()"><img class="icons-bg-white icon-image"
168
- src="/assets/icons/trash.svg"></span>
271
+ <span class="icon-small icons-danger" title="Delete role" onclick="deleterole()"><img class="icons-bg-white icon-image" src="/assets/icons/trash.svg"></span>
272
+ <%}%>
273
+ <% if(levels.update) {%>
274
+ <span class="icon-small icons-primary" data-bs-toggle="modal" data-bs-target="#renameModal" title="rename"><img class="icons-bg-white icon-image" src="/assets/icons/edit.svg"></span>
275
+ <%}%>
276
+ <% if(levels.create) {%>
277
+ <span class="icon-small icons-success" title="Add a new role" data-bs-toggle="modal" data-bs-target="#addModal"><img class="icons-bg-white icon-image" src="/assets/icons/plus.svg"></span>
169
278
  <%}%>
170
- <% if(levels.update) {%>
171
- <span class="icon-small icons-primary" data-bs-toggle="modal"
172
- data-bs-target="#renameModal" title="rename"><img
173
- class="icons-bg-white icon-image" src="/assets/icons/edit.svg"></span>
174
- <%}%>
175
- <% if(levels.create) {%>
176
- <span
177
- class="icon-small icons-success" title="Add a new role" data-bs-toggle="modal"
178
- data-bs-target="#addModal"><img class="icons-bg-white icon-image"
179
- src="/assets/icons/plus.svg"></span>
180
- <%}%>
181
-
182
-
183
279
  </div>
184
280
  </div>
185
281
  <h3 class="panel-title"><i class="fa fa-cog"></i> Settings</h3>
@@ -187,17 +283,13 @@ const body = `<div class="">
187
283
  </div>
188
284
  <div class="kv-panel-before">
189
285
  <div class="row">
190
- <form id="role-form" class="form-horizontal kv-form-horizontal" method="post"><input type="hidden"
191
- name="_csrf"
192
- value="<%- csrfToken %>">
193
- <div class="form-group field-role-role_name"><label class="control-label col-md-2"
194
- for="role-role_name">Role Name</label>
286
+ <form id="role-form" class="form-horizontal kv-form-horizontal" method="post">
287
+ <input type="hidden" name="_csrf" value="<%- csrfToken %>">
288
+ <div class="form-group field-role-role_name">
289
+ <label class="control-label col-md-2" for="role-role_name">Role Name</label>
195
290
  <div class="col-md-10"><select id="roleName" class="form-control form-select mb-3" name="name">
196
291
  <% for(var i = 0;i < results.length;i++){ %>
197
- <option value="<%- results[i].id %>"
198
- <% if(id == results[i].id){ %> selected=""
199
- <% } %>
200
- ><%- results[i].name %></option>
292
+ <option value="<%- results[i].id %>" <% if(id == results[i].id){ %> selected="" <% } %> ><%- results[i].name %></option>
201
293
  <% } %>
202
294
  </select></div>
203
295
  </div>
@@ -206,8 +298,7 @@ const body = `<div class="">
206
298
  <tr>
207
299
  <th>Name</th>
208
300
  <% for(var i = 0;i < actions.length;i++) { %>
209
- <th><%= actions[i] %> <input onclick='checkthis("<%= actions[i] %>")' type="checkbox"
210
- id="all<%= actions[i] %>"></th>
301
+ <th><%= actions[i] %> <input onclick='checkthis("<%= actions[i] %>")' type="checkbox" id="all<%= actions[i] %>"></th>
211
302
  <% } %>
212
303
  </tr>
213
304
  </thead>
@@ -215,13 +306,14 @@ const body = `<div class="">
215
306
  <% for(var i = 0;i < routes.length;i++) { %>
216
307
  <tr>
217
308
  <td>
218
- <% var ename = routes[i] %>
219
- <%= ename %></td>
309
+ <% var ename = routes[i];
310
+ var titleName = tabs.includes(routes[i]) ? '<a class="handle-access" data-bs-toggle="modal" data-bs-target="#tabaccess" href="#">'+routes[i]+'</a>' : routes[i] %>
311
+ <%- titleName %>
312
+ </td>
220
313
  <% for(var x = 0;x < actions.length;x++) { %>
221
314
  <td><input name="params[<%= ename %>][<%= actions[x] %>]" class="<%= actions[x] %>"
222
- <% if(json && json.hasOwnProperty(ename) && json[ename].indexOf(actions[x]) >= 0) { %> <%= 'checked="checked"' %>
223
- <% } %>
224
- title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox"></td>
315
+ <% if(json && json.hasOwnProperty(ename) && json[ename].indexOf(actions[x]) >= 0) { %> <%= 'checked="checked"' %> <% } %> title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox">
316
+ </td>
225
317
  <% } %>
226
318
  </tr>
227
319
  <% } %>
@@ -245,8 +337,9 @@ const body = `<div class="">
245
337
  <div class="modal-header"><h5 class="modal-title" id="exampleModalLabel">Rename title</h5>
246
338
  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
247
339
  </div>
248
- <div class="modal-body"><input type="text" class="form-control" id="rename" name="rename"
249
- value="<%- model[0].name %>"></div>
340
+ <div class="modal-body">
341
+ <input type="text" class="form-control" id="rename" name="rename" value="<%- model[0].name %>">
342
+ </div>
250
343
  <div class="modal-footer">
251
344
  <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
252
345
  <button type="button" class="btn btn-primary btn-update">Save changes</button>
@@ -261,11 +354,10 @@ const body = `<div class="">
261
354
  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
262
355
  </div>
263
356
  <div class="modal-body">
264
- <div class="form-group mb-3"><label for="ruas_sk">Role Name</label> <input type="text"
265
- class="form-control"
266
- id="role_name"
267
- name="role_name"
268
- placeholder="Role Name">
357
+ <div class="form-group mb-3">
358
+ <label for="ruas_sk">Role Name</label>
359
+ <input type="text" class="form-control" id="role_name" name="role_name" placeholder="Role Name">
360
+
269
361
  </div>
270
362
  </div>
271
363
  <div class="modal-footer">
@@ -275,9 +367,25 @@ const body = `<div class="">
275
367
  </div>
276
368
  </div>
277
369
  </div>
370
+ <div class="modal fade" id="tabaccess" tabindex="-1" aria-labelledby="tabaccessModalLabel" aria-hidden="true">
371
+ <div class="modal-dialog">
372
+ <div class="modal-content">
373
+ <div class="modal-header"><h5 class="modal-title" id="tabaccesstitle">Tab Access <span id="titleaccess"></span></h5>
374
+ <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
375
+ </div>
376
+ <div class="modal-body body-access">
377
+
378
+ </div>
379
+ <div class="modal-footer">
380
+ <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
381
+ <button type="button" class="btn btn-primary btn-save-tab">Save changes</button>
382
+ </div>
383
+ </div>
384
+ </div>
385
+ </div>
278
386
 
279
387
 
280
- `;
388
+ `
281
389
  const js = `<script>
282
390
  function checkthis(elm) {
283
391
  var cElem = $("#all" + elm);
@@ -287,6 +395,13 @@ const js = `<script>
287
395
  $("input." + elm).prop("checked", false);
288
396
  }
289
397
  }
398
+ function tabChecks(that,elm) {
399
+ if ($(that).is(":checked")) {
400
+ $("input." + elm).prop("checked", true);
401
+ } else {
402
+ $("input." + elm).prop("checked", false);
403
+ }
404
+ }
290
405
  $("#roleName").on('change', function () {
291
406
  location.href = "/zrole?id=" + $(this).val();
292
407
  });
@@ -334,7 +449,29 @@ const js = `<script>
334
449
  })
335
450
  }
336
451
  }
452
+
453
+ function handleAccess(table) {
454
+ $(".btn-save-tab").attr("data-table", table);
455
+ ajaxPost('/zrole/tab-access',{table:table, id: "<%- id %>"}, ((data) => {
456
+ $(".body-access").html(data);
457
+ }))
458
+ }
459
+ $(".handle-access").on("click", (e)=> handleAccess(e.target.text));
460
+ $(".btn-save-tab").on("click",(e) => {
461
+ let queryform = $("#tabform").serializeArray();
462
+ ajaxPost('/zrole/post-access', {
463
+ tabs: queryform,
464
+ l : $(".viewtab").length,
465
+ id: "<%- id %>",
466
+ table : e.target.getAttribute('data-table')
467
+ },((data) => {
468
+ toastrForm(data);
469
+ if(data.status == 1) {
470
+ setTimeout(() => {location.href= '';},2000);
471
+ }
472
+ }))
473
+ })
337
474
  </script>
338
- `;
475
+ `
339
476
 
340
- module.exports = router;
477
+ module.exports = router
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.0.54",
3
+ "version": "1.0.56",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"