neutral/bin/users.js
Raphix 9c1074de80
All checks were successful
Neutral/pipeline/head This commit looks good
Version 0.2.0 - PREVERSION - Ajout de certaines fonctionnalités
2023-11-02 18:16:15 +01:00

346 lines
9.1 KiB
JavaScript

const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const ulog = new LogType("Users")
const keygen = require("./keygen")
const uuid = require("uuid")
var usersList = new Map()
setup()
function setup() {
if(!fs.existsSync(__glob.USERS)) {
ulog.log("Création du fichier utilisateur dans : " + __glob.USERS)
fs.writeFileSync(__glob.USERS, JSON.stringify([], null, 2))
}
}
/**
*
* @returns Liste des utilisateurs
*/
module.exports.getUsers = function () {
return usersList
}
/**
* Get all users from Users Data Base
*/
module.exports.fetchUsers = function () {
ulog.step.init("fetch_user", "Récupération de tous les utilisateurs inscrit dans la base de donnée")
const userFile = getFile()
usersList = new Map()
for(var userFetched of userFile) {
const user = new this.User({
username: userFetched.username,
password: userFetched.password,
display_name: userFetched.display_name,
permission: userFetched.permission,
tokens: userFetched.tokens,
lastLogin: userFetched.lastLogin,
picture: userFetched.picture
})
usersList.set(user.username, user)
}
if(usersList.size == 0) {
const adminUser = new this.User({
"username": "admin",
"password": "neutral",
"display_name": "Administrateur",
"permission": [
"FILES_EXPLORER",
"SERVICES",
"SERVERS",
"PIPELINES",
"METRICS",
"USERS",
"LINKS",
"SETTINGS"
],
"tokens": [],
"lastLogin": "DEFAULT ACCOUNT",
"picture": "/images/users/default.jpg"
})
adminUser.register()
}
ulog.step.end("fetch_user")
}
/**
* User Class is used to access to default user's properties and methods
* @param {object} properties User properties with : username, password, display_name, permission...
*/
module.exports.User = class {
username = null
password = null;
display_name = null
permission = []
tokens = []
lastLogin = new Date()
picture = "/images/users/default.jpg"
constructor(properties) {
if(properties) {
this.username = properties.username
this.password = keygen.encrypt(properties.password)
this.display_name = properties.display_name
this.permission = properties.permission
this.tokens = properties.tokens
this.lastLogin = properties.lastLogin
this.picture = properties.picture
const userFile = getFile()
for(var userFetched of userFile) {
if(properties.username == userFetched.username) {
ulog.log("Récupération dans la base de donnée, de l'utilisateur : " + userFetched.username)
this.username = userFetched.username
this.password = userFetched.password
this.display_name = userFetched.display_name
this.permission = userFetched.permission
this.tokens = userFetched.tokens
this.lastLogin = userFetched.lastLogin
}
}
}
if(this.username == null) {
ulog.error("One of user is without username ! [IMPORANT_FIELD_IS_MISSING]")
this.username = Math.random()
}
if(this.password == null) {
ulog.error("'" + this.username + "' is without password ! Password reset to 'default' [IMPORANT_FIELD_IS_MISSING]")
this.password = keygen.encrypt("default")
}
if(this.display_name == null) {
ulog.warn("'" + this.username + "' is without display name !")
this.display_name = this.username
}
if(this.permission == null) {
ulog.warn("'" + this.username + "' has no longer permission !")
}
if(this.tokens == null) {
this.tokens = []
}
if(this.permission == null) {
this.permission = []
}
if(this.lastLogin == null) {
this.lastLogin = new Date()
}
if(this.picture == null) {
this.picture = "/images/users/default.jpg"
}
}
register() {
var alreadyExist = false
const userFile = getFile()
for(var userFetched of userFile) {
if(userFetched.username == this.username) {
userFile.splice(userFile.indexOf(userFetched), 1)
ulog.log("Mise à jour dans la base de donnée, de l'utilisateur : " + this.username)
alreadyExist = true
}
}
if(!alreadyExist) {
ulog.log("Création dans la base de donnée de l'utilisateur : " + this.username)
}
userFile.push(this)
updateFile(userFile)
usersList.set(this.username, this)
}
unregister() {
var alreadyExist = false
const userFile = getFile()
for(var userFetched of userFile) {
if(userFetched.username == this.username) {
userFile.splice(userFile.indexOf(userFetched), 1)
ulog.log("Mise à jour dans la base de donnée, de l'utilisateur : " + this.username)
alreadyExist = true
}
}
if(!alreadyExist) {
ulog.log("L'utilisateur n'est pas enregistré dans la base de donnée : " + this.username)
}
updateFile(userFile)
usersList.delete(this.username)
}
checkPermission(name) {
this.#sync()
if(this.permission.includes(name)) {
return true
} else {
return false
}
}
addPermission(name) {
this.#sync()
for(var perms of this.permission) {
if(name == perms) {
ulog.warn("'" + this.username + "' a déjà la permission : " + name)
return false
}
}
this.permission.push(name)
this.register()
}
removePermission(name) {
this.#sync()
var havePermission = false
for(var perms of this.permission) {
if(name == perms) {
havePermission = true
}
}
if(havePermission) {
this.permission.splice(this.permission.indexOf(name), 1)
this.register()
} else {
ulog.warn("'" + this.username + "' n'a pas la permission : " + name)
return false
}
}
setPassword(newPassword) {
this.#sync()
this.password = keygen.encrypt(newPassword)
this.register()
ulog.log("Le mot de passe de l'utilisateur a été modifié : " + this.username)
}
getPassword() {
this.#sync()
return keygen.decrypt(this.password)
}
generateToken() {
this.#sync()
const gToken = uuid.v4().toString()
this.tokens.push(gToken)
this.register()
return gToken
}
removeToken(token) {
this.#sync()
var haveToken = false
for(var aToken of this.tokens) {
if(token == aToken) {
haveToken = true
}
}
if(haveToken) {
this.tokens.splice(this.tokens.indexOf(token), 1)
this.register()
} else {
ulog.warn("'" + this.username + "' n'a pas le token : " + token)
return false
}
}
setPicture(text) {
this.#sync()
this.picture = text
this.register()
ulog.log("La photo de l'utilisateur a été modifié : " + this.username)
}
setDisplayName(text) {
this.#sync()
this.display_name = text
this.register()
ulog.log("Le nom d'affichage de l'utilisateur a été modifié : " + this.username)
}
setLastLogin(text) {
this.#sync()
this.lastLogin = text
this.register()
}
#sync() {
for(var userGet of usersList.keys()) {
const userFetched = usersList.get(userGet)
if(this.username == userFetched.username) {
this.username = userFetched.username
this.password = userFetched.password
this.display_name = userFetched.display_name
this.permission = userFetched.permission
this.tokens = userFetched.tokens
this.lastLogin = userFetched.lastLogin
}
}
}
}
/**
*
* @returns User File
*/
function getFile() {
const file = JSON.parse(fs.readFileSync(__glob.USERS))
return file
}
/**
* Update le fichier utilisateur avec un object
* @param {Array} file
*/
function updateFile(file) {
if(fs.existsSync(__glob.USERS)) {
ulog.log("Mise à jour du fichier utilisateur dans : " + __glob.USERS)
fs.writeFileSync(__glob.USERS, JSON.stringify(file, null, 2))
}
}