mirror of
https://github.com/swissmakers/swiss-datashare.git
synced 2026-04-11 10:27:01 +02:00
feat: add add new config strategy to frontend
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
# General
|
||||
APP_URL=http://localhost:3000
|
||||
ALLOW_REGISTRATION=true
|
||||
MAX_FILE_SIZE=5000000000
|
||||
ALLOW_UNAUTHENTICATED_SHARES=false
|
||||
|
||||
# Security
|
||||
JWT_SECRET=random-string
|
||||
|
||||
# Email
|
||||
EMAIL_RECIPIENTS_ENABLED=false
|
||||
SMTP_HOST=smtp.example.com
|
||||
SMTP_PORT=587
|
||||
SMTP_EMAIL=pingvin-share@example.com
|
||||
SMTP_PASSWORD=example
|
||||
@@ -5,6 +5,7 @@ import { User } from "@prisma/client";
|
||||
export class AdministratorGuard implements CanActivate {
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const { user }: { user: User } = context.switchToHttp().getRequest();
|
||||
if (!user) return false;
|
||||
return user.isAdministrator;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { Controller, Get } from "@nestjs/common";
|
||||
import { Body, Controller, Get, Param, Patch, UseGuards } from "@nestjs/common";
|
||||
import { AdministratorGuard } from "src/auth/guard/isAdmin.guard";
|
||||
import { ConfigService } from "./config.service";
|
||||
import { AdminConfigDTO } from "./dto/adminConfig.dto";
|
||||
import { ConfigDTO } from "./dto/config.dto";
|
||||
import UpdateConfigDTO from "./dto/updateConfig.dto";
|
||||
|
||||
@Controller("configs")
|
||||
export class ConfigController {
|
||||
@@ -8,11 +11,22 @@ export class ConfigController {
|
||||
|
||||
@Get()
|
||||
async list() {
|
||||
return new ConfigDTO().fromList(await this.configService.list())
|
||||
return new ConfigDTO().fromList(await this.configService.list());
|
||||
}
|
||||
|
||||
@Get("admin")
|
||||
@UseGuards(AdministratorGuard)
|
||||
async listForAdmin() {
|
||||
return await this.configService.listForAdmin();
|
||||
return new AdminConfigDTO().fromList(
|
||||
await this.configService.listForAdmin()
|
||||
);
|
||||
}
|
||||
|
||||
@Patch("admin/:key")
|
||||
@UseGuards(AdministratorGuard)
|
||||
async update(@Param("key") key: string, @Body() data: UpdateConfigDTO) {
|
||||
return new AdminConfigDTO().from(
|
||||
await this.configService.update(key, data.value)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { Inject, Injectable } from "@nestjs/common";
|
||||
import {
|
||||
BadRequestException,
|
||||
Inject,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from "@nestjs/common";
|
||||
import { Config } from "@prisma/client";
|
||||
import { PrismaService } from "src/prisma/prisma.service";
|
||||
|
||||
@@ -38,4 +43,23 @@ export class ConfigService {
|
||||
return configVariable;
|
||||
});
|
||||
}
|
||||
|
||||
async update(key: string, value: string | number | boolean) {
|
||||
const configVariable = await this.prisma.config.findUnique({
|
||||
where: { key },
|
||||
});
|
||||
|
||||
if (!configVariable || configVariable.locked)
|
||||
throw new NotFoundException("Config variable not found");
|
||||
|
||||
if (typeof value != configVariable.type)
|
||||
throw new BadRequestException(
|
||||
`Config variable must be of type ${configVariable.type}`
|
||||
);
|
||||
|
||||
return await this.prisma.config.update({
|
||||
where: { key },
|
||||
data: { value: value.toString() },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
23
backend/src/config/dto/adminConfig.dto.ts
Normal file
23
backend/src/config/dto/adminConfig.dto.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Expose, plainToClass } from "class-transformer";
|
||||
import { ConfigDTO } from "./config.dto";
|
||||
|
||||
export class AdminConfigDTO extends ConfigDTO {
|
||||
@Expose()
|
||||
default: string;
|
||||
|
||||
@Expose()
|
||||
secret: boolean;
|
||||
|
||||
@Expose()
|
||||
updatedAt: Date;
|
||||
|
||||
from(partial: Partial<AdminConfigDTO>) {
|
||||
return plainToClass(AdminConfigDTO, partial, { excludeExtraneousValues: true });
|
||||
}
|
||||
|
||||
fromList(partial: Partial<AdminConfigDTO>[]) {
|
||||
return partial.map((part) =>
|
||||
plainToClass(AdminConfigDTO, part, { excludeExtraneousValues: true })
|
||||
);
|
||||
}
|
||||
}
|
||||
8
backend/src/config/dto/updateConfig.dto.ts
Normal file
8
backend/src/config/dto/updateConfig.dto.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { IsNotEmpty } from "class-validator";
|
||||
|
||||
class UpdateConfigDTO {
|
||||
@IsNotEmpty()
|
||||
value: string | number | boolean;
|
||||
}
|
||||
|
||||
export default UpdateConfigDTO;
|
||||
Reference in New Issue
Block a user