zet-lib 1.0.55 → 1.0.57
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/Form.js +666 -649
- package/lib/zRoleRouter.js +100 -19
- package/lib/zRoute.js +52 -1
- package/package.json +1 -1
package/lib/zRoleRouter.js
CHANGED
|
@@ -144,34 +144,101 @@ router.post('/create', async (req, res) => {
|
|
|
144
144
|
})
|
|
145
145
|
|
|
146
146
|
router.post('/tab-access', async (req, res) => {
|
|
147
|
-
console.log(req.body)
|
|
148
147
|
let json = Util.jsonSuccess()
|
|
149
148
|
let html = ''
|
|
149
|
+
const checkedFn = (obj, name, index) => {
|
|
150
|
+
return obj.hasOwnProperty(name) && obj[name][index] ? 'checked' : ''
|
|
151
|
+
}
|
|
150
152
|
try {
|
|
151
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] || {}
|
|
152
163
|
let result = await connection.result({
|
|
153
164
|
table: 'zfields',
|
|
154
165
|
where: {
|
|
155
166
|
table: table,
|
|
156
167
|
},
|
|
157
168
|
})
|
|
158
|
-
html
|
|
159
|
-
|
|
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) => {
|
|
160
172
|
html += `<tr>
|
|
161
173
|
<td>${item}</td>
|
|
162
|
-
<td><input type="checkbox" name="${
|
|
163
|
-
<td><input type="checkbox" name="${
|
|
164
|
-
<td><input type="checkbox" name="${
|
|
165
|
-
<td><input type="checkbox" name="${
|
|
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>
|
|
166
178
|
</tr>`
|
|
167
179
|
})
|
|
168
|
-
html += `</table>`
|
|
180
|
+
html += `</tbody></table>`
|
|
181
|
+
html += `</form>`
|
|
169
182
|
} catch (e) {
|
|
170
183
|
json = Util.flashError(e.toString())
|
|
171
184
|
}
|
|
172
185
|
res.send(html)
|
|
173
186
|
})
|
|
174
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
|
+
|
|
175
242
|
router.delete('/delete/:id', async (req, res) => {
|
|
176
243
|
let json = Util.jsonSuccess()
|
|
177
244
|
let id = parseInt(req.params.id)
|
|
@@ -222,10 +289,7 @@ const body = `<div class="">
|
|
|
222
289
|
<label class="control-label col-md-2" for="role-role_name">Role Name</label>
|
|
223
290
|
<div class="col-md-10"><select id="roleName" class="form-control form-select mb-3" name="name">
|
|
224
291
|
<% for(var i = 0;i < results.length;i++){ %>
|
|
225
|
-
<option value="<%- results[i].id %>"
|
|
226
|
-
<% if(id == results[i].id){ %> selected=""
|
|
227
|
-
<% } %>
|
|
228
|
-
><%- results[i].name %></option>
|
|
292
|
+
<option value="<%- results[i].id %>" <% if(id == results[i].id){ %> selected="" <% } %> ><%- results[i].name %></option>
|
|
229
293
|
<% } %>
|
|
230
294
|
</select></div>
|
|
231
295
|
</div>
|
|
@@ -248,9 +312,7 @@ const body = `<div class="">
|
|
|
248
312
|
</td>
|
|
249
313
|
<% for(var x = 0;x < actions.length;x++) { %>
|
|
250
314
|
<td><input name="params[<%= ename %>][<%= actions[x] %>]" class="<%= actions[x] %>"
|
|
251
|
-
<% if(json && json.hasOwnProperty(ename) && json[ename].indexOf(actions[x]) >= 0) { %> <%= 'checked="checked"' %>
|
|
252
|
-
<% } %>
|
|
253
|
-
title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox">
|
|
315
|
+
<% if(json && json.hasOwnProperty(ename) && json[ename].indexOf(actions[x]) >= 0) { %> <%= 'checked="checked"' %> <% } %> title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox">
|
|
254
316
|
</td>
|
|
255
317
|
<% } %>
|
|
256
318
|
</tr>
|
|
@@ -305,11 +367,10 @@ const body = `<div class="">
|
|
|
305
367
|
</div>
|
|
306
368
|
</div>
|
|
307
369
|
</div>
|
|
308
|
-
|
|
309
370
|
<div class="modal fade" id="tabaccess" tabindex="-1" aria-labelledby="tabaccessModalLabel" aria-hidden="true">
|
|
310
371
|
<div class="modal-dialog">
|
|
311
372
|
<div class="modal-content">
|
|
312
|
-
<div class="modal-header"><h5 class="modal-title" id="tabaccesstitle">Tab Access
|
|
373
|
+
<div class="modal-header"><h5 class="modal-title" id="tabaccesstitle">Tab Access <span id="titleaccess"></span></h5>
|
|
313
374
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
314
375
|
</div>
|
|
315
376
|
<div class="modal-body body-access">
|
|
@@ -317,7 +378,7 @@ const body = `<div class="">
|
|
|
317
378
|
</div>
|
|
318
379
|
<div class="modal-footer">
|
|
319
380
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
320
|
-
<button type="button" class="btn btn-primary btn-
|
|
381
|
+
<button type="button" class="btn btn-primary btn-save-tab">Save changes</button>
|
|
321
382
|
</div>
|
|
322
383
|
</div>
|
|
323
384
|
</div>
|
|
@@ -334,6 +395,13 @@ const js = `<script>
|
|
|
334
395
|
$("input." + elm).prop("checked", false);
|
|
335
396
|
}
|
|
336
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
|
+
}
|
|
337
405
|
$("#roleName").on('change', function () {
|
|
338
406
|
location.href = "/zrole?id=" + $(this).val();
|
|
339
407
|
});
|
|
@@ -383,11 +451,24 @@ const js = `<script>
|
|
|
383
451
|
}
|
|
384
452
|
|
|
385
453
|
function handleAccess(table) {
|
|
386
|
-
|
|
454
|
+
$(".btn-save-tab").attr("data-table", table);
|
|
455
|
+
ajaxPost('/zrole/tab-access',{table:table, id: "<%- id %>"}, ((data) => {
|
|
387
456
|
$(".body-access").html(data);
|
|
388
457
|
}))
|
|
389
458
|
}
|
|
390
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
|
+
$(".btn-close").click();
|
|
470
|
+
}))
|
|
471
|
+
})
|
|
391
472
|
</script>
|
|
392
473
|
`
|
|
393
474
|
|
package/lib/zRoute.js
CHANGED
|
@@ -2494,6 +2494,7 @@ zRoute.moduleLib = (req, res, MYMODEL, relations, zForms = '', data = {}) => {
|
|
|
2494
2494
|
|
|
2495
2495
|
/*
|
|
2496
2496
|
Generate javascript code into runtime folder
|
|
2497
|
+
make it static js
|
|
2497
2498
|
*/
|
|
2498
2499
|
zRoute.generateJS = (req, res, MYMODEL, relations, zForms = '', data = {}) => {
|
|
2499
2500
|
//add additional script
|
|
@@ -2510,6 +2511,8 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = '', data = {}) => {
|
|
|
2510
2511
|
const MYMODELS = myCache.get('MYMODELS')
|
|
2511
2512
|
let widgets = MYMODEL.widgets,
|
|
2512
2513
|
widgetsArray = Object.keys(widgets) || []
|
|
2514
|
+
|
|
2515
|
+
let hasTabs = MYMODEL.hasOwnProperty('hasTabs') ? MYMODEL.hasTabs : false
|
|
2513
2516
|
let hasDatePicker = false
|
|
2514
2517
|
let hasNumber = false
|
|
2515
2518
|
let hasClockPicker = false
|
|
@@ -2658,6 +2661,39 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = '', data = {}) => {
|
|
|
2658
2661
|
}
|
|
2659
2662
|
}
|
|
2660
2663
|
|
|
2664
|
+
//if this module has tabs than call access
|
|
2665
|
+
if (hasTabs) {
|
|
2666
|
+
scriptForm += `$(()=> {
|
|
2667
|
+
ajaxPost('/ztab-access',{table:"${MYMODEL.table}"},(data)=> {
|
|
2668
|
+
let dataTabs = ${MYMODEL.hasOwnProperty('tabs') ? JSON.stringify(MYMODEL.tabs) : []};
|
|
2669
|
+
let viewAccess = data.view || [];
|
|
2670
|
+
let createAccess = data.create || [];
|
|
2671
|
+
let editAccess = data.edit || [];
|
|
2672
|
+
let deleteAccess = data.delete || [];
|
|
2673
|
+
dataTabs.map((_, index)=> {
|
|
2674
|
+
if(!deleteAccess[index]) {
|
|
2675
|
+
setTimeout(function () {$("#arr"+index).find(".icons-danger").remove();},1000)
|
|
2676
|
+
}
|
|
2677
|
+
if(!editAccess[index]) {
|
|
2678
|
+
$("#arr"+index).find("input").attr("disabled",true);
|
|
2679
|
+
$("#arr"+index).find("select").attr("disabled",true);
|
|
2680
|
+
}
|
|
2681
|
+
if(!createAccess[index]) {
|
|
2682
|
+
$("#arr"+index).find(".btn-plus").remove();
|
|
2683
|
+
$("#arr"+index).find("input").attr("disabled",true);
|
|
2684
|
+
$("#arr"+index).find("select").attr("disabled",true);
|
|
2685
|
+
}
|
|
2686
|
+
if(!viewAccess[index]) {
|
|
2687
|
+
$("#arr"+index).remove();
|
|
2688
|
+
$(".arr"+index).remove();
|
|
2689
|
+
}
|
|
2690
|
+
})
|
|
2691
|
+
});
|
|
2692
|
+
})
|
|
2693
|
+
|
|
2694
|
+
`
|
|
2695
|
+
}
|
|
2696
|
+
|
|
2661
2697
|
defaultScript = `${Util.newLine} $(function () {
|
|
2662
2698
|
$(".isfile").each(function (index, value) {
|
|
2663
2699
|
let filename = $(this).attr("data-filename") || "";
|
|
@@ -3164,6 +3200,21 @@ zRoute.buildForm = async (req, res) => {
|
|
|
3164
3200
|
res.send(tabledata)
|
|
3165
3201
|
}
|
|
3166
3202
|
|
|
3203
|
+
/*
|
|
3204
|
+
get tab access from table
|
|
3205
|
+
*/
|
|
3206
|
+
zRoute.tabAccess = async (req, res) => {
|
|
3207
|
+
try {
|
|
3208
|
+
let table = req.body.table
|
|
3209
|
+
let roles = myCache.get('ROLES')
|
|
3210
|
+
let roleId = res.locals.roleId
|
|
3211
|
+
let tabs = roles[roleId].tabs[table] || []
|
|
3212
|
+
res.json(tabs)
|
|
3213
|
+
} catch (e) {
|
|
3214
|
+
console.log(e)
|
|
3215
|
+
}
|
|
3216
|
+
}
|
|
3217
|
+
|
|
3167
3218
|
zRoute.buildFileModel = (json, index = 0) => {
|
|
3168
3219
|
//let text = `module.exports = { ${Util.newLine}`;
|
|
3169
3220
|
let text = ''
|
|
@@ -3351,7 +3402,7 @@ zRoute.modelsCache = async () => {
|
|
|
3351
3402
|
}
|
|
3352
3403
|
} catch (e) {
|
|
3353
3404
|
//debug(req,res,e.toString());
|
|
3354
|
-
console.log('modelsCache :', e
|
|
3405
|
+
console.log('modelsCache :', e + '')
|
|
3355
3406
|
}
|
|
3356
3407
|
|
|
3357
3408
|
return obj
|