74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
|
import {
|
||
|
Controller,
|
||
|
Get,
|
||
|
NotFoundException,
|
||
|
Param,
|
||
|
Query,
|
||
|
UseGuards,
|
||
|
} from '@nestjs/common';
|
||
|
import { Privileges } from 'src/decorators/privileges.decorator';
|
||
|
import { Scopes } from 'src/decorators/scopes.decorator';
|
||
|
import { OAuth2Guard } from 'src/guards/oauth2.guard';
|
||
|
import { PrivilegesGuard } from 'src/guards/privileges.guard';
|
||
|
import { ScopesGuard } from 'src/guards/scopes.guard';
|
||
|
import { UserService } from 'src/modules/objects/user/user.service';
|
||
|
import { FormUtilityService } from 'src/modules/utility/services/form-utility.service';
|
||
|
import { PaginationService } from 'src/modules/utility/services/paginate.service';
|
||
|
import { PageOptions } from 'src/types/pagination.interfaces';
|
||
|
|
||
|
const RELATIONS = ['picture', 'privileges'];
|
||
|
|
||
|
@Controller('/api/admin/users')
|
||
|
@UseGuards(OAuth2Guard, PrivilegesGuard, ScopesGuard)
|
||
|
export class UserAdminController {
|
||
|
constructor(
|
||
|
private _user: UserService,
|
||
|
private _paginate: PaginationService,
|
||
|
private _form: FormUtilityService,
|
||
|
) {}
|
||
|
|
||
|
@Get('')
|
||
|
@Scopes('management')
|
||
|
@Privileges('admin', 'admin:user')
|
||
|
async userList(@Query() options: { q?: string } & PageOptions) {
|
||
|
const search = options.q ? decodeURIComponent(options.q) : null;
|
||
|
const resultCount = await this._user.searchUsersCount(search, RELATIONS);
|
||
|
|
||
|
const pagination = this._paginate.paginate(options, resultCount);
|
||
|
|
||
|
const [list] = await this._user.searchUsers(
|
||
|
pagination.pageSize,
|
||
|
pagination.offset,
|
||
|
search,
|
||
|
RELATIONS,
|
||
|
);
|
||
|
|
||
|
return {
|
||
|
pagination,
|
||
|
list: this._form.stripObjectArray(list, ['password']),
|
||
|
};
|
||
|
}
|
||
|
|
||
|
@Get(':id')
|
||
|
@Scopes('management')
|
||
|
@Privileges('admin', 'admin:user')
|
||
|
async user(@Param('id') id: string) {
|
||
|
const user = await this._user.getById(parseInt(id, 10), RELATIONS);
|
||
|
if (!user) {
|
||
|
throw new NotFoundException('User not found');
|
||
|
}
|
||
|
return this._form.stripObject(user, ['password']);
|
||
|
}
|
||
|
|
||
|
@Get(':id/privileges')
|
||
|
@Scopes('management')
|
||
|
@Privileges('admin', 'admin:user')
|
||
|
async userPrivileges(@Param('id') id: string) {
|
||
|
const user = await this._user.getById(parseInt(id, 10), ['privileges']);
|
||
|
if (!user) {
|
||
|
throw new NotFoundException('User not found');
|
||
|
}
|
||
|
return user.privileges;
|
||
|
}
|
||
|
}
|