fix for change ownership of app (#10791)

This commit is contained in:
Midhun G S 2024-09-20 13:22:07 +05:30 committed by GitHub
parent e38eb34470
commit b38971cb01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 41 additions and 22 deletions

View file

@ -145,8 +145,9 @@ export class GroupPermissionsControllerV2 {
@CheckPolicies((ability: AppAbility) => ability.can(ORGANIZATION_RESOURCE_ACTIONS.ACCESS_PERMISSIONS, UserEntity))
@Put('user-role/edit')
async updateUserRole(@User() user, @Body() editRoleDto: EditUserRoleDto) {
const { organizationId } = user;
return await this.userRoleService.editDefaultGroupUserRole(editRoleDto, organizationId);
await this.userRoleService.editDefaultGroupUserRole(editRoleDto, user.organizationId, null, {
updatedAdmin: user.id,
});
}
@UseGuards(JwtAuthGuard, PoliciesGuard)

View file

@ -63,8 +63,8 @@ export class OrganizationUsersController {
@UseGuards(JwtAuthGuard, PoliciesGuard)
@CheckPolicies((ability: AppAbility) => ability.can(ORGANIZATION_RESOURCE_ACTIONS.UPDATE_USERS, UserEntity))
@Put(':id')
async updateUser(@Param('id') id: string, @Body() updateUserDto) {
await this.organizationUsersService.updateOrgUser(id, updateUserDto);
async updateUser(@Param('id') id: string, @Body() updateUserDto, @User() user) {
await this.organizationUsersService.updateOrgUser(id, updateUserDto, user.id);
return;
}

View file

@ -114,13 +114,14 @@ export class OrganizationUsersService {
});
}
async updateOrgUser(organizationUserId: string, updateUserDto) {
async updateOrgUser(organizationUserId: string, updateUserDto, adminId: string) {
const organizationUser = await this.organizationUsersRepository.findOne({ where: { id: organizationUserId } });
return await this.usersService.update(
await this.usersService.update(
organizationUser.userId,
updateUserDto,
null,
organizationUser.organizationId
organizationUser.organizationId,
adminId
);
}

View file

@ -79,7 +79,8 @@ export class UserRoleService {
async editDefaultGroupUserRole(
editRoleDto: EditUserRoleDto,
organizationId: string,
manager?: EntityManager
manager?: EntityManager,
options?: { updatedAdmin?: string }
): Promise<void> {
const { newRole, userId } = editRoleDto;
return await dbTransactionWrap(async (manager: EntityManager) => {
@ -115,18 +116,30 @@ export class UserRoleService {
},
});
if (userCreatedApps.length > 0) {
const user = await manager.findOne(User, {
where: {
id: userGroup.userId,
},
});
throw new BadRequestException({
message: {
error: ERROR_HANDLER.USER_IS_OWNER_OF_APPS(user.email),
data: userCreatedApps.map((app) => app.name),
title: 'Can not change user role',
},
});
if (options?.updatedAdmin) {
// Transfer the ownership
await manager.update(
App,
{
userId: userId,
organizationId: organizationId,
},
{ userId: options?.updatedAdmin }
);
} else {
const user = await manager.findOne(User, {
where: {
id: userGroup.userId,
},
});
throw new BadRequestException({
message: {
error: ERROR_HANDLER.USER_IS_OWNER_OF_APPS(user.email),
data: userCreatedApps.map((app) => app.name),
title: 'Can not change user role',
},
});
}
}
}
await this.groupPermissionsService.deleteGroupUser(userGroup.id, manager);

View file

@ -160,7 +160,7 @@ export class UsersService {
}, manager);
}
async update(userId: string, params: any, manager?: EntityManager, organizationId?: string) {
async update(userId: string, params: any, manager?: EntityManager, organizationId?: string, adminId?: string) {
const { forgotPasswordToken, password, firstName, lastName, addGroups, removeGroups, source, role } = params;
const hashedPassword = password ? bcrypt.hashSync(password, 10) : undefined;
@ -180,7 +180,11 @@ export class UsersService {
const user = await manager.findOne(User, { where: { id: userId } });
await this.removeUserGroupPermissionsIfExists(manager, user, removeGroups, organizationId);
if (role) await this.userRoleService.editDefaultGroupUserRole({ userId, newRole: role }, organizationId, manager);
if (role) {
await this.userRoleService.editDefaultGroupUserRole({ userId, newRole: role }, organizationId, manager, {
updatedAdmin: adminId,
});
}
await this.attachUserGroup(addGroups, organizationId, userId, manager);
return user;
}, manager);