revert 6f637968bdd5dea7f472e5a07517e072ed69715e
All checks were successful
Neutral/pipeline/head This commit looks good
All checks were successful
Neutral/pipeline/head This commit looks good
revert Version 1.0.0 - Basic Web Application
This commit is contained in:
128
bin/users.js
128
bin/users.js
@ -50,8 +50,10 @@ module.exports.fetchUsers = function () {
|
||||
username: userFetched.username,
|
||||
password: userFetched.password,
|
||||
display_name: userFetched.display_name,
|
||||
permission: userFetched.permission,
|
||||
tokens: userFetched.tokens,
|
||||
lastLogin: userFetched.lastLogin,
|
||||
picture: userFetched.picture
|
||||
|
||||
})
|
||||
|
||||
@ -61,10 +63,21 @@ module.exports.fetchUsers = function () {
|
||||
if(usersList.size == 0) {
|
||||
const adminUser = new this.User({
|
||||
"username": "admin",
|
||||
"password": "inventory",
|
||||
"password": "neutral",
|
||||
"display_name": "Administrateur",
|
||||
"permission": [
|
||||
"FILES_EXPLORER",
|
||||
"SERVICES",
|
||||
"SERVERS",
|
||||
"PIPELINES",
|
||||
"METRICS",
|
||||
"USERS",
|
||||
"LINKS",
|
||||
"SETTINGS"
|
||||
],
|
||||
"tokens": [],
|
||||
"lastLogin": "DEFAULT ACCOUNT",
|
||||
"picture": "/images/default.jpg"
|
||||
})
|
||||
|
||||
|
||||
@ -81,14 +94,16 @@ module.exports.fetchUsers = function () {
|
||||
|
||||
/**
|
||||
* User Class is used to access to default user's properties and methods
|
||||
* @param {object} properties User properties with : username, password, display_name
|
||||
* @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/default.jpg"
|
||||
|
||||
constructor(properties) {
|
||||
|
||||
@ -97,8 +112,10 @@ module.exports.User = class {
|
||||
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()
|
||||
|
||||
@ -108,6 +125,7 @@ module.exports.User = class {
|
||||
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
|
||||
}
|
||||
@ -132,14 +150,22 @@ module.exports.User = class {
|
||||
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/default.jpg"
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -185,7 +211,47 @@ module.exports.User = class {
|
||||
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()
|
||||
@ -227,6 +293,14 @@ module.exports.User = class {
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
@ -251,6 +325,24 @@ module.exports.User = class {
|
||||
this.register()
|
||||
}
|
||||
|
||||
setPicture(file) {
|
||||
this.#sync()
|
||||
var pictureDir = __glob.USERS_IMAGES + path.sep + uuid.v4().toString() + ".png"
|
||||
if(!fs.existsSync(__glob.USERS_IMAGES)) {
|
||||
fs.mkdirSync(__glob.USERS_IMAGES)
|
||||
}
|
||||
fs.writeFileSync(pictureDir, file)
|
||||
|
||||
this.picture = pictureDir.replace(__glob.USERS_IMAGES + path.sep, "/users/")
|
||||
this.register()
|
||||
}
|
||||
|
||||
setPermissions(permissions) {
|
||||
this.#sync()
|
||||
this.permission = permissions
|
||||
this.register()
|
||||
}
|
||||
|
||||
clearTokens() {
|
||||
this.#sync()
|
||||
this.tokens = []
|
||||
@ -265,6 +357,7 @@ module.exports.User = class {
|
||||
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
|
||||
}
|
||||
@ -289,10 +382,23 @@ module.exports.addUser = function(settings) {
|
||||
return "ALREADY_EXIST"
|
||||
} else {
|
||||
ulog.step.init("add_user", "Ajout d'un utilisateur dans la base de donnée : " + settings.username)
|
||||
var pictureDir = null
|
||||
|
||||
|
||||
if(settings.picture == null) {
|
||||
pictureDir = "/images/default.jpg"
|
||||
} else {
|
||||
pictureDir = __glob.USERS_IMAGES + path.sep + uuid.v4().toString() + ".png"
|
||||
fs.writeFileSync(pictureDir, settings.picture)
|
||||
|
||||
|
||||
}
|
||||
|
||||
const user = new this.User({
|
||||
username: settings.username,
|
||||
display_name: settings.display_name
|
||||
display_name: settings.display_name,
|
||||
permission: settings.permissions,
|
||||
picture: pictureDir.replace(__glob.USERS_IMAGES + path.sep, "/images/users/")
|
||||
|
||||
})
|
||||
|
||||
@ -345,8 +451,13 @@ module.exports.editUser = function(settings) {
|
||||
user.setPassword(settings.password)
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(settings.picture) {
|
||||
user.setPicture(settings.picture)
|
||||
}
|
||||
if(settings.permissions) {
|
||||
|
||||
user.setPermissions(settings.permissions)
|
||||
}
|
||||
ulog.step.end("edit_user")
|
||||
return "OK"
|
||||
} else {
|
||||
@ -386,7 +497,10 @@ module.exports.editMySelf = function (settings, user) {
|
||||
if(settings.password) {
|
||||
user.setPassword(settings.password)
|
||||
}
|
||||
|
||||
|
||||
if(settings.picture) {
|
||||
user.setPicture(settings.picture)
|
||||
}
|
||||
ulog.step.end("edit_user")
|
||||
return "OK"
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user