Version 1.0.0 - Ajout de WebMetrics et de l'édit utilisateur
All checks were successful
Neutral/pipeline/head This commit looks good

This commit is contained in:
2024-01-13 18:44:27 +01:00
parent 1b49de6286
commit 3484ff9ff7
21 changed files with 732 additions and 32 deletions

View File

@@ -10,7 +10,8 @@ const __glob = {
USERS: root + path.sep + "data" + path.sep + "users.json",
CONFIG: root + path.sep + "data" + path.sep + "config.json",
SHARED: root + path.sep + "data" + path.sep + "shared",
USERS_IMAGES: root + path.sep + "public" + path.sep + 'images' + path.sep + "users",
USERS_IMAGES: root + path.sep + "data" + path.sep + "user_images",
PACKAGE_JSON: root + path.sep + "package.json",
};

112
bin/metrics.js Normal file
View File

@@ -0,0 +1,112 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
var CryptoJS = require("crypto-js")
const { __glob } = require("./global-variables")
const { captureRejectionSymbol } = require("events")
const clog = new LogType("Metrics")
if(!fs.existsSync(__glob.DATA + path.sep + "metrics.json")) {
fs.writeFileSync(__glob.DATA + path.sep + "metrics.json", JSON.stringify([], null, 2))
}
module.exports.getDataMetrics = function() {
return JSON.parse(fs.readFileSync(__glob.DATA + path.sep + "metrics.json"))
}
module.exports.getMetrics = function() {
const metrics = this.getDataMetrics()
var metricsToReturn = new Array()
return new Promise(async (resolve, reject) => {
// Count the number processed
var processed = 0
await metrics.forEach(async (metric) => {
// Try to connect to the metric server with the key in query params named "privatekey"
// If the connection is successful, we add the metric to the list of metrics to return
const url = `http://${metric.address}:${metric.port}/metrics?privatekey=${metric.key}`
const res = await fetch(url, {
method: "GET",
headers: {
"Accept": "application/json",
},
credentials: "include"
}).then(res => res.json())
.then(res => {
if(res) {
metric.data = res
metricsToReturn.push(metric)
} else {
metric.data = "ERROR"
metricsToReturn.push(metric)
}
})
processed++
if(processed == metrics.length) {
resolve(metricsToReturn)
}
})
})
}
module.exports.addMetric = function(settings) {
const metrics = this.getDataMetrics()
const metric = {
id: makeid(8),
name: settings.name,
address: settings.address,
port: settings.port,
key: settings.key,
}
metrics.push(metric)
fs.writeFileSync(__glob.DATA + path.sep + "metrics.json", JSON.stringify(metrics, null, 2))
return "OK"
}
module.exports.deleteMetric = function(id) {
const metrics = this.getDataMetrics()
metrics.forEach((metric) => {
if(metric.id == id) {
metrics.splice(metrics.indexOf(metric), 1)
}
})
fs.writeFileSync(__glob.DATA + path.sep + "metrics.json", JSON.stringify(metrics, null, 2))
return "OK"
}
function makeid(length) {
var result = [];
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result.push(characters.charAt(Math.floor(Math.random() *
charactersLength)));
}
return result.join('');
}

View File

