1
0
Fork 0

cli - user update and remove

master
Ruben Meyer 3 years ago
parent 2057e68d82
commit dfed144964
  1. 101
      bin/cli/cmds/user_management.js

@ -51,6 +51,11 @@ module.exports = {
// user already exists
} else {
vorpal.log("User exists: ");
rep.forEach((el, i) => {
el["passhash"] = undefined; // not needed
rep[i] = el;
});
vorpal.log(rep);
}
}
@ -67,6 +72,10 @@ module.exports = {
if(profile.user === '*') {
global['modules'].database.getUsers((err, rep) => {
if(rep) {
rep.forEach((el, i) => {
el["passhash"] = undefined; // not needed
rep[i] = el;
});
global['logs'].log(rep);
}
if(err) {
@ -77,12 +86,29 @@ module.exports = {
} else {
// query users by first input <nickname>
global['modules'].database.getUser(profile.user, (err, rep) => {
if(rep) {
// user exists
if(rep && rep.length == 1) {
global['logs'].log("User exists: ");
rep.forEach((el, i) => {
el["passhash"] = undefined; // not needed
rep[i] = el;
});
global['logs'].log(rep);
} else {
global['logs'].warn("User "+profile.user+" doesn't exist.");
// found more than one user
if(rep && rep.length >= 2) {
global['logs'].warn("multiple users found for "+profile.user+".");
rep.forEach((el) => {
global['logs'].warn("found user with id: "+el._id);
});
// user does not exist
} else {
global['logs'].warn("User "+profile.user+" doesn't exist.");
}
}
// query error
if(err) {
global['logs'].error('$ user get '+profile.user);
global['logs'].error(err);
@ -90,30 +116,75 @@ module.exports = {
});
}
// update users, just one field
// update users, just one property
} else if(action === 'update') {
if(args.data.length < 2) vorpal.log("No data supplied.");
if(args.data.length < 2) global['logs'].error("No data supplied.");
else {
let field = args.data[0];
let property = args.data[0];
let param = args.data[1];
vorpal.log("Field: "+field+"; Param: "+param);
global['logs'].debug("Prop: "+property+"; Param: "+param);
// query user
global['modules'].database.getUser(profile.user, (err, rep) => {
// user exists
if(rep && rep.length == 1) {
let obj = {};
obj[property] = param;
global['modules'].database.updateUser(String(rep[0]._id), obj, (errUpd, repUpd) => {
// updated user
if(repUpd) {
global['logs'].log("User with id "+String(rep[0]._id)+" was updated.");
// user not updated
} else {
global['logs'].warn("User with id "+String(rep[0]._id)+" doesn't exist.");
}
// query error
if(errUpd) {
global['logs'].error('$ user update '+profile.user+' '+property+' '+param+' [on update]');
global['logs'].error(errUpd);
}
});
} else {
// found more than one user
if(rep && rep.length >= 2) {
global['logs'].warn("multiple users found for "+profile.user+". bad state. can't update.");
rep.forEach((el) => {
global['logs'].warn("found user with id: "+el._id);
});
// user does not exist
} else {
global['logs'].warn("User "+profile.user+" doesn't exist.");
}
}
// query error
if(err) {
global['logs'].error('$ user update '+profile.user+' '+field+' '+param+' [on query]');
global['logs'].error(err);
}
});
}
// remove users
} else if(action === 'remove' || action === 'delete') {
// haystack
let haystack = profile.user;
if(typeof profile.mail !== 'undefined' && profile.mail !== null) haystack = [profile.user, profile.mail];
// query user
global['modules'].database.getUser(haystack, (err, rep) => {
if(rep) {
vorpal.log("User exists. Deleting him.");
vorpal.log("user exists. deleting him.");
global['logs'].debug(rep);
// remove user
global['modules'].database.delUser(rep[0].email, (errDel, repDel) => {
if(repDel) {
vorpal.log("Deleted user.");
vorpal.log("deleted user.");
global['logs'].debug(repDel);
}
else {
vorpal.log("ERR: While deleting user.");
vorpal.log("ERR: while deleting user.");
global['logs'].debug(errDel);
}
});
@ -129,14 +200,14 @@ module.exports = {
vorpal.log("---");
vorpal.log("returning 0 or 1 and printing errors");
vorpal.log("returns 0 or 1 and printing errors");
} else if(args.nick === 'get') {
vorpal.log("user get <nickname or email>");
vorpal.log("<nickname or email>: searching both in both; format: foobar OR user@example.tld");
vorpal.log("---");
vorpal.log("user get * - to get all users");
vorpal.log("printing JSON-object of user data");
vorpal.log("prints JSON-object of user data");
} else if(args.nick === 'update') {
vorpal.log("user update <nickname> <field> <parameter>");
vorpal.log("<nickname>: user nickname");
@ -145,15 +216,15 @@ module.exports = {
vorpal.log("---");
vorpal.log("returning 0 or 1 and printing errors");
vorpal.log("printing JSON-object of updated user data");
vorpal.log("returns 0 or 1 and printing errors");
vorpal.log("prints JSON-object of updated user data");
} else if(args.nick === 'remove' || args.nick === 'delete') {
vorpal.log("user remove|delete <nickname>");
vorpal.log("<nickname>: user nickname");
vorpal.log("<nickname>: user nickname or mail");
vorpal.log("---");
vorpal.log("returning 0 or 1 and printing errors");
vorpal.log("returns 0 or 1 and printing errors");
}
}

Loading…
Cancel
Save