zet-lib 1.0.1 → 1.0.3
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/Util.js +12 -73
- package/lib/connection.js +18 -16
- package/lib/index.js +1 -0
- package/lib/views/zrole/index.ejs +121 -0
- package/lib/views/zrole/indexjs.ejs +57 -0
- package/lib/zRole.js +12 -9
- package/lib/zRoleRouter.js +165 -0
- package/lib/zRoute.js +7 -6
- package/lib/zapp.js +73 -72
- package/package.json +1 -1
package/lib/Util.js
CHANGED
|
@@ -234,7 +234,6 @@ Util.replaceAll = function (str, find, replace) {
|
|
|
234
234
|
for (let i = 0; i < find.length; i++) {
|
|
235
235
|
if (str.indexOf(find[i]) > -1) {
|
|
236
236
|
t = str.replace(new RegExp(Util.escapeRegExp(find[i]), 'g'), replace);
|
|
237
|
-
//console.log(t)
|
|
238
237
|
}
|
|
239
238
|
}
|
|
240
239
|
} else {
|
|
@@ -502,25 +501,7 @@ Util.getKey = function (obj, field) {
|
|
|
502
501
|
return t;
|
|
503
502
|
};
|
|
504
503
|
|
|
505
|
-
/**
|
|
506
|
-
* Camelize a string, cutting the string by multiple separators like
|
|
507
|
-
* hyphens, underscores and spaces.
|
|
508
|
-
*
|
|
509
|
-
* @param {text} string Text to camelize
|
|
510
|
-
* @return string Camelized text
|
|
511
|
-
*
|
|
512
|
-
* // someDatabaseFieldName
|
|
513
|
-
console.log(camelize("some_database_field_name"));
|
|
514
504
|
|
|
515
|
-
// someLabelThatNeedsToBeCamelized
|
|
516
|
-
console.log(camelize("Some label that needs to be camelized"));
|
|
517
|
-
|
|
518
|
-
// someJavascriptProperty
|
|
519
|
-
console.log(camelize("some-javascript-property"));
|
|
520
|
-
|
|
521
|
-
// someMixedStringWithSpacesUnderscoresAndHyphens
|
|
522
|
-
console.log(camelize("some-mixed_string with spaces_underscores-and-hyphens"));
|
|
523
|
-
*/
|
|
524
505
|
Util.camelize = function (text) {
|
|
525
506
|
return text.replace(/^([A-Z])|[\s-_]+(\w)/g, function (match, p1, p2, offset) {
|
|
526
507
|
if (p2) return p2.toUpperCase();
|
|
@@ -528,28 +509,13 @@ Util.camelize = function (text) {
|
|
|
528
509
|
});
|
|
529
510
|
}
|
|
530
511
|
|
|
531
|
-
/**
|
|
532
|
-
* Decamelizes a string with/without a custom separator (underscore by default).
|
|
533
|
-
*
|
|
534
|
-
* @param str String in camelcase
|
|
535
|
-
* @param separator Separator for the new decamelized string.
|
|
536
|
-
*
|
|
537
|
-
* // some database field name (separate with an empty space)
|
|
538
|
-
console.log(decamelize("someDatabaseFieldName", " "));
|
|
539
|
-
|
|
540
|
-
// some-label-that-needs-to-be-camelized (separate with an hyphen)
|
|
541
|
-
console.log(decamelize("someLabelThatNeedsToBeCamelized", "-"));
|
|
542
|
-
|
|
543
|
-
// some_javascript_property (separate with underscore)
|
|
544
|
-
console.log(decamelize("someJavascriptPraroperty", "_"));
|
|
545
|
-
*/
|
|
546
512
|
Util.decamelize = function (str, separator) {
|
|
547
513
|
separator = typeof separator === 'undefined' ? '_' : separator;
|
|
548
514
|
return str
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
515
|
+
.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
|
|
516
|
+
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2')
|
|
517
|
+
.replace("_", separator)
|
|
518
|
+
.toLowerCase();
|
|
553
519
|
}
|
|
554
520
|
|
|
555
521
|
/*
|
|
@@ -608,20 +574,20 @@ Util.asyncWrap = (fn) => {
|
|
|
608
574
|
Util.capitalizeAfterSpace = function (str) {
|
|
609
575
|
str = Util.replaceAll(str, "_", " ");
|
|
610
576
|
return str.replace(
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
577
|
+
/\w\S*/g,
|
|
578
|
+
function (txt) {
|
|
579
|
+
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
|
|
580
|
+
}
|
|
615
581
|
);
|
|
616
582
|
};
|
|
617
583
|
|
|
618
584
|
Util.capitalizeAfterSpaceTitle = function (str) {
|
|
619
585
|
str = Util.replaceAll(str, " ", "_");
|
|
620
586
|
return str.replace(
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
587
|
+
/\w\S*/g,
|
|
588
|
+
function (txt) {
|
|
589
|
+
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
|
|
590
|
+
}
|
|
625
591
|
);
|
|
626
592
|
};
|
|
627
593
|
|
|
@@ -682,21 +648,6 @@ Util.formatNumber = function (num, thousandSeparator='.') {
|
|
|
682
648
|
}
|
|
683
649
|
};
|
|
684
650
|
|
|
685
|
-
Util.dumpError = function (err) {
|
|
686
|
-
if (typeof err === 'object') {
|
|
687
|
-
if (err.message) {
|
|
688
|
-
console.log('\nMessage: ' + err.message)
|
|
689
|
-
}
|
|
690
|
-
if (err.stack) {
|
|
691
|
-
console.log('\nStacktrace:')
|
|
692
|
-
console.log('====================')
|
|
693
|
-
console.log(err.stack);
|
|
694
|
-
}
|
|
695
|
-
} else {
|
|
696
|
-
console.log(err);
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
|
|
700
651
|
Util.fileAttribute = function (filename) {
|
|
701
652
|
filename = filename.toLowerCase() || "";
|
|
702
653
|
let ext = filename.split('.').pop();
|
|
@@ -751,7 +702,6 @@ Util.fileView = function (dir, file, attributes={}) {
|
|
|
751
702
|
let withIcon = attributes.hasOwnProperty('withIcon') ? true : false;
|
|
752
703
|
let obj = Util.fileExtension(filename);
|
|
753
704
|
let className = attributes.hasOwnProperty('class') ? ` class="${attributes.class}" ` : '';
|
|
754
|
-
//console.log(JSON.stringify(obj))
|
|
755
705
|
if(filename.includes('https')) {
|
|
756
706
|
html = `<img src="${file}" ${className} class="img-responsive">`
|
|
757
707
|
} else {
|
|
@@ -1046,7 +996,6 @@ Util.readFile = function (filename) {
|
|
|
1046
996
|
return data;
|
|
1047
997
|
}
|
|
1048
998
|
} catch (err) {
|
|
1049
|
-
console.error(err)
|
|
1050
999
|
}
|
|
1051
1000
|
return ""
|
|
1052
1001
|
|
|
@@ -1056,7 +1005,6 @@ Util.dirExist = (dir, create = false) =>{
|
|
|
1056
1005
|
try {
|
|
1057
1006
|
if(create) {
|
|
1058
1007
|
fs.ensureDir(dir, err => {
|
|
1059
|
-
console.log(err); // => null
|
|
1060
1008
|
});
|
|
1061
1009
|
}
|
|
1062
1010
|
// check if directory exists
|
|
@@ -1064,7 +1012,6 @@ Util.dirExist = (dir, create = false) =>{
|
|
|
1064
1012
|
return true;
|
|
1065
1013
|
}
|
|
1066
1014
|
} catch (e) {
|
|
1067
|
-
console.log(e);
|
|
1068
1015
|
}
|
|
1069
1016
|
return false;
|
|
1070
1017
|
};
|
|
@@ -1083,7 +1030,6 @@ Util.getAllFiles = (dir) => {
|
|
|
1083
1030
|
files = fs.readdirSync(dir);
|
|
1084
1031
|
}
|
|
1085
1032
|
} catch (e) {
|
|
1086
|
-
console.log('error',e.toString());
|
|
1087
1033
|
return [];
|
|
1088
1034
|
}
|
|
1089
1035
|
return files;
|
|
@@ -1096,7 +1042,6 @@ Util.writeFile = (filename, content) => {
|
|
|
1096
1042
|
fs.writeFileSync(filename, content);
|
|
1097
1043
|
return true
|
|
1098
1044
|
} catch (e) {
|
|
1099
|
-
console.log(e)
|
|
1100
1045
|
return false
|
|
1101
1046
|
}
|
|
1102
1047
|
};
|
|
@@ -1106,13 +1051,11 @@ Util.deleteAllFiles = (dir) => {
|
|
|
1106
1051
|
fs.emptyDirSync(dir);
|
|
1107
1052
|
return true
|
|
1108
1053
|
} catch (e) {
|
|
1109
|
-
console.log(e)
|
|
1110
1054
|
return false
|
|
1111
1055
|
}
|
|
1112
1056
|
};
|
|
1113
1057
|
|
|
1114
1058
|
Util.findFilesName = (arr,filename) => {
|
|
1115
|
-
console.log(filename)
|
|
1116
1059
|
arr = arr || [];
|
|
1117
1060
|
return arr.filter((item) => item.includes(filename));
|
|
1118
1061
|
};
|
|
@@ -1132,12 +1075,8 @@ Util.getFiles = function (dir, token = "") {
|
|
|
1132
1075
|
<p class="text-ellipsis ui-selectee">${item}</p>
|
|
1133
1076
|
</div>`;
|
|
1134
1077
|
} else {
|
|
1135
|
-
console.log(dir);
|
|
1136
1078
|
let explode = dir.split(token);
|
|
1137
|
-
console.log(token)
|
|
1138
1079
|
let path = explode[1] || "";
|
|
1139
|
-
|
|
1140
|
-
console.log(path)
|
|
1141
1080
|
let state = "";
|
|
1142
1081
|
if (path == "") {
|
|
1143
1082
|
state = "/" + item;
|
package/lib/connection.js
CHANGED
|
@@ -156,6 +156,7 @@ connection.insert = async(obj) => {
|
|
|
156
156
|
console.log(sql);
|
|
157
157
|
console.log(arr);
|
|
158
158
|
console.log(e.toString());
|
|
159
|
+
throw e;
|
|
159
160
|
}
|
|
160
161
|
};
|
|
161
162
|
|
|
@@ -196,21 +197,15 @@ connection.update = async(obj) => {
|
|
|
196
197
|
const sql = `UPDATE "${table}" SET ${dataArr.join(", ")} ${wheres} RETURNING *`;
|
|
197
198
|
/* console.log(sql);
|
|
198
199
|
console.log(arr);*/
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
console.log("Error on Update ");
|
|
209
|
-
console.log(err)
|
|
210
|
-
return {}
|
|
211
|
-
}
|
|
212
|
-
*/
|
|
213
|
-
|
|
200
|
+
try {
|
|
201
|
+
const result = await pool.query(sql, arr);
|
|
202
|
+
return result.rows[0];
|
|
203
|
+
} catch (e) {
|
|
204
|
+
console.log(sql);
|
|
205
|
+
console.log(arr);
|
|
206
|
+
console.log(e.toString());
|
|
207
|
+
throw e;
|
|
208
|
+
}
|
|
214
209
|
};
|
|
215
210
|
|
|
216
211
|
connection.delete = async(obj) => {
|
|
@@ -228,7 +223,14 @@ connection.delete = async(obj) => {
|
|
|
228
223
|
const sql = `DELETE FROM "${table}" ${wheres}`
|
|
229
224
|
/*console.log(sql);
|
|
230
225
|
console.log(arr)*/
|
|
231
|
-
|
|
226
|
+
try {
|
|
227
|
+
return await pool.query(sql, arr);
|
|
228
|
+
} catch (e) {
|
|
229
|
+
console.log(sql);
|
|
230
|
+
console.log(arr);
|
|
231
|
+
console.log(e.toString());
|
|
232
|
+
throw e;
|
|
233
|
+
}
|
|
232
234
|
}
|
|
233
235
|
|
|
234
236
|
connection.driver = config.driver;
|
package/lib/index.js
CHANGED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
<div class="">
|
|
2
|
+
<div class="page-header"><h1>Roles</h1></div>
|
|
3
|
+
<div class="card panel panel-info boxy">
|
|
4
|
+
<div class="panel-heading">
|
|
5
|
+
<div class="float-end">
|
|
6
|
+
<div class="summary">
|
|
7
|
+
<% if(levels.delete) {%>
|
|
8
|
+
<span
|
|
9
|
+
class="icon-small icons-danger" title="Delete role" onclick="deleterole()"><img class="icons-bg-white icon-image"
|
|
10
|
+
src="/assets/icons/trash.svg"></span>
|
|
11
|
+
<%}%>
|
|
12
|
+
<% if(levels.update) {%>
|
|
13
|
+
<span class="icon-small icons-primary" data-bs-toggle="modal"
|
|
14
|
+
data-bs-target="#renameModal" title="rename"><img
|
|
15
|
+
class="icons-bg-white icon-image" src="/assets/icons/edit.svg"></span>
|
|
16
|
+
<%}%>
|
|
17
|
+
<% if(levels.create) {%>
|
|
18
|
+
<span
|
|
19
|
+
class="icon-small icons-success" title="Add a new role" data-bs-toggle="modal"
|
|
20
|
+
data-bs-target="#addModal"><img class="icons-bg-white icon-image"
|
|
21
|
+
src="/assets/icons/plus.svg"></span>
|
|
22
|
+
<%}%>
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
</div>
|
|
26
|
+
</div>
|
|
27
|
+
<h3 class="panel-title"><i class="fa fa-cog"></i> Settings</h3>
|
|
28
|
+
<div class="clearfix"></div>
|
|
29
|
+
</div>
|
|
30
|
+
<div class="kv-panel-before">
|
|
31
|
+
<div class="row">
|
|
32
|
+
<form id="role-form" class="form-horizontal kv-form-horizontal" method="post"><input type="hidden"
|
|
33
|
+
name="_csrf"
|
|
34
|
+
value="<%- csrfToken %>">
|
|
35
|
+
<div class="form-group field-role-role_name"><label class="control-label col-md-2"
|
|
36
|
+
for="role-role_name">Role Name</label>
|
|
37
|
+
<div class="col-md-10"><select id="roleName" class="form-control form-select mb-3" name="name">
|
|
38
|
+
<% for(var i = 0;i < results.length;i++){ %>
|
|
39
|
+
<option value="<%- results[i].id %>"
|
|
40
|
+
<% if(id == results[i].id){ %> selected=""
|
|
41
|
+
<% } %>
|
|
42
|
+
><%- results[i].name %></option>
|
|
43
|
+
<% } %>
|
|
44
|
+
</select></div>
|
|
45
|
+
</div>
|
|
46
|
+
<table class="table table-responsive">
|
|
47
|
+
<thead>
|
|
48
|
+
<tr>
|
|
49
|
+
<th>Name</th>
|
|
50
|
+
<% for(var i = 0;i < actions.length;i++) { %>
|
|
51
|
+
<th><%= actions[i] %> <input onclick='checkthis("<%= actions[i] %>")' type="checkbox"
|
|
52
|
+
id="all<%= actions[i] %>"></th>
|
|
53
|
+
<% } %>
|
|
54
|
+
</tr>
|
|
55
|
+
</thead>
|
|
56
|
+
<tbody>
|
|
57
|
+
<% for(var i = 0;i < routes.length;i++) { %>
|
|
58
|
+
<tr>
|
|
59
|
+
<td>
|
|
60
|
+
<% var ename = routes[i] %>
|
|
61
|
+
<%= ename %></td>
|
|
62
|
+
<% for(var x = 0;x < actions.length;x++) { %>
|
|
63
|
+
<td><input name="params[<%= ename %>][<%= actions[x] %>]" class="<%= actions[x] %>"
|
|
64
|
+
<% if(json && json.hasOwnProperty(ename) && json[ename].indexOf(actions[x]) >= 0) { %> <%= 'checked="checked"' %>
|
|
65
|
+
<% } %>
|
|
66
|
+
title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox"></td>
|
|
67
|
+
<% } %>
|
|
68
|
+
</tr>
|
|
69
|
+
<% } %>
|
|
70
|
+
</tbody>
|
|
71
|
+
</table>
|
|
72
|
+
<div class="row">
|
|
73
|
+
<div class="col-md-10 col-md-offset-1">
|
|
74
|
+
<% if(levels.update) {%>
|
|
75
|
+
<button type="submit" class="btn btn-primary">Update</button>
|
|
76
|
+
<%}%>
|
|
77
|
+
</div>
|
|
78
|
+
</div>
|
|
79
|
+
</form>
|
|
80
|
+
</div>
|
|
81
|
+
</div>
|
|
82
|
+
</div>
|
|
83
|
+
</div><!-- Modal -->
|
|
84
|
+
<div class="modal fade" id="renameModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
|
|
85
|
+
<div class="modal-dialog">
|
|
86
|
+
<div class="modal-content">
|
|
87
|
+
<div class="modal-header"><h5 class="modal-title" id="exampleModalLabel">Rename title</h5>
|
|
88
|
+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
89
|
+
</div>
|
|
90
|
+
<div class="modal-body"><input type="text" class="form-control" id="rename" name="rename"
|
|
91
|
+
value="<%- model[0].name %>"></div>
|
|
92
|
+
<div class="modal-footer">
|
|
93
|
+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
94
|
+
<button type="button" class="btn btn-primary btn-update">Save changes</button>
|
|
95
|
+
</div>
|
|
96
|
+
</div>
|
|
97
|
+
</div>
|
|
98
|
+
</div><!-- Modal -->
|
|
99
|
+
<div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="addModalLabel" aria-hidden="true">
|
|
100
|
+
<div class="modal-dialog">
|
|
101
|
+
<div class="modal-content">
|
|
102
|
+
<div class="modal-header"><h5 class="modal-title" id="exampleModalLabel">Add a New Role</h5>
|
|
103
|
+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
104
|
+
</div>
|
|
105
|
+
<div class="modal-body">
|
|
106
|
+
<div class="form-group mb-3"><label for="ruas_sk">Role Name</label> <input type="text"
|
|
107
|
+
class="form-control"
|
|
108
|
+
id="role_name"
|
|
109
|
+
name="role_name"
|
|
110
|
+
placeholder="Role Name">
|
|
111
|
+
</div>
|
|
112
|
+
</div>
|
|
113
|
+
<div class="modal-footer">
|
|
114
|
+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
115
|
+
<button type="button" class="btn btn-primary btn-add">Save changes</button>
|
|
116
|
+
</div>
|
|
117
|
+
</div>
|
|
118
|
+
</div>
|
|
119
|
+
</div>
|
|
120
|
+
|
|
121
|
+
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
function checkthis(elm) {
|
|
3
|
+
var cElem = $("#all" + elm);
|
|
4
|
+
if (cElem.is(":checked")) {
|
|
5
|
+
$("input." + elm).prop("checked", true);
|
|
6
|
+
} else {
|
|
7
|
+
$("input." + elm).prop("checked", false);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
$("#roleName").on('change', function () {
|
|
11
|
+
location.href = "/zrole?id=" + $(this).val();
|
|
12
|
+
});
|
|
13
|
+
var form = document.getElementById("role-form");
|
|
14
|
+
form.onsubmit = function (ev) {
|
|
15
|
+
ev.preventDefault();
|
|
16
|
+
var url = '/zrole/update/<%= id%>';
|
|
17
|
+
ajaxPost(url, $(this).serialize(), function (data) {
|
|
18
|
+
if (data.status == 1) {
|
|
19
|
+
toastr.success('Success', 'Updated Role');
|
|
20
|
+
} else {
|
|
21
|
+
toastr.error('Error!',data.data);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
$(".btn-update").on("click", function () {
|
|
26
|
+
ajaxPost('/zrole/rename/<%= id%>',{
|
|
27
|
+
rename : $("#rename").val()
|
|
28
|
+
}, function (data) {
|
|
29
|
+
toastrForm(data);
|
|
30
|
+
setTimeout(function () {
|
|
31
|
+
location.href= '';
|
|
32
|
+
},2000);
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
$(".btn-add").on("click", function () {
|
|
37
|
+
ajaxPost('/zrole/create/',{
|
|
38
|
+
name : $("#role_name").val()
|
|
39
|
+
}, function (data) {
|
|
40
|
+
toastrForm(data);
|
|
41
|
+
setTimeout(function () {
|
|
42
|
+
location.href= '';
|
|
43
|
+
},2000);
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
function deleterole() {
|
|
47
|
+
if(window.confirm('delete role selected ? ')) {
|
|
48
|
+
let id = "<%= id%>";
|
|
49
|
+
ajaxDelete('/zrole/delete/<%= id%>',{id:id}, function (data) {
|
|
50
|
+
toastrForm(data);
|
|
51
|
+
setTimeout(function () {
|
|
52
|
+
location.href= '/zrole';
|
|
53
|
+
},2000);
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
</script>
|
package/lib/zRole.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const Util = require('./Util');
|
|
2
2
|
const fs = require("fs");
|
|
3
3
|
const myCache = require("./cache");
|
|
4
|
+
const zRoute = require('./zRoute');
|
|
4
5
|
|
|
5
6
|
const a = {};
|
|
6
7
|
|
|
@@ -8,7 +9,7 @@ const a = {};
|
|
|
8
9
|
Please add your routes here
|
|
9
10
|
*/
|
|
10
11
|
|
|
11
|
-
const routes =
|
|
12
|
+
const routes = zRoute.ROUTES();
|
|
12
13
|
const cacheRoutes = myCache.get("ROUTES");
|
|
13
14
|
const cacheRoles = myCache.get("ROLES") || {};
|
|
14
15
|
|
|
@@ -18,7 +19,6 @@ if(cacheRoutes && cacheRoutes.length) {
|
|
|
18
19
|
a.routes = process.env.NODE_ENV == "production" ? Util.arrayDeletes(routes,["auth","test"]) : Util.arrayDeletes(routes,["generator","auth","test"]);
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
|
|
22
22
|
/*
|
|
23
23
|
Default actions
|
|
24
24
|
you can additional here
|
|
@@ -31,10 +31,10 @@ a.actions = ['index', 'create', 'update', 'delete', 'view', 'import', 'export','
|
|
|
31
31
|
|
|
32
32
|
a.params = function (roleId) {
|
|
33
33
|
let cacheRoles = myCache.get("ROLES");
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
if(cacheRoles && cacheRoles.hasOwnProperty(roleId)) {
|
|
35
|
+
return roleId ? cacheRoles[roleId].params : {};
|
|
36
|
+
}
|
|
37
|
+
return {}
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
a.rules = function (roleId) {
|
|
@@ -65,10 +65,14 @@ a.levels = (route, params) => {
|
|
|
65
65
|
obj[a.actions[i]] = true;
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
-
|
|
69
68
|
return obj;
|
|
70
69
|
};
|
|
71
70
|
|
|
71
|
+
a.myLevel = (req, res, table) => {
|
|
72
|
+
const levels = a.levels(table, a.routes.includes(table) ? a.rules(res.locals.roleId) : {});
|
|
73
|
+
return levels;
|
|
74
|
+
}
|
|
75
|
+
|
|
72
76
|
a.menuAccess = (res, menu) => {
|
|
73
77
|
if(Array.isArray(menu)) {
|
|
74
78
|
let isTrue = false;
|
|
@@ -129,9 +133,8 @@ a.access = (req, res, next) => {
|
|
|
129
133
|
req.session.sessionFlash = Util.flashError(LANGUAGE.no_access);
|
|
130
134
|
res.redirect(`${process.env.APP_AFTER_LOGIN}?setup=role`)
|
|
131
135
|
} else {
|
|
132
|
-
res.redirect(`${process.env.
|
|
136
|
+
res.redirect(`${process.env.APP_AFTER_LOGIN}`)
|
|
133
137
|
}
|
|
134
|
-
|
|
135
138
|
}
|
|
136
139
|
}
|
|
137
140
|
};
|
|
@@ -0,0 +1,165 @@
|
|
|
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 = './';
|
|
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
|
+
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');
|
|
35
|
+
//inject to end body
|
|
36
|
+
moduleLib.addModule(req, res, Util.readFile(path.join(dirname,'views/zrole/indexjs.ejs')));
|
|
37
|
+
res.render("layouts/"+layout, {
|
|
38
|
+
model: model,
|
|
39
|
+
table: "zrole",
|
|
40
|
+
id: id,
|
|
41
|
+
actions: zRole.actions,
|
|
42
|
+
routes: routes,
|
|
43
|
+
levels:myLevel,
|
|
44
|
+
json: json,
|
|
45
|
+
results: results,
|
|
46
|
+
csrfToken: req.csrfToken(),
|
|
47
|
+
bodyHTML :ejs.render(Util.readFile(path.join(dirname,'views/zrole/index.ejs')),{
|
|
48
|
+
model: model,
|
|
49
|
+
table: "zrole",
|
|
50
|
+
id: id,
|
|
51
|
+
actions: zRole.actions,
|
|
52
|
+
routes: routes,
|
|
53
|
+
levels:myLevel,
|
|
54
|
+
json: json,
|
|
55
|
+
results: results,
|
|
56
|
+
csrfToken: req.csrfToken(),
|
|
57
|
+
})
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
router.post('/update/:id',async function (req, res, next) {
|
|
62
|
+
const data = {};
|
|
63
|
+
const name = req.body.name;
|
|
64
|
+
const params = req.body.params;
|
|
65
|
+
const newKey = {};
|
|
66
|
+
Object.keys(params).map( (key) => {
|
|
67
|
+
const arr = [];
|
|
68
|
+
for (const k in params[key]) {
|
|
69
|
+
arr.push(k);
|
|
70
|
+
}
|
|
71
|
+
newKey[key] = arr;
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const json = {};
|
|
75
|
+
json.params = JSON.stringify(newKey);
|
|
76
|
+
try {
|
|
77
|
+
await connection.update({
|
|
78
|
+
table : "zrole",
|
|
79
|
+
data : {
|
|
80
|
+
params : json.params
|
|
81
|
+
},
|
|
82
|
+
where : {
|
|
83
|
+
id : req.params.id
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
data.status = 1;
|
|
87
|
+
data.data = 1;
|
|
88
|
+
await zCache.ROLES();
|
|
89
|
+
|
|
90
|
+
if(env == "production") {
|
|
91
|
+
pm2.connect(function (err) {
|
|
92
|
+
if (err) {
|
|
93
|
+
//console.log(err.toString());
|
|
94
|
+
}
|
|
95
|
+
pm2.restart(process.env.PM2_NAME, (err, proc) => {
|
|
96
|
+
//io.to(room).emit("message","Restart done")
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
res.json(data);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
data.status = 0;
|
|
103
|
+
data.data = error;
|
|
104
|
+
res.json(data);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
router.post('/rename/:id', async (req,res) => {
|
|
109
|
+
let json = Util.jsonSuccess();
|
|
110
|
+
try {
|
|
111
|
+
const id = req.params.id;
|
|
112
|
+
const rename = req.body.rename;
|
|
113
|
+
await connection.update({
|
|
114
|
+
table : "zrole",
|
|
115
|
+
where : {
|
|
116
|
+
id : id
|
|
117
|
+
},
|
|
118
|
+
data : {name : rename}
|
|
119
|
+
})
|
|
120
|
+
} catch (e) {
|
|
121
|
+
json = Util.flashError(e.toString())
|
|
122
|
+
}
|
|
123
|
+
res.json(json);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
router.post('/create', async (req,res) => {
|
|
127
|
+
let json = Util.jsonSuccess();
|
|
128
|
+
try {
|
|
129
|
+
const name = req.body.name;
|
|
130
|
+
await connection.insert({
|
|
131
|
+
table : "zrole",
|
|
132
|
+
data : {
|
|
133
|
+
name : name
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
await zCache.ROLES();
|
|
137
|
+
} catch (e) {
|
|
138
|
+
json = Util.flashError(e.toString())
|
|
139
|
+
}
|
|
140
|
+
res.json(json);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
router.delete('/delete/:id', async (req,res) => {
|
|
144
|
+
let json = Util.jsonSuccess();
|
|
145
|
+
let id = parseInt(req.params.id);
|
|
146
|
+
if(id > 3){
|
|
147
|
+
try {
|
|
148
|
+
const name = req.body.name;
|
|
149
|
+
await connection.delete({
|
|
150
|
+
table : "zrole",
|
|
151
|
+
where : {
|
|
152
|
+
id : id
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
await zCache.ROLES();
|
|
156
|
+
} catch (e) {
|
|
157
|
+
json = Util.flashError(e.toString())
|
|
158
|
+
}
|
|
159
|
+
} else {
|
|
160
|
+
json = Util.flashError('Delete error, not allowed');
|
|
161
|
+
}
|
|
162
|
+
res.json(json);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
module.exports = router;
|
package/lib/zRoute.js
CHANGED
|
@@ -43,9 +43,10 @@ zRoute.ROUTES = () => {
|
|
|
43
43
|
let arr = [];
|
|
44
44
|
const dir = `${dirRoot}/routes`;
|
|
45
45
|
let routes = Util.getAllFiles(dir);
|
|
46
|
+
let nots = ['index','zindex','auth'];
|
|
46
47
|
for (const item of routes) {
|
|
47
48
|
let name = item.replace('.js', '');
|
|
48
|
-
if
|
|
49
|
+
if(!nots.includes(name)){
|
|
49
50
|
arr.push(name)
|
|
50
51
|
}
|
|
51
52
|
}
|
|
@@ -714,10 +715,10 @@ zRoute.dataTableFilter = (MYMODEL, relations, filter) => {
|
|
|
714
715
|
types[key] = 'input';
|
|
715
716
|
break;
|
|
716
717
|
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
718
|
+
/*case "json" :
|
|
719
|
+
dataTable[key] = `<input type="number" class="form-control form-control-sm" value="${value}" id="data_table_${key}" >`;
|
|
720
|
+
types[key] = 'input';
|
|
721
|
+
break;*/
|
|
721
722
|
|
|
722
723
|
case "virtual" :
|
|
723
724
|
dataTable[key] = ``;
|
|
@@ -2393,7 +2394,7 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
|
|
|
2393
2394
|
let obj = {};
|
|
2394
2395
|
const MYMODELS = myCache.get('MYMODELS');
|
|
2395
2396
|
let widgets = MYMODEL.widgets,
|
|
2396
|
-
|
|
2397
|
+
widgetsArray = Object.keys(widgets) || [];
|
|
2397
2398
|
let hasDatePicker = false;
|
|
2398
2399
|
let hasNumber = false;
|
|
2399
2400
|
let hasClockPicker = false;
|
package/lib/zapp.js
CHANGED
|
@@ -2,78 +2,79 @@ const config = require('dotenv').config();
|
|
|
2
2
|
const dirRoot = process.env.dirRoot;
|
|
3
3
|
const menuGenerator = require('./menuGenerator');
|
|
4
4
|
module.exports = (req,res,next) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
5
|
+
res.locals.renderHead = "";
|
|
6
|
+
res.locals.renderBody = "";
|
|
7
|
+
res.locals.bodyHTML = '';
|
|
8
|
+
res.locals.renderEnd = "";
|
|
9
|
+
res.locals.titleApp = process.env.APP_TITLE;
|
|
10
|
+
res.locals.descriptionApp = process.env.APP_DESCRIPTION;
|
|
11
|
+
res.locals.moduleHead = "";
|
|
12
|
+
res.locals.relationsVariable = "";
|
|
13
|
+
res.locals.moduleEnd = "";
|
|
14
|
+
res.locals.menuApp = "home";
|
|
15
|
+
res.locals.routeName = "index";
|
|
16
|
+
res.locals.userId = -1;
|
|
17
|
+
res.locals.csrfToken = "";
|
|
18
|
+
res.locals.roleId = 0;
|
|
19
|
+
res.locals.token = "guest";
|
|
20
|
+
res.locals.companyId = 0;
|
|
21
|
+
res.locals.userId = 0;
|
|
22
|
+
res.locals.userAvatar = "/img/user.png";
|
|
23
|
+
res.locals.zuser = {
|
|
24
|
+
fullname: 'test',
|
|
25
|
+
role: {name: "test"}
|
|
26
|
+
};
|
|
27
|
+
res.locals.frameworkcss = "bootstrap5";
|
|
28
|
+
res.locals.startup = 0;
|
|
29
|
+
global.frameworkcss = "bootstrap5";
|
|
30
|
+
global.LANGUAGE = require('./languages/lang_en');
|
|
31
|
+
res.locals.socketUrl = process.env.APP_URL;
|
|
32
|
+
res.locals.zcompanies = [];
|
|
33
|
+
res.locals.isLogin = false;
|
|
34
|
+
res.locals.objectStores = {};
|
|
35
|
+
res.locals.MENU = [];
|
|
36
|
+
res.locals.MENU_SYSTEMS = [];
|
|
37
|
+
res.locals.MENU_ALL = [];
|
|
38
|
+
if (req.session) {
|
|
39
|
+
let reqSession = req.session;
|
|
40
|
+
if (reqSession.hasOwnProperty('user')) {
|
|
41
|
+
const tUser = req.session.user;
|
|
42
|
+
if (tUser && Object.prototype.hasOwnProperty.call(tUser, "id")) {
|
|
43
|
+
tUser.role = {
|
|
44
|
+
name: "Admin"
|
|
25
45
|
};
|
|
26
|
-
res.locals.
|
|
27
|
-
res.locals.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
res.locals.
|
|
31
|
-
res.locals.
|
|
32
|
-
res.locals.
|
|
33
|
-
res.locals.
|
|
34
|
-
res.locals.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
name: "Admin"
|
|
44
|
-
};
|
|
45
|
-
res.locals.isLogin = true;
|
|
46
|
-
res.locals.token = tUser.token;
|
|
47
|
-
res.locals.roleId = tUser.role_id;
|
|
48
|
-
res.locals.isLogin = true;
|
|
49
|
-
res.locals.zuser = tUser;
|
|
50
|
-
res.locals.userId = tUser.id;
|
|
51
|
-
res.locals.companyId = tUser.company.id;
|
|
52
|
-
res.locals.userAvatar = tUser.image ? tUser.image.indexOf("http") > -1 ? tUser.image : "/uploads/zuser/" + tUser.image : "/img/user.png";
|
|
53
|
-
res.locals.zcompanies = tUser.companies;
|
|
54
|
-
if(tUser.language){
|
|
55
|
-
let objLanguage = {
|
|
56
|
-
1 : "lang_en",
|
|
57
|
-
2 : "lang_id",
|
|
58
|
-
3 : "lang_jp",
|
|
59
|
-
4 : "lang_fr"
|
|
60
|
-
};
|
|
61
|
-
global.LANGUAGE = require(`./languages/${objLanguage[tUser.language]}`);
|
|
62
|
-
}
|
|
63
|
-
global.layout = "two";
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
res.locals.MENU = menuGenerator.menu(req, res);
|
|
67
|
-
res.locals.MENU_SYSTEMS = menuGenerator.systems(req, res);
|
|
68
|
-
res.locals.MENU_ALL = menuGenerator.menuPlus(req, res);
|
|
46
|
+
res.locals.isLogin = true;
|
|
47
|
+
res.locals.token = tUser.token;
|
|
48
|
+
res.locals.roleId = tUser.role_id;
|
|
49
|
+
res.locals.isLogin = true;
|
|
50
|
+
res.locals.zuser = tUser;
|
|
51
|
+
res.locals.userId = tUser.id;
|
|
52
|
+
res.locals.companyId = tUser.company.id;
|
|
53
|
+
res.locals.userAvatar = tUser.image ? tUser.image.indexOf("http") > -1 ? tUser.image : "/uploads/zuser/" + tUser.image : "/img/user.png";
|
|
54
|
+
res.locals.zcompanies = tUser.companies;
|
|
55
|
+
if(tUser.language){
|
|
56
|
+
let objLanguage = {
|
|
57
|
+
1 : "lang_en",
|
|
58
|
+
2 : "lang_id",
|
|
59
|
+
3 : "lang_jp",
|
|
60
|
+
4 : "lang_fr"
|
|
61
|
+
};
|
|
62
|
+
global.LANGUAGE = require(`./languages/${objLanguage[tUser.language]}`);
|
|
69
63
|
}
|
|
70
|
-
global.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
64
|
+
global.layout = "two";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
res.locals.MENU = menuGenerator.menu(req, res);
|
|
68
|
+
res.locals.MENU_SYSTEMS = menuGenerator.systems(req, res);
|
|
69
|
+
res.locals.MENU_ALL = menuGenerator.menuPlus(req, res);
|
|
70
|
+
}
|
|
71
|
+
global.COMPANY_ID = res.locals.companyId;
|
|
72
|
+
global.USER_ID = res.locals.userId;
|
|
73
|
+
res.locals.LANGUAGE = global.LANGUAGE;
|
|
74
|
+
res.locals.currency = {};
|
|
75
|
+
res.locals.settings = {};
|
|
76
|
+
res.locals.sessionFlash = req.session.sessionFlash;
|
|
77
|
+
delete req.session.sessionFlash;
|
|
78
|
+
res.locals.sessionFlashc = req.session.sessionFlashc;
|
|
79
|
+
next();
|
|
79
80
|
};
|