@uniorganization/uni-lib 4.3.0 → 4.3.1

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.
@@ -3,7 +3,6 @@ import { UserCampaign } from './user-campaign.entity';
3
3
  import { UserRoles } from './user-roles.entity';
4
4
  import { RawRoster } from './raw_roster.entity';
5
5
  export declare class Users {
6
- clientId: number;
7
6
  id: number;
8
7
  username: string;
9
8
  password: string;
@@ -20,10 +20,6 @@ let Users = class Users {
20
20
  }
21
21
  };
22
22
  exports.Users = Users;
23
- __decorate([
24
- (0, typeorm_1.Column)({ name: 'client_id', type: 'integer' }),
25
- __metadata("design:type", Number)
26
- ], Users.prototype, "clientId", void 0);
27
23
  __decorate([
28
24
  (0, typeorm_1.PrimaryGeneratedColumn)({ name: 'ID' }),
29
25
  __metadata("design:type", Number)
@@ -38,7 +38,6 @@ let JwtStrategy = class JwtStrategy extends (0, passport_1.PassportStrategy)(pas
38
38
  var _a, _b, _c;
39
39
  const signedUser = payload === null || payload === void 0 ? void 0 : payload.user;
40
40
  const effectiveClientId = Number(signedUser === null || signedUser === void 0 ? void 0 : signedUser.clientId);
41
- const homeClientId = Number(signedUser === null || signedUser === void 0 ? void 0 : signedUser.homeClientId);
42
41
  const originalClientId = (_a = signedUser === null || signedUser === void 0 ? void 0 : signedUser.originalClientId) !== null && _a !== void 0 ? _a : null;
43
42
  const now = Math.floor(Date.now() / 1000);
44
43
  if ((payload === null || payload === void 0 ? void 0 : payload.type) !== 'access' ||
@@ -47,8 +46,6 @@ let JwtStrategy = class JwtStrategy extends (0, passport_1.PassportStrategy)(pas
47
46
  String(signedUser.id) !== payload.sub ||
48
47
  !Number.isInteger(effectiveClientId) ||
49
48
  effectiveClientId <= 0 ||
50
- !Number.isInteger(homeClientId) ||
51
- homeClientId <= 0 ||
52
49
  !Number.isInteger(payload.exp) ||
53
50
  payload.exp <= now ||
54
51
  !this.validTokenIdentifier(payload.jti) ||
@@ -70,8 +67,8 @@ let JwtStrategy = class JwtStrategy extends (0, passport_1.PassportStrategy)(pas
70
67
  throw new common_1.UnauthorizedException();
71
68
  }
72
69
  const resolved = impersonating
73
- ? yield this.authorization.resolveImpersonation(signedUser.id, originalClientId, homeClientId, effectiveClientId)
74
- : yield this.authorization.resolve(signedUser.id, effectiveClientId, homeClientId);
70
+ ? yield this.authorization.resolveImpersonation(signedUser.id, originalClientId, effectiveClientId)
71
+ : yield this.authorization.resolve(signedUser.id, effectiveClientId);
75
72
  const { identity, snapshot } = resolved;
76
73
  const isMasterTenantAdmin = !impersonating &&
77
74
  snapshot.clientId === tenant_1.NEWTECH_CLIENT_ID &&
@@ -91,7 +88,6 @@ let JwtStrategy = class JwtStrategy extends (0, passport_1.PassportStrategy)(pas
91
88
  password_expired: identity.passwordExpired,
92
89
  viewEvaluations: identity.viewEvaluations,
93
90
  ern: identity.ern,
94
- homeClientId: identity.homeClientId,
95
91
  clientId: snapshot.clientId,
96
92
  clientAlias: snapshot.clientAlias,
97
93
  clientName: snapshot.clientName,
@@ -138,8 +138,6 @@ let RedisTenantStateStore = RedisTenantStateStore_1 = class RedisTenantStateStor
138
138
  return Boolean(value &&
139
139
  Number.isInteger(value.userId) &&
140
140
  value.userId > 0 &&
141
- Number.isInteger(value.homeClientId) &&
142
- value.homeClientId > 0 &&
143
141
  Number.isInteger(value.clientId) &&
144
142
  value.clientId > 0 &&
145
143
  typeof value.clientName === 'string' &&
@@ -165,7 +163,6 @@ let RedisTenantStateStore = RedisTenantStateStore_1 = class RedisTenantStateStor
165
163
  Number.isInteger(value.authzVersion) &&
166
164
  value.authzVersion > 0 &&
167
165
  ((_a = value.identity) === null || _a === void 0 ? void 0 : _a.id) === value.userId &&
168
- value.identity.homeClientId === value.homeClientId &&
169
166
  typeof value.cachedAt === 'string');
170
167
  }
171
168
  };
@@ -12,8 +12,8 @@ export declare class TenantAuthorizationService {
12
12
  private readonly userRoleRepository;
13
13
  private readonly logger;
14
14
  constructor(store: TenantAuthorizationStore, userRepository: Repository<Users>, clientRepository: Repository<Client>, userRoleRepository: Repository<UserRoles>);
15
- resolve(userId: number, clientId: number, expectedHomeClientId: number): Promise<ResolvedTenantAuthorization>;
16
- resolveImpersonation(userId: number, originClientId: number, homeClientId: number, targetClientId: number): Promise<ResolvedTenantAuthorization>;
15
+ resolve(userId: number, clientId: number, _legacyOriginClientId?: number): Promise<ResolvedTenantAuthorization>;
16
+ resolveImpersonation(userId: number, originClientId: number, targetClientIdOrLegacyOriginClientId: number, legacyTargetClientId?: number): Promise<ResolvedTenantAuthorization>;
17
17
  invalidate(userId: number, clientId: number): Promise<void>;
18
18
  invalidateMany(clientId: number, userIds: readonly number[]): Promise<void>;
19
19
  private loadFromDatabase;
@@ -41,36 +41,36 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
41
41
  this.userRoleRepository = userRoleRepository;
42
42
  this.logger = new common_1.Logger(TenantAuthorizationService_1.name);
43
43
  }
44
- resolve(userId, clientId, expectedHomeClientId) {
44
+ resolve(userId, clientId, _legacyOriginClientId) {
45
45
  return __awaiter(this, void 0, void 0, function* () {
46
- var _a;
46
+ var _a, _b;
47
47
  this.assertPositiveInteger(userId);
48
48
  this.assertPositiveInteger(clientId);
49
- this.assertPositiveInteger(expectedHomeClientId);
50
49
  const key = (0, tenant_authorization_keys_1.tenantAuthorizationKey)(clientId, userId);
51
50
  const cached = yield this.readCache(key);
52
51
  if (cached &&
53
52
  cached.userId === userId &&
54
53
  cached.clientId === clientId &&
55
- cached.homeClientId === expectedHomeClientId &&
56
54
  ((_a = cached.identity) === null || _a === void 0 ? void 0 : _a.id) === userId &&
57
- cached.identity.homeClientId === expectedHomeClientId &&
58
55
  this.isFresh(cached.cachedAt) &&
59
- cached.roles.length > 0) {
56
+ cached.roles.length > 0 &&
57
+ Array.isArray(cached.permissions) &&
58
+ ((_b = cached.accessRole) === null || _b === void 0 ? void 0 : _b.id)) {
60
59
  return { identity: cached.identity, snapshot: cached };
61
60
  }
62
- const resolved = yield this.loadFromDatabase(userId, clientId, expectedHomeClientId);
61
+ const resolved = yield this.loadFromDatabase(userId, clientId);
63
62
  yield this.writeCache(key, resolved.snapshot);
64
63
  return resolved;
65
64
  });
66
65
  }
67
- resolveImpersonation(userId, originClientId, homeClientId, targetClientId) {
66
+ resolveImpersonation(userId, originClientId, targetClientIdOrLegacyOriginClientId, legacyTargetClientId) {
68
67
  return __awaiter(this, void 0, void 0, function* () {
68
+ const targetClientId = legacyTargetClientId !== null && legacyTargetClientId !== void 0 ? legacyTargetClientId : targetClientIdOrLegacyOriginClientId;
69
69
  if (originClientId !== tenant_constants_1.NEWTECH_CLIENT_ID ||
70
70
  targetClientId === tenant_constants_1.NEWTECH_CLIENT_ID) {
71
71
  throw new common_1.UnauthorizedException();
72
72
  }
73
- const origin = yield this.resolve(userId, originClientId, homeClientId);
73
+ const origin = yield this.resolve(userId, originClientId);
74
74
  const isNewtechMaster = origin.snapshot.permissions.includes('master.users.read') ||
75
75
  origin.snapshot.roles.some((role) => role.moduleName.toLowerCase() === 'root' &&
76
76
  role.roleName.toLowerCase() === 'sysadmin');
@@ -82,7 +82,6 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
82
82
  }
83
83
  const snapshot = {
84
84
  userId,
85
- homeClientId,
86
85
  clientId: targetClientId,
87
86
  clientName: targetClient.name,
88
87
  clientAlias: targetClient.alias,
@@ -123,8 +122,9 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
123
122
  }
124
123
  });
125
124
  }
126
- loadFromDatabase(userId, clientId, expectedHomeClientId) {
125
+ loadFromDatabase(userId, clientId) {
127
126
  return __awaiter(this, void 0, void 0, function* () {
127
+ var _a, _b;
128
128
  const [user, client, assignments, accessRows] = yield Promise.all([
129
129
  this.userRepository.findOne({ where: { id: userId } }),
130
130
  this.clientRepository.findOne({
@@ -165,20 +165,31 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
165
165
  ]);
166
166
  if (!user ||
167
167
  !Number(user.is_active) ||
168
- user.clientId !== expectedHomeClientId ||
169
168
  !client ||
170
- accessRows.length !== 1) {
169
+ accessRows.length > 1 ||
170
+ (accessRows.length === 0 && assignments.length === 0)) {
171
171
  throw new common_1.UnauthorizedException();
172
172
  }
173
173
  const accessRow = accessRows[0];
174
- const accessRole = {
175
- id: Number(accessRow.id),
176
- code: accessRow.code,
177
- name: accessRow.name,
178
- scope: accessRow.scope,
179
- clientId: accessRow.roleClientId == null ? null : Number(accessRow.roleClientId),
180
- };
181
- const usingLegacyCompatibility = accessRole.code.startsWith('migrated-');
174
+ const usingLegacyCompatibility = accessRows.length === 0 || accessRow.code.startsWith('migrated-');
175
+ const legacyRole = (_a = assignments[0]) === null || _a === void 0 ? void 0 : _a.role;
176
+ const accessRole = accessRow
177
+ ? {
178
+ id: Number(accessRow.id),
179
+ code: accessRow.code,
180
+ name: accessRow.name,
181
+ scope: accessRow.scope,
182
+ clientId: accessRow.roleClientId == null
183
+ ? null
184
+ : Number(accessRow.roleClientId),
185
+ }
186
+ : {
187
+ id: Number(legacyRole === null || legacyRole === void 0 ? void 0 : legacyRole.id),
188
+ code: `legacy-${Number(legacyRole === null || legacyRole === void 0 ? void 0 : legacyRole.id)}`,
189
+ name: (_b = legacyRole === null || legacyRole === void 0 ? void 0 : legacyRole.roleName) !== null && _b !== void 0 ? _b : 'Legacy access',
190
+ scope: 'CLIENT',
191
+ clientId,
192
+ };
182
193
  const roles = usingLegacyCompatibility && assignments.length
183
194
  ? assignments.map((assignment) => {
184
195
  var _a, _b, _c, _d, _e, _f;
@@ -215,7 +226,6 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
215
226
  : [];
216
227
  const identity = {
217
228
  id: user.id,
218
- homeClientId: user.clientId,
219
229
  firstName: user.first_name,
220
230
  username: user.username,
221
231
  lastName: user.last_name,
@@ -230,7 +240,6 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
230
240
  };
231
241
  const snapshot = {
232
242
  userId,
233
- homeClientId: user.clientId,
234
243
  clientId,
235
244
  clientName: client.name,
236
245
  clientAlias: client.alias,
@@ -240,7 +249,7 @@ let TenantAuthorizationService = TenantAuthorizationService_1 = class TenantAuth
240
249
  permissions: Array.isArray(accessRow.permissions)
241
250
  ? accessRow.permissions
242
251
  : [],
243
- authzVersion: Number(accessRow.authzVersion),
252
+ authzVersion: accessRow ? Number(accessRow.authzVersion) : 1,
244
253
  identity,
245
254
  cachedAt: new Date().toISOString(),
246
255
  };
@@ -6,7 +6,6 @@ export interface TenantRoleAuthorization {
6
6
  }
7
7
  export interface TenantAuthorizationSnapshot {
8
8
  userId: number;
9
- homeClientId: number;
10
9
  clientId: number;
11
10
  clientName: string;
12
11
  clientAlias: string;
@@ -17,6 +16,7 @@ export interface TenantAuthorizationSnapshot {
17
16
  authzVersion: number;
18
17
  identity: TenantAuthorizationIdentity;
19
18
  cachedAt: string;
19
+ [key: string]: unknown;
20
20
  }
21
21
  export interface TenantAccessRoleAuthorization {
22
22
  id: number;
@@ -27,7 +27,6 @@ export interface TenantAccessRoleAuthorization {
27
27
  }
28
28
  export interface TenantAuthorizationIdentity {
29
29
  id: number;
30
- homeClientId: number;
31
30
  firstName?: string;
32
31
  username?: string;
33
32
  lastName?: string;
@@ -39,6 +38,7 @@ export interface TenantAuthorizationIdentity {
39
38
  passwordExpired?: number;
40
39
  viewEvaluations?: boolean;
41
40
  ern?: string;
41
+ [key: string]: unknown;
42
42
  }
43
43
  export interface ResolvedTenantAuthorization {
44
44
  identity: TenantAuthorizationIdentity;
@@ -12,7 +12,6 @@ export interface TenantContextState {
12
12
  }
13
13
  export interface AuthenticatedTenantUser {
14
14
  id: number;
15
- homeClientId: number;
16
15
  clientId: number;
17
16
  clientAlias?: string;
18
17
  clientName?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniorganization/uni-lib",
3
- "version": "4.3.0",
3
+ "version": "4.3.1",
4
4
  "description": "UNI Library",
5
5
  "author": "Jhomiguel",
6
6
  "main": "dist/index.js",