generated from companieshouse/node-review-web-starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigationMiddleware.ts
180 lines (173 loc) · 6.28 KB
/
navigationMiddleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { NextFunction, Request, Response } from "express";
import * as constants from "../lib/constants";
import * as url from "node:url";
import logger from "../lib/Logger";
import { Navigation } from "../types/navigation";
import { UserRole, AcspMembership } from "private-api-sdk-node/dist/services/acsp-manage-users/types";
import { getExtraData } from "../lib/utils/sessionUtils";
export const NAVIGATION: Navigation = {
[constants.CHECK_MEMBER_DETAILS_FULL_URL]: {
allowedReferers: [
constants.ADD_USER_FULL_URL,
constants.CHECK_MEMBER_DETAILS_FULL_URL,
constants.CANNOT_ADD_USER_FULL_URL
],
redirectTo: constants.MANAGE_USERS_FULL_URL,
allowedUserRoles: [UserRole.OWNER, UserRole.ADMIN]
},
[constants.CONFIRMATION_MEMBER_ADDED_FULL_URL]: {
allowedReferers: [
constants.CHECK_MEMBER_DETAILS_FULL_URL,
constants.CONFIRMATION_MEMBER_ADDED_FULL_URL
],
redirectTo: constants.MANAGE_USERS_FULL_URL,
allowedUserRoles: [
UserRole.OWNER,
UserRole.ADMIN
]
},
[constants.REMOVE_MEMBER_CHECK_DETAILS_FULL_URL]: {
allowedReferers: [
constants.REMOVE_MEMBER_CHECK_DETAILS_FULL_URL,
constants.MANAGE_USERS_FULL_URL,
constants.STOP_PAGE_ADD_ACCOUNT_OWNER_FULL_URL
],
redirectTo: constants.MANAGE_USERS_FULL_URL,
allowedUserRoles: [
UserRole.OWNER,
UserRole.ADMIN
]
},
[constants.CONFIRMATION_MEMBER_REMOVED_FULL_URL]: {
allowedReferers: [
constants.REMOVE_MEMBER_CHECK_DETAILS_FULL_URL,
constants.CONFIRMATION_MEMBER_REMOVED_FULL_URL
],
redirectTo: constants.MANAGE_USERS_FULL_URL,
allowedUserRoles: [
UserRole.OWNER,
UserRole.ADMIN
]
},
[constants.CONFIRMATION_YOU_ARE_REMOVED_FULL_URL]: {
allowedReferers: [
constants.REMOVE_MEMBER_CHECK_DETAILS_FULL_URL,
constants.CONFIRMATION_YOU_ARE_REMOVED_FULL_URL
],
redirectTo: constants.CHS_SEARCH_REGISTER_PAGE,
allowedUserRoles: [
UserRole.OWNER,
UserRole.ADMIN
]
},
[constants.CANNOT_ADD_USER_FULL_URL]: {
allowedReferers: [
constants.CHECK_MEMBER_DETAILS_FULL_URL,
constants.CANNOT_ADD_USER_FULL_URL
],
redirectTo: constants.MANAGE_USERS_FULL_URL,
allowedUserRoles: [UserRole.OWNER, UserRole.ADMIN]
},
[constants.STOP_PAGE_ADD_ACCOUNT_OWNER_FULL_URL]: {
allowedReferers: [
constants.REMOVE_MEMBER_CHECK_DETAILS_FULL_URL,
constants.MANAGE_USERS_FULL_URL,
constants.STOP_PAGE_ADD_ACCOUNT_OWNER_FULL_URL
],
redirectTo: constants.MANAGE_USERS_FULL_URL,
allowedUserRoles: [
UserRole.OWNER,
UserRole.ADMIN
]
},
[constants.ADD_USER_FULL_URL]: {
allowedReferers: [],
redirectTo: constants.VIEW_USERS_FULL_URL,
allowedUserRoles: [
UserRole.OWNER,
UserRole.ADMIN
]
},
[constants.EDIT_MEMBER_ROLE_FULL_URL]: {
allowedReferers: [
constants.EDIT_MEMBER_ROLE_FULL_URL,
constants.MANAGE_USERS_FULL_URL,
constants.CHECK_EDIT_MEMBER_ROLE_DETAILS_FULL_URL
],
redirectTo: constants.MANAGE_USERS_FULL_URL,
allowedUserRoles: [
UserRole.OWNER,
UserRole.ADMIN
]
},
[constants.CHECK_EDIT_MEMBER_ROLE_DETAILS_FULL_URL]: {
allowedReferers: [
constants.EDIT_MEMBER_ROLE_FULL_URL,
constants.CHECK_EDIT_MEMBER_ROLE_DETAILS_FULL_URL
],
redirectTo: constants.MANAGE_USERS_FULL_URL,
allowedUserRoles: [
UserRole.OWNER,
UserRole.ADMIN
]
},
[constants.CONFIRMATION_MEMBER_ROLE_EDITED_FULL_URL]: {
allowedReferers: [
constants.CHECK_EDIT_MEMBER_ROLE_DETAILS_FULL_URL,
constants.CONFIRMATION_MEMBER_ROLE_EDITED_FULL_URL
],
redirectTo: constants.MANAGE_USERS_FULL_URL,
allowedUserRoles: [
UserRole.OWNER,
UserRole.ADMIN
]
},
[constants.VIEW_USERS_FULL_URL]: {
allowedReferers: [],
redirectTo: constants.MANAGE_USERS_FULL_URL,
allowedUserRoles: [
UserRole.STANDARD
]
},
[constants.MANAGE_USERS_FULL_URL]: {
allowedReferers: [],
redirectTo: constants.VIEW_USERS_FULL_URL,
allowedUserRoles: [
UserRole.ADMIN,
UserRole.OWNER
]
}
};
export const navigationMiddleware = (req: Request, res: Response, next: NextFunction): void => {
const callerURL = url.parse(req.headers.referer || "", true).pathname || "";
let currentPath = url.parse(req.originalUrl, true).pathname || "";
// this is to strip the path parameter which is redundant in this context
if (currentPath.startsWith(constants.REMOVE_MEMBER_CHECK_DETAILS_FULL_URL)) {
currentPath = constants.REMOVE_MEMBER_CHECK_DETAILS_FULL_URL;
} else if (currentPath.startsWith(constants.EDIT_MEMBER_ROLE_FULL_URL)) {
currentPath = constants.EDIT_MEMBER_ROLE_FULL_URL;
}
if (!NAVIGATION[currentPath]) {
logger.info("navigation not found for the current path.");
logger.info(currentPath);
return next();
}
const { allowedReferers, redirectTo, allowedUserRoles } = NAVIGATION[currentPath];
if (allowedReferers?.length) {
const refererAllowed = allowedReferers.some(ref => callerURL.includes(ref));
if (!refererAllowed) {
logger.info(`calling redirect, callerurl: ${callerURL}, currentPath: ${currentPath}`);
return res.redirect(redirectTo);
}
}
if (allowedUserRoles?.length) {
const acspMembership: AcspMembership = getExtraData(req.session, constants.LOGGED_USER_ACSP_MEMBERSHIP);
const { userRole } = acspMembership;
if (!allowedUserRoles.includes(userRole)) {
logger.info(`calling redirect, role ${userRole} not permitted for currentPath: ${currentPath}`);
return res.redirect(redirectTo);
}
}
logger.info("referer and role are ok.");
return next();
};