@@ -13,6 +13,7 @@ const plog = new LogType("Web")
const cook = require("cookie")
const http = require("http")
const servermetrics = require("./server-metrics.js")
const metrics = require("./metrics.js")
const pm2 = require('pm2');
/**
@@ -56,6 +57,11 @@ module.exports.serverIO = function(server) {
* POST REQUEST
*/
PostRequest("US_EDIT_PERSONNAL", async (settings) => {
user = auth.getUserByToken(token)
PostAnswer("US_EDIT_PERSONNAL", await users.editMySelf(settings, user))
})
if(user.checkPermission("FILES_EXPLORER")) {
PostRequest("FX_GET", (root) => {
@@ -217,6 +223,22 @@ module.exports.serverIO = function(server) {
})
}
if(user.checkPermission("METRICS")) {
GetRequest("MT_ALL", async () => {
GetAnswer("MT_ALL", await metrics.getMetrics())
})
PostRequest("MT_ADD", async (settings) => {
PostAnswer("MT_ADD", await metrics.addMetric(settings))
})
PostRequest("MT_DELETE", async (settings) => {
PostAnswer("MT_DELETE", await metrics.deleteMetric(settings))
})
}
socket.on("disconnect", (reason) => {
plog.log("Déconnexion du panel par '" + user.username + "' avec le socket : " + socket.id)

View File

@@ -77,7 +77,7 @@ module.exports.fetchUsers = function () {
],
"tokens": [],
"lastLogin": "DEFAULT ACCOUNT",
"picture": "/images/users/default.jpg"
"picture": "/images/default.jpg"
})
@@ -103,7 +103,7 @@ module.exports.User = class {
permission = []
tokens = []
lastLogin = new Date()
picture = "/images/users/default.jpg"
picture = "/images/default.jpg"
constructor(properties) {
@@ -164,7 +164,7 @@ module.exports.User = class {
this.lastLogin = new Date()
}
if(this.picture == null) {
this.picture = "/images/users/default.jpg"
this.picture = "/images/default.jpg"
}
@@ -330,7 +330,7 @@ module.exports.User = class {
var pictureDir = __glob.USERS_IMAGES + path.sep + uuid.v4().toString() + ".png"
fs.writeFileSync(pictureDir, file)
this.picture = pictureDir.replace(__glob.USERS_IMAGES + path.sep, "/images/users/")
this.picture = pictureDir.replace(__glob.USERS_IMAGES + path.sep, "/users/")
this.register()
}
@@ -383,7 +383,7 @@ module.exports.addUser = function(settings) {
if(settings.picture == null) {
pictureDir = "/images/users/default.jpg"
pictureDir = "/images/default.jpg"
} else {
pictureDir = __glob.USERS_IMAGES + path.sep + uuid.v4().toString() + ".png"
fs.writeFileSync(pictureDir, settings.picture)
@@ -465,6 +465,53 @@ module.exports.editUser = function(settings) {
}
}
module.exports.editMySelf = function (settings, user) {
if(user.username == settings.actualUsername) {
if(settings.username == '') {
ulog.error("Le nom d'utilisateur est manquant")
return "USERNAME_MISSING"
} else if(settings.display_name == '') {
ulog.error("Le nom d'affichage est manquant")
return "DISPLAY_NAME_MISSING"
} else {
ulog.step.init("edit_user", "Modification d'un utilisateur dans la base de donnée : " + settings.username)
const user = this.fetchUsers().get(settings.username)
if(user) {
console.log(settings)
if(settings.newusername && settings.newusername != settings.username) {
if(this.getUser(settings.newusername)) {
ulog.error("L'utilisateur existe déjà : " + settings.username)
return "ALREADY_EXIST"
} else {
user.setNewUsername(settings.newusername)
}
}
if(settings.display_name) {
user.setDisplayName(settings.display_name)
}
if(settings.password) {
user.setPassword(settings.password)
}
if(settings.picture) {
user.setPicture(settings.picture)
}
ulog.step.end("edit_user")
return "OK"
} else {
ulog.step.end("edit_user")
return "NOT_EXIST"
}
}
} else {
ulog.error("Vous ne pouvez pas modifier les informations d'un autre utilisateur !")
return "NOT_ALLOWED"
}
}
module.exports.clearTokens = function(username) {
const user = this.fetchUsers().get(username)

View File

@@ -6,7 +6,7 @@
var log = require("loguix")
var {LogType} = require("loguix")
var { __glob } = require("./global-variables")
log.setup(__glob.LOGS)
log.setup(__glob.LOGS, __glob.PACKAGE_JSON)
const wlog = new LogType("Serveur")