remove role from users if deleted

This commit is contained in:
rubikscraft
2022-03-30 13:36:41 +02:00
parent 3af43c2e1a
commit 4b39fb91ea
3 changed files with 41 additions and 4 deletions

View File

@@ -1,6 +1,8 @@
import { Injectable } from '@nestjs/common';
import { AsyncFailable, HasFailed } from 'picsur-shared/dist/types';
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { AsyncFailable, Fail, HasFailed } from 'picsur-shared/dist/types';
import { makeUnique } from 'picsur-shared/dist/util/unique';
import { Repository } from 'typeorm';
import { Permissions } from '../../models/dto/permissions.dto';
import { EUserBackend } from '../../models/entities/user.entity';
import { RolesService } from '../roledb/roledb.service';
@@ -10,7 +12,12 @@ import { UsersService } from './userdb.service';
@Injectable()
export class UserRolesService {
private readonly logger = new Logger('UserRolesService');
constructor(
@InjectRepository(EUserBackend)
private usersRepository: Repository<EUserBackend>,
private usersService: UsersService,
private rolesService: RolesService,
) {}
@@ -48,4 +55,22 @@ export class UserRolesService {
return this.usersService.setRoles(userToModify, newRoles);
}
public async removeRoleEveryone(role: string): AsyncFailable<true> {
try {
await this.usersRepository
.createQueryBuilder('user')
.update()
.set({
roles: () => 'ARRAY_REMOVE(roles, :role)',
})
.where('roles @> ARRAY[:role]', { role })
.execute();
} catch (e) {
this.logger.error(e);
return Fail("Couldn't remove role from everyone");
}
return true;
}
}

View File

@@ -20,6 +20,7 @@ import {
} from 'picsur-shared/dist/dto/api/roles.dto';
import { HasFailed } from 'picsur-shared/dist/types';
import { RolesService } from '../../../collections/roledb/roledb.service';
import { UserRolesService } from '../../../collections/userdb/userrolesdb.service';
import { RequiredPermissions } from '../../../decorators/permissions.decorator';
import { Permission } from '../../../models/dto/permissions.dto';
import {
@@ -35,7 +36,10 @@ import { isPermissionsArray } from '../../../models/validators/permissions.valid
export class RolesController {
private readonly logger = new Logger('RolesController');
constructor(private rolesService: RolesService) {}
constructor(
private rolesService: RolesService,
private userRolesService: UserRolesService,
) {}
@Get('list')
async getRoles(): Promise<RoleListResponse> {
@@ -111,6 +115,13 @@ export class RolesController {
throw new InternalServerErrorException('Could not delete role');
}
const success = await this.userRolesService.removeRoleEveryone(role.name);
if (HasFailed(success)) {
throw new InternalServerErrorException(
'Could not remove role from users',
);
}
return deletedRole;
}

View File

@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { RolesModule } from '../../../collections/roledb/roledb.module';
import { UsersModule } from '../../../collections/userdb/userdb.module';
import { RolesController } from './roles.controller';
@Module({
imports: [RolesModule],
imports: [RolesModule, UsersModule],
controllers: [RolesController],
})
export class RolesApiModule {}