node-server-engine 1.1.0 → 1.1.2
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,4 +1,4 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
|
|
2
|
+
/** Middleware to check if the user has at least one of the required permissions (case-insensitive). */
|
|
3
|
+
declare function checkPermission(requiredActions: string | string[]): (req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
|
|
3
4
|
export { checkPermission };
|
|
4
|
-
export default checkPermission;
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return (req, res, next)
|
|
6
|
+
exports.checkPermission = checkPermission;
|
|
7
|
+
/** Middleware to check if the user has at least one of the required permissions (case-insensitive). */
|
|
8
|
+
function checkPermission(requiredActions) {
|
|
9
|
+
return function (req, res, next) {
|
|
10
10
|
const user = req.user;
|
|
11
11
|
if (!user || !user.permissions) {
|
|
12
12
|
return res.status(403).json({
|
|
@@ -29,7 +29,5 @@ const checkPermission = requiredActions => {
|
|
|
29
29
|
}
|
|
30
30
|
next();
|
|
31
31
|
};
|
|
32
|
-
}
|
|
33
|
-
exports.checkPermission = checkPermission;
|
|
34
|
-
var _default = exports.default = checkPermission;
|
|
32
|
+
}
|
|
35
33
|
//# sourceMappingURL=checkPermission.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkPermission.js","names":["checkPermission","requiredActions","req","res","next","user","permissions","status","json","message","requiredActionsArray","Array","isArray","requiredActionsLower","map","action","toLowerCase","hasPermission","some","permission","includes"
|
|
1
|
+
{"version":3,"file":"checkPermission.js","names":["checkPermission","requiredActions","req","res","next","user","permissions","status","json","message","requiredActionsArray","Array","isArray","requiredActionsLower","map","action","toLowerCase","hasPermission","some","permission","includes"],"sources":["../../../src/middleware/checkPermission/checkPermission.ts"],"sourcesContent":["import { Request, Response, NextFunction } from 'express';\n\n/** Middleware to check if the user has at least one of the required permissions (case-insensitive). */\nfunction checkPermission(requiredActions: string | string[]) {\n return function (req: Request, res: Response, next: NextFunction) {\n const user = req.user;\n\n if (!user || !user.permissions) {\n return res.status(403).json({ message: 'User does not have permissions' });\n }\n\n // Normalize input to an array of strings\n const requiredActionsArray = Array.isArray(requiredActions)\n ? requiredActions\n : [requiredActions];\n\n // Normalize required actions to lowercase\n const requiredActionsLower = requiredActionsArray.map(action => action.toLowerCase());\n\n // Check if the user has any of the required permissions (case-insensitive)\n const hasPermission = user.permissions.some((permission: string) =>\n requiredActionsLower.includes(permission.toLowerCase())\n );\n\n if (!hasPermission) {\n return res.status(403).json({ message: 'Permission denied' });\n }\n\n next();\n };\n}\n\nexport { checkPermission };"],"mappings":";;;;;;AAEA;AACA,SAASA,eAAeA,CAACC,eAAkC,EAAE;EAC3D,OAAO,UAAUC,GAAY,EAAEC,GAAa,EAAEC,IAAkB,EAAE;IAChE,MAAMC,IAAI,GAAGH,GAAG,CAACG,IAAI;IAErB,IAAI,CAACA,IAAI,IAAI,CAACA,IAAI,CAACC,WAAW,EAAE;MAC9B,OAAOH,GAAG,CAACI,MAAM,CAAC,GAAG,CAAC,CAACC,IAAI,CAAC;QAAEC,OAAO,EAAE;MAAiC,CAAC,CAAC;IAC5E;;IAEA;IACA,MAAMC,oBAAoB,GAAGC,KAAK,CAACC,OAAO,CAACX,eAAe,CAAC,GACvDA,eAAe,GACf,CAACA,eAAe,CAAC;;IAErB;IACA,MAAMY,oBAAoB,GAAGH,oBAAoB,CAACI,GAAG,CAACC,MAAM,IAAIA,MAAM,CAACC,WAAW,CAAC,CAAC,CAAC;;IAErF;IACA,MAAMC,aAAa,GAAGZ,IAAI,CAACC,WAAW,CAACY,IAAI,CAAEC,UAAkB,IAC7DN,oBAAoB,CAACO,QAAQ,CAACD,UAAU,CAACH,WAAW,CAAC,CAAC,CACxD,CAAC;IAED,IAAI,CAACC,aAAa,EAAE;MAClB,OAAOd,GAAG,CAACI,MAAM,CAAC,GAAG,CAAC,CAACC,IAAI,CAAC;QAAEC,OAAO,EAAE;MAAoB,CAAC,CAAC;IAC/D;IAEAL,IAAI,CAAC,CAAC;EACR,CAAC;AACH","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-server-engine",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Framework used to develop backend services. This package ships with a lot of features to standardize the creation of services, letting you focus on the business logic.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return (req: Request, res: Response, next: NextFunction)
|
|
3
|
+
/** Middleware to check if the user has at least one of the required permissions (case-insensitive). */
|
|
4
|
+
function checkPermission(requiredActions: string | string[]) {
|
|
5
|
+
return function (req: Request, res: Response, next: NextFunction) {
|
|
6
6
|
const user = req.user;
|
|
7
7
|
|
|
8
8
|
if (!user || !user.permissions) {
|
|
9
|
-
return res
|
|
10
|
-
.status(403)
|
|
11
|
-
.json({ message: 'User does not have permissions' });
|
|
9
|
+
return res.status(403).json({ message: 'User does not have permissions' });
|
|
12
10
|
}
|
|
13
11
|
|
|
14
12
|
// Normalize input to an array of strings
|
|
@@ -17,9 +15,7 @@ const checkPermission = (requiredActions: string | string[]) => {
|
|
|
17
15
|
: [requiredActions];
|
|
18
16
|
|
|
19
17
|
// Normalize required actions to lowercase
|
|
20
|
-
const requiredActionsLower = requiredActionsArray.map(
|
|
21
|
-
action.toLowerCase()
|
|
22
|
-
);
|
|
18
|
+
const requiredActionsLower = requiredActionsArray.map(action => action.toLowerCase());
|
|
23
19
|
|
|
24
20
|
// Check if the user has any of the required permissions (case-insensitive)
|
|
25
21
|
const hasPermission = user.permissions.some((permission: string) =>
|
|
@@ -32,7 +28,6 @@ const checkPermission = (requiredActions: string | string[]) => {
|
|
|
32
28
|
|
|
33
29
|
next();
|
|
34
30
|
};
|
|
35
|
-
}
|
|
31
|
+
}
|
|
36
32
|
|
|
37
|
-
export { checkPermission };
|
|
38
|
-
export default checkPermission;
|
|
33
|
+
export { checkPermission };
|