77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
Param,
|
|
ParseUUIDPipe,
|
|
Patch,
|
|
Post,
|
|
Put,
|
|
Req,
|
|
} from '@nestjs/common';
|
|
import { RequirePermissions } from '../../authorization/decorators/require-permissions.decorator';
|
|
import { CurrentAuth } from '../../auth/decorators/current-auth.decorator';
|
|
import type {
|
|
AuthPrincipal,
|
|
RequestWithContext,
|
|
} from '../../common/http/request-context';
|
|
import { CreateRoleDto } from './dto/create-role.dto';
|
|
import { ReplaceRolePermissionsDto } from './dto/replace-role-permissions.dto';
|
|
import { UpdateRoleDto } from './dto/update-role.dto';
|
|
import { RolesService } from './roles.service';
|
|
|
|
@Controller('roles')
|
|
export class RolesController {
|
|
constructor(private readonly roles: RolesService) {}
|
|
|
|
@Get()
|
|
@RequirePermissions('roles.read')
|
|
list() {
|
|
return this.roles.list();
|
|
}
|
|
|
|
@Get('permissions')
|
|
@RequirePermissions('roles.read')
|
|
listPermissions() {
|
|
return this.roles.listPermissions();
|
|
}
|
|
|
|
@Post()
|
|
@RequirePermissions('roles.manage')
|
|
create(
|
|
@Body() dto: CreateRoleDto,
|
|
@CurrentAuth() principal: AuthPrincipal,
|
|
@Req() request: RequestWithContext,
|
|
) {
|
|
return this.roles.create(dto, principal, request);
|
|
}
|
|
|
|
@Get(':id')
|
|
@RequirePermissions('roles.read')
|
|
getById(@Param('id', new ParseUUIDPipe({ version: '4' })) id: string) {
|
|
return this.roles.getById(id);
|
|
}
|
|
|
|
@Patch(':id')
|
|
@RequirePermissions('roles.manage')
|
|
update(
|
|
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
|
@Body() dto: UpdateRoleDto,
|
|
@CurrentAuth() principal: AuthPrincipal,
|
|
@Req() request: RequestWithContext,
|
|
) {
|
|
return this.roles.update(id, dto, principal, request);
|
|
}
|
|
|
|
@Put(':id/permissions')
|
|
@RequirePermissions('roles.manage')
|
|
replacePermissions(
|
|
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
|
@Body() dto: ReplaceRolePermissionsDto,
|
|
@CurrentAuth() principal: AuthPrincipal,
|
|
@Req() request: RequestWithContext,
|
|
) {
|
|
return this.roles.replacePermissions(id, dto, principal, request);
|
|
}
|
|
}
|