diff --git a/src/controller/admin/exportController.ts b/src/controller/admin/exportController.ts new file mode 100644 index 0000000..739610c --- /dev/null +++ b/src/controller/admin/exportController.ts @@ -0,0 +1,64 @@ +import { Request, Response } from "express"; +import MemberService from "../../service/memberService"; +import AwardService from "../../service/awardService"; +import CommunicationTypeService from "../../service/communicationTypeService"; +import ExecutivePositionService from "../../service/executivePositionService"; +import crypto from "crypto"; +import { type BinaryLike } from "node:crypto"; + +/** + * @description get all data stored in the database as a single json object + * @param req {Request} Express req object + * @param res {Response} Express res object + * @returns {Promise<*>} + */ +export async function getDatabaseExport(req: Request, res: Response): Promise { + const { secret } = req.body; + try { + if (!secret) { + res.status(400).send('Empty encryption key'); + return; + } + const iv = await new Promise((resolve, reject) => { + // create initialization vector + crypto.randomBytes(16, (err, iv) => { + if (err) { + reject(err); + return; + } + resolve(iv); + }); + }); + + let data: any = {}; + + console.log('Exporting awards...'); + data.awards = await AwardService.getAll(); + console.log(`Exported ${data.awards.length} awards`); + console.log('Exporting communication types...'); + data.communicationTypes = await CommunicationTypeService.getAll(); + console.log(`Exported ${data.communicationTypes.length} communicationTypes`); + console.log('Exporting executivePositions...'); + data.executivePositions = await ExecutivePositionService.getAll(); + console.log(`Exported ${data.executivePositions.length} executivePositions`); + console.log('Exporting members...'); + data.members = await MemberService.getAll(); + console.log(`Exported ${data.members.length} members`); + + console.log(`Encrypting data...`); + // encrypt data + const dataStr: string = JSON.stringify(data); + let base64Key = crypto.createHash('sha256').update(String(secret)).digest('base64'); + const key = Buffer.from(base64Key, 'base64') + let cipher = crypto.createCipheriv("aes-256-gcm", key, iv as BinaryLike); + const encryptedData: string = cipher.update(dataStr, 'utf8', 'hex') + cipher.final('hex'); + + res.json({ + data: encryptedData, + }); + } + catch(ex) { + console.log(ex); + res.status(500).send(ex.message ? ex.message : 'Exception!'); + } +} diff --git a/src/routes/admin/database.ts b/src/routes/admin/database.ts new file mode 100644 index 0000000..8b90340 --- /dev/null +++ b/src/routes/admin/database.ts @@ -0,0 +1,11 @@ +import express, { Request, Response } from "express"; +import { getDatabaseExport } from "../../controller/admin/exportController"; + +const router = express.Router({ mergeParams: true }); + +router.post("/", async (req: Request, res: Response) => { + await getDatabaseExport(req, res); +}); + + +export default router; diff --git a/src/routes/admin/index.ts b/src/routes/admin/index.ts index c0c1ab9..2eb358f 100644 --- a/src/routes/admin/index.ts +++ b/src/routes/admin/index.ts @@ -12,6 +12,8 @@ import member from "./member"; import role from "./role"; import user from "./user"; +import database from "./database"; + var router = express.Router({ mergeParams: true }); router.use("/award", PermissionHelper.passCheckMiddleware("read", "settings", "award"), award); @@ -37,4 +39,6 @@ router.use("/member", PermissionHelper.passCheckMiddleware("read", "club", "memb router.use("/role", PermissionHelper.passCheckMiddleware("read", "user", "role"), role); router.use("/user", PermissionHelper.passCheckMiddleware("read", "user", "user"), user); +router.use("/database", PermissionHelper.passCheckMiddleware("read", "settings", "database"), database); + export default router; diff --git a/src/type/permissionTypes.ts b/src/type/permissionTypes.ts index 3bd54c8..a8da5a9 100644 --- a/src/type/permissionTypes.ts +++ b/src/type/permissionTypes.ts @@ -11,7 +11,8 @@ export type PermissionModule = | "communication" | "membership_status" | "user" - | "role"; + | "role" + | "database"; export type PermissionType = "read" | "create" | "update" | "delete"; @@ -47,10 +48,11 @@ export const permissionModules: Array = [ "membership_status", "user", "role", + "database", ]; export const permissionTypes: Array = ["read", "create", "update", "delete"]; export const sectionsAndModules: SectionsAndModulesObject = { club: ["member", "calendar", "newsletter", "protocoll"], - settings: ["qualification", "award", "executive_position", "communication", "membership_status"], + settings: ["qualification", "award", "executive_position", "communication", "membership_status", "database"], user: ["user", "role"], };