neutral-old/neutral-functions.js

393 lines
10 KiB
JavaScript
Raw Normal View History

2023-02-18 21:09:04 +00:00
var fs = require("fs")
var uuid = require('uuid')
var path = require("path")
var CryptoJS = require("crypto-js")
2023-02-22 12:57:27 +00:00
var Jimp = require("jimp")
2023-02-18 21:09:04 +00:00
2023-02-23 13:47:21 +00:00
module.exports.checkUser = () => {
const userDir = fs.readdirSync(__dirname + path.sep + "users" + path.sep)
if(userDir.length == 0) {
this.createUser("root","neutral",4,"Administrateur")
}
}
2023-02-18 21:09:04 +00:00
2023-02-22 12:57:27 +00:00
module.exports.createUser = (name, password, level, fullname) => {
2023-02-18 21:09:04 +00:00
2023-02-22 12:57:27 +00:00
if(this.getUUID(name) == false) {
const passcrypt = CryptoJS.AES.encrypt(password, "D*G-KaPdSgVkYp3s");
const userUUID = uuid.v4();
const userData = {
"username":name,
"password": passcrypt.toString(),
"uuid": userUUID,
"fullname": fullname,
"permissionLevel":level,
2023-04-01 14:48:13 +00:00
"tokens":{},
2023-02-22 12:57:27 +00:00
"lastconnexion":0
}
const src = __dirname + path.sep + "public" + path.sep + "images" + path.sep + "standalone.png"
const dest = __dirname + path.sep + "public" + path.sep + "images" + path.sep + "userspics" + path.sep + name + ".png"
fs.copyFileSync(src, dest)
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + userUUID + ".json", JSON.stringify(userData, null, 2))
return "USER_CREATED"
} else {
return "USER_ALREADY_EXIST"
}
}
2023-02-23 13:47:21 +00:00
2023-02-22 12:57:27 +00:00
module.exports.deleteUser = (name) => {
const userUUID = this.getUUID(name)
if(userUUID != false) {
fs.rmSync(__dirname + path.sep + "users" + path.sep + userUUID + ".json")
fs.rmSync(__dirname + path.sep + "public" + path.sep + "images" + path.sep + "userspics"+ path.sep + name+ ".png")
return "USER_DELETED"
} else {
return "USER_ALREADY_DELETED"
2023-02-18 21:09:04 +00:00
}
2023-02-22 12:57:27 +00:00
}
module.exports.deleteToken = (username) => {
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + this.getUUID(username) + ".json", 'utf-8'))
2023-04-28 20:36:23 +00:00
userData.tokens = {}
2023-02-22 12:57:27 +00:00
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + this.getUUID(username) + ".json", JSON.stringify(userData, null, 2))
2023-02-18 21:09:04 +00:00
}
module.exports.checkToken = (req, res) => {
const tokens = this.getAllToken()
const users = this.getUsers()
if(req.cookies.tokenID == null) {
2023-02-22 12:57:27 +00:00
const checkTokenData = {"name":false}
return checkTokenData;
2023-02-18 21:09:04 +00:00
} else if(tokens.has(req.cookies.tokenID)) {
const user = tokens.get(req.cookies.tokenID)
2023-02-21 11:02:37 +00:00
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + users.get(user) + ".json", 'utf-8'))
2023-02-18 21:09:04 +00:00
2023-02-22 12:57:27 +00:00
userData.lastconnexion = Date.now()
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + userData.uuid + ".json", JSON.stringify(userData, null, 2))
2023-04-01 14:48:13 +00:00
if(userData.tokens[req.cookies.tokenID].livableToken == true) {
2023-02-18 21:09:04 +00:00
2023-02-22 12:57:27 +00:00
const checkTokenData = {"name":user, "permLevel": userData.permissionLevel, "fullname":userData.fullname, "lastconnexion":userData.lastconnexion};
return checkTokenData;
2023-02-18 21:09:04 +00:00
} else {
2023-04-01 14:48:13 +00:00
const tokenDate = new Date(userData.tokens[req.cookies.tokenID].createdAt)
2023-02-18 21:09:04 +00:00
const nowDate = new Date(Date.now())
if(tokenDate.getDay() == nowDate.getDay() && tokenDate.getMonth() == nowDate.getMonth()) {
2023-02-22 12:57:27 +00:00
const checkTokenData = {"name":user, "permLevel": userData.permissionLevel, "fullname":userData.fullname, "lastconnexion":userData.lastconnexion};
return checkTokenData;
2023-02-18 21:09:04 +00:00
} else {
2023-04-01 14:48:13 +00:00
this.clearSpecificCookie(req.cookies.tokenID);
2023-02-21 11:02:37 +00:00
res.clearCookie('tokenID');
2023-04-01 14:48:13 +00:00
2023-02-22 12:57:27 +00:00
const checkTokenData = {"name":false}
return checkTokenData;
2023-02-18 21:09:04 +00:00
}
}
} else {
2023-02-22 12:57:27 +00:00
const checkTokenData = {"name":false}
return checkTokenData;
2023-02-18 21:09:04 +00:00
}
2023-02-22 12:57:27 +00:00
2023-02-18 21:09:04 +00:00
2023-04-01 14:48:13 +00:00
}
module.exports.clearSpecificCookie = (token) => {
const tokens = this.getAllToken()
const user = tokens.get(token)
const users = this.getUsers()
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + users.get(user) + ".json", 'utf-8'))
userData["tokens"].token = null;
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + userData.uuid + ".json", JSON.stringify(userData, null, 2))
}
module.exports.clearCookies = (token) => {
const tokens = this.getAllToken()
const user = tokens.get(token)
const users = this.getUsers()
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + users.get(user) + ".json", 'utf-8'))
userData["tokens"] = {};
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + userData.uuid + ".json", JSON.stringify(userData, null, 2))
2023-02-18 21:09:04 +00:00
}
2023-02-21 11:02:37 +00:00
module.exports.getFullName = (username) => {
2023-02-22 12:57:27 +00:00
2023-02-21 11:02:37 +00:00
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + this.getUUID(username) + ".json", 'utf-8'))
2023-02-22 12:57:27 +00:00
2023-02-21 11:02:37 +00:00
return userData.fullname
}
2023-02-18 21:09:04 +00:00
module.exports.generateTokenID = (username, userData, req, users) => {
const tokenID = uuid.v4()
const date = Date.now()
var newUserData = userData;
var livable = false;
if(req.body.remindus == true) {
livable = true;
}
2023-04-01 14:48:13 +00:00
newUserData.tokens[tokenID] = {
"tokenID":tokenID,
"livableToken": livable,
"createdAt": date
}
2023-02-18 21:09:04 +00:00
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + users.get(username) + ".json", JSON.stringify(newUserData, null, 2))
return tokenID
}
module.exports.getUsers = () => {
const users = new Map();
fs.readdirSync(__dirname + path.sep + "users").forEach(file => {
2023-02-21 11:02:37 +00:00
const fileData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + file, 'utf-8'))
2023-02-18 21:09:04 +00:00
users.set(fileData.username, fileData.uuid)
})
return users
}
module.exports.getAllToken = () => {
const token = new Map();
fs.readdirSync(__dirname + path.sep + "users").forEach(file => {
2023-02-21 11:02:37 +00:00
const fileData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + file, 'utf-8'))
2023-04-01 14:48:13 +00:00
for(var tokenIDs of Object.keys(fileData.tokens)) {
token.set(fileData.tokens[tokenIDs].tokenID, fileData.username)
console.log(token)
}
2023-02-18 21:09:04 +00:00
})
return token
2023-02-21 11:02:37 +00:00
}
module.exports.getUUID = (username) => {
const users = new Map();
fs.readdirSync(__dirname + path.sep + "users").forEach(fileD => {
const fileData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + fileD, 'utf-8'))
users.set(fileData.username, fileData.uuid)
})
2023-02-22 12:57:27 +00:00
var data = users.get(username);
if(typeof data == "undefined") {
data = false
}
return data;
2023-02-21 11:02:37 +00:00
}
module.exports.changeFullName = (username, req, res) => {
2023-02-22 12:57:27 +00:00
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + this.getUUID(this.checkToken(req, res).name) + ".json", 'utf-8'))
2023-02-21 11:02:37 +00:00
Object.defineProperties(userData, {
fullname: {
value: username,
writable: true
}
})
const user = this.getUUIDRequest(req);
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + user + ".json", JSON.stringify(userData, null, 2))
}
module.exports.getUUIDRequest = (req) => {
const users = this.getUsers()
const tokens = this.getAllToken()
users.get(tokens.get(req.cookies.tokenID))
if(req.cookies.tokenID == null) {
return false;
} else if(tokens.has(req.cookies.tokenID)) {
return users.get(tokens.get(req.cookies.tokenID))
} else {
return false;
}
}
module.exports.changePassword = (req, res) => {
2023-02-22 12:57:27 +00:00
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + this.getUUID(this.checkToken(req, res).name) + ".json", 'utf-8'))
2023-02-21 11:02:37 +00:00
const oldp = CryptoJS.AES.encrypt(req.body.additional, "D*G-KaPdSgVkYp3s")
const newp = CryptoJS.AES.encrypt(req.body.value, "D*G-KaPdSgVkYp3s")
if(CryptoJS.AES.decrypt(oldp, "D*G-KaPdSgVkYp3s").toString(CryptoJS.enc.Utf8) == CryptoJS.AES.decrypt(userData.password, "D*G-KaPdSgVkYp3s").toString(CryptoJS.enc.Utf8)) {
Object.defineProperties(userData, {
password: {
value: newp.toString(),
writable: true
}
})
const user = this.getUUIDRequest(req);
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + user + ".json", JSON.stringify(userData, null, 2))
res.send({"result":"success", "content": "<span style='color:rgb(130, 255, 163);'>Le mot de passe a été changé avec succès.</span>"})
} else {
res.send({"result":"success", "content": "Le mot de passe actuel est éronné."})
}
}
2023-02-22 12:57:27 +00:00
module.exports.getAll = (username) => {
const users = this.getUsers()
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + users.get(username) + ".json", 'utf-8'))
const allData = {"username":username, "permLevel": userData.permissionLevel, "fullname":userData.fullname, "lastconnexion":userData.lastconnexion};
return allData
}
module.exports.editUser = (requestData) => {
if(this.getUUID(requestData.original) != false) {
const userData = JSON.parse(fs.readFileSync(__dirname + path.sep + "users" + path.sep + this.getUUID(requestData.original) + ".json", 'utf-8'))
userData.token = {};
userData.username = requestData.username;
userData.fullname = requestData.fullname;
userData.permissionLevel = requestData.permLevel;
if(requestData.password != "") {
userData.password = CryptoJS.AES.encrypt(requestData.password, "D*G-KaPdSgVkYp3s").toString()
}
fs.renameSync(__dirname + path.sep + "public" + path.sep + "images" + path.sep + "userspics" + path.sep + requestData.original + ".png",__dirname + path.sep + "public" + path.sep + "images" + path.sep + "userspics" + path.sep + requestData.username + ".png")
fs.writeFileSync(__dirname + path.sep + "users" + path.sep + this.getUUID(requestData.original) + ".json", JSON.stringify(userData, null, 2))
return true;
} else {
return false;
}
}