@reldens/cms 0.24.0 → 0.26.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.
@@ -1,227 +1,228 @@
1
- /**
2
- *
3
- * Reldens - Router
4
- *
5
- */
6
-
7
- const { Logger } = require('@reldens/utils');
8
-
9
- class Router
10
- {
11
-
12
- constructor(props)
13
- {
14
- this.app = props.app;
15
- this.applicationFramework = props.applicationFramework;
16
- this.bodyParser = props.bodyParser;
17
- this.session = props.session;
18
- this.secret = props.secret;
19
- this.rootPath = props.rootPath;
20
- this.adminRoleId = props.adminRoleId;
21
- this.authenticationCallback = props.authenticationCallback;
22
- this.uploaderFactory = props.uploaderFactory;
23
- this.buckets = props.buckets;
24
- this.blackList = props.blackList;
25
- this.loginPath = props.loginPath;
26
- this.logoutPath = props.logoutPath;
27
- this.viewPath = props.viewPath;
28
- this.editPath = props.editPath;
29
- this.savePath = props.savePath;
30
- this.deletePath = props.deletePath;
31
- this.resources = props.resources;
32
- this.emitEvent = props.emitEvent;
33
- this.fetchUploadProperties = props.fetchUploadProperties;
34
- this.adminContents = props.adminContents;
35
- this.generateListRouteContent = props.generateListRouteContent;
36
- this.generateViewRouteContent = props.generateViewRouteContent;
37
- this.generateEditRouteContent = props.generateEditRouteContent;
38
- this.processDeleteEntities = props.processDeleteEntities;
39
- this.processSaveEntity = props.processSaveEntity;
40
- this.checkAndReloadAdminTemplates = props.checkAndReloadAdminTemplates;
41
- this.setupAdminRouter();
42
- }
43
-
44
- setupAdminRouter()
45
- {
46
- if(!this.applicationFramework){
47
- Logger.critical('ApplicationFramework is required for AdminRouter setup.');
48
- return false;
49
- }
50
- this.adminRouter = this.applicationFramework.Router();
51
- if(this.session){
52
- if(!this.secret){
53
- Logger.warning('Admin Manager "secret" key was not provided.');
54
- }
55
- this.adminRouter.use(this.session({secret: this.secret, resave: false, saveUninitialized: true}));
56
- }
57
- if(!this.bodyParser){
58
- Logger.critical('BodyParser is required for AdminRouter setup.');
59
- return false;
60
- }
61
- this.adminRouter.use(this.bodyParser.json());
62
- return true;
63
- }
64
-
65
- async reloadTemplatesIfNeeded()
66
- {
67
- if(!this.checkAndReloadAdminTemplates){
68
- return false;
69
- }
70
- return await this.checkAndReloadAdminTemplates();
71
- }
72
-
73
- setupAdminRoutes()
74
- {
75
- this.adminRouter.get(this.loginPath, async (req, res) => {
76
- await this.reloadTemplatesIfNeeded();
77
- return res.send(this.adminContents().login);
78
- });
79
- this.adminRouter.post(this.loginPath, async (req, res) => {
80
- //await this.reloadTemplatesIfNeeded();
81
- let { email, password } = req.body;
82
- let loginResult = await this.authenticationCallback(email, password, this.adminRoleId);
83
- if(loginResult){
84
- req.session.user = loginResult;
85
- return res.redirect(this.rootPath);
86
- }
87
- return res.redirect(this.rootPath+this.loginPath+'?login-error=true');
88
- });
89
- this.adminRouter.get('/', this.isAuthenticated.bind(this), async (req, res) => {
90
- await this.reloadTemplatesIfNeeded();
91
- return res.send(this.adminContents().dashboard);
92
- });
93
- this.adminRouter.get(this.logoutPath, (req, res) => {
94
- req.session.destroy();
95
- res.redirect(this.rootPath+this.loginPath);
96
- });
97
- this.app.use(this.rootPath, this.adminRouter);
98
- }
99
-
100
- async setupEntitiesRoutes()
101
- {
102
- let resources = this.resources();
103
- if(!resources || 0 === resources.length){
104
- return;
105
- }
106
- for(let driverResource of resources){
107
- let entityPath = driverResource.entityPath;
108
- let entityRoute = '/'+entityPath;
109
- this.adminRouter.get(entityRoute, this.isAuthenticated.bind(this), async (req, res) => {
110
- await this.reloadTemplatesIfNeeded();
111
- return res.send(await this.generateListRouteContent(req, driverResource, entityPath));
112
- });
113
- this.adminRouter.post(entityRoute, this.isAuthenticated.bind(this), async (req, res) => {
114
- await this.reloadTemplatesIfNeeded();
115
- return res.send(await this.generateListRouteContent(req, driverResource, entityPath));
116
- });
117
- this.adminRouter.get(entityRoute+this.viewPath, this.isAuthenticated.bind(this), async (req, res) => {
118
- await this.reloadTemplatesIfNeeded();
119
- let routeContents = await this.generateViewRouteContent(req, driverResource, entityPath);
120
- if('' === routeContents){
121
- return res.redirect(this.rootPath+'/'+entityPath+'?result=errorView');
122
- }
123
- return res.send(routeContents);
124
- });
125
- this.adminRouter.get(entityRoute+this.editPath, this.isAuthenticated.bind(this), async (req, res) => {
126
- await this.reloadTemplatesIfNeeded();
127
- await this.emitEvent('reldens.adminBeforeEntityEdit', {
128
- req,
129
- res,
130
- driverResource,
131
- entityPath
132
- });
133
- let routeContents = await this.generateEditRouteContent(req, driverResource, entityPath);
134
- if('' === routeContents){
135
- return res.redirect(this.rootPath+'/'+entityPath+'?result=errorEdit');
136
- }
137
- return res.send(routeContents);
138
- });
139
- this.setupSavePath(entityRoute, driverResource, entityPath);
140
- this.adminRouter.post(entityRoute+this.deletePath, this.isAuthenticated.bind(this), async (req, res) => {
141
- //await this.reloadTemplatesIfNeeded();
142
- return res.redirect(await this.processDeleteEntities(req, res, driverResource, entityPath));
143
- });
144
- await this.emitEvent('reldens.setupEntitiesRoutes', {
145
- entityPath,
146
- entityRoute,
147
- driverResource
148
- });
149
- }
150
- }
151
-
152
- setupSavePath(entityRoute, driverResource, entityPath)
153
- {
154
- let uploadProperties = this.fetchUploadProperties(driverResource);
155
- if(0 === Object.keys(uploadProperties || {}).length){
156
- this.adminRouter.post(
157
- entityRoute+this.savePath,
158
- this.isAuthenticated.bind(this),
159
- async (req, res) => {
160
- //await this.reloadTemplatesIfNeeded();
161
- await this.emitEvent('reldens.adminBeforeEntitySave', {
162
- req,
163
- res,
164
- driverResource,
165
- entityPath
166
- });
167
- return res.redirect(await this.processSaveEntity(req, res, driverResource, entityPath));
168
- }
169
- );
170
- return;
171
- }
172
- let fields = [];
173
- let allowedFileTypes = {};
174
- for(let uploadPropertyKey of Object.keys(uploadProperties)){
175
- let property = uploadProperties[uploadPropertyKey];
176
- allowedFileTypes[uploadPropertyKey] = property.allowedTypes || false;
177
- let field = {name: uploadPropertyKey};
178
- if(!property.isArray){
179
- field.maxCount = 1;
180
- }
181
- fields.push(field);
182
- this.buckets[uploadPropertyKey] = property.bucket;
183
- }
184
- this.adminRouter.post(
185
- entityRoute + this.savePath,
186
- this.isAuthenticated.bind(this),
187
- this.uploaderFactory.createUploader(fields, this.buckets, allowedFileTypes),
188
- async (req, res) => {
189
- //await this.reloadTemplatesIfNeeded();
190
- await this.emitEvent('reldens.adminBeforeEntitySave', {
191
- req,
192
- res,
193
- driverResource,
194
- entityPath
195
- });
196
- return res.redirect(await this.processSaveEntity(req, res, driverResource, entityPath));
197
- }
198
- );
199
- }
200
-
201
- isAuthenticated(req, res, next)
202
- {
203
- let allowContinue = {result: true, callback: null};
204
- let event = {req, res, next, allowContinue};
205
- this.emitEvent('reldens.adminIsAuthenticated', event);
206
- let returnPath = this.rootPath+this.loginPath;
207
- if(false === allowContinue.result){
208
- return res.redirect(returnPath);
209
- }
210
- if(null !== allowContinue.callback){
211
- return allowContinue.callback(event);
212
- }
213
- let user = req.session?.user;
214
- if(!user){
215
- return res.redirect(returnPath);
216
- }
217
- let userBlackList = this.blackList[user.role_id] || [];
218
- if(-1 !== userBlackList.indexOf(req.path)){
219
- let referrer = String(req.headers?.referer || '');
220
- return res.redirect('' !== referrer ? referrer : returnPath);
221
- }
222
- return next();
223
- }
224
-
225
- }
226
-
227
- module.exports.Router = Router;
1
+ /**
2
+ *
3
+ * Reldens - Router
4
+ *
5
+ */
6
+
7
+ const { Logger } = require('@reldens/utils');
8
+
9
+ class Router
10
+ {
11
+
12
+ constructor(props)
13
+ {
14
+ this.app = props.app;
15
+ this.applicationFramework = props.applicationFramework;
16
+ this.bodyParser = props.bodyParser;
17
+ this.session = props.session;
18
+ this.secret = props.secret;
19
+ this.rootPath = props.rootPath;
20
+ this.adminRoleId = props.adminRoleId;
21
+ this.authenticationCallback = props.authenticationCallback;
22
+ this.uploaderFactory = props.uploaderFactory;
23
+ this.buckets = props.buckets;
24
+ this.blackList = props.blackList;
25
+ this.loginPath = props.loginPath;
26
+ this.logoutPath = props.logoutPath;
27
+ this.viewPath = props.viewPath;
28
+ this.editPath = props.editPath;
29
+ this.savePath = props.savePath;
30
+ this.deletePath = props.deletePath;
31
+ this.resources = props.resources;
32
+ this.emitEvent = props.emitEvent;
33
+ this.fetchUploadProperties = props.fetchUploadProperties;
34
+ this.adminContents = props.adminContents;
35
+ this.generateListRouteContent = props.generateListRouteContent;
36
+ this.generateViewRouteContent = props.generateViewRouteContent;
37
+ this.generateEditRouteContent = props.generateEditRouteContent;
38
+ this.processDeleteEntities = props.processDeleteEntities;
39
+ this.processSaveEntity = props.processSaveEntity;
40
+ this.checkAndReloadAdminTemplates = props.checkAndReloadAdminTemplates;
41
+ this.setupAdminRouter();
42
+ }
43
+
44
+ setupAdminRouter()
45
+ {
46
+ if(!this.applicationFramework){
47
+ Logger.critical('ApplicationFramework is required for AdminRouter setup.');
48
+ return false;
49
+ }
50
+ this.adminRouter = this.applicationFramework.Router();
51
+ if(this.session){
52
+ if(!this.secret){
53
+ Logger.warning('Admin Manager "secret" key was not provided.');
54
+ }
55
+ this.adminRouter.use(this.session({secret: this.secret, resave: false, saveUninitialized: true}));
56
+ }
57
+ if(!this.bodyParser){
58
+ Logger.critical('BodyParser is required for AdminRouter setup.');
59
+ return false;
60
+ }
61
+ this.adminRouter.use(this.bodyParser.json());
62
+ return true;
63
+ }
64
+
65
+ async reloadTemplatesIfNeeded()
66
+ {
67
+ if(!this.checkAndReloadAdminTemplates){
68
+ return false;
69
+ }
70
+ return await this.checkAndReloadAdminTemplates();
71
+ }
72
+
73
+ setupAdminRoutes()
74
+ {
75
+ this.adminRouter.get(this.loginPath, async (req, res) => {
76
+ await this.reloadTemplatesIfNeeded();
77
+ return res.send(this.adminContents().login);
78
+ });
79
+ this.adminRouter.post(this.loginPath, async (req, res) => {
80
+ //await this.reloadTemplatesIfNeeded();
81
+ let { email, password } = req.body;
82
+ let loginResult = await this.authenticationCallback(email, password, this.adminRoleId);
83
+ if(loginResult){
84
+ req.session.user = loginResult;
85
+ return res.redirect(this.rootPath);
86
+ }
87
+ return res.redirect(this.rootPath+this.loginPath+'?login-error=true');
88
+ });
89
+ this.adminRouter.get('/', this.isAuthenticated.bind(this), async (req, res) => {
90
+ await this.reloadTemplatesIfNeeded();
91
+ return res.send(this.adminContents().dashboard);
92
+ });
93
+ this.adminRouter.get(this.logoutPath, (req, res) => {
94
+ req.session.destroy();
95
+ res.redirect(this.rootPath+this.loginPath);
96
+ });
97
+ this.app.use(this.rootPath, this.adminRouter);
98
+ }
99
+
100
+ async setupEntitiesRoutes()
101
+ {
102
+ let resources = this.resources();
103
+ if(!resources || 0 === resources.length){
104
+ return;
105
+ }
106
+ for(let driverResource of resources){
107
+ let entityPath = driverResource.entityPath;
108
+ let entityRoute = '/'+entityPath;
109
+ this.adminRouter.get(entityRoute, this.isAuthenticated.bind(this), async (req, res) => {
110
+ await this.reloadTemplatesIfNeeded();
111
+ return res.send(await this.generateListRouteContent(req, driverResource, entityPath));
112
+ });
113
+ this.adminRouter.post(entityRoute, this.isAuthenticated.bind(this), async (req, res) => {
114
+ await this.reloadTemplatesIfNeeded();
115
+ return res.send(await this.generateListRouteContent(req, driverResource, entityPath));
116
+ });
117
+ this.adminRouter.get(entityRoute+this.viewPath, this.isAuthenticated.bind(this), async (req, res) => {
118
+ await this.reloadTemplatesIfNeeded();
119
+ let routeContents = await this.generateViewRouteContent(req, driverResource, entityPath);
120
+ if('' === routeContents){
121
+ return res.redirect(this.rootPath+'/'+entityPath+'?result=errorView');
122
+ }
123
+ return res.send(routeContents);
124
+ });
125
+ this.adminRouter.get(entityRoute+this.editPath, this.isAuthenticated.bind(this), async (req, res) => {
126
+ await this.reloadTemplatesIfNeeded();
127
+ await this.emitEvent('reldens.adminBeforeEntityEdit', {
128
+ req,
129
+ res,
130
+ driverResource,
131
+ entityPath
132
+ });
133
+ let routeContents = await this.generateEditRouteContent(req, driverResource, entityPath);
134
+ if('' === routeContents){
135
+ return res.redirect(this.rootPath+'/'+entityPath+'?result=errorEdit');
136
+ }
137
+ return res.send(routeContents);
138
+ });
139
+ this.setupSavePath(entityRoute, driverResource, entityPath);
140
+ this.adminRouter.post(entityRoute+this.deletePath, this.isAuthenticated.bind(this), async (req, res) => {
141
+ //await this.reloadTemplatesIfNeeded();
142
+ return res.redirect(await this.processDeleteEntities(req, res, driverResource, entityPath));
143
+ });
144
+ await this.emitEvent('reldens.setupEntitiesRoutes', {
145
+ entityPath,
146
+ entityRoute,
147
+ driverResource
148
+ });
149
+ }
150
+ }
151
+
152
+ setupSavePath(entityRoute, driverResource, entityPath)
153
+ {
154
+ let uploadProperties = this.fetchUploadProperties(driverResource);
155
+ if(0 === Object.keys(uploadProperties || {}).length){
156
+ this.adminRouter.post(
157
+ entityRoute+this.savePath,
158
+ this.isAuthenticated.bind(this),
159
+ async (req, res) => {
160
+ //await this.reloadTemplatesIfNeeded();
161
+ await this.emitEvent('reldens.adminBeforeEntitySave', {
162
+ req,
163
+ res,
164
+ driverResource,
165
+ entityPath
166
+ });
167
+ return res.redirect(await this.processSaveEntity(req, res, driverResource, entityPath));
168
+ }
169
+ );
170
+ return;
171
+ }
172
+ let fields = [];
173
+ let allowedFileTypes = {};
174
+ let entityBuckets = {};
175
+ for(let uploadPropertyKey of Object.keys(uploadProperties)){
176
+ let property = uploadProperties[uploadPropertyKey];
177
+ allowedFileTypes[uploadPropertyKey] = property.allowedTypes || false;
178
+ let field = {name: uploadPropertyKey};
179
+ if(!property.isArray){
180
+ field.maxCount = 1;
181
+ }
182
+ fields.push(field);
183
+ entityBuckets[uploadPropertyKey] = property.bucket;
184
+ }
185
+ this.adminRouter.post(
186
+ entityRoute + this.savePath,
187
+ this.isAuthenticated.bind(this),
188
+ this.uploaderFactory.createUploader(fields, entityBuckets, allowedFileTypes),
189
+ async (req, res) => {
190
+ //await this.reloadTemplatesIfNeeded();
191
+ await this.emitEvent('reldens.adminBeforeEntitySave', {
192
+ req,
193
+ res,
194
+ driverResource,
195
+ entityPath
196
+ });
197
+ return res.redirect(await this.processSaveEntity(req, res, driverResource, entityPath));
198
+ }
199
+ );
200
+ }
201
+
202
+ isAuthenticated(req, res, next)
203
+ {
204
+ let allowContinue = {result: true, callback: null};
205
+ let event = {req, res, next, allowContinue};
206
+ this.emitEvent('reldens.adminIsAuthenticated', event);
207
+ let returnPath = this.rootPath+this.loginPath;
208
+ if(false === allowContinue.result){
209
+ return res.redirect(returnPath);
210
+ }
211
+ if(null !== allowContinue.callback){
212
+ return allowContinue.callback(event);
213
+ }
214
+ let user = req.session?.user;
215
+ if(!user){
216
+ return res.redirect(returnPath);
217
+ }
218
+ let userBlackList = this.blackList[user.role_id] || [];
219
+ if(-1 !== userBlackList.indexOf(req.path)){
220
+ let referrer = String(req.headers?.referer || '');
221
+ return res.redirect('' !== referrer ? referrer : returnPath);
222
+ }
223
+ return next();
224
+ }
225
+
226
+ }
227
+
228
+ module.exports.Router = Router;