From a989a50e4b88d36e7112f427948e4b66c5dcd4a3 Mon Sep 17 00:00:00 2001 From: orl0pl Date: Fri, 23 Jun 2023 18:03:22 +0200 Subject: [PATCH] finished making admin routes #8 --- db/notes.json | 19 +++++- routes/adminRoutes.ts | 138 +++++++++++++++++++++++++++++++++++++++--- server.ts | 9 ++- static/mainstyles.css | 2 +- static/style-g.css | 2 +- static/style.css | 2 +- views/addPerson.ejs | 55 +++++++++++++++++ views/addSubject.ejs | 50 +++++++++++++++ views/adminPanel.tsx | 5 ++ views/editPerson.ejs | 55 +++++++++++++++++ views/editSubject.ejs | 50 +++++++++++++++ 11 files changed, 372 insertions(+), 15 deletions(-) create mode 100644 views/addPerson.ejs create mode 100644 views/addSubject.ejs create mode 100644 views/editPerson.ejs create mode 100644 views/editSubject.ejs diff --git a/db/notes.json b/db/notes.json index 36e0cd3..168542f 100644 --- a/db/notes.json +++ b/db/notes.json @@ -35,12 +35,29 @@ "editor", "user" ] + }, + { + "id": 4, + "name": "user", + "password": "user", + "roles": [ + "user" + ] + }, + { + "id": "5", + "name": "test", + "password": "12345678", + "roles": [ + "user", + "editor" + ] } ], "subjects": [ { "id": 0, - "name": "Example Subject", + "name": "Matma", "infos": [ { "id": 0, diff --git a/routes/adminRoutes.ts b/routes/adminRoutes.ts index 13b00aa..8fcf095 100644 --- a/routes/adminRoutes.ts +++ b/routes/adminRoutes.ts @@ -2,6 +2,9 @@ import { Request, Response } from "express"; import ReactDOMServer from 'react-dom/server'; import AdminPanel from "../views/adminPanel"; import { data } from "../server"; +import iconmapper from "../utils/iconmapper"; +import saveChangesToNotes from "../utils/saveNotes"; +import { randomBytes } from "crypto"; export function adminPanelRoute(req: Request, res: Response) { if (req.account?.roles.includes("admin")) { const jsx = ReactDOMServer.renderToString(AdminPanel({ @@ -13,17 +16,46 @@ export function adminPanelRoute(req: Request, res: Response) { res.send("not admin"); } } -export function editSubjectRoute(req: Request, res: Response) { - if (req.account?.roles.includes("admin")) { - res.send("admin"); +export function editSubjectRoute(req: Request<{ id: number }>, res: Response) { + if (req.account?.roles.includes("admin")&&data.subjects[req.params.id]) { + res.render("editSubject", { + url: '../../../', + mi: iconmapper, + oldName: data.subjects[req.params.id].name + }) } else { res.send("not admin"); } } -export function deleteSubjectRoute(req: Request, res: Response) { - if (req.account?.roles.includes("admin")) { - res.send("admin"); +export function editSubjectPOSTRoute(req: Request<{ id: number }, {}, { name: string }>, res: Response) { + if (req.account?.roles.includes("admin")&&data.subjects[req.params.id]&&req.body.name) { + data.subjects[req.params.id].name = req.body.name; + saveChangesToNotes(); + res.send("zmieniono nazwę"); + } + else { + res.send("not admin"); + } +} +export function deleteSubjectRoute(req: Request<{ id: number }>, res: Response) { + if (req.account?.roles.includes("admin")&&data.subjects[req.params.id]) { + res.render('delete', { + url: '../../../', + deletionTypeName: 'przedmiot', + mi: iconmapper, + verificationCode: `${req.params.id}-${randomBytes(2).toString('hex')}`, + }) + } + else { + res.send("not admin"); + } +} +export function deleteSubjectPOSTRoute(req: Request<{ id: number }>, res: Response) { + if (req.account?.roles.includes("admin")&&data.subjects[req.params.id]) { + data.subjects.splice(req.params.id, 1); + saveChangesToNotes(); + res.send("przedmiot usunięty"); } else { res.send("not admin"); @@ -31,7 +63,25 @@ export function deleteSubjectRoute(req: Request, res: Response) { } export function addSubjectRoute(req: Request, res: Response) { if (req.account?.roles.includes("admin")) { - res.send("admin"); + res.render("addSubject", { + url: '../../', + mi: iconmapper + }) + } + else { + res.send("not admin"); + } +} +export function addSubjectPOSTRoute(req: Request<{},{},{ name: string }>, res: Response) { + if (req.account?.roles.includes("admin")&&req.body.name) { + data.subjects.push({ + id: data.subjects.length, + name: req.body.name, + infos: [], + lessons: [] + }) + saveChangesToNotes(); + res.send("added subject"); } else { res.send("not admin"); @@ -39,13 +89,83 @@ export function addSubjectRoute(req: Request, res: Response) { } export function addPersonRoute(req: Request, res: Response) { if (req.account?.roles.includes("admin")) { - res.send("admin"); + res.render("addPerson", { + url: '../../', + mi: iconmapper, + }) + } + else { + res.send("not admin"); + } +} +export function addPersonPOSTRoute(req: Request<{},{},{name:string, password:string, user: 'on'|undefined, editor: 'on'|undefined, admin: 'on'|undefined, }>, res: Response) { + var roles: Array<'user' | 'editor' | 'admin'> = [] + if (req.body.user == 'on') roles.push('user'); + if (req.body.editor == 'on') roles.push('editor'); + if (req.body.admin == 'on') roles.push('admin'); + + if (req.account?.roles.includes("admin")&&req.body.name&&req.body.password) { + console.log(req.body); + data.persons.push({ + id: data.persons.length, + name: req.body.name, + password: req.body.password, + roles: roles, + }) + saveChangesToNotes(); + res.send("added person"); + } + else { + res.send("not admin"); + } +} +export function editPersonRolesRoute(req: Request<{ id: number }>, res: Response){ + if (req.account?.roles.includes("admin")) { + res.render("editPerson", { + url: '../../../', + mi: iconmapper, + }) + } + else { + res.send("not admin"); + } +} +export function editPersonRolesPOSTRoute(req: Request<{ id: number },{},{name:string, password:string, user: 'on'|undefined, editor: 'on'|undefined, admin: 'on'|undefined}>, res: Response){ + var roles: Array<'user' | 'editor' | 'admin'> = [] + if (req.body.user == 'on') roles.push('user'); + if (req.body.editor == 'on') roles.push('editor'); + if (req.body.admin == 'on') roles.push('admin'); + + if (req.account?.roles.includes("admin")) { + console.log(req.body); + const editedUser = { + id: req.params.id, + name: req.body.name, + password: req.body.password, + roles: roles, + } + data.persons[req.params.id] = editedUser; + saveChangesToNotes(); + res.send("edited person"); + } + else { + res.send("not admin"); + } +} +export function deletePersonRoute(req: Request<{ id: number }>, res: Response){ + if (req.account?.roles.includes("admin")) { + res.render('delete', { + url: '../../../', + deletionTypeName: 'osobę', + mi: iconmapper, + verificationCode: `${req.params.id}-${randomBytes(2).toString('hex')}`, + }) } else { res.send("not admin"); } } -export function editPersonRoles(req: Request, res: Response){ +export function deletePersonPOSTRoute(req: Request, res: Response){ if (req.account?.roles.includes("admin")) { res.send("admin"); } diff --git a/server.ts b/server.ts index 92844b1..ba8a7c4 100644 --- a/server.ts +++ b/server.ts @@ -25,7 +25,7 @@ import {searchPOSTRoute, searchRoute} from './routes/searchRoutes'; import historyRoute from './routes/historyRoute'; import { addLessonPOSTRoute, addLessonRoute } from './routes/addLessonRoutes'; import { deleteNoteRoute, deleteNotePOSTRoute, deleteExerciseRoute, deleteExercisePOSTRoute, deleteLessonRoute, deleteLessonPOSTRoute } from './routes/deleteRoutes'; -import { addPersonRoute, addSubjectRoute, adminPanelRoute, deleteSubjectRoute, editPersonRoles, editSubjectRoute } from './routes/adminRoutes'; +import { addPersonPOSTRoute, addPersonRoute, addSubjectPOSTRoute, addSubjectRoute, adminPanelRoute, deleteSubjectPOSTRoute, deleteSubjectRoute, editPersonRolesPOSTRoute, editPersonRolesRoute, editSubjectPOSTRoute, editSubjectRoute } from './routes/adminRoutes'; var json = fs.readFileSync('db/notes.json', 'utf8'); export const data = Convert.toDataBase(json); export var dataRaw: DataBase = JSON.parse(fs.readFileSync('db/notes.json', 'utf8')); @@ -108,10 +108,15 @@ app.post('/search', searchPOSTRoute) app.get('/adminpanel', adminPanelRoute) app.get('/adminpanel/edit/:id', editSubjectRoute) +app.post('/adminpanel/edit/:id', editSubjectPOSTRoute) app.get('/adminpanel/delete/:id', deleteSubjectRoute) +app.post('/adminpanel/delete/:id', deleteSubjectPOSTRoute) app.get('/adminpanel/add', addSubjectRoute) +app.post('/adminpanel/add', addSubjectPOSTRoute) app.get('/adminpanel/add-person', addPersonRoute) -app.get('/adminpanel/edit-roles/:id', editPersonRoles) +app.post('/adminpanel/add-person', addPersonPOSTRoute) +app.get('/adminpanel/edit-roles/:id', editPersonRolesRoute) +app.post('/adminpanel/edit-roles/:id', editPersonRolesPOSTRoute) app.listen(1447, () => { console.log(`⚡️[NOTAMARK]: Running at http://localhost:1447`); diff --git a/static/mainstyles.css b/static/mainstyles.css index e8af251..f8f7339 100644 --- a/static/mainstyles.css +++ b/static/mainstyles.css @@ -161,7 +161,7 @@ body { border-radius: 8px; } -input { +input[type="text"], input[type="password"] { all: unset; background-color: var(--md-sys-color-surface-container-highest); padding: 12px 16px; diff --git a/static/style-g.css b/static/style-g.css index 32c5a79..d992a8d 100644 --- a/static/style-g.css +++ b/static/style-g.css @@ -48,7 +48,7 @@ body { min-height: 100%; } -input { +input:not([type="checkbox"]) { all: unset; background-color: var(--md-sys-color-surface-container-highest); padding: 12px 16px; diff --git a/static/style.css b/static/style.css index d0534cb..c4d57a3 100644 --- a/static/style.css +++ b/static/style.css @@ -113,7 +113,7 @@ body { flex: 1; } -textarea, input, span#realDateOrReference { +textarea, input:not([type="checkbox"]), span#realDateOrReference { all: unset; resize: none; flex: 1; diff --git a/views/addPerson.ejs b/views/addPerson.ejs new file mode 100644 index 0000000..8367665 --- /dev/null +++ b/views/addPerson.ejs @@ -0,0 +1,55 @@ + + + + + Zaloguj się + + + + + + + + + Edytor + + + + +
+ + <%= mi('account-plus') %> + + Dodaj osobę + + + Role: + Użytkownik: + Edytor: + Administrator: + +
+ + \ No newline at end of file diff --git a/views/addSubject.ejs b/views/addSubject.ejs new file mode 100644 index 0000000..9b114ae --- /dev/null +++ b/views/addSubject.ejs @@ -0,0 +1,50 @@ + + + + + Zaloguj się + + + + + + + + + Edytor + + + + +
+ + <%= mi('plus-box-multiple') %> + + Dodaj przedmiot + + +
+ + \ No newline at end of file diff --git a/views/adminPanel.tsx b/views/adminPanel.tsx index d53fd48..8cbe71b 100644 --- a/views/adminPanel.tsx +++ b/views/adminPanel.tsx @@ -96,6 +96,11 @@ export default function AdminPanel({ data, url }: { data: DataBase, url: string + {/* + + */} diff --git a/views/editPerson.ejs b/views/editPerson.ejs new file mode 100644 index 0000000..e4b9ab5 --- /dev/null +++ b/views/editPerson.ejs @@ -0,0 +1,55 @@ + + + + + Zaloguj się + + + + + + + + + Edytor + + + + +
+ + <%= mi('account-edit') %> + + Edytuj użytkownika + + + Role: + Użytkownik: + Edytor: + Administrator: + +
+ + \ No newline at end of file diff --git a/views/editSubject.ejs b/views/editSubject.ejs new file mode 100644 index 0000000..667a9a3 --- /dev/null +++ b/views/editSubject.ejs @@ -0,0 +1,50 @@ + + + + + Zaloguj się + + + + + + + + + Edytor + + + + +
+ + <%= mi('plus-box-multiple') %> + + Edytuj przedmiot + + +
+ + \ No newline at end of file