@sendhome/common 1.0.167 → 1.0.169
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.
|
@@ -13,8 +13,9 @@ declare global {
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
-
export declare const checkUserAccess: (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
export declare const checkUserAccess: (allowedRoles: {
|
|
17
|
+
client?: string[];
|
|
18
|
+
provider?: string[];
|
|
19
|
+
admin?: string[];
|
|
20
|
+
}) => (req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>;
|
|
20
21
|
export {};
|
|
@@ -1,26 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.checkUserAccess = void 0;
|
|
4
|
-
const checkUserAccess = (
|
|
4
|
+
const checkUserAccess = (allowedRoles) => {
|
|
5
5
|
return (req, res, next) => {
|
|
6
|
-
const { account_type, provider_level =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const { account_type, provider_level = "", admin_level = "", } = req.currentUser || {};
|
|
7
|
+
console.log("req.currentUser", req.currentUser);
|
|
8
|
+
console.log("allowedRoles", allowedRoles);
|
|
9
|
+
// Check for client access if account_type is 'client' and client level is in allowedRoles.client
|
|
10
|
+
if (account_type === "client" && allowedRoles.client) {
|
|
11
|
+
if (allowedRoles.client.includes("client")) {
|
|
12
|
+
return next(); // Client is allowed to access this route
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
return res
|
|
16
|
+
.status(403)
|
|
17
|
+
.send({ error: "Clinet level not authorized for this resource" });
|
|
18
|
+
}
|
|
10
19
|
}
|
|
11
|
-
// Check provider
|
|
12
|
-
if (account_type ===
|
|
13
|
-
if (
|
|
14
|
-
return
|
|
20
|
+
// Check for provider access if account_type is 'provider' and provider level is in allowedRoles.provider
|
|
21
|
+
if (account_type === "provider" && allowedRoles.provider) {
|
|
22
|
+
if (allowedRoles.provider.includes(provider_level)) {
|
|
23
|
+
return next(); // Provider level is authorized
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
return res
|
|
27
|
+
.status(403)
|
|
28
|
+
.send({ error: "Provider level not authorized for this resource" });
|
|
15
29
|
}
|
|
16
30
|
}
|
|
17
|
-
// Check admin
|
|
18
|
-
if (account_type ===
|
|
19
|
-
if (
|
|
20
|
-
return
|
|
31
|
+
// Check for admin access if account_type is 'admin' and admin level is in allowedRoles.admin
|
|
32
|
+
if (account_type === "admin" && allowedRoles.admin) {
|
|
33
|
+
if (allowedRoles.admin.includes(admin_level)) {
|
|
34
|
+
return next(); // Admin level is authorized
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return res
|
|
38
|
+
.status(403)
|
|
39
|
+
.send({ error: "Admin level not authorized for this resource" });
|
|
21
40
|
}
|
|
22
41
|
}
|
|
23
|
-
|
|
42
|
+
// If none of the conditions match, deny access
|
|
43
|
+
return res
|
|
44
|
+
.status(403)
|
|
45
|
+
.send({
|
|
46
|
+
error: "You do not have the necessary permissions to access this resource",
|
|
47
|
+
});
|
|
24
48
|
};
|
|
25
49
|
};
|
|
26
50
|
exports.checkUserAccess = checkUserAccess;
|