97 lines
2.8 KiB
TypeScript
97 lines
2.8 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
Param,
|
|
ParseUUIDPipe,
|
|
Patch,
|
|
Post,
|
|
Put,
|
|
Query,
|
|
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 { ChangeUserStatusDto } from './dto/change-user-status.dto';
|
|
import { CreateUserDto } from './dto/create-user.dto';
|
|
import { ListUsersQueryDto } from './dto/list-users-query.dto';
|
|
import { ReplaceUserRolesDto } from './dto/replace-user-roles.dto';
|
|
import { ResetUserPasswordDto } from './dto/reset-user-password.dto';
|
|
import { UpdateUserDto } from './dto/update-user.dto';
|
|
import { UsersService } from './users.service';
|
|
|
|
@Controller('users')
|
|
export class UsersController {
|
|
constructor(private readonly users: UsersService) {}
|
|
|
|
@Get()
|
|
@RequirePermissions('users.read')
|
|
list(@Query() query: ListUsersQueryDto) {
|
|
return this.users.list(query);
|
|
}
|
|
|
|
@Post()
|
|
@RequirePermissions('users.create')
|
|
create(
|
|
@Body() dto: CreateUserDto,
|
|
@CurrentAuth() principal: AuthPrincipal,
|
|
@Req() request: RequestWithContext,
|
|
) {
|
|
return this.users.create(dto, principal, request);
|
|
}
|
|
|
|
@Get(':id')
|
|
@RequirePermissions('users.read')
|
|
getById(@Param('id', new ParseUUIDPipe({ version: '4' })) id: string) {
|
|
return this.users.getById(id);
|
|
}
|
|
|
|
@Patch(':id')
|
|
@RequirePermissions('users.update')
|
|
update(
|
|
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
|
@Body() dto: UpdateUserDto,
|
|
@CurrentAuth() principal: AuthPrincipal,
|
|
@Req() request: RequestWithContext,
|
|
) {
|
|
return this.users.update(id, dto, principal, request);
|
|
}
|
|
|
|
@Patch(':id/status')
|
|
@RequirePermissions('users.change_status')
|
|
changeStatus(
|
|
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
|
@Body() dto: ChangeUserStatusDto,
|
|
@CurrentAuth() principal: AuthPrincipal,
|
|
@Req() request: RequestWithContext,
|
|
) {
|
|
return this.users.changeStatus(id, dto, principal, request);
|
|
}
|
|
|
|
@Post(':id/reset-password')
|
|
@RequirePermissions('users.update')
|
|
resetPassword(
|
|
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
|
@Body() dto: ResetUserPasswordDto,
|
|
@CurrentAuth() principal: AuthPrincipal,
|
|
@Req() request: RequestWithContext,
|
|
) {
|
|
return this.users.resetPassword(id, dto, principal, request);
|
|
}
|
|
|
|
@Put(':id/roles')
|
|
@RequirePermissions('users.assign_roles')
|
|
replaceRoles(
|
|
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
|
@Body() dto: ReplaceUserRolesDto,
|
|
@CurrentAuth() principal: AuthPrincipal,
|
|
@Req() request: RequestWithContext,
|
|
) {
|
|
return this.users.replaceRoles(id, dto, principal, request);
|
|
}
|
|
}
|