Add user list and update user management

This commit is contained in:
Ninjdai 2023-12-07 12:33:59 +01:00
parent 651f870415
commit be62b85c52
13 changed files with 111 additions and 26 deletions

View File

@ -9,7 +9,7 @@ loadDatabase();
const submitEvent = new EventEmitter();
await launchWeb(submitEvent);
submitEvent.on("call", async (call) => {
submitEvent.on("contacts/call", async (call) => {
let vote;
if (call.vote) {
console.log(`${call.phone} va voter`);
@ -23,7 +23,7 @@ submitEvent.on("call", async (call) => {
{ where: { phone: call.phone } },
);
});
submitEvent.on("add", (request) => {
submitEvent.on("contacts/add", (request) => {
console.log(request);
});

View File

@ -1,8 +0,0 @@
export default {
path: "/api/contacts/add",
requiresLogin: true,
type: "post",
async execute(request, response) {
global.events.submitEvent.emit("add", request.body);
},
}

View File

@ -1,12 +0,0 @@
import { permissionBits } from '../../../../utils/permissions.js';
export default {
path: "/api/contacts/call",
requiresLogin: true,
permissions: permissionBits.CALL,
type: "post",
async execute(request, response) {
global.events.submitEvent.emit("call", request.body);
response.redirect("/calls");
},
}

View File

@ -0,0 +1,22 @@
import { permissionBits } from "../../../../utils/permissions.js";
export default {
path: "/api/contacts(/*)?",
requiresLogin: true,
permissions: permissionBits.CALL,
type: "post",
async execute(request, response) {
const phone = request.originalUrl.split("/")[3];
const operation = request.originalUrl.split("/")[4];
switch(operation) {
case 'call':
global.events.submitEvent.emit("contacts/call", request.body);
response.redirect("/calls");
break
case 'add':
global.events.submitEvent.emit("contacts/add", request.body);
//response.redirect("/");
break
}
},
};

View File

@ -1,4 +1,6 @@
import { permissionBits } from '../../../../utils/permissions.js';
import { readFile } from 'fs/promises';
import { navbar } from '../../../../utils/navbar.js';
export default {
path: "/dashboard/users",
@ -6,6 +8,9 @@ export default {
permissions: permissionBits.ADMIN,
type: "get",
async execute(request, response) {
response.redirect("/dashboard/users/list");
const html = await readFile(`${process.env.WWW}/dashboard/users/index.html`);
return await response.send(html.toString()
.replace('<NAVBAR>', navbar(request.session))
);
},
}

View File

@ -0,0 +1,44 @@
import { navbar } from '../../../../utils/navbar.js';
import { readFile } from 'fs/promises';
import { permissionBits, checkPermissions } from '../../../../utils/permissions.js';
export default {
path: "/dashboard/users/list",
requiresLogin: true,
permissions: permissionBits.ADMIN,
type: "get",
async execute(request, response) {
const userList = await global.database.users.findAll();
const userTable = genUserTable(userList);
const html = await readFile(`${process.env.WWW}/dashboard/users/list.html`);
return await response.send(html.toString()
.replace('<NAVBAR>', navbar(request.session))
.replace('<USERTABLE>', userTable)
);
},
}
function genUserTable(users) {
let res = `
<table>
<tr>
<th>Identifiant</th>
<th>Permissions</th>
<th>Action</th>
</tr>`;
for(const user of users) {
const userPermDict = checkPermissions(user.permissions);
let userPerms = [];
for (const [key, value] of Object.entries(userPermDict)) {
if(value && key != "default") userPerms.push(key);
};
res += `
<tr>
<td>${user.username}</td>
<td>${userPerms.join(", ")}</td>
<td><button>Supprimer</button></td>
</tr>`;
}
res += `</table>`;
return res
}

View File

@ -26,6 +26,7 @@ function checkPermissions(userPermissionBits) {
if(userPermissionBits & permissionBits.ADMIN) {
permissions.admin = true;
}
return permissions;
}
/*
let userPermissionBits = permissionBits.DEFAULT;

4
web.js
View File

@ -44,8 +44,8 @@ async function launchWeb() {
app.get("*", async (request, response) => {
const [path, args] = parseURL(request.originalUrl);
//console.log(parseURL(request.originalUrl));
console.log(`GET: ${path}${args ? "?" + args : ""}`);
const argNum = Object.keys(args).length;
console.log(`GET: ${path} ${argNum > 0 ? '('+argNum+' args)' : ""}`);
let handled = global.handler.get[path];
if(!handled) for(const path of Object.keys(global.handler.get)) {

View File

@ -9,7 +9,7 @@
<NAVBAR>
<h1>Dashboard</h1>
<p><a href='/calls'>Phoning</a></p>
<p><a href='/dashboard/users/create'>Ajouter un utilisateur</a></p>
<p><a href='/dashboard/users'>Gestion des utilisateurs</a></p>
<p><a href='/logout'>Déconnexion</a></p>
</body>
</html>

View File

@ -2,6 +2,7 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Utilisateurs</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;

View File

@ -0,0 +1,15 @@
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<title>Utilisateurs</title>
</head>
<body>
<NAVBAR>
<h1>Gestion des utilisateurs</h1>
<p><a href='/dashboard/users/list'>Liste des utilisateurs</a></p>
<p><a href='/dashboard/users/create'>Ajouter un utilisateur</a></p>
<p><a href='/dashboard'>Retour</a></p>
</body>
</html>

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Utilisateurs</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
table, th, td {
border:1px solid black;
}
</style>
</head>
<body>
<NAVBAR>
<USERTABLE>
</body>
</html>