diff --git a/bin/global-variables.js b/bin/global-variables.js index 6b12d64..d563837 100644 --- a/bin/global-variables.js +++ b/bin/global-variables.js @@ -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", }; diff --git a/bin/metrics.js b/bin/metrics.js new file mode 100644 index 0000000..a461b3b --- /dev/null +++ b/bin/metrics.js @@ -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(''); +} \ No newline at end of file diff --git a/bin/servers.js b/bin/servers.js index 336917a..54c0609 100644 --- a/bin/servers.js +++ b/bin/servers.js @@ -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) diff --git a/bin/users.js b/bin/users.js index dd2f29e..0861aeb 100644 --- a/bin/users.js +++ b/bin/users.js @@ -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) diff --git a/bin/www b/bin/www index 111feaa..aeb5560 100644 --- a/bin/www +++ b/bin/www @@ -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") diff --git a/main.js b/main.js index 955a27c..5a258c0 100644 --- a/main.js +++ b/main.js @@ -48,6 +48,7 @@ function setup() { app.use(express.static(path.join(__dirname, 'public'))); app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))) app.use("/shared", express.static(__glob.SHARED)) + app.use("/users", express.static(__glob.USERS_IMAGES)) getRouters() diff --git a/package-lock.json b/package-lock.json index 7990bd0..e358e81 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "neutral", - "version": "0.6.0", + "version": "0.7.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "neutral", - "version": "0.6.0", + "version": "0.7.0", "license": "ISC", "dependencies": { "cookie-parser": "~1.4.4", @@ -16,7 +16,7 @@ "express": "~4.16.1", "http-errors": "~1.6.3", "install": "^0.13.0", - "loguix": "1.4.1", + "loguix": "^1.4.2", "nodemon": "^3.0.1", "os-utils": "^0.0.14", "pm2": "^5.3.0", @@ -1326,8 +1326,9 @@ } }, "node_modules/loguix": { - "version": "1.4.1", - "license": "ISC" + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/loguix/-/loguix-1.4.2.tgz", + "integrity": "sha512-fWp699F5Dqszpnriwotr3XvsaPXYAjmV3X2L4tmqUXgwHggvx1Fak1z3Ac7CeuVdIdYY5l85UQrEYPmhAb+IUw==" }, "node_modules/lru-cache": { "version": "6.0.0", diff --git a/package.json b/package.json index 2803d9a..4abaaa8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "neutral", - "version": "0.6.1", + "version": "1.0.0", "description": "Panel d'administration de Raphix", "main": "index.js", "scripts": { @@ -27,7 +27,7 @@ "express": "~4.16.1", "http-errors": "~1.6.3", "install": "^0.13.0", - "loguix": "1.4.1", + "loguix": "^1.4.2", "nodemon": "^3.0.1", "os-utils": "^0.0.14", "pm2": "^5.3.0", diff --git a/public/images/users/default.jpg b/public/images/default.jpg similarity index 100% rename from public/images/users/default.jpg rename to public/images/default.jpg diff --git a/public/images/users/8e63fe73-66c4-4f48-9220-c07fef237eee.png b/public/images/users/8e63fe73-66c4-4f48-9220-c07fef237eee.png deleted file mode 100644 index ca40fc8..0000000 Binary files a/public/images/users/8e63fe73-66c4-4f48-9220-c07fef237eee.png and /dev/null differ diff --git a/public/javascripts/basics.js b/public/javascripts/basics.js index 860fd92..08ed476 100644 --- a/public/javascripts/basics.js +++ b/public/javascripts/basics.js @@ -1180,6 +1180,86 @@ class User { } } +/** + * Permet de créer un composant de vue de type "Metrics" + */ +class Metric { + properties; + View; + Component; + data; + constructor(settings) { + this.properties = settings.properties + this.View = settings.View + this.Component = settings.Component + this.data = settings.properties.data + } + + generateHTML() { + return ` +
${this.properties.name}
+${this.properties.id}
+${data.description}
${data.name}
Valeur : ${data.value}
Voulez-vous vraiment supprimer la métrique ${this.properties.name} ?
+ + + + ` + }) + + const deleteCButton = getID(`${this.properties.id}_deleteconfirm`) + + deleteCButton.addEventListener("click", () => { + const request = post(`MT_DELETE`, this.properties.id) + request.then((answer) => { + if(answer == "OK") { + this.View.destroyPopup() + this.View.destroy() + this.Component.forceWindow() + + } else { + console.log(answer) + } + }) + }) + }) + } + + + +} /** * Permet de créer un item de la barre des tâches @@ -1196,7 +1276,6 @@ class ViewItem { show() { const viewItems = getID("views-items") - const item = document.createElement("div") item.id = `${this.window.ViewProperties.title}_item` item.classList.add("view-item") diff --git a/public/javascripts/filexplorer.js b/public/javascripts/filexplorer.js index 55db436..8fc329b 100644 --- a/public/javascripts/filexplorer.js +++ b/public/javascripts/filexplorer.js @@ -14,7 +14,7 @@ explorer.createWindow(() => { }) - + @@ -58,6 +58,9 @@ explorer.createWindow(() => {Chargement en cours ...
+Chargement en cours ...
+Chargement en cours ...
+Une erreur est survenue lors du chargement des WebMetrik
+Adresse
+ +Port
+ +Nom
+ +Clé d'authentification
+ + + +Une erreur est survenue lors de l'ajout de la WebMetrik
` + }) + } + }) + }) + + }) + + + } +}) + + + + /* +Chargement en cours ...
+Mes informations
+Nom d'utilisateur
+ +Nom d'affichage
+ +Mot de passe
+ +Photo de profil
+ + + + +Mes permissions
+" + permValue + "
") + + }) + + usSettingsPermissions.innerHTML = permValid.join("") + }) + + usSettingsSave.addEventListener("click", () => { + const request = post(`US_EDIT_PERSONNAL`, {username: actualUsername, newusername: usSettingsUsername.value, display_name: usSettingsDisplayname.value, password: usSettingsPassword.value, picture: usSettingsPicture.files[0]}) + request.then((answer) => { + if(answer == "ALREADY_EXIST") { + + returnInfo.err("L'utilisateur existe déjà !") + + } else if(answer == "USERNAME_MISSING") { + + returnInfo.err("Le nom d'utilisateur est manquant !") + } else if(answer == "DISPLAY_NAME_MISSING") { + + returnInfo.err("Le nom d'affichage est manquant !") + } else if(answer == "PASSWORD_MISSING") { + + returnInfo.err("Le mot de passe est manquant !") + } else { + + View.destroy() + loadUserInfo() + } + }) + }) + + +}) + +document.addEventListener("click", (e) => { + if(e.target != menulogo) { + menu.style.display = "none" + } +}) \ No newline at end of file diff --git a/public/javascripts/pipeline.js b/public/javascripts/pipeline.js index 3260870..2e9fbfd 100644 --- a/public/javascripts/pipeline.js +++ b/public/javascripts/pipeline.js @@ -36,7 +36,7 @@ pipelines.createWindow(() => { View.setContent(`Chargement en cours ...
+