zet-lib 1.3.38 → 1.3.40
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/LICENSE +21 -21
- package/README.md +15 -15
- package/lib/ErrorWithCode.js +6 -6
- package/lib/Form.js +1019 -1019
- package/lib/Mail.js +68 -68
- package/lib/Modal.js +95 -95
- package/lib/Pool.js +437 -437
- package/lib/UI.js +7 -7
- package/lib/Util.js +1384 -1367
- package/lib/access.js +6 -6
- package/lib/cache.js +3 -3
- package/lib/connection.js +409 -409
- package/lib/debug.js +22 -22
- package/lib/index.js +36 -36
- package/lib/io.js +44 -44
- package/lib/languages/lang_en.js +125 -125
- package/lib/languages/lang_fr.js +125 -125
- package/lib/languages/lang_id.js +126 -126
- package/lib/languages/lang_jp.js +125 -125
- package/lib/moduleLib.js +661 -661
- package/lib/tableForm.js +10 -10
- package/lib/views/generator.ejs +598 -598
- package/lib/views/generator_layout.ejs +224 -224
- package/lib/views/generatorjs.ejs +927 -927
- package/lib/zAppRouter.js +1637 -1637
- package/lib/zCache.js +301 -301
- package/lib/zComponent.js +27 -27
- package/lib/zFn.js +58 -58
- package/lib/zFunction.js +20 -20
- package/lib/zGeneratorRouter.js +1641 -1641
- package/lib/zMenuRouter.js +556 -556
- package/lib/zPage.js +188 -188
- package/lib/zReport.js +982 -982
- package/lib/zRole.js +256 -256
- package/lib/zRoleRouter.js +609 -609
- package/lib/zRoute.js +5025 -5019
- package/lib/zTester.js +93 -93
- package/lib/zapp.js +65 -65
- package/lib/zdataTable.js +330 -330
- package/package.json +56 -56
package/lib/zRoleRouter.js
CHANGED
|
@@ -1,609 +1,609 @@
|
|
|
1
|
-
const express = require('express')
|
|
2
|
-
const router = express.Router()
|
|
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')
|
|
18
|
-
|
|
19
|
-
router.get('/', csrfProtection, async function (req, res, next) {
|
|
20
|
-
let dirname = path.resolve(__dirname)
|
|
21
|
-
let id = req.query.id
|
|
22
|
-
if (id == undefined) {
|
|
23
|
-
id = 1
|
|
24
|
-
}
|
|
25
|
-
const model = await connection.results({
|
|
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 dummies = []
|
|
34
|
-
let tabs = []
|
|
35
|
-
zfields.map((item) => {
|
|
36
|
-
let itemTabs = item.tabs || []
|
|
37
|
-
if (itemTabs.length) {
|
|
38
|
-
tabs.push(item.table)
|
|
39
|
-
}
|
|
40
|
-
if (item.json) {
|
|
41
|
-
if (item.json.dummy == 1) {
|
|
42
|
-
dummies.push(item.table)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
const json = model[0].params
|
|
47
|
-
const routes = zRole.routes.filter((item) => !dummies.includes(item))
|
|
48
|
-
const results = await connection.results({ table: 'zrole' })
|
|
49
|
-
const myLevel = zRole.myLevel(req, res, 'zrole')
|
|
50
|
-
//inject to end body
|
|
51
|
-
let datas = {
|
|
52
|
-
model: model,
|
|
53
|
-
tabs: tabs,
|
|
54
|
-
dummies: dummies,
|
|
55
|
-
table: 'zrole',
|
|
56
|
-
id: id,
|
|
57
|
-
actions: zRole.actions,
|
|
58
|
-
routes: routes,
|
|
59
|
-
levels: myLevel,
|
|
60
|
-
json: json,
|
|
61
|
-
results: results,
|
|
62
|
-
csrfToken: req.csrfToken(),
|
|
63
|
-
}
|
|
64
|
-
const bodyHTML = ejs.render(body, datas)
|
|
65
|
-
const endBody = ejs.render(js, datas)
|
|
66
|
-
datas.bodyHTML = bodyHTML
|
|
67
|
-
moduleLib.addModule(req, res, endBody)
|
|
68
|
-
res.render('layouts/' + layout, datas)
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
router.post('/update/:id', async function (req, res, next) {
|
|
72
|
-
const data = {}
|
|
73
|
-
const name = req.body.name
|
|
74
|
-
const params = req.body.params
|
|
75
|
-
const newKey = {}
|
|
76
|
-
Object.keys(params).map((key) => {
|
|
77
|
-
const arr = []
|
|
78
|
-
for (const k in params[key]) {
|
|
79
|
-
arr.push(k)
|
|
80
|
-
}
|
|
81
|
-
newKey[key] = arr
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
const json = {}
|
|
85
|
-
json.params = JSON.stringify(newKey)
|
|
86
|
-
try {
|
|
87
|
-
await connection.update({
|
|
88
|
-
table: 'zrole',
|
|
89
|
-
data: {
|
|
90
|
-
params: json.params,
|
|
91
|
-
},
|
|
92
|
-
where: {
|
|
93
|
-
id: req.params.id,
|
|
94
|
-
},
|
|
95
|
-
})
|
|
96
|
-
data.status = 1
|
|
97
|
-
data.data = 1
|
|
98
|
-
await zCache.ROLES()
|
|
99
|
-
|
|
100
|
-
if (env == 'production') {
|
|
101
|
-
pm2.connect(function (err) {
|
|
102
|
-
if (err) {
|
|
103
|
-
//console.log(err.toString());
|
|
104
|
-
}
|
|
105
|
-
pm2.restart(process.env.PM2_NAME, (err, proc) => {
|
|
106
|
-
//io.to(room).emit("message","Restart done")
|
|
107
|
-
})
|
|
108
|
-
})
|
|
109
|
-
}
|
|
110
|
-
res.json(data)
|
|
111
|
-
} catch (error) {
|
|
112
|
-
data.status = 0
|
|
113
|
-
data.data = error
|
|
114
|
-
res.json(data)
|
|
115
|
-
}
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
router.post('/rename/:id', async (req, res) => {
|
|
119
|
-
let json = Util.jsonSuccess()
|
|
120
|
-
try {
|
|
121
|
-
const id = req.params.id
|
|
122
|
-
const rename = req.body.rename
|
|
123
|
-
await connection.update({
|
|
124
|
-
table: 'zrole',
|
|
125
|
-
where: {
|
|
126
|
-
id: id,
|
|
127
|
-
},
|
|
128
|
-
data: { name: rename },
|
|
129
|
-
})
|
|
130
|
-
} catch (e) {
|
|
131
|
-
json = Util.flashError(e.toString())
|
|
132
|
-
}
|
|
133
|
-
res.json(json)
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
router.post('/create', async (req, res) => {
|
|
137
|
-
let json = Util.jsonSuccess()
|
|
138
|
-
try {
|
|
139
|
-
const name = req.body.name
|
|
140
|
-
await connection.insert({
|
|
141
|
-
table: 'zrole',
|
|
142
|
-
data: {
|
|
143
|
-
name: name,
|
|
144
|
-
},
|
|
145
|
-
})
|
|
146
|
-
await zCache.ROLES()
|
|
147
|
-
} catch (e) {
|
|
148
|
-
json = Util.flashError(e.toString())
|
|
149
|
-
}
|
|
150
|
-
res.json(json)
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
router.post('/tab-access', async (req, res) => {
|
|
154
|
-
let json = Util.jsonSuccess()
|
|
155
|
-
let html = ''
|
|
156
|
-
const checkedFn = (obj, name, index) => {
|
|
157
|
-
return obj.hasOwnProperty(name) && obj[name][index] ? 'checked' : ''
|
|
158
|
-
}
|
|
159
|
-
try {
|
|
160
|
-
const table = req.body.table
|
|
161
|
-
const id = req.body.id
|
|
162
|
-
let tabRole = await connection.result({
|
|
163
|
-
table: 'zrole',
|
|
164
|
-
where: {
|
|
165
|
-
id: id,
|
|
166
|
-
},
|
|
167
|
-
})
|
|
168
|
-
let mytabs = tabRole.tabs || {}
|
|
169
|
-
let mytabsRole = mytabs[table] || {}
|
|
170
|
-
let result = await connection.result({
|
|
171
|
-
table: 'zfields',
|
|
172
|
-
where: {
|
|
173
|
-
table: table,
|
|
174
|
-
},
|
|
175
|
-
})
|
|
176
|
-
html += `<form id="tabform">`
|
|
177
|
-
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>`
|
|
178
|
-
result.tabs.map((item, index) => {
|
|
179
|
-
html += `<tr>
|
|
180
|
-
<td>${item}</td>
|
|
181
|
-
<td><input type="checkbox" class="viewtab" name="view___${index}" ${checkedFn(mytabsRole, 'view', index)} /></td>
|
|
182
|
-
<td><input type="checkbox" class="createtab" name="create___${index}" ${checkedFn(mytabsRole, 'create', index)} /></td>
|
|
183
|
-
<td><input type="checkbox" class="edittab" name="edit___${index}" ${checkedFn(mytabsRole, 'edit', index)} /></td>
|
|
184
|
-
<td><input type="checkbox" class="deletetab" name="delete___${index}" ${checkedFn(mytabsRole, 'delete', index)} /></td>
|
|
185
|
-
</tr>`
|
|
186
|
-
})
|
|
187
|
-
html += `</tbody></table>`
|
|
188
|
-
html += `</form>`
|
|
189
|
-
} catch (e) {
|
|
190
|
-
json = Util.flashError(e.toString())
|
|
191
|
-
}
|
|
192
|
-
res.send(html)
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
router.post('/approval-access', async (req, res) => {
|
|
196
|
-
let json = Util.jsonSuccess()
|
|
197
|
-
let html = ''
|
|
198
|
-
const checkedFn = (obj, name) => {
|
|
199
|
-
return obj.hasOwnProperty(name) && obj[name] ? 'checked' : ''
|
|
200
|
-
}
|
|
201
|
-
try {
|
|
202
|
-
const table = req.body.table
|
|
203
|
-
const id = req.body.id
|
|
204
|
-
let tabRole = await connection.result({
|
|
205
|
-
table: 'zrole',
|
|
206
|
-
where: {
|
|
207
|
-
id: id,
|
|
208
|
-
},
|
|
209
|
-
})
|
|
210
|
-
let myapprovals = tabRole.approvals || {}
|
|
211
|
-
let myapprovalsRole = myapprovals[table] || {}
|
|
212
|
-
let results = await connection.results({
|
|
213
|
-
table: 'zapproval_type',
|
|
214
|
-
order_by: ['id asc'],
|
|
215
|
-
})
|
|
216
|
-
html += `<form id="approvalform">`
|
|
217
|
-
html += `<table class="table table-hover"><thead><tr><th>Name </th><th>Level <input type="checkbox" onclick="tabApprovals(this)" id="checkapprovals" /></th></tr></thead><tbody></tbody>`
|
|
218
|
-
results.map((item) => {
|
|
219
|
-
if (item.id != 1) {
|
|
220
|
-
html += `<tr>
|
|
221
|
-
<td>${item.name}</td>
|
|
222
|
-
<td><input type="checkbox" class="checkapproval" name="approval_${item.id}" ${checkedFn(myapprovalsRole, item.id)} /></td>
|
|
223
|
-
</tr>`
|
|
224
|
-
}
|
|
225
|
-
})
|
|
226
|
-
html += `</tbody></table>`
|
|
227
|
-
html += `</form>`
|
|
228
|
-
} catch (e) {
|
|
229
|
-
json = Util.flashError(e.toString())
|
|
230
|
-
}
|
|
231
|
-
res.send(html)
|
|
232
|
-
})
|
|
233
|
-
|
|
234
|
-
router.post('/post-access', async (req, res) => {
|
|
235
|
-
let json = Util.jsonSuccess()
|
|
236
|
-
let table = req.body.table || ''
|
|
237
|
-
if (table == '') {
|
|
238
|
-
res.json(Util.flashError('Table is empty!'))
|
|
239
|
-
return false
|
|
240
|
-
}
|
|
241
|
-
let tabs = req.body.tabs || []
|
|
242
|
-
let id = req.body.id
|
|
243
|
-
let obj = tabs.reduce((acc, item) => {
|
|
244
|
-
acc[item.name] = item.value
|
|
245
|
-
return acc
|
|
246
|
-
}, {})
|
|
247
|
-
let mytabs = {}
|
|
248
|
-
let l = req.body.l
|
|
249
|
-
let roles = {}
|
|
250
|
-
let arr = ['view', 'create', 'edit', 'delete']
|
|
251
|
-
for (var i = 0; i < l; i++) {
|
|
252
|
-
arr.map((item) => {
|
|
253
|
-
if (!roles.hasOwnProperty(item)) {
|
|
254
|
-
roles[item] = []
|
|
255
|
-
}
|
|
256
|
-
if (obj.hasOwnProperty(`${item}___${i}`)) {
|
|
257
|
-
roles[item].push(true)
|
|
258
|
-
} else {
|
|
259
|
-
roles[item].push(false)
|
|
260
|
-
}
|
|
261
|
-
})
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
try {
|
|
265
|
-
let tabRole = await connection.result({
|
|
266
|
-
table: 'zrole',
|
|
267
|
-
where: {
|
|
268
|
-
id: id,
|
|
269
|
-
},
|
|
270
|
-
})
|
|
271
|
-
mytabs = tabRole.tabs || {}
|
|
272
|
-
mytabs[table] = roles
|
|
273
|
-
await connection.update({
|
|
274
|
-
table: 'zrole',
|
|
275
|
-
data: {
|
|
276
|
-
tabs: JSON.stringify(mytabs),
|
|
277
|
-
},
|
|
278
|
-
where: {
|
|
279
|
-
id: id,
|
|
280
|
-
},
|
|
281
|
-
})
|
|
282
|
-
} catch (err) {
|
|
283
|
-
json = Util.jsonError(err + '')
|
|
284
|
-
}
|
|
285
|
-
res.json(json)
|
|
286
|
-
})
|
|
287
|
-
|
|
288
|
-
router.post('/post-approval', async (req, res) => {
|
|
289
|
-
let json = Util.jsonSuccess()
|
|
290
|
-
let table = req.body.table || ''
|
|
291
|
-
if (table == '') {
|
|
292
|
-
res.json(Util.flashError('Table is empty!'))
|
|
293
|
-
return false
|
|
294
|
-
}
|
|
295
|
-
let datas = req.body.datas || []
|
|
296
|
-
console.log(datas)
|
|
297
|
-
let id = req.body.id
|
|
298
|
-
if (datas.length == 0) {
|
|
299
|
-
res.json(Util.flashError('Data is empty!'))
|
|
300
|
-
return false
|
|
301
|
-
}
|
|
302
|
-
let myapprovals = {}
|
|
303
|
-
try {
|
|
304
|
-
let result = await connection.result({
|
|
305
|
-
table: 'zrole',
|
|
306
|
-
where: {
|
|
307
|
-
id: id,
|
|
308
|
-
},
|
|
309
|
-
})
|
|
310
|
-
myapprovals = result.approvals || {}
|
|
311
|
-
let obj = {}
|
|
312
|
-
datas.map((item) => {
|
|
313
|
-
let id = item.name.replace('approval_', '')
|
|
314
|
-
obj[id] = true
|
|
315
|
-
})
|
|
316
|
-
myapprovals[table] = obj
|
|
317
|
-
await connection.update({
|
|
318
|
-
table: 'zrole',
|
|
319
|
-
data: {
|
|
320
|
-
approvals: JSON.stringify(myapprovals),
|
|
321
|
-
},
|
|
322
|
-
where: {
|
|
323
|
-
id: id,
|
|
324
|
-
},
|
|
325
|
-
})
|
|
326
|
-
} catch (err) {
|
|
327
|
-
json = Util.jsonError(err + '')
|
|
328
|
-
}
|
|
329
|
-
res.json(json)
|
|
330
|
-
})
|
|
331
|
-
|
|
332
|
-
router.delete('/delete/:id', async (req, res) => {
|
|
333
|
-
let json = Util.jsonSuccess()
|
|
334
|
-
let id = parseInt(req.params.id)
|
|
335
|
-
if (id > 3) {
|
|
336
|
-
try {
|
|
337
|
-
const name = req.body.name
|
|
338
|
-
await connection.delete({
|
|
339
|
-
table: 'zrole',
|
|
340
|
-
where: {
|
|
341
|
-
id: id,
|
|
342
|
-
},
|
|
343
|
-
})
|
|
344
|
-
await zCache.ROLES()
|
|
345
|
-
} catch (e) {
|
|
346
|
-
json = Util.flashError(e.toString())
|
|
347
|
-
}
|
|
348
|
-
} else {
|
|
349
|
-
json = Util.flashError('Delete error, not allowed')
|
|
350
|
-
}
|
|
351
|
-
res.json(json)
|
|
352
|
-
})
|
|
353
|
-
|
|
354
|
-
const body = `<div class="">
|
|
355
|
-
<div class="page-header"><h1>Roles</h1></div>
|
|
356
|
-
<div class="card panel panel-info boxy">
|
|
357
|
-
<div class="panel-heading">
|
|
358
|
-
<div class="float-end">
|
|
359
|
-
<div class="summary">
|
|
360
|
-
<% if(levels.delete) {%>
|
|
361
|
-
<span class="icon-small icons-danger" title="Delete role" onclick="deleterole()"><img class="icons-bg-white icon-image" src="/assets/icons/trash.svg"></span>
|
|
362
|
-
<%}%>
|
|
363
|
-
<% if(levels.update) {%>
|
|
364
|
-
<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>
|
|
365
|
-
<%}%>
|
|
366
|
-
<% if(levels.create) {%>
|
|
367
|
-
<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>
|
|
368
|
-
<%}%>
|
|
369
|
-
</div>
|
|
370
|
-
</div>
|
|
371
|
-
<h3 class="panel-title"><i class="fa fa-cog"></i> Settings</h3>
|
|
372
|
-
<div class="clearfix"></div>
|
|
373
|
-
</div>
|
|
374
|
-
<div class="kv-panel-before">
|
|
375
|
-
<div class="row">
|
|
376
|
-
<form id="role-form" class="form-horizontal kv-form-horizontal" method="post">
|
|
377
|
-
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
|
|
378
|
-
<div class="form-group field-role-role_name">
|
|
379
|
-
<label class="control-label col-md-2" for="role-role_name">Role Name</label>
|
|
380
|
-
<div class="col-md-10"><select id="roleName" class="form-control form-select mb-3" name="name">
|
|
381
|
-
<% for(var i = 0;i < results.length;i++){ %>
|
|
382
|
-
<option value="<%- results[i].id %>" <% if(id == results[i].id){ %> selected="" <% } %> ><%- results[i].name %></option>
|
|
383
|
-
<% } %>
|
|
384
|
-
</select></div>
|
|
385
|
-
</div>
|
|
386
|
-
<table class="table table-responsive">
|
|
387
|
-
<thead>
|
|
388
|
-
<tr>
|
|
389
|
-
<th>Name</th>
|
|
390
|
-
<% for(var i = 0;i < actions.length;i++) { %>
|
|
391
|
-
<th><%= actions[i] %> <input onclick='checkthis("<%= actions[i] %>")' type="checkbox" id="all<%= actions[i] %>"></th>
|
|
392
|
-
<% } %>
|
|
393
|
-
<th>Approve Level</th>
|
|
394
|
-
<th>Tabs</th>
|
|
395
|
-
</tr>
|
|
396
|
-
</thead>
|
|
397
|
-
<tbody>
|
|
398
|
-
<% for(var i = 0;i < routes.length;i++) { %>
|
|
399
|
-
<tr>
|
|
400
|
-
<td>
|
|
401
|
-
<% var ename = routes[i];
|
|
402
|
-
const handleTag = "handleAccess('"+routes[i]+"')";
|
|
403
|
-
const handleApproval = "handleApproval('"+routes[i]+"')";
|
|
404
|
-
const spanApproval = '<span class="handle-access icon-small icons-light" onclick="'+handleApproval+'" data-bs-toggle="modal" data-bs-target="#modalapproval" ><img class="icons-bg-blue gridview icon-image" src="/assets/icons/lock-access.svg"></span>';
|
|
405
|
-
const spanRole = tabs.includes(routes[i]) ? '<span class="handle-access icon-small icons-light" onclick="'+handleTag+'" data-bs-toggle="modal" data-bs-target="#tabaccess" ><img class="icons-bg-blue gridview icon-image" src="/assets/icons/lock-access.svg"></span>' : '' %>
|
|
406
|
-
<a href="/<%- ename %>" target="_blank"><%- ename %></a>
|
|
407
|
-
</td>
|
|
408
|
-
<% for(var x = 0;x < actions.length;x++) { %>
|
|
409
|
-
<td><input name="params[<%= ename %>][<%= actions[x] %>]" class="<%= actions[x] %>"
|
|
410
|
-
<% if(json && json.hasOwnProperty(ename) && json[ename].indexOf(actions[x]) >= 0) { %> <%= 'checked="checked"' %> <% } %> title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox">
|
|
411
|
-
</td>
|
|
412
|
-
<% } %>
|
|
413
|
-
<td><%- spanApproval %></td>
|
|
414
|
-
<td><%- spanRole %></td>
|
|
415
|
-
</tr>
|
|
416
|
-
<% } %>
|
|
417
|
-
</tbody>
|
|
418
|
-
</table>
|
|
419
|
-
<div class="row">
|
|
420
|
-
<div class="col-md-10 col-md-offset-1">
|
|
421
|
-
<% if(levels.update) {%>
|
|
422
|
-
<button type="submit" class="btn btn-primary">Update</button>
|
|
423
|
-
<%}%>
|
|
424
|
-
</div>
|
|
425
|
-
</div>
|
|
426
|
-
</form>
|
|
427
|
-
</div>
|
|
428
|
-
</div>
|
|
429
|
-
</div>
|
|
430
|
-
</div><!-- Modal -->
|
|
431
|
-
<div class="modal fade" id="renameModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
|
|
432
|
-
<div class="modal-dialog">
|
|
433
|
-
<div class="modal-content">
|
|
434
|
-
<div class="modal-header"><h5 class="modal-title" id="exampleModalLabel">Rename title</h5>
|
|
435
|
-
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
436
|
-
</div>
|
|
437
|
-
<div class="modal-body">
|
|
438
|
-
<input type="text" class="form-control" id="rename" name="rename" value="<%- model[0].name %>">
|
|
439
|
-
</div>
|
|
440
|
-
<div class="modal-footer">
|
|
441
|
-
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
442
|
-
<button type="button" class="btn btn-primary btn-update">Save changes</button>
|
|
443
|
-
</div>
|
|
444
|
-
</div>
|
|
445
|
-
</div>
|
|
446
|
-
</div><!-- Modal -->
|
|
447
|
-
<div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="addModalLabel" aria-hidden="true">
|
|
448
|
-
<div class="modal-dialog">
|
|
449
|
-
<div class="modal-content">
|
|
450
|
-
<div class="modal-header"><h5 class="modal-title" id="exampleModalLabel">Add a New Role</h5>
|
|
451
|
-
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
452
|
-
</div>
|
|
453
|
-
<div class="modal-body">
|
|
454
|
-
<div class="form-group mb-3">
|
|
455
|
-
<label for="ruas_sk">Role Name</label>
|
|
456
|
-
<input type="text" class="form-control" id="role_name" name="role_name" placeholder="Role Name">
|
|
457
|
-
|
|
458
|
-
</div>
|
|
459
|
-
</div>
|
|
460
|
-
<div class="modal-footer">
|
|
461
|
-
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
462
|
-
<button type="button" class="btn btn-primary btn-add">Save changes</button>
|
|
463
|
-
</div>
|
|
464
|
-
</div>
|
|
465
|
-
</div>
|
|
466
|
-
</div>
|
|
467
|
-
<div class="modal fade" id="tabaccess" tabindex="-1" aria-labelledby="tabaccessModalLabel" aria-hidden="true">
|
|
468
|
-
<div class="modal-dialog">
|
|
469
|
-
<div class="modal-content">
|
|
470
|
-
<div class="modal-header"><h5 class="modal-title" id="tabaccesstitle">Tab Access <span id="titleaccess"></span></h5>
|
|
471
|
-
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
472
|
-
</div>
|
|
473
|
-
<div class="modal-body body-access"></div>
|
|
474
|
-
<div class="modal-footer">
|
|
475
|
-
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
476
|
-
<button type="button" class="btn btn-primary btn-save-tab">Save changes</button>
|
|
477
|
-
</div>
|
|
478
|
-
</div>
|
|
479
|
-
</div>
|
|
480
|
-
</div>
|
|
481
|
-
<div class="modal fade" id="modalapproval" tabindex="-1" aria-labelledby="tabapprovalModalLabel" aria-hidden="true">
|
|
482
|
-
<div class="modal-dialog">
|
|
483
|
-
<div class="modal-content">
|
|
484
|
-
<div class="modal-header"><h5 class="modal-title">Approval Level <span id="approvalname"></span> </h5>
|
|
485
|
-
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
486
|
-
</div>
|
|
487
|
-
<div class="modal-body body-approval"></div>
|
|
488
|
-
<div class="modal-footer">
|
|
489
|
-
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
490
|
-
<button type="button" class="btn btn-primary btn-save-approval">Save changes</button>
|
|
491
|
-
</div>
|
|
492
|
-
</div>
|
|
493
|
-
</div>
|
|
494
|
-
</div>
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
`
|
|
498
|
-
const js = `<script>
|
|
499
|
-
function checkthis(elm) {
|
|
500
|
-
var cElem = $("#all" + elm);
|
|
501
|
-
if (cElem.is(":checked")) {
|
|
502
|
-
$("input." + elm).prop("checked", true);
|
|
503
|
-
} else {
|
|
504
|
-
$("input." + elm).prop("checked", false);
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
function tabChecks(that,elm) {
|
|
508
|
-
if ($(that).is(":checked")) {
|
|
509
|
-
$("input." + elm).prop("checked", true);
|
|
510
|
-
} else {
|
|
511
|
-
$("input." + elm).prop("checked", false);
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
function tabApprovals(that) {
|
|
515
|
-
let elm = 'checkapproval';
|
|
516
|
-
if ($(that).is(":checked")) {
|
|
517
|
-
$("input." + elm).prop("checked", true);
|
|
518
|
-
} else {
|
|
519
|
-
$("input." + elm).prop("checked", false);
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
$("#roleName").on('change', function () {
|
|
523
|
-
location.href = "/zrole?id=" + $(this).val();
|
|
524
|
-
});
|
|
525
|
-
var form = document.getElementById("role-form");
|
|
526
|
-
form.onsubmit = function (ev) {
|
|
527
|
-
ev.preventDefault();
|
|
528
|
-
var url = '/zrole/update/<%= id%>';
|
|
529
|
-
ajaxPost(url, $(this).serialize(), function (data) {
|
|
530
|
-
if (data.status == 1) {
|
|
531
|
-
toastr.success('Success', 'Updated Role');
|
|
532
|
-
} else {
|
|
533
|
-
toastr.error('Error!',data.data);
|
|
534
|
-
}
|
|
535
|
-
});
|
|
536
|
-
}
|
|
537
|
-
$(".btn-update").on("click", function () {
|
|
538
|
-
ajaxPost('/zrole/rename/<%= id%>',{
|
|
539
|
-
rename : $("#rename").val()
|
|
540
|
-
}, function (data) {
|
|
541
|
-
toastrForm(data);
|
|
542
|
-
setTimeout(function () {
|
|
543
|
-
location.href= '';
|
|
544
|
-
},2000);
|
|
545
|
-
})
|
|
546
|
-
})
|
|
547
|
-
|
|
548
|
-
$(".btn-add").on("click", function () {
|
|
549
|
-
ajaxPost('/zrole/create/',{
|
|
550
|
-
name : $("#role_name").val()
|
|
551
|
-
}, function (data) {
|
|
552
|
-
toastrForm(data);
|
|
553
|
-
setTimeout(function () {
|
|
554
|
-
location.href= '';
|
|
555
|
-
},2000);
|
|
556
|
-
})
|
|
557
|
-
})
|
|
558
|
-
function deleterole() {
|
|
559
|
-
if(window.confirm('delete role selected ? ')) {
|
|
560
|
-
let id = "<%= id%>";
|
|
561
|
-
ajaxDelete('/zrole/delete/<%= id%>',{id:id}, function (data) {
|
|
562
|
-
toastrForm(data);
|
|
563
|
-
setTimeout(function () {
|
|
564
|
-
location.href= '/zrole';
|
|
565
|
-
},2000);
|
|
566
|
-
})
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
function handleApproval(table) {
|
|
570
|
-
$(".btn-save-approval").attr("data-table", table);
|
|
571
|
-
$("#approvalname").html(table);
|
|
572
|
-
ajaxPost('/zrole/approval-access',{table:table, id: "<%- id %>"}, ((data) => {
|
|
573
|
-
$(".body-approval").html(data);
|
|
574
|
-
}))
|
|
575
|
-
}
|
|
576
|
-
function handleAccess(table) {
|
|
577
|
-
$(".btn-save-tab").attr("data-table", table);
|
|
578
|
-
ajaxPost('/zrole/tab-access',{table:table, id: "<%- id %>"}, ((data) => {
|
|
579
|
-
$(".body-access").html(data);
|
|
580
|
-
}))
|
|
581
|
-
}
|
|
582
|
-
$(".btn-save-tab").on("click",(e) => {
|
|
583
|
-
let queryform = $("#tabform").serializeArray();
|
|
584
|
-
ajaxPost('/zrole/post-access', {
|
|
585
|
-
tabs: queryform,
|
|
586
|
-
l : $(".viewtab").length,
|
|
587
|
-
id: "<%- id %>",
|
|
588
|
-
table : e.target.getAttribute('data-table')
|
|
589
|
-
},((data) => {
|
|
590
|
-
toastrForm(data);
|
|
591
|
-
$(".btn-close").click();
|
|
592
|
-
}))
|
|
593
|
-
})
|
|
594
|
-
|
|
595
|
-
$(".btn-save-approval").on("click",(e) => {
|
|
596
|
-
let queryform = $("#approvalform").serializeArray();
|
|
597
|
-
ajaxPost('/zrole/post-approval', {
|
|
598
|
-
datas: queryform,
|
|
599
|
-
id: "<%- id %>",
|
|
600
|
-
table : e.target.getAttribute('data-table')
|
|
601
|
-
},((data) => {
|
|
602
|
-
toastrForm(data);
|
|
603
|
-
$(".btn-close").click();
|
|
604
|
-
}))
|
|
605
|
-
})
|
|
606
|
-
</script>
|
|
607
|
-
`
|
|
608
|
-
|
|
609
|
-
module.exports = router
|
|
1
|
+
const express = require('express')
|
|
2
|
+
const router = express.Router()
|
|
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')
|
|
18
|
+
|
|
19
|
+
router.get('/', csrfProtection, async function (req, res, next) {
|
|
20
|
+
let dirname = path.resolve(__dirname)
|
|
21
|
+
let id = req.query.id
|
|
22
|
+
if (id == undefined) {
|
|
23
|
+
id = 1
|
|
24
|
+
}
|
|
25
|
+
const model = await connection.results({
|
|
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 dummies = []
|
|
34
|
+
let tabs = []
|
|
35
|
+
zfields.map((item) => {
|
|
36
|
+
let itemTabs = item.tabs || []
|
|
37
|
+
if (itemTabs.length) {
|
|
38
|
+
tabs.push(item.table)
|
|
39
|
+
}
|
|
40
|
+
if (item.json) {
|
|
41
|
+
if (item.json.dummy == 1) {
|
|
42
|
+
dummies.push(item.table)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
const json = model[0].params
|
|
47
|
+
const routes = zRole.routes.filter((item) => !dummies.includes(item))
|
|
48
|
+
const results = await connection.results({ table: 'zrole' })
|
|
49
|
+
const myLevel = zRole.myLevel(req, res, 'zrole')
|
|
50
|
+
//inject to end body
|
|
51
|
+
let datas = {
|
|
52
|
+
model: model,
|
|
53
|
+
tabs: tabs,
|
|
54
|
+
dummies: dummies,
|
|
55
|
+
table: 'zrole',
|
|
56
|
+
id: id,
|
|
57
|
+
actions: zRole.actions,
|
|
58
|
+
routes: routes,
|
|
59
|
+
levels: myLevel,
|
|
60
|
+
json: json,
|
|
61
|
+
results: results,
|
|
62
|
+
csrfToken: req.csrfToken(),
|
|
63
|
+
}
|
|
64
|
+
const bodyHTML = ejs.render(body, datas)
|
|
65
|
+
const endBody = ejs.render(js, datas)
|
|
66
|
+
datas.bodyHTML = bodyHTML
|
|
67
|
+
moduleLib.addModule(req, res, endBody)
|
|
68
|
+
res.render('layouts/' + layout, datas)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
router.post('/update/:id', async function (req, res, next) {
|
|
72
|
+
const data = {}
|
|
73
|
+
const name = req.body.name
|
|
74
|
+
const params = req.body.params
|
|
75
|
+
const newKey = {}
|
|
76
|
+
Object.keys(params).map((key) => {
|
|
77
|
+
const arr = []
|
|
78
|
+
for (const k in params[key]) {
|
|
79
|
+
arr.push(k)
|
|
80
|
+
}
|
|
81
|
+
newKey[key] = arr
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const json = {}
|
|
85
|
+
json.params = JSON.stringify(newKey)
|
|
86
|
+
try {
|
|
87
|
+
await connection.update({
|
|
88
|
+
table: 'zrole',
|
|
89
|
+
data: {
|
|
90
|
+
params: json.params,
|
|
91
|
+
},
|
|
92
|
+
where: {
|
|
93
|
+
id: req.params.id,
|
|
94
|
+
},
|
|
95
|
+
})
|
|
96
|
+
data.status = 1
|
|
97
|
+
data.data = 1
|
|
98
|
+
await zCache.ROLES()
|
|
99
|
+
|
|
100
|
+
if (env == 'production') {
|
|
101
|
+
pm2.connect(function (err) {
|
|
102
|
+
if (err) {
|
|
103
|
+
//console.log(err.toString());
|
|
104
|
+
}
|
|
105
|
+
pm2.restart(process.env.PM2_NAME, (err, proc) => {
|
|
106
|
+
//io.to(room).emit("message","Restart done")
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
res.json(data)
|
|
111
|
+
} catch (error) {
|
|
112
|
+
data.status = 0
|
|
113
|
+
data.data = error
|
|
114
|
+
res.json(data)
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
router.post('/rename/:id', async (req, res) => {
|
|
119
|
+
let json = Util.jsonSuccess()
|
|
120
|
+
try {
|
|
121
|
+
const id = req.params.id
|
|
122
|
+
const rename = req.body.rename
|
|
123
|
+
await connection.update({
|
|
124
|
+
table: 'zrole',
|
|
125
|
+
where: {
|
|
126
|
+
id: id,
|
|
127
|
+
},
|
|
128
|
+
data: { name: rename },
|
|
129
|
+
})
|
|
130
|
+
} catch (e) {
|
|
131
|
+
json = Util.flashError(e.toString())
|
|
132
|
+
}
|
|
133
|
+
res.json(json)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
router.post('/create', async (req, res) => {
|
|
137
|
+
let json = Util.jsonSuccess()
|
|
138
|
+
try {
|
|
139
|
+
const name = req.body.name
|
|
140
|
+
await connection.insert({
|
|
141
|
+
table: 'zrole',
|
|
142
|
+
data: {
|
|
143
|
+
name: name,
|
|
144
|
+
},
|
|
145
|
+
})
|
|
146
|
+
await zCache.ROLES()
|
|
147
|
+
} catch (e) {
|
|
148
|
+
json = Util.flashError(e.toString())
|
|
149
|
+
}
|
|
150
|
+
res.json(json)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
router.post('/tab-access', async (req, res) => {
|
|
154
|
+
let json = Util.jsonSuccess()
|
|
155
|
+
let html = ''
|
|
156
|
+
const checkedFn = (obj, name, index) => {
|
|
157
|
+
return obj.hasOwnProperty(name) && obj[name][index] ? 'checked' : ''
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
const table = req.body.table
|
|
161
|
+
const id = req.body.id
|
|
162
|
+
let tabRole = await connection.result({
|
|
163
|
+
table: 'zrole',
|
|
164
|
+
where: {
|
|
165
|
+
id: id,
|
|
166
|
+
},
|
|
167
|
+
})
|
|
168
|
+
let mytabs = tabRole.tabs || {}
|
|
169
|
+
let mytabsRole = mytabs[table] || {}
|
|
170
|
+
let result = await connection.result({
|
|
171
|
+
table: 'zfields',
|
|
172
|
+
where: {
|
|
173
|
+
table: table,
|
|
174
|
+
},
|
|
175
|
+
})
|
|
176
|
+
html += `<form id="tabform">`
|
|
177
|
+
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>`
|
|
178
|
+
result.tabs.map((item, index) => {
|
|
179
|
+
html += `<tr>
|
|
180
|
+
<td>${item}</td>
|
|
181
|
+
<td><input type="checkbox" class="viewtab" name="view___${index}" ${checkedFn(mytabsRole, 'view', index)} /></td>
|
|
182
|
+
<td><input type="checkbox" class="createtab" name="create___${index}" ${checkedFn(mytabsRole, 'create', index)} /></td>
|
|
183
|
+
<td><input type="checkbox" class="edittab" name="edit___${index}" ${checkedFn(mytabsRole, 'edit', index)} /></td>
|
|
184
|
+
<td><input type="checkbox" class="deletetab" name="delete___${index}" ${checkedFn(mytabsRole, 'delete', index)} /></td>
|
|
185
|
+
</tr>`
|
|
186
|
+
})
|
|
187
|
+
html += `</tbody></table>`
|
|
188
|
+
html += `</form>`
|
|
189
|
+
} catch (e) {
|
|
190
|
+
json = Util.flashError(e.toString())
|
|
191
|
+
}
|
|
192
|
+
res.send(html)
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
router.post('/approval-access', async (req, res) => {
|
|
196
|
+
let json = Util.jsonSuccess()
|
|
197
|
+
let html = ''
|
|
198
|
+
const checkedFn = (obj, name) => {
|
|
199
|
+
return obj.hasOwnProperty(name) && obj[name] ? 'checked' : ''
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
const table = req.body.table
|
|
203
|
+
const id = req.body.id
|
|
204
|
+
let tabRole = await connection.result({
|
|
205
|
+
table: 'zrole',
|
|
206
|
+
where: {
|
|
207
|
+
id: id,
|
|
208
|
+
},
|
|
209
|
+
})
|
|
210
|
+
let myapprovals = tabRole.approvals || {}
|
|
211
|
+
let myapprovalsRole = myapprovals[table] || {}
|
|
212
|
+
let results = await connection.results({
|
|
213
|
+
table: 'zapproval_type',
|
|
214
|
+
order_by: ['id asc'],
|
|
215
|
+
})
|
|
216
|
+
html += `<form id="approvalform">`
|
|
217
|
+
html += `<table class="table table-hover"><thead><tr><th>Name </th><th>Level <input type="checkbox" onclick="tabApprovals(this)" id="checkapprovals" /></th></tr></thead><tbody></tbody>`
|
|
218
|
+
results.map((item) => {
|
|
219
|
+
if (item.id != 1) {
|
|
220
|
+
html += `<tr>
|
|
221
|
+
<td>${item.name}</td>
|
|
222
|
+
<td><input type="checkbox" class="checkapproval" name="approval_${item.id}" ${checkedFn(myapprovalsRole, item.id)} /></td>
|
|
223
|
+
</tr>`
|
|
224
|
+
}
|
|
225
|
+
})
|
|
226
|
+
html += `</tbody></table>`
|
|
227
|
+
html += `</form>`
|
|
228
|
+
} catch (e) {
|
|
229
|
+
json = Util.flashError(e.toString())
|
|
230
|
+
}
|
|
231
|
+
res.send(html)
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
router.post('/post-access', async (req, res) => {
|
|
235
|
+
let json = Util.jsonSuccess()
|
|
236
|
+
let table = req.body.table || ''
|
|
237
|
+
if (table == '') {
|
|
238
|
+
res.json(Util.flashError('Table is empty!'))
|
|
239
|
+
return false
|
|
240
|
+
}
|
|
241
|
+
let tabs = req.body.tabs || []
|
|
242
|
+
let id = req.body.id
|
|
243
|
+
let obj = tabs.reduce((acc, item) => {
|
|
244
|
+
acc[item.name] = item.value
|
|
245
|
+
return acc
|
|
246
|
+
}, {})
|
|
247
|
+
let mytabs = {}
|
|
248
|
+
let l = req.body.l
|
|
249
|
+
let roles = {}
|
|
250
|
+
let arr = ['view', 'create', 'edit', 'delete']
|
|
251
|
+
for (var i = 0; i < l; i++) {
|
|
252
|
+
arr.map((item) => {
|
|
253
|
+
if (!roles.hasOwnProperty(item)) {
|
|
254
|
+
roles[item] = []
|
|
255
|
+
}
|
|
256
|
+
if (obj.hasOwnProperty(`${item}___${i}`)) {
|
|
257
|
+
roles[item].push(true)
|
|
258
|
+
} else {
|
|
259
|
+
roles[item].push(false)
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
try {
|
|
265
|
+
let tabRole = await connection.result({
|
|
266
|
+
table: 'zrole',
|
|
267
|
+
where: {
|
|
268
|
+
id: id,
|
|
269
|
+
},
|
|
270
|
+
})
|
|
271
|
+
mytabs = tabRole.tabs || {}
|
|
272
|
+
mytabs[table] = roles
|
|
273
|
+
await connection.update({
|
|
274
|
+
table: 'zrole',
|
|
275
|
+
data: {
|
|
276
|
+
tabs: JSON.stringify(mytabs),
|
|
277
|
+
},
|
|
278
|
+
where: {
|
|
279
|
+
id: id,
|
|
280
|
+
},
|
|
281
|
+
})
|
|
282
|
+
} catch (err) {
|
|
283
|
+
json = Util.jsonError(err + '')
|
|
284
|
+
}
|
|
285
|
+
res.json(json)
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
router.post('/post-approval', async (req, res) => {
|
|
289
|
+
let json = Util.jsonSuccess()
|
|
290
|
+
let table = req.body.table || ''
|
|
291
|
+
if (table == '') {
|
|
292
|
+
res.json(Util.flashError('Table is empty!'))
|
|
293
|
+
return false
|
|
294
|
+
}
|
|
295
|
+
let datas = req.body.datas || []
|
|
296
|
+
console.log(datas)
|
|
297
|
+
let id = req.body.id
|
|
298
|
+
if (datas.length == 0) {
|
|
299
|
+
res.json(Util.flashError('Data is empty!'))
|
|
300
|
+
return false
|
|
301
|
+
}
|
|
302
|
+
let myapprovals = {}
|
|
303
|
+
try {
|
|
304
|
+
let result = await connection.result({
|
|
305
|
+
table: 'zrole',
|
|
306
|
+
where: {
|
|
307
|
+
id: id,
|
|
308
|
+
},
|
|
309
|
+
})
|
|
310
|
+
myapprovals = result.approvals || {}
|
|
311
|
+
let obj = {}
|
|
312
|
+
datas.map((item) => {
|
|
313
|
+
let id = item.name.replace('approval_', '')
|
|
314
|
+
obj[id] = true
|
|
315
|
+
})
|
|
316
|
+
myapprovals[table] = obj
|
|
317
|
+
await connection.update({
|
|
318
|
+
table: 'zrole',
|
|
319
|
+
data: {
|
|
320
|
+
approvals: JSON.stringify(myapprovals),
|
|
321
|
+
},
|
|
322
|
+
where: {
|
|
323
|
+
id: id,
|
|
324
|
+
},
|
|
325
|
+
})
|
|
326
|
+
} catch (err) {
|
|
327
|
+
json = Util.jsonError(err + '')
|
|
328
|
+
}
|
|
329
|
+
res.json(json)
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
router.delete('/delete/:id', async (req, res) => {
|
|
333
|
+
let json = Util.jsonSuccess()
|
|
334
|
+
let id = parseInt(req.params.id)
|
|
335
|
+
if (id > 3) {
|
|
336
|
+
try {
|
|
337
|
+
const name = req.body.name
|
|
338
|
+
await connection.delete({
|
|
339
|
+
table: 'zrole',
|
|
340
|
+
where: {
|
|
341
|
+
id: id,
|
|
342
|
+
},
|
|
343
|
+
})
|
|
344
|
+
await zCache.ROLES()
|
|
345
|
+
} catch (e) {
|
|
346
|
+
json = Util.flashError(e.toString())
|
|
347
|
+
}
|
|
348
|
+
} else {
|
|
349
|
+
json = Util.flashError('Delete error, not allowed')
|
|
350
|
+
}
|
|
351
|
+
res.json(json)
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
const body = `<div class="">
|
|
355
|
+
<div class="page-header"><h1>Roles</h1></div>
|
|
356
|
+
<div class="card panel panel-info boxy">
|
|
357
|
+
<div class="panel-heading">
|
|
358
|
+
<div class="float-end">
|
|
359
|
+
<div class="summary">
|
|
360
|
+
<% if(levels.delete) {%>
|
|
361
|
+
<span class="icon-small icons-danger" title="Delete role" onclick="deleterole()"><img class="icons-bg-white icon-image" src="/assets/icons/trash.svg"></span>
|
|
362
|
+
<%}%>
|
|
363
|
+
<% if(levels.update) {%>
|
|
364
|
+
<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>
|
|
365
|
+
<%}%>
|
|
366
|
+
<% if(levels.create) {%>
|
|
367
|
+
<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>
|
|
368
|
+
<%}%>
|
|
369
|
+
</div>
|
|
370
|
+
</div>
|
|
371
|
+
<h3 class="panel-title"><i class="fa fa-cog"></i> Settings</h3>
|
|
372
|
+
<div class="clearfix"></div>
|
|
373
|
+
</div>
|
|
374
|
+
<div class="kv-panel-before">
|
|
375
|
+
<div class="row">
|
|
376
|
+
<form id="role-form" class="form-horizontal kv-form-horizontal" method="post">
|
|
377
|
+
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
|
|
378
|
+
<div class="form-group field-role-role_name">
|
|
379
|
+
<label class="control-label col-md-2" for="role-role_name">Role Name</label>
|
|
380
|
+
<div class="col-md-10"><select id="roleName" class="form-control form-select mb-3" name="name">
|
|
381
|
+
<% for(var i = 0;i < results.length;i++){ %>
|
|
382
|
+
<option value="<%- results[i].id %>" <% if(id == results[i].id){ %> selected="" <% } %> ><%- results[i].name %></option>
|
|
383
|
+
<% } %>
|
|
384
|
+
</select></div>
|
|
385
|
+
</div>
|
|
386
|
+
<table class="table table-responsive">
|
|
387
|
+
<thead>
|
|
388
|
+
<tr>
|
|
389
|
+
<th>Name</th>
|
|
390
|
+
<% for(var i = 0;i < actions.length;i++) { %>
|
|
391
|
+
<th><%= actions[i] %> <input onclick='checkthis("<%= actions[i] %>")' type="checkbox" id="all<%= actions[i] %>"></th>
|
|
392
|
+
<% } %>
|
|
393
|
+
<th>Approve Level</th>
|
|
394
|
+
<th>Tabs</th>
|
|
395
|
+
</tr>
|
|
396
|
+
</thead>
|
|
397
|
+
<tbody>
|
|
398
|
+
<% for(var i = 0;i < routes.length;i++) { %>
|
|
399
|
+
<tr>
|
|
400
|
+
<td>
|
|
401
|
+
<% var ename = routes[i];
|
|
402
|
+
const handleTag = "handleAccess('"+routes[i]+"')";
|
|
403
|
+
const handleApproval = "handleApproval('"+routes[i]+"')";
|
|
404
|
+
const spanApproval = '<span class="handle-access icon-small icons-light" onclick="'+handleApproval+'" data-bs-toggle="modal" data-bs-target="#modalapproval" ><img class="icons-bg-blue gridview icon-image" src="/assets/icons/lock-access.svg"></span>';
|
|
405
|
+
const spanRole = tabs.includes(routes[i]) ? '<span class="handle-access icon-small icons-light" onclick="'+handleTag+'" data-bs-toggle="modal" data-bs-target="#tabaccess" ><img class="icons-bg-blue gridview icon-image" src="/assets/icons/lock-access.svg"></span>' : '' %>
|
|
406
|
+
<a href="/<%- ename %>" target="_blank"><%- ename %></a>
|
|
407
|
+
</td>
|
|
408
|
+
<% for(var x = 0;x < actions.length;x++) { %>
|
|
409
|
+
<td><input name="params[<%= ename %>][<%= actions[x] %>]" class="<%= actions[x] %>"
|
|
410
|
+
<% if(json && json.hasOwnProperty(ename) && json[ename].indexOf(actions[x]) >= 0) { %> <%= 'checked="checked"' %> <% } %> title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox">
|
|
411
|
+
</td>
|
|
412
|
+
<% } %>
|
|
413
|
+
<td><%- spanApproval %></td>
|
|
414
|
+
<td><%- spanRole %></td>
|
|
415
|
+
</tr>
|
|
416
|
+
<% } %>
|
|
417
|
+
</tbody>
|
|
418
|
+
</table>
|
|
419
|
+
<div class="row">
|
|
420
|
+
<div class="col-md-10 col-md-offset-1">
|
|
421
|
+
<% if(levels.update) {%>
|
|
422
|
+
<button type="submit" class="btn btn-primary">Update</button>
|
|
423
|
+
<%}%>
|
|
424
|
+
</div>
|
|
425
|
+
</div>
|
|
426
|
+
</form>
|
|
427
|
+
</div>
|
|
428
|
+
</div>
|
|
429
|
+
</div>
|
|
430
|
+
</div><!-- Modal -->
|
|
431
|
+
<div class="modal fade" id="renameModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
|
|
432
|
+
<div class="modal-dialog">
|
|
433
|
+
<div class="modal-content">
|
|
434
|
+
<div class="modal-header"><h5 class="modal-title" id="exampleModalLabel">Rename title</h5>
|
|
435
|
+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
436
|
+
</div>
|
|
437
|
+
<div class="modal-body">
|
|
438
|
+
<input type="text" class="form-control" id="rename" name="rename" value="<%- model[0].name %>">
|
|
439
|
+
</div>
|
|
440
|
+
<div class="modal-footer">
|
|
441
|
+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
442
|
+
<button type="button" class="btn btn-primary btn-update">Save changes</button>
|
|
443
|
+
</div>
|
|
444
|
+
</div>
|
|
445
|
+
</div>
|
|
446
|
+
</div><!-- Modal -->
|
|
447
|
+
<div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="addModalLabel" aria-hidden="true">
|
|
448
|
+
<div class="modal-dialog">
|
|
449
|
+
<div class="modal-content">
|
|
450
|
+
<div class="modal-header"><h5 class="modal-title" id="exampleModalLabel">Add a New Role</h5>
|
|
451
|
+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
452
|
+
</div>
|
|
453
|
+
<div class="modal-body">
|
|
454
|
+
<div class="form-group mb-3">
|
|
455
|
+
<label for="ruas_sk">Role Name</label>
|
|
456
|
+
<input type="text" class="form-control" id="role_name" name="role_name" placeholder="Role Name">
|
|
457
|
+
|
|
458
|
+
</div>
|
|
459
|
+
</div>
|
|
460
|
+
<div class="modal-footer">
|
|
461
|
+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
462
|
+
<button type="button" class="btn btn-primary btn-add">Save changes</button>
|
|
463
|
+
</div>
|
|
464
|
+
</div>
|
|
465
|
+
</div>
|
|
466
|
+
</div>
|
|
467
|
+
<div class="modal fade" id="tabaccess" tabindex="-1" aria-labelledby="tabaccessModalLabel" aria-hidden="true">
|
|
468
|
+
<div class="modal-dialog">
|
|
469
|
+
<div class="modal-content">
|
|
470
|
+
<div class="modal-header"><h5 class="modal-title" id="tabaccesstitle">Tab Access <span id="titleaccess"></span></h5>
|
|
471
|
+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
472
|
+
</div>
|
|
473
|
+
<div class="modal-body body-access"></div>
|
|
474
|
+
<div class="modal-footer">
|
|
475
|
+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
476
|
+
<button type="button" class="btn btn-primary btn-save-tab">Save changes</button>
|
|
477
|
+
</div>
|
|
478
|
+
</div>
|
|
479
|
+
</div>
|
|
480
|
+
</div>
|
|
481
|
+
<div class="modal fade" id="modalapproval" tabindex="-1" aria-labelledby="tabapprovalModalLabel" aria-hidden="true">
|
|
482
|
+
<div class="modal-dialog">
|
|
483
|
+
<div class="modal-content">
|
|
484
|
+
<div class="modal-header"><h5 class="modal-title">Approval Level <span id="approvalname"></span> </h5>
|
|
485
|
+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
486
|
+
</div>
|
|
487
|
+
<div class="modal-body body-approval"></div>
|
|
488
|
+
<div class="modal-footer">
|
|
489
|
+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
490
|
+
<button type="button" class="btn btn-primary btn-save-approval">Save changes</button>
|
|
491
|
+
</div>
|
|
492
|
+
</div>
|
|
493
|
+
</div>
|
|
494
|
+
</div>
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
`
|
|
498
|
+
const js = `<script>
|
|
499
|
+
function checkthis(elm) {
|
|
500
|
+
var cElem = $("#all" + elm);
|
|
501
|
+
if (cElem.is(":checked")) {
|
|
502
|
+
$("input." + elm).prop("checked", true);
|
|
503
|
+
} else {
|
|
504
|
+
$("input." + elm).prop("checked", false);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
function tabChecks(that,elm) {
|
|
508
|
+
if ($(that).is(":checked")) {
|
|
509
|
+
$("input." + elm).prop("checked", true);
|
|
510
|
+
} else {
|
|
511
|
+
$("input." + elm).prop("checked", false);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
function tabApprovals(that) {
|
|
515
|
+
let elm = 'checkapproval';
|
|
516
|
+
if ($(that).is(":checked")) {
|
|
517
|
+
$("input." + elm).prop("checked", true);
|
|
518
|
+
} else {
|
|
519
|
+
$("input." + elm).prop("checked", false);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
$("#roleName").on('change', function () {
|
|
523
|
+
location.href = "/zrole?id=" + $(this).val();
|
|
524
|
+
});
|
|
525
|
+
var form = document.getElementById("role-form");
|
|
526
|
+
form.onsubmit = function (ev) {
|
|
527
|
+
ev.preventDefault();
|
|
528
|
+
var url = '/zrole/update/<%= id%>';
|
|
529
|
+
ajaxPost(url, $(this).serialize(), function (data) {
|
|
530
|
+
if (data.status == 1) {
|
|
531
|
+
toastr.success('Success', 'Updated Role');
|
|
532
|
+
} else {
|
|
533
|
+
toastr.error('Error!',data.data);
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
$(".btn-update").on("click", function () {
|
|
538
|
+
ajaxPost('/zrole/rename/<%= id%>',{
|
|
539
|
+
rename : $("#rename").val()
|
|
540
|
+
}, function (data) {
|
|
541
|
+
toastrForm(data);
|
|
542
|
+
setTimeout(function () {
|
|
543
|
+
location.href= '';
|
|
544
|
+
},2000);
|
|
545
|
+
})
|
|
546
|
+
})
|
|
547
|
+
|
|
548
|
+
$(".btn-add").on("click", function () {
|
|
549
|
+
ajaxPost('/zrole/create/',{
|
|
550
|
+
name : $("#role_name").val()
|
|
551
|
+
}, function (data) {
|
|
552
|
+
toastrForm(data);
|
|
553
|
+
setTimeout(function () {
|
|
554
|
+
location.href= '';
|
|
555
|
+
},2000);
|
|
556
|
+
})
|
|
557
|
+
})
|
|
558
|
+
function deleterole() {
|
|
559
|
+
if(window.confirm('delete role selected ? ')) {
|
|
560
|
+
let id = "<%= id%>";
|
|
561
|
+
ajaxDelete('/zrole/delete/<%= id%>',{id:id}, function (data) {
|
|
562
|
+
toastrForm(data);
|
|
563
|
+
setTimeout(function () {
|
|
564
|
+
location.href= '/zrole';
|
|
565
|
+
},2000);
|
|
566
|
+
})
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
function handleApproval(table) {
|
|
570
|
+
$(".btn-save-approval").attr("data-table", table);
|
|
571
|
+
$("#approvalname").html(table);
|
|
572
|
+
ajaxPost('/zrole/approval-access',{table:table, id: "<%- id %>"}, ((data) => {
|
|
573
|
+
$(".body-approval").html(data);
|
|
574
|
+
}))
|
|
575
|
+
}
|
|
576
|
+
function handleAccess(table) {
|
|
577
|
+
$(".btn-save-tab").attr("data-table", table);
|
|
578
|
+
ajaxPost('/zrole/tab-access',{table:table, id: "<%- id %>"}, ((data) => {
|
|
579
|
+
$(".body-access").html(data);
|
|
580
|
+
}))
|
|
581
|
+
}
|
|
582
|
+
$(".btn-save-tab").on("click",(e) => {
|
|
583
|
+
let queryform = $("#tabform").serializeArray();
|
|
584
|
+
ajaxPost('/zrole/post-access', {
|
|
585
|
+
tabs: queryform,
|
|
586
|
+
l : $(".viewtab").length,
|
|
587
|
+
id: "<%- id %>",
|
|
588
|
+
table : e.target.getAttribute('data-table')
|
|
589
|
+
},((data) => {
|
|
590
|
+
toastrForm(data);
|
|
591
|
+
$(".btn-close").click();
|
|
592
|
+
}))
|
|
593
|
+
})
|
|
594
|
+
|
|
595
|
+
$(".btn-save-approval").on("click",(e) => {
|
|
596
|
+
let queryform = $("#approvalform").serializeArray();
|
|
597
|
+
ajaxPost('/zrole/post-approval', {
|
|
598
|
+
datas: queryform,
|
|
599
|
+
id: "<%- id %>",
|
|
600
|
+
table : e.target.getAttribute('data-table')
|
|
601
|
+
},((data) => {
|
|
602
|
+
toastrForm(data);
|
|
603
|
+
$(".btn-close").click();
|
|
604
|
+
}))
|
|
605
|
+
})
|
|
606
|
+
</script>
|
|
607
|
+
`
|
|
608
|
+
|
|
609
|
+
module.exports = router
|