From 9923593a6f002f8b28087dcd1aba6ec3e62805da Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 25 Aug 2022 20:22:32 +0200 Subject: [PATCH] Added dto for Admin Panel, moved UserRole to dto --- dto/src/index.ts | 7 ++++- dto/src/requests/admin.ts | 17 ++++++++++ dto/src/requests/index.ts | 1 + dto/src/responses/admin.ts | 61 ++++++++++++++++++++++++++++++++++++ dto/src/responses/index.ts | 1 + dto/src/utils.ts | 6 ++++ src/app.module.ts | 3 +- src/authguards.ts | 3 +- src/controller/filesystem.ts | 3 +- src/entities.ts | 7 +---- src/services/auth/base.ts | 3 +- src/services/auth/gitlab.ts | 3 +- 12 files changed, 102 insertions(+), 13 deletions(-) create mode 100644 dto/src/requests/admin.ts create mode 100644 dto/src/responses/admin.ts diff --git a/dto/src/index.ts b/dto/src/index.ts index 1c0dbe0..704c086 100644 --- a/dto/src/index.ts +++ b/dto/src/index.ts @@ -1,3 +1,8 @@ export * as Requests from './requests'; export * as Responses from './responses'; -export { validateSync, validateAsync, validateAsyncInline } from './utils'; +export { + UserRole, + validateSync, + validateAsync, + validateAsyncInline +} from './utils'; diff --git a/dto/src/requests/admin.ts b/dto/src/requests/admin.ts new file mode 100644 index 0000000..c91071c --- /dev/null +++ b/dto/src/requests/admin.ts @@ -0,0 +1,17 @@ +import { BaseRequest } from './base'; +import { IsEnum, IsNumber } from 'class-validator'; +import { UserRole } from '../utils'; + +class AdminRequest extends BaseRequest { + @IsNumber() + user: number; +} + +export class SetUserRole extends AdminRequest { + @IsEnum(UserRole) + role: UserRole; +} + +export class LogoutAll extends AdminRequest {} +export class DeleteUser extends AdminRequest {} +export class DisableTfa extends AdminRequest {} diff --git a/dto/src/requests/index.ts b/dto/src/requests/index.ts index 8f26264..684e96f 100644 --- a/dto/src/requests/index.ts +++ b/dto/src/requests/index.ts @@ -1,3 +1,4 @@ export * from './base'; export * as Auth from './auth'; export * as FS from './fs'; +export * as Admin from './admin'; diff --git a/dto/src/responses/admin.ts b/dto/src/responses/admin.ts new file mode 100644 index 0000000..f62183b --- /dev/null +++ b/dto/src/responses/admin.ts @@ -0,0 +1,61 @@ +import { SuccessResponse } from './base'; +import { + IsArray, + IsBoolean, + IsEnum, + IsNotEmpty, + IsNumber, + IsString, + ValidateNested +} from 'class-validator'; +import { UserRole, ValidateConstructor } from '../utils'; + +@ValidateConstructor +export class GetUsersEntry { + constructor( + id: number, + gitlab: boolean, + name: string, + role: UserRole, + tfaEnabled: boolean + ) { + this.id = id; + this.gitlab = gitlab; + this.name = name; + this.role = role; + this.tfaEnabled = tfaEnabled; + } + + @IsNumber() + id: number; + + @IsBoolean() + gitlab: boolean; + + @IsString() + @IsNotEmpty() + name: string; + + @IsEnum(UserRole) + role: UserRole; + + @IsBoolean() + tfaEnabled: boolean; +} + +@ValidateConstructor +export class GetUsers extends SuccessResponse { + constructor(users: GetUsersEntry[]) { + super(); + this.users = users; + } + + @IsArray() + @ValidateNested({ each: true }) + users: GetUsersEntry[]; +} + +export class LogoutAllUser extends SuccessResponse {} +export class DeleteUser extends SuccessResponse {} +export class SetUserRole extends SuccessResponse {} +export class DisableTfa extends SuccessResponse {} diff --git a/dto/src/responses/index.ts b/dto/src/responses/index.ts index c1d0397..0c84bc4 100644 --- a/dto/src/responses/index.ts +++ b/dto/src/responses/index.ts @@ -2,3 +2,4 @@ export * from './base'; export * as Auth from './auth'; export * as FS from './fs'; export * as User from './user'; +export * as Admin from './admin'; diff --git a/dto/src/utils.ts b/dto/src/utils.ts index 308f965..3551fbc 100644 --- a/dto/src/utils.ts +++ b/dto/src/utils.ts @@ -1,5 +1,11 @@ import { validate, validateSync as _validateSync } from 'class-validator'; +export enum UserRole { + ADMIN = 2, + USER = 1, + DISABLED = 0 +} + export function validateSync(data: T): void { const errors = _validateSync(data); if (errors.length > 0) { diff --git a/src/app.module.ts b/src/app.module.ts index 776c66e..160474f 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,12 +1,13 @@ import { Controller, Get, Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; -import { INode, JWTToken, User, UserRole } from './entities'; +import { INode, JWTToken, User } from './entities'; import FileSystemModule from './modules/filesystem'; import { JWTAuthGuard, Role, RoleGuard } from './authguards'; import AuthModule from './modules/auth'; import { ServeStaticModule } from '@nestjs/serve-static'; import { join } from 'path'; import { cwd } from 'process'; +import { UserRole } from 'dto'; declare const PROD: boolean | undefined; diff --git a/src/authguards.ts b/src/authguards.ts index bbaf6a0..c146dbd 100644 --- a/src/authguards.ts +++ b/src/authguards.ts @@ -6,7 +6,8 @@ import { } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; import { Reflector } from '@nestjs/core'; -import { User, UserRole } from './entities'; +import { User } from './entities'; +import { UserRole } from 'dto'; const IS_PUBLIC_KEY = 'isPublic'; export const Public = () => SetMetadata(IS_PUBLIC_KEY, true); diff --git a/src/controller/filesystem.ts b/src/controller/filesystem.ts index 3bd3841..d3f1840 100644 --- a/src/controller/filesystem.ts +++ b/src/controller/filesystem.ts @@ -9,9 +9,8 @@ import { StreamableFile, ValidationPipe } from '@nestjs/common'; -import { Responses, Requests, validateAsyncInline } from 'dto'; +import { Responses, Requests, validateAsyncInline, UserRole } from 'dto'; import FileSystemService from 'services/filesystem'; -import { UserRole } from 'entities'; import { Role } from 'authguards'; @Controller('api/fs') diff --git a/src/entities.ts b/src/entities.ts index 7c3f631..bd3fc14 100644 --- a/src/entities.ts +++ b/src/entities.ts @@ -6,12 +6,7 @@ import { OneToMany, OneToOne } from 'typeorm'; - -export enum UserRole { - ADMIN = 2, - USER = 1, - DISABLED = 0 -} +import { UserRole } from 'dto'; export enum tfaTypes { NONE = 0, diff --git a/src/services/auth/base.ts b/src/services/auth/base.ts index a89cbb6..181c43a 100644 --- a/src/services/auth/base.ts +++ b/src/services/auth/base.ts @@ -4,11 +4,12 @@ import { UnauthorizedException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { JWTToken, User, UserRole } from 'entities'; +import { JWTToken, User } from 'entities'; import { LessThanOrEqual, Repository } from 'typeorm'; import * as argon2 from 'argon2'; import FileSystemService from 'services/filesystem'; import * as jwt from 'jsonwebtoken'; +import { UserRole } from 'dto'; export const jwtSecret = 'CUM'; diff --git a/src/services/auth/gitlab.ts b/src/services/auth/gitlab.ts index 2cf3622..2209f20 100644 --- a/src/services/auth/gitlab.ts +++ b/src/services/auth/gitlab.ts @@ -1,9 +1,10 @@ -import { User, UserRole } from 'entities'; +import { User } from 'entities'; import { FastifyRequest } from 'fastify'; import axios from 'axios'; import * as argon2 from 'argon2'; import { ForbiddenException, UnauthorizedException } from '@nestjs/common'; import TfaAuthService from './tfa'; +import { UserRole } from 'dto'; const GITLAB_ID = '98bcbad78cb1f880d1d1de62291d70a791251a7bea077bfe7df111ef3c115760';