zet-lib 1.0.0
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 -0
- package/README.md +4 -0
- package/lib/Form.js +717 -0
- package/lib/Mail.js +70 -0
- package/lib/Modal.js +95 -0
- package/lib/UI.js +15 -0
- package/lib/Util.js +1242 -0
- package/lib/access.js +6 -0
- package/lib/cache.js +4 -0
- package/lib/connection.js +304 -0
- package/lib/debug.js +22 -0
- package/lib/index.js +29 -0
- package/lib/io.js +45 -0
- package/lib/languages/lang_en.js +115 -0
- package/lib/languages/lang_fr.js +114 -0
- package/lib/languages/lang_id.js +116 -0
- package/lib/languages/lang_jp.js +114 -0
- package/lib/menuGenerator.js +236 -0
- package/lib/moduleLib.js +427 -0
- package/lib/tableForm.js +10 -0
- package/lib/zCache.js +326 -0
- package/lib/zComponent.js +27 -0
- package/lib/zFn.js +59 -0
- package/lib/zFunction.js +20 -0
- package/lib/zMenuCollections.js +62 -0
- package/lib/zReport.js +930 -0
- package/lib/zRole.js +140 -0
- package/lib/zRoute.js +3226 -0
- package/lib/zTester.js +93 -0
- package/lib/zapp.js +79 -0
- package/lib/zdataTable.js +130 -0
- package/package.json +50 -0
package/lib/zRole.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const Util = require('./Util');
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const myCache = require("./cache");
|
|
4
|
+
|
|
5
|
+
const a = {};
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
Please add your routes here
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const routes = fs.readdirSync("./routes/").reduce((result, item) => [...result, item.replace(".js","")],[]);
|
|
12
|
+
const cacheRoutes = myCache.get("ROUTES");
|
|
13
|
+
const cacheRoles = myCache.get("ROLES") || {};
|
|
14
|
+
|
|
15
|
+
if(cacheRoutes && cacheRoutes.length) {
|
|
16
|
+
a.routes = process.env.NODE_ENV == "production" ? Util.arrayDeletes(cacheRoutes,["auth","test"]) : Util.arrayDeletes(cacheRoutes,["generator","auth","test"]);
|
|
17
|
+
} else {
|
|
18
|
+
a.routes = process.env.NODE_ENV == "production" ? Util.arrayDeletes(routes,["auth","test"]) : Util.arrayDeletes(routes,["generator","auth","test"]);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
/*
|
|
23
|
+
Default actions
|
|
24
|
+
you can additional here
|
|
25
|
+
*/
|
|
26
|
+
a.actions = ['index', 'create', 'update', 'delete', 'view', 'import', 'export','approval'];
|
|
27
|
+
|
|
28
|
+
/*
|
|
29
|
+
all in table roles
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
a.params = function (roleId) {
|
|
33
|
+
let cacheRoles = myCache.get("ROLES");
|
|
34
|
+
if(cacheRoles && cacheRoles.hasOwnProperty(roleId)) {
|
|
35
|
+
return roleId ? cacheRoles[roleId].params : {};
|
|
36
|
+
}
|
|
37
|
+
return {}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
a.rules = function (roleId) {
|
|
41
|
+
return a.params(roleId);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
a.list = (roleId, route) => {
|
|
45
|
+
let params = a.params(roleId);
|
|
46
|
+
return a.levels(route, params)
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
a.levels = (route, params) => {
|
|
50
|
+
let obj = {};
|
|
51
|
+
if (a.routes.indexOf(route) > -1) {
|
|
52
|
+
for (let i = 0; i < a.actions.length; i++) {
|
|
53
|
+
if (params.hasOwnProperty(route)) {
|
|
54
|
+
if (params[route].indexOf(a.actions[i]) > -1) {
|
|
55
|
+
obj[a.actions[i]] = true;
|
|
56
|
+
} else {
|
|
57
|
+
obj[a.actions[i]] = false;
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
obj[a.actions[i]] = false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
for (let i = 0; i < a.actions.length; i++) {
|
|
65
|
+
obj[a.actions[i]] = true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return obj;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
a.menuAccess = (res, menu) => {
|
|
73
|
+
if(Array.isArray(menu)) {
|
|
74
|
+
let isTrue = false;
|
|
75
|
+
for(let i = 0; i < menu.length; i++) {
|
|
76
|
+
let r = a.menuAccess(res,menu[i]);
|
|
77
|
+
if(r == true){
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
if(Util.in_array(menu, a.routes)){
|
|
83
|
+
let params = a.params(res.locals.roleId);
|
|
84
|
+
let arr = Object.keys(params) || [];
|
|
85
|
+
if(Util.in_array(menu, arr)){
|
|
86
|
+
return true;
|
|
87
|
+
} else {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return false;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
a.isAccess = (roleId, route, action) => {
|
|
99
|
+
let params = a.params(roleId);
|
|
100
|
+
if(a.routes.includes(route)) {
|
|
101
|
+
if(!params[route]){
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if(a.actions.includes(action)){
|
|
105
|
+
if(params[route].includes(action)){
|
|
106
|
+
return true;
|
|
107
|
+
} else {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
return true;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
//get access page after login
|
|
119
|
+
a.access = (req, res, next) => {
|
|
120
|
+
if (req.session.user === null || typeof req.session.user === 'undefined') {
|
|
121
|
+
req.session.sessionFlash = Util.flashError(LANGUAGE.login_first);
|
|
122
|
+
res.redirect(`${process.env.APP_AFTER_LOGOUT}`);
|
|
123
|
+
} else {
|
|
124
|
+
const isAccess = a.isAccess(res.locals.roleId, req.route, req.action);
|
|
125
|
+
if (isAccess) {
|
|
126
|
+
next();
|
|
127
|
+
} else {
|
|
128
|
+
if(a.isAccess(res.locals.roleId,"zrole","index")) {
|
|
129
|
+
req.session.sessionFlash = Util.flashError(LANGUAGE.no_access);
|
|
130
|
+
res.redirect(`${process.env.APP_AFTER_LOGIN}?setup=role`)
|
|
131
|
+
} else {
|
|
132
|
+
res.redirect(`${process.env.app.APP_AFTER_LOGIN}`)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
module.exports = a;
|