Skip to content

Commit

Permalink
refactor(worker): small code sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Baulig committed Oct 4, 2024
1 parent 52182ae commit 352a43c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
31 changes: 21 additions & 10 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,11 @@ export class UserService extends ServiceBase<UserListResponse, UserList> impleme
}
// check if the assignable_by_roles contain createAccessRole
for (let user of usersList ?? []) {
const targetUserRoleIds = Array.from(
new Set(user.role_associations?.map(
const targetUserRoleIds = [...new Set(
user.role_associations?.map(
ra => ra.role
))
);
)
)];

if (!targetUserRoleIds?.length) {
continue;
Expand All @@ -820,20 +820,31 @@ export class UserService extends ServiceBase<UserListResponse, UserList> impleme
}, {});

if (rolesData?.items?.length < targetUserRoleIds.length) {
const message = `One or more of the target role IDs are invalid ${targetUserRoleIds},` +
` no such role exist in the system`;
this.logger.error(message, rolesData);
const found = rolesData?.items?.map(item => item.payload?.id);
const missing = targetUserRoleIds.filter(id => !found?.includes(id));
const message = `The following role IDs [${
missing?.join(', ')
}] are either invalid or the assigning user does not have the required permission.`;
this.logger.error(message);
return returnStatus(400, message, user.id);
}

if (rolesData?.items?.length > 0) {
for (let targetRole of rolesData.items) {
if (targetRole?.payload?.id) {
if (!targetRole?.payload?.assignable_by_roles ||
!createAccessRole.some((role) => targetRole?.payload?.assignable_by_roles?.includes(role))) {
!createAccessRole.some(
(role) => targetRole?.payload?.assignable_by_roles?.includes(role)
)
) {
const userNameId = user?.name ? user.name : user?.id;
let message = `The target role ${targetRole.payload.id} cannot be assigned to` +
` user ${userNameId} as the user role ${createAccessRole} does not have the required permission`;
let message = `The target role ${
targetRole.payload.id
} cannot be assigned to user ${
userNameId
} as the user roles [${
createAccessRole.join(', ')
}] does not have the required permission`;
this.logger.verbose(message);
return returnStatus(403, message, user.id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class Worker {
logger.error('Error reading jobs files');
}
}
if (externalJobFiles && externalJobFiles.length > 0) {
if (externalJobFiles?.length > 0) {
externalJobFiles.forEach(async (externalFile) => {
if (externalFile.endsWith('.js') || externalFile.endsWith('.cjs')) {
const require_dir = process.env.EXTERNAL_JOBS_REQUIRE_DIR ?? './jobs/';
Expand Down
8 changes: 4 additions & 4 deletions test/service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1724,7 +1724,7 @@ describe('testing identity-srv', () => {
const result = await userService.create({ items: [testUser], subject });
result!.items![0]!.status!.code!.should.equal(400);
result!.items![0]!.status!.message!.should.equal(
`One or more of the target role IDs are invalid ${ testUser.role_associations!.map(ra => ra.role) }, no such role exist in the system`
`The following role IDs [invalid_role] are either invalid or the assigning user does not have the required permission.`
);
result!.items![0]!.status!.id!.should.equal('testuser');
result!.operation_status!.code!.should.equal(200);
Expand All @@ -1750,7 +1750,7 @@ describe('testing identity-srv', () => {

result!.items![0]!.status!.code!.should.equal(400);
result!.items![0]!.status!.message!.should.equal(
`One or more of the target role IDs are invalid ${ testUser.role_associations!.map(ra => ra.role) }, no such role exist in the system`
`The following role IDs [invalid_role] are either invalid or the assigning user does not have the required permission.`
);
result!.items![0]!.status!.id!.should.equal('testuser');
// first user created, validate result
Expand All @@ -1770,7 +1770,7 @@ describe('testing identity-srv', () => {
testUser.role_associations![0]!.role = 'super-admin-r-id';
const result = await userService.create({ items: [testUser], subject });
result!.items![0]!.status!.code!.should.equal(403);
result!.items![0]!.status!.message!.should.equal('The target role super-admin-r-id cannot be assigned to user test.user as the user role admin-r-id,admin-r-id,user-r-id does not have the required permission');
result!.items![0]!.status!.message!.should.equal('The target role super-admin-r-id cannot be assigned to user test.user as the user roles [admin-r-id, admin-r-id, user-r-id] does not have the required permission');
result!.items![0]!.status!.id!.should.equal('testuser');
result!.operation_status!.code!.should.equal(200);
result!.operation_status!.message!.should.equal('success');
Expand Down Expand Up @@ -1798,7 +1798,7 @@ describe('testing identity-srv', () => {
await redisClient.set(hrScopeskey, JSON.stringify(subjectResolved.hierarchical_scopes));
const result = await userService.create({ items: [testUser], subject });
result!.items![0]!.status!.code!.should.equal(403);
result!.items![0]!.status!.message!.should.equal('The target role user-r-id cannot be assigned to user test.user as user role does not have permissions');
result!.items![0]!.status!.message!.should.equal('The target role user-r-id cannot be assigned to user test.user as the user roles [] does not have the required permission');
result!.items![0]!.status!.id!.should.equal('testuser');
result!.operation_status!.code!.should.equal(200);
result!.operation_status!.message!.should.equal('success');
Expand Down

0 comments on commit 352a43c

Please sign in to comment.