zet-lib 1.0.54 → 1.0.55
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/zRoleRouter.js +192 -138
- package/package.json +1 -1
package/lib/zRoleRouter.js
CHANGED
|
@@ -1,160 +1,198 @@
|
|
|
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
|
|
27
|
-
where
|
|
28
|
-
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:
|
|
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
|
-
|
|
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(
|
|
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(
|
|
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
|
|
72
|
-
data
|
|
73
|
-
params
|
|
81
|
+
table: 'zrole',
|
|
82
|
+
data: {
|
|
83
|
+
params: json.params,
|
|
74
84
|
},
|
|
75
|
-
where
|
|
76
|
-
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 ==
|
|
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
|
|
108
|
-
where
|
|
109
|
-
id
|
|
117
|
+
table: 'zrole',
|
|
118
|
+
where: {
|
|
119
|
+
id: id,
|
|
110
120
|
},
|
|
111
|
-
data
|
|
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
|
|
125
|
-
data
|
|
126
|
-
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.
|
|
137
|
-
|
|
138
|
-
let
|
|
139
|
-
|
|
146
|
+
router.post('/tab-access', async (req, res) => {
|
|
147
|
+
console.log(req.body)
|
|
148
|
+
let json = Util.jsonSuccess()
|
|
149
|
+
let html = ''
|
|
150
|
+
try {
|
|
151
|
+
const table = req.body.table
|
|
152
|
+
let result = await connection.result({
|
|
153
|
+
table: 'zfields',
|
|
154
|
+
where: {
|
|
155
|
+
table: table,
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
html = `<table class="table table-hover"><thead><tr><th>Tab</th><th>View</th><th>Create</th><th>Edit</th><th>Delete</th></tr></thead>`
|
|
159
|
+
result.tabs.map((item) => {
|
|
160
|
+
html += `<tr>
|
|
161
|
+
<td>${item}</td>
|
|
162
|
+
<td><input type="checkbox" name="${table}___${item}___view" /></td>
|
|
163
|
+
<td><input type="checkbox" name="${table}___${item}___create" /></td>
|
|
164
|
+
<td><input type="checkbox" name="${table}___${item}___edit" /></td>
|
|
165
|
+
<td><input type="checkbox" name="${table}___${item}___delete" /></td>
|
|
166
|
+
</tr>`
|
|
167
|
+
})
|
|
168
|
+
html += `</table>`
|
|
169
|
+
} catch (e) {
|
|
170
|
+
json = Util.flashError(e.toString())
|
|
171
|
+
}
|
|
172
|
+
res.send(html)
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
router.delete('/delete/:id', async (req, res) => {
|
|
176
|
+
let json = Util.jsonSuccess()
|
|
177
|
+
let id = parseInt(req.params.id)
|
|
178
|
+
if (id > 3) {
|
|
140
179
|
try {
|
|
141
|
-
const name = req.body.name
|
|
180
|
+
const name = req.body.name
|
|
142
181
|
await connection.delete({
|
|
143
|
-
table
|
|
144
|
-
where
|
|
145
|
-
id
|
|
146
|
-
}
|
|
147
|
-
})
|
|
148
|
-
await zCache.ROLES()
|
|
182
|
+
table: 'zrole',
|
|
183
|
+
where: {
|
|
184
|
+
id: id,
|
|
185
|
+
},
|
|
186
|
+
})
|
|
187
|
+
await zCache.ROLES()
|
|
149
188
|
} catch (e) {
|
|
150
189
|
json = Util.flashError(e.toString())
|
|
151
190
|
}
|
|
152
191
|
} else {
|
|
153
|
-
json = Util.flashError('Delete error, not allowed')
|
|
192
|
+
json = Util.flashError('Delete error, not allowed')
|
|
154
193
|
}
|
|
155
|
-
res.json(json)
|
|
156
|
-
})
|
|
157
|
-
|
|
194
|
+
res.json(json)
|
|
195
|
+
})
|
|
158
196
|
|
|
159
197
|
const body = `<div class="">
|
|
160
198
|
<div class="page-header"><h1>Roles</h1></div>
|
|
@@ -163,23 +201,14 @@ const body = `<div class="">
|
|
|
163
201
|
<div class="float-end">
|
|
164
202
|
<div class="summary">
|
|
165
203
|
<% if(levels.delete) {%>
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
204
|
+
<span class="icon-small icons-danger" title="Delete role" onclick="deleterole()"><img class="icons-bg-white icon-image" src="/assets/icons/trash.svg"></span>
|
|
205
|
+
<%}%>
|
|
206
|
+
<% if(levels.update) {%>
|
|
207
|
+
<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>
|
|
208
|
+
<%}%>
|
|
209
|
+
<% if(levels.create) {%>
|
|
210
|
+
<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
211
|
<%}%>
|
|
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
212
|
</div>
|
|
184
213
|
</div>
|
|
185
214
|
<h3 class="panel-title"><i class="fa fa-cog"></i> Settings</h3>
|
|
@@ -187,11 +216,10 @@ const body = `<div class="">
|
|
|
187
216
|
</div>
|
|
188
217
|
<div class="kv-panel-before">
|
|
189
218
|
<div class="row">
|
|
190
|
-
<form id="role-form" class="form-horizontal kv-form-horizontal" method="post"
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
for="role-role_name">Role Name</label>
|
|
219
|
+
<form id="role-form" class="form-horizontal kv-form-horizontal" method="post">
|
|
220
|
+
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
|
|
221
|
+
<div class="form-group field-role-role_name">
|
|
222
|
+
<label class="control-label col-md-2" for="role-role_name">Role Name</label>
|
|
195
223
|
<div class="col-md-10"><select id="roleName" class="form-control form-select mb-3" name="name">
|
|
196
224
|
<% for(var i = 0;i < results.length;i++){ %>
|
|
197
225
|
<option value="<%- results[i].id %>"
|
|
@@ -206,8 +234,7 @@ const body = `<div class="">
|
|
|
206
234
|
<tr>
|
|
207
235
|
<th>Name</th>
|
|
208
236
|
<% 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>
|
|
237
|
+
<th><%= actions[i] %> <input onclick='checkthis("<%= actions[i] %>")' type="checkbox" id="all<%= actions[i] %>"></th>
|
|
211
238
|
<% } %>
|
|
212
239
|
</tr>
|
|
213
240
|
</thead>
|
|
@@ -215,13 +242,16 @@ const body = `<div class="">
|
|
|
215
242
|
<% for(var i = 0;i < routes.length;i++) { %>
|
|
216
243
|
<tr>
|
|
217
244
|
<td>
|
|
218
|
-
<% var ename = routes[i]
|
|
219
|
-
|
|
245
|
+
<% var ename = routes[i];
|
|
246
|
+
var titleName = tabs.includes(routes[i]) ? '<a class="handle-access" data-bs-toggle="modal" data-bs-target="#tabaccess" href="#">'+routes[i]+'</a>' : routes[i] %>
|
|
247
|
+
<%- titleName %>
|
|
248
|
+
</td>
|
|
220
249
|
<% for(var x = 0;x < actions.length;x++) { %>
|
|
221
250
|
<td><input name="params[<%= ename %>][<%= actions[x] %>]" class="<%= actions[x] %>"
|
|
222
251
|
<% if(json && json.hasOwnProperty(ename) && json[ename].indexOf(actions[x]) >= 0) { %> <%= 'checked="checked"' %>
|
|
223
252
|
<% } %>
|
|
224
|
-
title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox"
|
|
253
|
+
title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox">
|
|
254
|
+
</td>
|
|
225
255
|
<% } %>
|
|
226
256
|
</tr>
|
|
227
257
|
<% } %>
|
|
@@ -245,8 +275,9 @@ const body = `<div class="">
|
|
|
245
275
|
<div class="modal-header"><h5 class="modal-title" id="exampleModalLabel">Rename title</h5>
|
|
246
276
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
247
277
|
</div>
|
|
248
|
-
<div class="modal-body"
|
|
249
|
-
|
|
278
|
+
<div class="modal-body">
|
|
279
|
+
<input type="text" class="form-control" id="rename" name="rename" value="<%- model[0].name %>">
|
|
280
|
+
</div>
|
|
250
281
|
<div class="modal-footer">
|
|
251
282
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
252
283
|
<button type="button" class="btn btn-primary btn-update">Save changes</button>
|
|
@@ -261,11 +292,10 @@ const body = `<div class="">
|
|
|
261
292
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
262
293
|
</div>
|
|
263
294
|
<div class="modal-body">
|
|
264
|
-
<div class="form-group mb-3"
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
placeholder="Role Name">
|
|
295
|
+
<div class="form-group mb-3">
|
|
296
|
+
<label for="ruas_sk">Role Name</label>
|
|
297
|
+
<input type="text" class="form-control" id="role_name" name="role_name" placeholder="Role Name">
|
|
298
|
+
|
|
269
299
|
</div>
|
|
270
300
|
</div>
|
|
271
301
|
<div class="modal-footer">
|
|
@@ -276,8 +306,25 @@ const body = `<div class="">
|
|
|
276
306
|
</div>
|
|
277
307
|
</div>
|
|
278
308
|
|
|
309
|
+
<div class="modal fade" id="tabaccess" tabindex="-1" aria-labelledby="tabaccessModalLabel" aria-hidden="true">
|
|
310
|
+
<div class="modal-dialog">
|
|
311
|
+
<div class="modal-content">
|
|
312
|
+
<div class="modal-header"><h5 class="modal-title" id="tabaccesstitle">Tab Access </h5>
|
|
313
|
+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
314
|
+
</div>
|
|
315
|
+
<div class="modal-body body-access">
|
|
316
|
+
|
|
317
|
+
</div>
|
|
318
|
+
<div class="modal-footer">
|
|
319
|
+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
320
|
+
<button type="button" class="btn btn-primary btn-access">Save changes</button>
|
|
321
|
+
</div>
|
|
322
|
+
</div>
|
|
323
|
+
</div>
|
|
324
|
+
</div>
|
|
279
325
|
|
|
280
|
-
|
|
326
|
+
|
|
327
|
+
`
|
|
281
328
|
const js = `<script>
|
|
282
329
|
function checkthis(elm) {
|
|
283
330
|
var cElem = $("#all" + elm);
|
|
@@ -334,7 +381,14 @@ const js = `<script>
|
|
|
334
381
|
})
|
|
335
382
|
}
|
|
336
383
|
}
|
|
384
|
+
|
|
385
|
+
function handleAccess(table) {
|
|
386
|
+
ajaxPost('/zrole/tab-access',{table:table}, ((data) => {
|
|
387
|
+
$(".body-access").html(data);
|
|
388
|
+
}))
|
|
389
|
+
}
|
|
390
|
+
$(".handle-access").on("click", (e)=> handleAccess(e.target.text));
|
|
337
391
|
</script>
|
|
338
|
-
|
|
392
|
+
`
|
|
339
393
|
|
|
340
|
-
module.exports = router
|
|
394
|
+
module.exports = router
|