Add user list and update user management
This commit is contained in:
parent
651f870415
commit
be62b85c52
4
index.js
4
index.js
@ -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);
|
||||
});
|
||||
|
||||
|
@ -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);
|
||||
},
|
||||
}
|
@ -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");
|
||||
},
|
||||
}
|
22
src/html/pages/api/contacts.js
Normal file
22
src/html/pages/api/contacts.js
Normal 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
|
||||
}
|
||||
},
|
||||
};
|
@ -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))
|
||||
);
|
||||
},
|
||||
}
|
||||
|
44
src/html/pages/dashboard/users_list.js
Normal file
44
src/html/pages/dashboard/users_list.js
Normal 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
|
||||
}
|
@ -26,6 +26,7 @@ function checkPermissions(userPermissionBits) {
|
||||
if(userPermissionBits & permissionBits.ADMIN) {
|
||||
permissions.admin = true;
|
||||
}
|
||||
return permissions;
|
||||
}
|
||||
/*
|
||||
let userPermissionBits = permissionBits.DEFAULT;
|
||||
|
4
web.js
4
web.js
@ -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)) {
|
||||
|
@ -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>
|
||||
|
@ -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;
|
||||
|
15
www/dashboard/users/index.html
Normal file
15
www/dashboard/users/index.html
Normal 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>
|
17
www/dashboard/users/list.html
Normal file
17
www/dashboard/users/list.html
Normal 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>
|
Loading…
Reference in New Issue
Block a user