vr-commons 1.0.91 → 1.0.93
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.
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { User, SecurityClearance } from "vr-models";
|
|
2
|
+
/**
|
|
3
|
+
* Get user's security clearance level
|
|
4
|
+
* If user with security clearance is provided, extract level directly
|
|
5
|
+
* Otherwise fetch from database
|
|
6
|
+
*/
|
|
7
|
+
export declare function getUserLevel(userOrId: User | string, securityClearance?: SecurityClearance): Promise<{
|
|
8
|
+
level: number;
|
|
9
|
+
role: string;
|
|
10
|
+
clearance: SecurityClearance | null;
|
|
11
|
+
}>;
|
|
12
|
+
/**
|
|
13
|
+
* Check if admin level is superior to target level (strictly greater)
|
|
14
|
+
*/
|
|
15
|
+
export declare function isSuperior(adminLevel: number, targetLevel: number): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Check if admin level is subordinate to target level (strictly less)
|
|
18
|
+
*/
|
|
19
|
+
export declare function isSubordinate(adminLevel: number, targetLevel: number): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Check if admin level is equal to target level
|
|
22
|
+
*/
|
|
23
|
+
export declare function isRankEqual(adminLevel: number, targetLevel: number): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Check if admin level is subordinate or equal to target level
|
|
26
|
+
*/
|
|
27
|
+
export declare function isSubordinateOrEqual(adminLevel: number, targetLevel: number): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Check if admin level is superior or equal to target level
|
|
30
|
+
*/
|
|
31
|
+
export declare function isSuperiorOrEqual(adminLevel: number, targetLevel: number): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Validate hierarchy for an action with custom message
|
|
34
|
+
*/
|
|
35
|
+
export declare function validateHierarchy(adminLevel: number, targetLevel: number, action: string, allowEqual?: boolean): {
|
|
36
|
+
allowed: boolean;
|
|
37
|
+
message?: string;
|
|
38
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getUserLevel = getUserLevel;
|
|
4
|
+
exports.isSuperior = isSuperior;
|
|
5
|
+
exports.isSubordinate = isSubordinate;
|
|
6
|
+
exports.isRankEqual = isRankEqual;
|
|
7
|
+
exports.isSubordinateOrEqual = isSubordinateOrEqual;
|
|
8
|
+
exports.isSuperiorOrEqual = isSuperiorOrEqual;
|
|
9
|
+
exports.validateHierarchy = validateHierarchy;
|
|
10
|
+
// src/utils/hierarchy.utils.ts
|
|
11
|
+
const vr_models_1 = require("vr-models");
|
|
12
|
+
/**
|
|
13
|
+
* Get user's security clearance level
|
|
14
|
+
* If user with security clearance is provided, extract level directly
|
|
15
|
+
* Otherwise fetch from database
|
|
16
|
+
*/
|
|
17
|
+
async function getUserLevel(userOrId, securityClearance) {
|
|
18
|
+
let user = null;
|
|
19
|
+
if (typeof userOrId === "string") {
|
|
20
|
+
user = await vr_models_1.User.findByPk(userOrId, {
|
|
21
|
+
include: [{ model: vr_models_1.SecurityClearance, as: "securityClearance" }],
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
user = userOrId;
|
|
26
|
+
}
|
|
27
|
+
if (!user) {
|
|
28
|
+
throw new Error("User not found");
|
|
29
|
+
}
|
|
30
|
+
// Use provided security clearance or get from user
|
|
31
|
+
const clearance = securityClearance || user.securityClearance;
|
|
32
|
+
if (!clearance) {
|
|
33
|
+
throw new Error("Security clearance not found for user");
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
level: clearance.level,
|
|
37
|
+
role: clearance.role,
|
|
38
|
+
clearance,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check if admin level is superior to target level (strictly greater)
|
|
43
|
+
*/
|
|
44
|
+
function isSuperior(adminLevel, targetLevel) {
|
|
45
|
+
return adminLevel > targetLevel;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Check if admin level is subordinate to target level (strictly less)
|
|
49
|
+
*/
|
|
50
|
+
function isSubordinate(adminLevel, targetLevel) {
|
|
51
|
+
return adminLevel < targetLevel;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Check if admin level is equal to target level
|
|
55
|
+
*/
|
|
56
|
+
function isRankEqual(adminLevel, targetLevel) {
|
|
57
|
+
return adminLevel === targetLevel;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Check if admin level is subordinate or equal to target level
|
|
61
|
+
*/
|
|
62
|
+
function isSubordinateOrEqual(adminLevel, targetLevel) {
|
|
63
|
+
return adminLevel <= targetLevel;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Check if admin level is superior or equal to target level
|
|
67
|
+
*/
|
|
68
|
+
function isSuperiorOrEqual(adminLevel, targetLevel) {
|
|
69
|
+
return adminLevel >= targetLevel;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Validate hierarchy for an action with custom message
|
|
73
|
+
*/
|
|
74
|
+
function validateHierarchy(adminLevel, targetLevel, action, allowEqual = false) {
|
|
75
|
+
if (isSuperior(adminLevel, targetLevel)) {
|
|
76
|
+
return { allowed: true };
|
|
77
|
+
}
|
|
78
|
+
if (isRankEqual(adminLevel, targetLevel)) {
|
|
79
|
+
if (allowEqual) {
|
|
80
|
+
return { allowed: true };
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
allowed: false,
|
|
84
|
+
message: `Cannot ${action} users with equal role level (${adminLevel} = ${targetLevel})`,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (isSubordinate(adminLevel, targetLevel)) {
|
|
88
|
+
return {
|
|
89
|
+
allowed: false,
|
|
90
|
+
message: `Cannot ${action} users with higher role level (${adminLevel} < ${targetLevel})`,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return { allowed: false, message: `Cannot ${action} this user` };
|
|
94
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -8,4 +8,5 @@ export { banUtil } from "./bans.utils";
|
|
|
8
8
|
export { suspensionUtil } from "./suspension.utils";
|
|
9
9
|
export { createDeviceSession } from "./session.utils";
|
|
10
10
|
export { generateOTP, getOTPExpiry, getVerificationMethod, sendEmail, sendSMS, sendVerificationCode, } from "./verification.utils";
|
|
11
|
+
export { getUserLevel, isRankEqual, isSubordinate, isSubordinateOrEqual, isSuperior, isSuperiorOrEqual, validateHierarchy, } from "./hierarchy.utils";
|
|
11
12
|
export { PendingRegistration, VerificationMethod, ConfirmResponse, } from "./types";
|
package/dist/utils/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sendVerificationCode = exports.sendSMS = exports.sendEmail = exports.getVerificationMethod = exports.getOTPExpiry = exports.generateOTP = exports.createDeviceSession = exports.suspensionUtil = exports.banUtil = exports.hasHigherAuthority = exports.getModeratableRoles = exports.canModerateUser = exports.canModerate = exports.softDeleteUser = exports.hashPassword = exports.validatePassword = exports.validateUniqueFields = exports.generateJacketId = exports.getSortOrder = exports.generateEventSearchConditions = exports.generateUserSearchConditions = exports.findSecurityClearanceByRole = exports.getUsersByRole = exports.getUserById = exports.isAccountAccessible = exports.canModifyAccount = exports.hasAllPermissions = exports.hasAnyPermission = exports.hasPermission = exports.hasRole = exports.formatUserListResponse = exports.formatUserProfile = exports.sendErrorResponse = exports.sendSuccessResponse = exports.logEvent = exports.formatTimeRemaining = exports.getTokenTimeRemaining = exports.generateToken = exports.shouldRefreshToken = exports.verifyToken = exports.generateRiderToken = exports.generatePassengerToken = exports.generateAdminToken = exports.checkSuspensionStatus = exports.checkIsUserBannedOrSuspended = exports.checkBanStatus = exports.hasActiveDependencies = exports.checkAccountDependencies = void 0;
|
|
3
|
+
exports.isRankEqual = exports.getUserLevel = exports.sendVerificationCode = exports.sendSMS = exports.sendEmail = exports.getVerificationMethod = exports.getOTPExpiry = exports.generateOTP = exports.createDeviceSession = exports.suspensionUtil = exports.banUtil = exports.hasHigherAuthority = exports.getModeratableRoles = exports.canModerateUser = exports.canModerate = exports.softDeleteUser = exports.hashPassword = exports.validatePassword = exports.validateUniqueFields = exports.generateJacketId = exports.getSortOrder = exports.generateEventSearchConditions = exports.generateUserSearchConditions = exports.findSecurityClearanceByRole = exports.getUsersByRole = exports.getUserById = exports.isAccountAccessible = exports.canModifyAccount = exports.hasAllPermissions = exports.hasAnyPermission = exports.hasPermission = exports.hasRole = exports.formatUserListResponse = exports.formatUserProfile = exports.sendErrorResponse = exports.sendSuccessResponse = exports.logEvent = exports.formatTimeRemaining = exports.getTokenTimeRemaining = exports.generateToken = exports.shouldRefreshToken = exports.verifyToken = exports.generateRiderToken = exports.generatePassengerToken = exports.generateAdminToken = exports.checkSuspensionStatus = exports.checkIsUserBannedOrSuspended = exports.checkBanStatus = exports.hasActiveDependencies = exports.checkAccountDependencies = void 0;
|
|
4
|
+
exports.validateHierarchy = exports.isSuperiorOrEqual = exports.isSuperior = exports.isSubordinateOrEqual = exports.isSubordinate = void 0;
|
|
4
5
|
var account_utils_1 = require("./account.utils");
|
|
5
6
|
Object.defineProperty(exports, "checkAccountDependencies", { enumerable: true, get: function () { return account_utils_1.checkAccountDependencies; } });
|
|
6
7
|
Object.defineProperty(exports, "hasActiveDependencies", { enumerable: true, get: function () { return account_utils_1.hasActiveDependencies; } });
|
|
@@ -66,3 +67,11 @@ Object.defineProperty(exports, "getVerificationMethod", { enumerable: true, get:
|
|
|
66
67
|
Object.defineProperty(exports, "sendEmail", { enumerable: true, get: function () { return verification_utils_1.sendEmail; } });
|
|
67
68
|
Object.defineProperty(exports, "sendSMS", { enumerable: true, get: function () { return verification_utils_1.sendSMS; } });
|
|
68
69
|
Object.defineProperty(exports, "sendVerificationCode", { enumerable: true, get: function () { return verification_utils_1.sendVerificationCode; } });
|
|
70
|
+
var hierarchy_utils_1 = require("./hierarchy.utils");
|
|
71
|
+
Object.defineProperty(exports, "getUserLevel", { enumerable: true, get: function () { return hierarchy_utils_1.getUserLevel; } });
|
|
72
|
+
Object.defineProperty(exports, "isRankEqual", { enumerable: true, get: function () { return hierarchy_utils_1.isRankEqual; } });
|
|
73
|
+
Object.defineProperty(exports, "isSubordinate", { enumerable: true, get: function () { return hierarchy_utils_1.isSubordinate; } });
|
|
74
|
+
Object.defineProperty(exports, "isSubordinateOrEqual", { enumerable: true, get: function () { return hierarchy_utils_1.isSubordinateOrEqual; } });
|
|
75
|
+
Object.defineProperty(exports, "isSuperior", { enumerable: true, get: function () { return hierarchy_utils_1.isSuperior; } });
|
|
76
|
+
Object.defineProperty(exports, "isSuperiorOrEqual", { enumerable: true, get: function () { return hierarchy_utils_1.isSuperiorOrEqual; } });
|
|
77
|
+
Object.defineProperty(exports, "validateHierarchy", { enumerable: true, get: function () { return hierarchy_utils_1.validateHierarchy; } });
|
|
@@ -1,144 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// import { z } from "zod";
|
|
3
|
-
// import { dateValidator, normalizeDate } from "./dateRange.validations";
|
|
4
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
3
|
exports.exportSuspensionsSchema = exports.exportBansSchema = exports.reviewAppealSchema = exports.extendSuspensionSchema = exports.revokeSuspensionSchema = exports.revokeBanSchema = exports.listPendingAppealsSchema = exports.listSuspensionsSchema = exports.listBansSchema = exports.createSuspensionSchema = exports.createBanSchema = exports.submitSuspensionAppealSchema = exports.submitBanAppealSchema = exports.getUserSuspensionsSchema = exports.getUserBansSchema = exports.getUserRestrictionsSchema = exports.getSuspensionSchema = exports.getBanSchema = void 0;
|
|
6
|
-
// const uuidSchema = z.string().uuid("Invalid UUID format");
|
|
7
|
-
// // Base query schema with common pagination fields
|
|
8
|
-
// const paginationSchema = z.object({
|
|
9
|
-
// page: z
|
|
10
|
-
// .string()
|
|
11
|
-
// .optional()
|
|
12
|
-
// .transform((val) => (val ? parseInt(val) : 1))
|
|
13
|
-
// .pipe(z.number().min(1)),
|
|
14
|
-
// limit: z
|
|
15
|
-
// .string()
|
|
16
|
-
// .optional()
|
|
17
|
-
// .transform((val) => (val ? parseInt(val) : 20))
|
|
18
|
-
// .pipe(z.number().min(1).max(100)),
|
|
19
|
-
// });
|
|
20
|
-
// export const listBansSchema = z.object({
|
|
21
|
-
// query: paginationSchema
|
|
22
|
-
// .extend({
|
|
23
|
-
// userId: z.string().uuid("Invalid user ID").optional(),
|
|
24
|
-
// status: z.enum(["active", "revoked"]).optional(),
|
|
25
|
-
// appealStatus: z.enum(["pending", "approved", "rejected"]).optional(),
|
|
26
|
-
// isPermanent: z.enum(["true", "false"]).optional(),
|
|
27
|
-
// fromDate: dateValidator.optional(),
|
|
28
|
-
// toDate: dateValidator.optional(),
|
|
29
|
-
// search: z.string().optional(),
|
|
30
|
-
// sortBy: z.enum(["bannedAt", "createdAt", "reason"]).optional(),
|
|
31
|
-
// sortOrder: z.enum(["ASC", "DESC"]).optional(),
|
|
32
|
-
// })
|
|
33
|
-
// .transform((data) => {
|
|
34
|
-
// // Normalize dates for DB queries
|
|
35
|
-
// if (data.fromDate) {
|
|
36
|
-
// data.fromDate = normalizeDate(data.fromDate);
|
|
37
|
-
// }
|
|
38
|
-
// if (data.toDate) {
|
|
39
|
-
// data.toDate = normalizeDate(data.toDate);
|
|
40
|
-
// }
|
|
41
|
-
// return data;
|
|
42
|
-
// }),
|
|
43
|
-
// });
|
|
44
|
-
// export const listSuspensionsSchema = z.object({
|
|
45
|
-
// query: paginationSchema
|
|
46
|
-
// .extend({
|
|
47
|
-
// userId: z.string().uuid("Invalid user ID").optional(),
|
|
48
|
-
// status: z.enum(["active", "expired", "revoked"]).optional(),
|
|
49
|
-
// appealStatus: z.enum(["pending", "approved", "rejected"]).optional(),
|
|
50
|
-
// fromDate: dateValidator.optional(),
|
|
51
|
-
// toDate: dateValidator.optional(),
|
|
52
|
-
// search: z.string().optional(),
|
|
53
|
-
// sortBy: z.enum(["startedAt", "endsAt", "createdAt", "reason"]).optional(),
|
|
54
|
-
// sortOrder: z.enum(["ASC", "DESC"]).optional(),
|
|
55
|
-
// })
|
|
56
|
-
// .transform((data) => {
|
|
57
|
-
// // Normalize dates for DB queries
|
|
58
|
-
// if (data.fromDate) {
|
|
59
|
-
// data.fromDate = normalizeDate(data.fromDate);
|
|
60
|
-
// }
|
|
61
|
-
// if (data.toDate) {
|
|
62
|
-
// data.toDate = normalizeDate(data.toDate);
|
|
63
|
-
// }
|
|
64
|
-
// return data;
|
|
65
|
-
// }),
|
|
66
|
-
// });
|
|
67
|
-
// export const revokeBanSchema = z.object({
|
|
68
|
-
// params: z.object({ banId: uuidSchema }),
|
|
69
|
-
// body: z.object({ revocationReason: z.string().max(500).optional() }).strict(),
|
|
70
|
-
// });
|
|
71
|
-
// export const revokeSuspensionSchema = z.object({
|
|
72
|
-
// params: z.object({ suspensionId: uuidSchema }),
|
|
73
|
-
// body: z.object({ revocationReason: z.string().max(500).optional() }).strict(),
|
|
74
|
-
// });
|
|
75
|
-
// export const extendSuspensionSchema = z.object({
|
|
76
|
-
// params: z.object({ suspensionId: uuidSchema }),
|
|
77
|
-
// body: z
|
|
78
|
-
// .object({
|
|
79
|
-
// newEndDate: dateValidator.refine(
|
|
80
|
-
// (date) => {
|
|
81
|
-
// const normalizedDate = normalizeDate(date);
|
|
82
|
-
// return new Date(normalizedDate) > new Date();
|
|
83
|
-
// },
|
|
84
|
-
// { message: "New end date must be in the future" }
|
|
85
|
-
// ),
|
|
86
|
-
// extensionReason: z.string().max(500).optional(),
|
|
87
|
-
// })
|
|
88
|
-
// .strict()
|
|
89
|
-
// .transform((data) => ({
|
|
90
|
-
// ...data,
|
|
91
|
-
// newEndDate: normalizeDate(data.newEndDate), // Normalize for DB
|
|
92
|
-
// })),
|
|
93
|
-
// });
|
|
94
|
-
// export const listPendingAppealsSchema = z.object({
|
|
95
|
-
// query: paginationSchema
|
|
96
|
-
// .extend({
|
|
97
|
-
// type: z.enum(["ban", "suspension"]).optional(),
|
|
98
|
-
// fromDate: dateValidator.optional(),
|
|
99
|
-
// toDate: dateValidator.optional(),
|
|
100
|
-
// })
|
|
101
|
-
// .transform((data) => {
|
|
102
|
-
// // Normalize dates for DB queries
|
|
103
|
-
// if (data.fromDate) {
|
|
104
|
-
// data.fromDate = normalizeDate(data.fromDate);
|
|
105
|
-
// }
|
|
106
|
-
// if (data.toDate) {
|
|
107
|
-
// data.toDate = normalizeDate(data.toDate);
|
|
108
|
-
// }
|
|
109
|
-
// return data;
|
|
110
|
-
// }),
|
|
111
|
-
// });
|
|
112
|
-
// export const reviewAppealSchema = z.object({
|
|
113
|
-
// params: z.object({
|
|
114
|
-
// banId: uuidSchema.optional(),
|
|
115
|
-
// suspensionId: uuidSchema.optional(),
|
|
116
|
-
// }),
|
|
117
|
-
// body: z
|
|
118
|
-
// .object({
|
|
119
|
-
// appealStatus: z.enum(["approved", "rejected"]),
|
|
120
|
-
// adminNotes: z.string().max(1000).optional(),
|
|
121
|
-
// })
|
|
122
|
-
// .strict(),
|
|
123
|
-
// });
|
|
124
|
-
// export const exportBansSchema = z.object({
|
|
125
|
-
// query: z
|
|
126
|
-
// .object({
|
|
127
|
-
// fromDate: dateValidator.optional(),
|
|
128
|
-
// toDate: dateValidator.optional(),
|
|
129
|
-
// format: z.enum(["csv"]).default("csv"),
|
|
130
|
-
// })
|
|
131
|
-
// .transform((data) => {
|
|
132
|
-
// // Normalize dates for DB queries
|
|
133
|
-
// if (data.fromDate) {
|
|
134
|
-
// data.fromDate = normalizeDate(data.fromDate);
|
|
135
|
-
// }
|
|
136
|
-
// if (data.toDate) {
|
|
137
|
-
// data.toDate = normalizeDate(data.toDate);
|
|
138
|
-
// }
|
|
139
|
-
// return data;
|
|
140
|
-
// }),
|
|
141
|
-
// });
|
|
142
4
|
// src/validations/moderation.validations.ts
|
|
143
5
|
const zod_1 = require("zod");
|
|
144
6
|
const dateRange_validations_1 = require("./dateRange.validations");
|