1
0
Fork 0

cli - user update

This commit is contained in:
Ruben Meyer 2019-09-01 21:29:36 +02:00
parent e618dd046b
commit c85b0deea7
3 changed files with 179 additions and 70 deletions

View File

@ -5,7 +5,7 @@
*/ */
module.exports = { module.exports = {
'command': 'user <action> <nick> [pass] [mail] [group] [data...]', 'command': 'user <action> <nick> [data...]',
'description': 'add, get, update or remove an user', 'description': 'add, get, update or remove an user',
'actionDependencies': ['vorpal'], 'actionDependencies': ['vorpal'],
'action': (actionDependencies) => { 'action': (actionDependencies) => {
@ -15,39 +15,82 @@ module.exports = {
let action = args.action.toLowerCase(); let action = args.action.toLowerCase();
let profile = { let profile = {
user: args.nick, user: args.nick,
pass: (args.pass) ? global['app'].modules.auth.generateHash(args.pass) : "-", pass: null,
mail: null, mail: null,
group: 0 group: 0
}; };
if(!args.mail) profile.mail = profile.user;
if(args.mail) profile.mail = args.mail;
if(args.group) profile.group = args.group;
// add a new user
if(action === 'add') { if(action === 'add') {
if(Array.isArray(args.data) && args.data.length >= 1) {
// set data
profile.pass = global['modules'].auth.generateHash(args.data[0]);
if(args.data.length >= 2) profile.mail = args.data[1];
if(args.data.length >= 3) profile.group = args.data[2];
global['app'].modules.database.getUser([profile.user, profile.mail], (err, rep) => { // haystack verifying
if(err) vorpal.log("ERR: While finding user"); let haystack = profile.user;
else { if(typeof profile.mail !== 'undefined' && profile.mail !== null) haystack = [profile.user, profile.mail];
if (!rep) {
global['app'].modules.database.addUser(profile.user, profile.mail, profile.pass, profile.group, (errAdd, repAdd) => { // query user by haystack
if(errAdd) vorpal.log("ERR: While adding user"); global['modules'].database.getUser(haystack, (err, rep) => {
else vorpal.log("Reply: "+rep); if(err) {
}); global['logs'].error("ERR: While finding user");
} else { global['logs'].error(err);
vorpal.log("User exists: ");
vorpal.log(rep);
} }
} else {
}); // no users exist, add user
if (!rep) {
global['modules'].database.addUser(profile.user, (profile.mail || ''), profile.pass, profile.group, (errAdd, repAdd) => {
if(errAdd) {
global['logs'].error("ERR: While adding user");
global['logs'].error(errAdd);
}
else vorpal.log("Reply: "+repAdd);
});
// user already exists
} else {
vorpal.log("User exists: ");
vorpal.log(rep);
}
}
});
// missing data
} else {
global['logs'].log("No data is present or is missing. Please see:");
global['logs'].log("$ user help add");
cb();
}
// query users
} else if(action === 'get') { } else if(action === 'get') {
global['app'].modules.database.getUser([profile.user, profile.mail], (err, rep) => { // wildcard catch-all
if(rep) { if(profile.user === '*') {
vorpal.log("User exists: "); global['modules'].database.getUsers((err, rep) => {
vorpal.log(rep); if(rep) {
} else { global['logs'].log(rep);
vorpal.log("User "+profile.user+" / "+profile.mail+" doesn't exist."); }
} if(err) {
}); global['logs'].error('$ user get *');
global['logs'].error(err);
}
});
} else {
// query users by first input <nickname>
global['modules'].database.getUser(profile.user, (err, rep) => {
if(rep) {
global['logs'].log("User exists: ");
global['logs'].log(rep);
} else {
global['logs'].warn("User "+profile.user+" doesn't exist.");
}
if(err) {
global['logs'].error('$ user get '+profile.user);
global['logs'].error(err);
}
});
}
// update users, just one field
} else if(action === 'update') { } else if(action === 'update') {
if(args.data.length < 2) vorpal.log("No data supplied."); if(args.data.length < 2) vorpal.log("No data supplied.");
else { else {
@ -56,18 +99,62 @@ module.exports = {
vorpal.log("Field: "+field+"; Param: "+param); vorpal.log("Field: "+field+"; Param: "+param);
} }
} else if(action === 'remove' || action === 'delete') { } else if(action === 'remove' || action === 'delete') {
global['app'].modules.database.getUser([profile.user, profile.mail], (err, rep) => { // haystack
let haystack = profile.user;
if(typeof profile.mail !== 'undefined' && profile.mail !== null) haystack = [profile.user, profile.mail];
global['modules'].database.getUser(haystack, (err, rep) => {
if(rep) { if(rep) {
vorpal.log("User exists. Deleting him."); vorpal.log("User exists. Deleting him.");
vorpal.log(rep); global['logs'].debug(rep);
global['app'].modules.database.delUser(rep.email, (errDel, repDel) => { global['modules'].database.delUser(rep[0].email, (errDel, repDel) => {
if(repDel) vorpal.log("Deleted user."); if(repDel) {
else vorpal.log("ERR: While deleting user."); vorpal.log("Deleted user.");
global['logs'].debug(repDel);
}
else {
vorpal.log("ERR: While deleting user.");
global['logs'].debug(errDel);
}
}); });
} }
}); });
} else if(action === 'genpass') { } else if(action === 'help') {
vorpal.log(profile.pass); if(args.nick === 'add') {
vorpal.log("user add <nickname> <raw password> <mail> [group]");
vorpal.log("<nickname>: user nickname");
vorpal.log("<raw password>: will be hashed ASAP");
vorpal.log("<mail>: format: user@example.tld");
vorpal.log("[group]: not needed; only Numbers; group id");
vorpal.log("---");
vorpal.log("returning 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");
} else if(args.nick === 'update') {
vorpal.log("user update <nickname> <field> <parameter>");
vorpal.log("<nickname>: user nickname");
vorpal.log("<field>: string");
vorpal.log("<parameter>: mixed data; will be converted to Boolean, Number or String");
vorpal.log("---");
vorpal.log("returning 0 or 1 and printing errors");
vorpal.log("printing 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("---");
vorpal.log("returning 0 or 1 and printing errors");
}
} }
cb(); cb();

View File

@ -46,7 +46,7 @@ models.user = new Schema({
// group // group
models.group = new Schema({ models.group = new Schema({
name: String, // recognizable application name; ex. "Administration" name: String, // recognizable group name; ex. "Administration"
created: {type: Date, default: Date.now}, created: {type: Date, default: Date.now},
roles: {type: String, default: ""} // roles; separated by commas "a,b,a.b,c.*,d.z.*" roles: {type: String, default: ""} // roles; separated by commas "a,b,a.b,c.*,d.z.*"
}); });
@ -61,16 +61,16 @@ models.application = new Schema({
// activities // activities
models.activity = new Schema({ models.activity = new Schema({
userId: Schema.Types.ObjectId, userId: Schema.Types.ObjectId, // reference to user
date: { type: Date, default: Date.now}, date: { type: Date, default: Date.now},
uri: { type: String, default: ""}, uri: { type: String, default: ""}, // full path url e.g. http://localhost/activity_url?a=s
state: { type: Boolean, default: false } state: { type: Boolean, default: false } // successed or failed
}); });
// used authcodes // used authcodes
models.authCode = new Schema({ models.authCode = new Schema({
applicationId: Schema.Types.ObjectId, applicationId: Schema.Types.ObjectId, // reference to application
userId: Schema.Types.ObjectId, userId: Schema.Types.ObjectId, // reference to user
token: String, // generated token, only usable in combination with userId and applicationId token: String, // generated token, only usable in combination with userId and applicationId
timestamp: { type: Date, default: Date.now } timestamp: { type: Date, default: Date.now }
}); });

View File

@ -46,29 +46,28 @@ global['gds'].db.on('error', (data) => {
/** /**
* Adds User to Database * Adds User to Database
* @author Ruben Meyer * @author Ruben Meyer
* @param {String} firstname First name * @param {String} nick nickname
* @param {String} lastname Last name * @param {String} email email
* @param {String} email E-Mail * @param {String} passhash hashed password
* @param {String} password hashed PW
* @param {Number} group Group id (normally 0 -> user) * @param {Number} group Group id (normally 0 -> user)
* @param {Function} callback Callback function (error, reply) * @param {Function} callback Callback function (error, reply)
*/ */
methods.addUser = (nick, email, pass, group, callback) => { methods.addUser = (nick, email, passhash, group, callback) => {
if(typeof callback !== 'function') callback = function() {}; if(typeof callback !== 'function') callback = function() {};
if(typeof nick !== 'string') return callback(new TypeError('nick is not a string::database.addUser('+nick+','+email+','+pass+','+group+',callback)', module.filename)); if(typeof nick !== 'string') return callback(new TypeError('nick is not a string::database.addUser('+nick+','+email+','+passhash+','+group+',callback)', module.filename));
if(typeof email !== 'string') return callback(new TypeError('email is not a string::database.addUser('+nick+','+email+','+pass+','+group+',callback)', module.filename)); if(typeof email !== 'string') return callback(new TypeError('email is not a string::database.addUser('+nick+','+email+','+passhash+','+group+',callback)', module.filename));
if(typeof pass !== 'string') return callback(new TypeError('pass is not a string::database.addUser('+nick+','+email+','+pass+','+group+',callback)', module.filename)); if(typeof passhash !== 'string') return callback(new TypeError('passhash is not a string::database.addUser('+nick+','+email+','+passhash+','+group+',callback)', module.filename));
if(isNaN(group)) return callback(new TypeError('group is not a number::database.addUser('+nick+','+email+','+pass+','+group+',callback)', module.filename)); if(isNaN(group)) return callback(new TypeError('group is not a number::database.addUser('+nick+','+email+','+passhash+','+group+',callback)', module.filename));
var User = models.user; let userModel = models.user;
let tempUser = new User(); let user = new userModel();
tempUser.nickname = nick; user.nickname = nick;
tempUser.email = email; user.email = email;
tempUser.passhash = pass; user.passhash = passhash;
tempUser.group = group; user.group = group;
tempUser.save((err) => { user.save((err) => {
if(!err) callback(null, 1); if(!err) callback(null, 1);
else callback(err); else callback(err);
}); });
@ -86,11 +85,12 @@ methods.delUser = (haystack, callback) => {
if(typeof callback !== 'function') callback = function() {}; if(typeof callback !== 'function') callback = function() {};
if(typeof haystack !== 'string') return callback(new TypeError('haystack is not a string::database.delUser('+haystack+',callback)', module.filename)); if(typeof haystack !== 'string') return callback(new TypeError('haystack is not a string::database.delUser('+haystack+',callback)', module.filename));
var User = models.user; let userModel = models.user;
User.find().or([{nickname: haystack}, {email: haystack}]) userModel.findOneAndDelete().or([{nickname: haystack}, {email: haystack}])
.then((users) => { .then((rep) => {
// TODO delete user // TODO delete user
global['logs'].debug('deleted user: '+haystack);
callback(null, 1); callback(null, 1);
}).catch((err) => { }).catch((err) => {
callback(err); callback(err);
@ -99,7 +99,28 @@ methods.delUser = (haystack, callback) => {
/** /**
* gets entry by email * get all users
* @author Ruben Meyer
* @param {Function} callback Callback function (reply -> Array users)
*/
methods.getUsers = (callback) => {
if(typeof callback !== 'function') callback = function() {};
let userModel = models.user;
userModel.find({})
.then((users) => {
if(users.length > 0)
return callback(null, users);
else
return callback(null, false);
}).catch((err) => {
return callback(err);
});
};
/**
* query users by email, nickname or rememberme token
* @author Ruben Meyer * @author Ruben Meyer
* @param {String|String[]} haystack email or nick * @param {String|String[]} haystack email or nick
* @param {Function} callback Callback function (reply -> Array users) * @param {Function} callback Callback function (reply -> Array users)
@ -108,7 +129,7 @@ methods.getUser = (haystack, callback) => {
if(typeof callback !== 'function') callback = function() {}; if(typeof callback !== 'function') callback = function() {};
if(typeof haystack !== 'string' && typeof haystack !== 'object') return callback(new TypeError('email or nickname is not a string|object::database.getUser('+haystack+',callback)', module.filename)); if(typeof haystack !== 'string' && typeof haystack !== 'object') return callback(new TypeError('email or nickname is not a string|object::database.getUser('+haystack+',callback)', module.filename));
var User = models.user; let userModel = models.user;
let or = []; let or = [];
if(typeof haystack === 'string') { if(typeof haystack === 'string') {
@ -125,7 +146,7 @@ methods.getUser = (haystack, callback) => {
} }
} }
User.find().or(or) userModel.find().or(or)
.then((users) => { .then((users) => {
if(users.length > 0) if(users.length > 0)
return callback(null, users); return callback(null, users);
@ -144,13 +165,13 @@ methods.getUser = (haystack, callback) => {
* @param {Object} obj data * @param {Object} obj data
* @param {Function} callback Callback function * @param {Function} callback Callback function
*/ */
methods.updateUserProfile = (id, obj, callback) => { methods.updateUser = (id, obj, callback) => {
if(typeof callback !== 'function') callback = function() {}; if(typeof callback !== 'function') callback = function() {};
if(typeof id !== 'string') return callback(new TypeError('id is not a string::database.updateUserProfile('+id+','+JSON.stringify(obj)+',callback)', module.filename)); if(typeof id !== 'string') return callback(new TypeError('id is not a string::database.updateUser('+id+','+JSON.stringify(obj)+',callback)', module.filename));
if(typeof obj !== 'object') return callback(new TypeError('obj is not an object::database.updateUserProfile('+id+','+JSON.stringify(obj)+',callback)', module.filename)); if(typeof obj !== 'object') return callback(new TypeError('obj is not an object::database.updateUser('+id+','+JSON.stringify(obj)+',callback)', module.filename));
var User = models.user; let userModel = models.user;
User.findByIdAndUpdate(id, obj, (err, data) => { userModel.findByIdAndUpdate(id, obj, (err, data) => {
if(err) callback(err); if(err) callback(err);
else callback(null, data); else callback(null, data);
}); });
@ -161,11 +182,12 @@ methods.updateUserProfile = (id, obj, callback) => {
/** /**
* updates data based on login * updates data based on login
* @author Ruben Meyer * @author Ruben Meyer
* @TODO UPDATE METHOD; PROBABLY OUTDATED
* @param {Number} id User ID * @param {Number} id User ID
* @param {Object} options options JSON -> remember * @param {Object} data data JSON -> remember
* @param {Function} callback Callback function (date => 'Login Date', token => 'RememberMe Cookie Token') * @param {Function} callback Callback function (date => 'Login Date', token => 'RememberMe Cookie Token')
*/ */
methods.updateNewAction = (id, options, callback) => { methods.addActivity = (id, data, callback) => {
if(typeof callback !== 'function') callback = function() {}; if(typeof callback !== 'function') callback = function() {};
if(typeof id !== 'string') return callback(new TypeError('id is not a string::database.updateNewAction('+id+','+JSON.stringify(options)+',callback)', module.filename)); if(typeof id !== 'string') return callback(new TypeError('id is not a string::database.updateNewAction('+id+','+JSON.stringify(options)+',callback)', module.filename));
if(typeof options !== 'object' && options !== null) return callback(new TypeError('obj is not an object::database.updateUserProfile('+id+','+JSON.stringify(obj)+',callback)', module.filename)); if(typeof options !== 'object' && options !== null) return callback(new TypeError('obj is not an object::database.updateUserProfile('+id+','+JSON.stringify(obj)+',callback)', module.filename));
@ -331,8 +353,8 @@ methods.verifyAppCall = (obj, callback) => {
methods.userCount = (callback) => { methods.userCount = (callback) => {
if(typeof callback !== 'function') callback = function() {}; if(typeof callback !== 'function') callback = function() {};
var User = models.user; let userModel = models.user;
User.countDocuments({}, (err, count) => { userModel.countDocuments({}, (err, count) => {
callback((err) ? err : count); callback((err) ? err : count);
}); });
}; };