permissions-contractx 1.0.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.
- package/LICENSE +21 -0
- package/README.md +1397 -0
- package/dist/constants/contractx-permissions.constants.d.ts +310 -0
- package/dist/constants/contractx-permissions.constants.d.ts.map +1 -0
- package/dist/constants/contractx-permissions.constants.js +1061 -0
- package/dist/constants/contractx-roles.constants.d.ts +295 -0
- package/dist/constants/contractx-roles.constants.d.ts.map +1 -0
- package/dist/constants/contractx-roles.constants.js +238 -0
- package/dist/constants/index.d.ts +3 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +18 -0
- package/dist/constants/security.constants.d.ts +77 -0
- package/dist/constants/security.constants.d.ts.map +1 -0
- package/dist/constants/security.constants.js +139 -0
- package/dist/decorators/current-user.decorator.d.ts +73 -0
- package/dist/decorators/current-user.decorator.d.ts.map +1 -0
- package/dist/decorators/current-user.decorator.js +91 -0
- package/dist/decorators/index.d.ts +5 -0
- package/dist/decorators/index.d.ts.map +1 -0
- package/dist/decorators/index.js +20 -0
- package/dist/decorators/permissions.decorator.d.ts +97 -0
- package/dist/decorators/permissions.decorator.d.ts.map +1 -0
- package/dist/decorators/permissions.decorator.js +106 -0
- package/dist/decorators/public.decorator.d.ts +18 -0
- package/dist/decorators/public.decorator.d.ts.map +1 -0
- package/dist/decorators/public.decorator.js +22 -0
- package/dist/decorators/roles.decorator.d.ts +79 -0
- package/dist/decorators/roles.decorator.d.ts.map +1 -0
- package/dist/decorators/roles.decorator.js +87 -0
- package/dist/guards/index.d.ts +4 -0
- package/dist/guards/index.d.ts.map +1 -0
- package/dist/guards/index.js +19 -0
- package/dist/guards/jwt-auth.guard.d.ts +21 -0
- package/dist/guards/jwt-auth.guard.d.ts.map +1 -0
- package/dist/guards/jwt-auth.guard.js +115 -0
- package/dist/guards/permissions.guard.d.ts +14 -0
- package/dist/guards/permissions.guard.d.ts.map +1 -0
- package/dist/guards/permissions.guard.js +77 -0
- package/dist/guards/roles.guard.d.ts +13 -0
- package/dist/guards/roles.guard.d.ts.map +1 -0
- package/dist/guards/roles.guard.js +59 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/interfaces/index.d.ts +2 -0
- package/dist/interfaces/index.d.ts.map +1 -0
- package/dist/interfaces/index.js +17 -0
- package/dist/interfaces/jwt-payload.interface.d.ts +93 -0
- package/dist/interfaces/jwt-payload.interface.d.ts.map +1 -0
- package/dist/interfaces/jwt-payload.interface.js +2 -0
- package/dist/modules/index.d.ts +2 -0
- package/dist/modules/index.d.ts.map +1 -0
- package/dist/modules/index.js +17 -0
- package/dist/modules/permissions-contractx.module.d.ts +41 -0
- package/dist/modules/permissions-contractx.module.d.ts.map +1 -0
- package/dist/modules/permissions-contractx.module.js +215 -0
- package/dist/services/contractx-authorization.service.d.ts +107 -0
- package/dist/services/contractx-authorization.service.d.ts.map +1 -0
- package/dist/services/contractx-authorization.service.js +362 -0
- package/dist/services/contractx-document-compliance.service.d.ts +85 -0
- package/dist/services/contractx-document-compliance.service.d.ts.map +1 -0
- package/dist/services/contractx-document-compliance.service.js +536 -0
- package/dist/services/contractx-validation.service.d.ts +76 -0
- package/dist/services/contractx-validation.service.d.ts.map +1 -0
- package/dist/services/contractx-validation.service.js +305 -0
- package/dist/services/index.d.ts +6 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +20 -0
- package/dist/services/user-context.service.d.ts +114 -0
- package/dist/services/user-context.service.d.ts.map +1 -0
- package/dist/services/user-context.service.js +199 -0
- package/dist/test-document-compliance.d.ts +7 -0
- package/dist/test-document-compliance.d.ts.map +1 -0
- package/dist/test-document-compliance.js +118 -0
- package/package.json +405 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.ContractXValidationService = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const contractx_roles_constants_1 = require("../constants/contractx-roles.constants");
|
|
12
|
+
const contractx_permissions_constants_1 = require("../constants/contractx-permissions.constants");
|
|
13
|
+
let ContractXValidationService = class ContractXValidationService {
|
|
14
|
+
/**
|
|
15
|
+
* Validate a single role
|
|
16
|
+
*/
|
|
17
|
+
validateSingleRole(role) {
|
|
18
|
+
const errors = [];
|
|
19
|
+
const warnings = [];
|
|
20
|
+
if (!(0, contractx_roles_constants_1.validateRole)(role)) {
|
|
21
|
+
errors.push(`Invalid role: ${role}. Must be one of: ${contractx_roles_constants_1.VALID_CONTRACTX_ROLES.join(', ')}`);
|
|
22
|
+
return { isValid: false, errors, warnings };
|
|
23
|
+
}
|
|
24
|
+
const metadata = (0, contractx_roles_constants_1.getRoleMetadata)(role);
|
|
25
|
+
if (!metadata) {
|
|
26
|
+
errors.push(`Role metadata not found for: ${role}`);
|
|
27
|
+
return { isValid: false, errors, warnings };
|
|
28
|
+
}
|
|
29
|
+
if ((0, contractx_roles_constants_1.isSystemRole)(role)) {
|
|
30
|
+
warnings.push(`System role detected: ${role}. Ensure proper authorization for system-level access.`);
|
|
31
|
+
}
|
|
32
|
+
if ((0, contractx_roles_constants_1.isAdminRole)(role)) {
|
|
33
|
+
warnings.push(`Admin role detected: ${role}. This role has elevated privileges.`);
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
isValid: true,
|
|
37
|
+
errors,
|
|
38
|
+
warnings,
|
|
39
|
+
roleInfo: {
|
|
40
|
+
name: metadata.name,
|
|
41
|
+
description: metadata.description,
|
|
42
|
+
type: metadata.type,
|
|
43
|
+
scope: metadata.scope,
|
|
44
|
+
level: metadata.level,
|
|
45
|
+
tenant: metadata.tenant,
|
|
46
|
+
isSystem: metadata.isSystem,
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
validateRoles(roles) {
|
|
51
|
+
const errors = [];
|
|
52
|
+
const warnings = [];
|
|
53
|
+
if (!Array.isArray(roles)) {
|
|
54
|
+
errors.push('Roles must be an array');
|
|
55
|
+
return { isValid: false, errors, warnings };
|
|
56
|
+
}
|
|
57
|
+
if (roles.length === 0) {
|
|
58
|
+
warnings.push('No roles provided. User will have no role-based access.');
|
|
59
|
+
}
|
|
60
|
+
const roleInfos = [];
|
|
61
|
+
for (const role of roles) {
|
|
62
|
+
const validation = this.validateSingleRole(role);
|
|
63
|
+
errors.push(...validation.errors);
|
|
64
|
+
warnings.push(...validation.warnings);
|
|
65
|
+
if (validation.roleInfo) {
|
|
66
|
+
roleInfos.push(validation.roleInfo);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// Check for role conflicts
|
|
70
|
+
const tenants = new Set(roleInfos.map(r => r.tenant));
|
|
71
|
+
if (tenants.size > 1 && !tenants.has('system')) {
|
|
72
|
+
warnings.push('Mixed tenant roles detected. Ensure this is intentional.');
|
|
73
|
+
}
|
|
74
|
+
// Check for duplicate roles
|
|
75
|
+
const uniqueRoles = new Set(roles);
|
|
76
|
+
if (uniqueRoles.size !== roles.length) {
|
|
77
|
+
warnings.push('Duplicate roles detected in the array.');
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
isValid: errors.length === 0,
|
|
81
|
+
errors,
|
|
82
|
+
warnings,
|
|
83
|
+
metadata: { roleInfos, tenantCount: tenants.size },
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
validateSinglePermission(permission) {
|
|
87
|
+
const errors = [];
|
|
88
|
+
const warnings = [];
|
|
89
|
+
const permissionInfo = [];
|
|
90
|
+
if (!(0, contractx_permissions_constants_1.validatePermission)(permission)) {
|
|
91
|
+
errors.push(`Invalid permission: ${permission}. Must follow format 'module:action'`);
|
|
92
|
+
return { isValid: false, errors, warnings, permissionInfo };
|
|
93
|
+
}
|
|
94
|
+
const module = (0, contractx_permissions_constants_1.getModuleFromPermission)(permission);
|
|
95
|
+
const action = (0, contractx_permissions_constants_1.getActionFromPermission)(permission);
|
|
96
|
+
if (!module) {
|
|
97
|
+
errors.push(`Invalid module in permission: ${permission}`);
|
|
98
|
+
return { isValid: false, errors, warnings, permissionInfo };
|
|
99
|
+
}
|
|
100
|
+
if (!action) {
|
|
101
|
+
errors.push(`Invalid action in permission: ${permission}`);
|
|
102
|
+
return { isValid: false, errors, warnings, permissionInfo };
|
|
103
|
+
}
|
|
104
|
+
permissionInfo.push({
|
|
105
|
+
module,
|
|
106
|
+
action,
|
|
107
|
+
isValid: true,
|
|
108
|
+
});
|
|
109
|
+
// Add warnings for sensitive permissions
|
|
110
|
+
if (permission.includes('delete') || permission.includes('admin')) {
|
|
111
|
+
warnings.push(`Sensitive permission detected: ${permission}. Ensure proper authorization.`);
|
|
112
|
+
}
|
|
113
|
+
if (module === 'security_control' || module === 'configuration') {
|
|
114
|
+
warnings.push(`System-level permission detected: ${permission}. High privilege access.`);
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
isValid: true,
|
|
118
|
+
errors,
|
|
119
|
+
warnings,
|
|
120
|
+
permissionInfo,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
validatePermissions(permissions) {
|
|
124
|
+
const errors = [];
|
|
125
|
+
const warnings = [];
|
|
126
|
+
const permissionInfo = [];
|
|
127
|
+
if (!Array.isArray(permissions)) {
|
|
128
|
+
errors.push('Permissions must be an array');
|
|
129
|
+
return { isValid: false, errors, warnings, permissionInfo };
|
|
130
|
+
}
|
|
131
|
+
if (permissions.length === 0) {
|
|
132
|
+
warnings.push('No permissions provided. User will have no permission-based access.');
|
|
133
|
+
}
|
|
134
|
+
for (const permission of permissions) {
|
|
135
|
+
const validation = this.validateSinglePermission(permission);
|
|
136
|
+
errors.push(...validation.errors);
|
|
137
|
+
warnings.push(...validation.warnings);
|
|
138
|
+
permissionInfo.push(...validation.permissionInfo || []);
|
|
139
|
+
}
|
|
140
|
+
const uniquePermissions = new Set(permissions);
|
|
141
|
+
if (uniquePermissions.size !== permissions.length) {
|
|
142
|
+
warnings.push('Duplicate permissions detected in the array.');
|
|
143
|
+
}
|
|
144
|
+
const modules = new Set(permissionInfo.map(p => p.module));
|
|
145
|
+
const actions = new Set(permissionInfo.map(p => p.action));
|
|
146
|
+
return {
|
|
147
|
+
isValid: errors.length === 0,
|
|
148
|
+
errors,
|
|
149
|
+
warnings,
|
|
150
|
+
permissionInfo,
|
|
151
|
+
metadata: {
|
|
152
|
+
moduleCount: modules.size,
|
|
153
|
+
actionCount: actions.size,
|
|
154
|
+
modules: Array.from(modules),
|
|
155
|
+
actions: Array.from(actions),
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Validate a complete user (roles + permissions)
|
|
161
|
+
*/
|
|
162
|
+
validateUser(roles, permissions) {
|
|
163
|
+
const errors = [];
|
|
164
|
+
const warnings = [];
|
|
165
|
+
// Validate roles
|
|
166
|
+
const roleValidation = this.validateRoles(roles);
|
|
167
|
+
errors.push(...roleValidation.errors);
|
|
168
|
+
warnings.push(...roleValidation.warnings);
|
|
169
|
+
// Validate permissions
|
|
170
|
+
const permissionValidation = this.validatePermissions(permissions);
|
|
171
|
+
errors.push(...permissionValidation.errors);
|
|
172
|
+
warnings.push(...permissionValidation.warnings);
|
|
173
|
+
// Determine tenant type
|
|
174
|
+
const roleInfos = roleValidation.metadata?.roleInfos || [];
|
|
175
|
+
const tenants = new Set(roleInfos.map((r) => r.tenant));
|
|
176
|
+
let tenant = 'mixed';
|
|
177
|
+
if (tenants.size === 1) {
|
|
178
|
+
tenant = Array.from(tenants)[0];
|
|
179
|
+
}
|
|
180
|
+
else if (tenants.has('system')) {
|
|
181
|
+
tenant = 'system';
|
|
182
|
+
}
|
|
183
|
+
// Calculate access level (highest role level)
|
|
184
|
+
const accessLevel = Math.max(...roleInfos.map((r) => r.level), 0);
|
|
185
|
+
// Cross-validation: check if permissions align with roles
|
|
186
|
+
const expectedPermissions = this.getExpectedPermissionsForRoles(roles);
|
|
187
|
+
const missingPermissions = expectedPermissions.filter(p => !permissions.includes(p));
|
|
188
|
+
const extraPermissions = permissions.filter(p => !expectedPermissions.includes(p));
|
|
189
|
+
if (missingPermissions.length > 0) {
|
|
190
|
+
warnings.push(`Missing expected permissions for roles: ${missingPermissions.slice(0, 5).join(', ')}${missingPermissions.length > 5 ? '...' : ''}`);
|
|
191
|
+
}
|
|
192
|
+
if (extraPermissions.length > 0) {
|
|
193
|
+
warnings.push(`Extra permissions not typically associated with roles: ${extraPermissions.slice(0, 5).join(', ')}${extraPermissions.length > 5 ? '...' : ''}`);
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
isValid: errors.length === 0,
|
|
197
|
+
errors,
|
|
198
|
+
warnings,
|
|
199
|
+
userInfo: {
|
|
200
|
+
hasValidRoles: roleValidation.isValid,
|
|
201
|
+
hasValidPermissions: permissionValidation.isValid,
|
|
202
|
+
roleCount: roles.length,
|
|
203
|
+
permissionCount: permissions.length,
|
|
204
|
+
tenant,
|
|
205
|
+
accessLevel,
|
|
206
|
+
},
|
|
207
|
+
metadata: {
|
|
208
|
+
roleValidation: roleValidation.metadata,
|
|
209
|
+
permissionValidation: permissionValidation.metadata,
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Get expected permissions for a set of roles (based on ODS matrix)
|
|
215
|
+
*/
|
|
216
|
+
getExpectedPermissionsForRoles(roles) {
|
|
217
|
+
const allPermissions = new Set();
|
|
218
|
+
for (const role of roles) {
|
|
219
|
+
const rolePermissions = contractx_permissions_constants_1.ODS_ROLE_PERMISSIONS[role];
|
|
220
|
+
if (rolePermissions) {
|
|
221
|
+
rolePermissions.forEach(permission => allPermissions.add(permission));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return Array.from(allPermissions);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Check if user has specific module access
|
|
228
|
+
*/
|
|
229
|
+
checkModuleAccess(permissions, module, action) {
|
|
230
|
+
return (0, contractx_permissions_constants_1.hasModuleAccess)(permissions, module, action);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Check if user has any access to a module
|
|
234
|
+
*/
|
|
235
|
+
checkAnyModuleAccess(permissions, module) {
|
|
236
|
+
return (0, contractx_permissions_constants_1.hasAnyModuleAccess)(permissions, module);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Get all modules the user has access to
|
|
240
|
+
*/
|
|
241
|
+
getUserModules(permissions) {
|
|
242
|
+
const modules = new Set();
|
|
243
|
+
for (const permission of permissions) {
|
|
244
|
+
const module = (0, contractx_permissions_constants_1.getModuleFromPermission)(permission);
|
|
245
|
+
if (module) {
|
|
246
|
+
modules.add(module);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return Array.from(modules);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Get user's effective permissions (roles + explicit permissions)
|
|
253
|
+
*/
|
|
254
|
+
getEffectivePermissions(roles, permissions) {
|
|
255
|
+
const rolePermissions = this.getExpectedPermissionsForRoles(roles);
|
|
256
|
+
const allPermissions = new Set([...rolePermissions, ...permissions]);
|
|
257
|
+
return Array.from(allPermissions);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Parse ODS permission string (e.g., "c,r,u,d,s,f") into permission array
|
|
261
|
+
*/
|
|
262
|
+
parseOdsString(odsString, module) {
|
|
263
|
+
const actions = (0, contractx_permissions_constants_1.parseOdsPermissions)(odsString);
|
|
264
|
+
return actions.map(action => `${module}:${action}`);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Validate JWT payload structure
|
|
268
|
+
*/
|
|
269
|
+
validateJwtPayload(payload) {
|
|
270
|
+
const errors = [];
|
|
271
|
+
const warnings = [];
|
|
272
|
+
// Required fields
|
|
273
|
+
if (!payload.sub) {
|
|
274
|
+
errors.push('Missing required field: sub (user ID)');
|
|
275
|
+
}
|
|
276
|
+
if (!payload.fullName) {
|
|
277
|
+
errors.push('Missing required field: fullName');
|
|
278
|
+
}
|
|
279
|
+
if (!Array.isArray(payload.role)) {
|
|
280
|
+
errors.push('Missing or invalid field: role (must be array)');
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
const roleValidation = this.validateRoles(payload.role);
|
|
284
|
+
errors.push(...roleValidation.errors);
|
|
285
|
+
warnings.push(...roleValidation.warnings);
|
|
286
|
+
}
|
|
287
|
+
if (!Array.isArray(payload.permissions)) {
|
|
288
|
+
errors.push('Missing or invalid field: permissions (must be array)');
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
const permissionValidation = this.validatePermissions(payload.permissions);
|
|
292
|
+
errors.push(...permissionValidation.errors);
|
|
293
|
+
warnings.push(...permissionValidation.warnings);
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
isValid: errors.length === 0,
|
|
297
|
+
errors,
|
|
298
|
+
warnings,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
exports.ContractXValidationService = ContractXValidationService;
|
|
303
|
+
exports.ContractXValidationService = ContractXValidationService = __decorate([
|
|
304
|
+
(0, common_1.Injectable)()
|
|
305
|
+
], ContractXValidationService);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './user-context.service';
|
|
2
|
+
export * from './contractx-validation.service';
|
|
3
|
+
export * from './contractx-authorization.service';
|
|
4
|
+
export { ValidationResult, RoleValidationResult, PermissionValidationResult, UserValidationResult, } from './contractx-validation.service';
|
|
5
|
+
export { AuthorizationContext, AuthorizationResult, AccessMatrix, } from './contractx-authorization.service';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AACA,cAAc,wBAAwB,CAAC;AACvC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAGlD,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,0BAA0B,EAC1B,oBAAoB,GACrB,MAAM,gCAAgC,CAAC;AAExC,OAAO,EACL,oBAAoB,EACpB,mBAAmB,EACnB,YAAY,GACb,MAAM,mCAAmC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// Service exports
|
|
18
|
+
__exportStar(require("./user-context.service"), exports);
|
|
19
|
+
__exportStar(require("./contractx-validation.service"), exports);
|
|
20
|
+
__exportStar(require("./contractx-authorization.service"), exports);
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { JwtPayload } from '../interfaces';
|
|
2
|
+
/**
|
|
3
|
+
* Request-scoped service to manage current user context
|
|
4
|
+
* Provides convenient methods to access user information and check permissions
|
|
5
|
+
*/
|
|
6
|
+
export declare class UserContextService {
|
|
7
|
+
private user;
|
|
8
|
+
/**
|
|
9
|
+
* Set the current user context
|
|
10
|
+
* This is typically called by the authentication guard
|
|
11
|
+
*/
|
|
12
|
+
setUser(user: JwtPayload): void;
|
|
13
|
+
/**
|
|
14
|
+
* Get the current authenticated user
|
|
15
|
+
*/
|
|
16
|
+
getUser(): JwtPayload | null;
|
|
17
|
+
/**
|
|
18
|
+
* Get the current user's ID
|
|
19
|
+
*/
|
|
20
|
+
getUserId(): string | null;
|
|
21
|
+
/**
|
|
22
|
+
* Get the current user's full name
|
|
23
|
+
*/
|
|
24
|
+
getUserFullName(): string | null;
|
|
25
|
+
/**
|
|
26
|
+
* Get the current user's email
|
|
27
|
+
*/
|
|
28
|
+
getUserEmail(): string | null;
|
|
29
|
+
/**
|
|
30
|
+
* Get the current user's client ID
|
|
31
|
+
*/
|
|
32
|
+
getClientId(): string | null;
|
|
33
|
+
/**
|
|
34
|
+
* Get the current user's session ID
|
|
35
|
+
*/
|
|
36
|
+
getSessionId(): string | null;
|
|
37
|
+
/**
|
|
38
|
+
* Get all user roles
|
|
39
|
+
*/
|
|
40
|
+
getUserRoles(): string[];
|
|
41
|
+
/**
|
|
42
|
+
* Get all user permissions
|
|
43
|
+
*/
|
|
44
|
+
getUserPermissions(): string[];
|
|
45
|
+
/**
|
|
46
|
+
* Check if user has a specific role
|
|
47
|
+
*/
|
|
48
|
+
hasRole(role: string): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Check if user has any of the specified roles
|
|
51
|
+
*/
|
|
52
|
+
hasAnyRole(roles: string[]): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Check if user has all of the specified roles
|
|
55
|
+
*/
|
|
56
|
+
hasAllRoles(roles: string[]): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Check if user has a specific permission
|
|
59
|
+
*/
|
|
60
|
+
hasPermission(permission: string): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Check if user has any of the specified permissions
|
|
63
|
+
*/
|
|
64
|
+
hasAnyPermission(permissions: string[]): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Check if user has all of the specified permissions
|
|
67
|
+
*/
|
|
68
|
+
hasAllPermissions(permissions: string[]): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Check if user is authenticated
|
|
71
|
+
*/
|
|
72
|
+
isAuthenticated(): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Check if user is a superadmin
|
|
75
|
+
*/
|
|
76
|
+
isSuperAdmin(): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Check if user has admin privileges (superadmin or contract admin)
|
|
79
|
+
*/
|
|
80
|
+
isAdmin(): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Check if user has client-side role
|
|
83
|
+
*/
|
|
84
|
+
isClientUser(): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Check if user has provider-side role
|
|
87
|
+
*/
|
|
88
|
+
isProviderUser(): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Check if user can access a specific module based on permissions
|
|
91
|
+
*/
|
|
92
|
+
canAccessModule(module: string): boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Get user's permissions for a specific module
|
|
95
|
+
*/
|
|
96
|
+
getModulePermissions(module: string): string[];
|
|
97
|
+
/**
|
|
98
|
+
* Check if user can perform a specific action on a module
|
|
99
|
+
*/
|
|
100
|
+
canPerformAction(module: string, action: string): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Get user summary for logging/debugging
|
|
103
|
+
*/
|
|
104
|
+
getUserSummary(): {
|
|
105
|
+
id: string | null;
|
|
106
|
+
name: string | null;
|
|
107
|
+
email: string | null;
|
|
108
|
+
roles: string[];
|
|
109
|
+
permissionCount: number;
|
|
110
|
+
isAdmin: boolean;
|
|
111
|
+
clientId: string | null;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=user-context.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-context.service.d.ts","sourceRoot":"","sources":["../../src/services/user-context.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C;;;GAGG;AACH,qBACa,kBAAkB;IAC7B,OAAO,CAAC,IAAI,CAA2B;IAEvC;;;OAGG;IACH,OAAO,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI;IAI/B;;OAEG;IACH,OAAO,IAAI,UAAU,GAAG,IAAI;IAI5B;;OAEG;IACH,SAAS,IAAI,MAAM,GAAG,IAAI;IAI1B;;OAEG;IACH,eAAe,IAAI,MAAM,GAAG,IAAI;IAIhC;;OAEG;IACH,YAAY,IAAI,MAAM,GAAG,IAAI;IAI7B;;OAEG;IACH,WAAW,IAAI,MAAM,GAAG,IAAI;IAI5B;;OAEG;IACH,YAAY,IAAI,MAAM,GAAG,IAAI;IAI7B;;OAEG;IACH,YAAY,IAAI,MAAM,EAAE;IAIxB;;OAEG;IACH,kBAAkB,IAAI,MAAM,EAAE;IAI9B;;OAEG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI9B;;OAEG;IACH,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO;IAKpC;;OAEG;IACH,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO;IAKrC;;OAEG;IACH,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO;IAI1C;;OAEG;IACH,gBAAgB,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO;IAKhD;;OAEG;IACH,iBAAiB,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO;IAKjD;;OAEG;IACH,eAAe,IAAI,OAAO;IAI1B;;OAEG;IACH,YAAY,IAAI,OAAO;IAIvB;;OAEG;IACH,OAAO,IAAI,OAAO;IAQlB;;OAEG;IACH,YAAY,IAAI,OAAO;IAWvB;;OAEG;IACH,cAAc,IAAI,OAAO;IAWzB;;OAEG;IACH,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAOxC;;OAEG;IACH,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE;IAM9C;;OAEG;IACH,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO;IAIzD;;OAEG;IACH,cAAc,IAAI;QAChB,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;QAClB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;QACpB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;QACrB,KAAK,EAAE,MAAM,EAAE,CAAC;QAChB,eAAe,EAAE,MAAM,CAAC;QACxB,OAAO,EAAE,OAAO,CAAC;QACjB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;KACzB;CAWF"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.UserContextService = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
/**
|
|
12
|
+
* Request-scoped service to manage current user context
|
|
13
|
+
* Provides convenient methods to access user information and check permissions
|
|
14
|
+
*/
|
|
15
|
+
let UserContextService = class UserContextService {
|
|
16
|
+
constructor() {
|
|
17
|
+
this.user = null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Set the current user context
|
|
21
|
+
* This is typically called by the authentication guard
|
|
22
|
+
*/
|
|
23
|
+
setUser(user) {
|
|
24
|
+
this.user = user;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Get the current authenticated user
|
|
28
|
+
*/
|
|
29
|
+
getUser() {
|
|
30
|
+
return this.user;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get the current user's ID
|
|
34
|
+
*/
|
|
35
|
+
getUserId() {
|
|
36
|
+
return this.user?.sub || null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get the current user's full name
|
|
40
|
+
*/
|
|
41
|
+
getUserFullName() {
|
|
42
|
+
return this.user?.fullName || null;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get the current user's email
|
|
46
|
+
*/
|
|
47
|
+
getUserEmail() {
|
|
48
|
+
return this.user?.email || null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get the current user's client ID
|
|
52
|
+
*/
|
|
53
|
+
getClientId() {
|
|
54
|
+
return this.user?.clientId || null;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get the current user's session ID
|
|
58
|
+
*/
|
|
59
|
+
getSessionId() {
|
|
60
|
+
return this.user?.sessionId || null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get all user roles
|
|
64
|
+
*/
|
|
65
|
+
getUserRoles() {
|
|
66
|
+
return this.user?.role || [];
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get all user permissions
|
|
70
|
+
*/
|
|
71
|
+
getUserPermissions() {
|
|
72
|
+
return this.user?.permissions || [];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Check if user has a specific role
|
|
76
|
+
*/
|
|
77
|
+
hasRole(role) {
|
|
78
|
+
return this.getUserRoles().includes(role);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Check if user has any of the specified roles
|
|
82
|
+
*/
|
|
83
|
+
hasAnyRole(roles) {
|
|
84
|
+
const userRoles = this.getUserRoles();
|
|
85
|
+
return roles.some(role => userRoles.includes(role));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Check if user has all of the specified roles
|
|
89
|
+
*/
|
|
90
|
+
hasAllRoles(roles) {
|
|
91
|
+
const userRoles = this.getUserRoles();
|
|
92
|
+
return roles.every(role => userRoles.includes(role));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if user has a specific permission
|
|
96
|
+
*/
|
|
97
|
+
hasPermission(permission) {
|
|
98
|
+
return this.getUserPermissions().includes(permission);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Check if user has any of the specified permissions
|
|
102
|
+
*/
|
|
103
|
+
hasAnyPermission(permissions) {
|
|
104
|
+
const userPermissions = this.getUserPermissions();
|
|
105
|
+
return permissions.some(permission => userPermissions.includes(permission));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Check if user has all of the specified permissions
|
|
109
|
+
*/
|
|
110
|
+
hasAllPermissions(permissions) {
|
|
111
|
+
const userPermissions = this.getUserPermissions();
|
|
112
|
+
return permissions.every(permission => userPermissions.includes(permission));
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Check if user is authenticated
|
|
116
|
+
*/
|
|
117
|
+
isAuthenticated() {
|
|
118
|
+
return this.user !== null;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Check if user is a superadmin
|
|
122
|
+
*/
|
|
123
|
+
isSuperAdmin() {
|
|
124
|
+
return this.hasRole('superadmin');
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Check if user has admin privileges (superadmin or contract admin)
|
|
128
|
+
*/
|
|
129
|
+
isAdmin() {
|
|
130
|
+
return this.hasAnyRole([
|
|
131
|
+
'superadmin',
|
|
132
|
+
'client_contract_admin',
|
|
133
|
+
'provider_contract_admin',
|
|
134
|
+
]);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Check if user has client-side role
|
|
138
|
+
*/
|
|
139
|
+
isClientUser() {
|
|
140
|
+
return this.hasAnyRole([
|
|
141
|
+
'client_contract_admin',
|
|
142
|
+
'client_performance_manager',
|
|
143
|
+
'client_finance_manager',
|
|
144
|
+
'client_reports_manager',
|
|
145
|
+
'client_relationship_manager',
|
|
146
|
+
'client_risk_manager',
|
|
147
|
+
]);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Check if user has provider-side role
|
|
151
|
+
*/
|
|
152
|
+
isProviderUser() {
|
|
153
|
+
return this.hasAnyRole([
|
|
154
|
+
'provider_contract_admin',
|
|
155
|
+
'provider_performance_manager',
|
|
156
|
+
'provider_finance_manager',
|
|
157
|
+
'provider_reports_manager',
|
|
158
|
+
'provider_relationship_manager',
|
|
159
|
+
'provider_risk_manager',
|
|
160
|
+
]);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Check if user can access a specific module based on permissions
|
|
164
|
+
*/
|
|
165
|
+
canAccessModule(module) {
|
|
166
|
+
const modulePermissions = this.getUserPermissions().filter(permission => permission.startsWith(`${module}.`));
|
|
167
|
+
return modulePermissions.length > 0;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Get user's permissions for a specific module
|
|
171
|
+
*/
|
|
172
|
+
getModulePermissions(module) {
|
|
173
|
+
return this.getUserPermissions().filter(permission => permission.startsWith(`${module}.`));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Check if user can perform a specific action on a module
|
|
177
|
+
*/
|
|
178
|
+
canPerformAction(module, action) {
|
|
179
|
+
return this.hasPermission(`${module}.${action}`);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Get user summary for logging/debugging
|
|
183
|
+
*/
|
|
184
|
+
getUserSummary() {
|
|
185
|
+
return {
|
|
186
|
+
id: this.getUserId(),
|
|
187
|
+
name: this.getUserFullName(),
|
|
188
|
+
email: this.getUserEmail(),
|
|
189
|
+
roles: this.getUserRoles(),
|
|
190
|
+
permissionCount: this.getUserPermissions().length,
|
|
191
|
+
isAdmin: this.isAdmin(),
|
|
192
|
+
clientId: this.getClientId(),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
exports.UserContextService = UserContextService;
|
|
197
|
+
exports.UserContextService = UserContextService = __decorate([
|
|
198
|
+
(0, common_1.Injectable)({ scope: common_1.Scope.REQUEST })
|
|
199
|
+
], UserContextService);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Test script to validate permissions-contractx package compliance with PermisosRoles.odt document
|
|
4
|
+
*/
|
|
5
|
+
declare function main(): Promise<void>;
|
|
6
|
+
export { main as testDocumentCompliance };
|
|
7
|
+
//# sourceMappingURL=test-document-compliance.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-document-compliance.d.ts","sourceRoot":"","sources":["../src/test-document-compliance.ts"],"names":[],"mappings":";AACA;;GAEG;AAiBH,iBAAe,IAAI,kBA2GlB;AAOD,OAAO,EAAE,IAAI,IAAI,sBAAsB,EAAE,CAAC"}
|