UNPKG

openhim-core

Version:

The OpenHIM core application that provides logging and routing of http requests

282 lines (250 loc) 9.04 kB
var Q, User, authorisation, config, contact, htmlMessageTemplate, logger, moment, plainMessageTemplate, randtoken, utils; User = require('../model/users').User; Q = require('q'); logger = require('winston'); authorisation = require('./authorisation'); moment = require('moment'); randtoken = require('rand-token'); contact = require('../contact'); config = require("../config/config"); config.newUserExpiry = config.get('newUserExpiry'); utils = require("../utils"); /* * Get authentication details */ exports.authenticate = function*(email) { var e, user; email = unescape(email); try { user = (yield User.findOne({ email: email }).exec()); if (!user) { return utils.logAndSetResponse(this, 404, "Could not find user by email " + email, 'info'); } else { return this.body = { salt: user.passwordSalt, ts: new Date() }; } } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Error during authentication " + e, 'error'); } }; /* New User Set Password Functions */ exports.getNewUser = function*(token) { var e, projectionRestriction, result; token = unescape(token); try { projectionRestriction = { "firstname": 1, "surname": 1, "msisdn": 1, "token": 1, "locked": 1, "expiry": 1, "_id": 0 }; result = (yield User.findOne({ token: token }, projectionRestriction).exec()); if (!result) { this.body = "User with token " + token + " could not be found."; return this.status = 404; } else { if (moment(result.expiry).utc().format() < moment().utc().format()) { this.body = "User with token " + token + " has expired to set their password."; return this.status = 410; } else { return this.body = result; } } } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Could not find user with token " + token + " via the API " + e, 'error'); } }; exports.updateNewUser = function*(token) { var e, msisdn, newUserOldData, newUserUpdate, userData; token = unescape(token); userData = this.request.body; try { newUserOldData = (yield User.findOne({ token: token }).exec()); if (!newUserOldData) { this.body = "User with token " + token + " could not be found."; this.status = 404; return; } else { if (moment(newUserOldData.expiry).utc().format() < moment().utc().format()) { this.body = "User with token " + token + " has expired to set their password."; this.status = 410; return; } } } catch (_error) { e = _error; utils.logAndSetResponse(this, 500, "Could not find user with token " + token + " via the API " + e, 'error'); return; } if (userData.msisdn) { msisdn = userData.msisdn; } else { msisdn = null; } newUserUpdate = { firstname: userData.firstname, surname: userData.surname, token: null, locked: false, expiry: null, msisdn: msisdn, passwordAlgorithm: userData.passwordAlgorithm, passwordSalt: userData.passwordSalt, passwordHash: userData.passwordHash }; try { (yield User.findOneAndUpdate({ token: token }, newUserUpdate).exec()); this.body = "Successfully set new user password."; return logger.info("New user updated by token " + token); } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Could not update user with token " + token + " via the API " + e, 'error'); } }; /* New User Set Password Functions */ plainMessageTemplate = function(firstname, setPasswordLink) { return "<---------- New User - Set Password ---------->\nHi " + firstname + ",\n\nA profile has been created for you on OpenHIM\nFollow the below link to set your password and log into OpenHIM Console\n" + setPasswordLink + "\n<---------- New User - Set Password ---------->"; }; htmlMessageTemplate = function(firstname, setPasswordLink) { return "<h1>New OpenHIM Profile</h1>\n<p>Hi " + firstname + ",<br/><br/>A profile has been created for you on OpenHIM</p>\n<p>Follow the below link to set your password and log into OpenHIM Console</p>\n<p>" + setPasswordLink + "</p>"; }; /* * Adds a user */ exports.addUser = function*() { var consoleURL, duration, durationType, e, htmlMessage, plainMessage, result, setPasswordLink, token, user, userData; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to addUser denied.", 'info'); return; } userData = this.request.body; token = randtoken.generate(32); userData.token = token; userData.locked = true; duration = config.newUserExpiry.duration; durationType = config.newUserExpiry.durationType; userData.expiry = moment().add(duration, durationType).utc().format(); consoleURL = config.alerts.consoleURL; setPasswordLink = consoleURL + "/#/set-password/" + token; try { user = new User(userData); result = (yield Q.ninvoke(user, 'save')); plainMessage = plainMessageTemplate(userData.firstname, setPasswordLink); htmlMessage = htmlMessageTemplate(userData.firstname, setPasswordLink); contact.contactUser('email', userData.email, 'OpenHIM Console Profile', plainMessage, htmlMessage, function() { return logger.info('The email has been sent to the new user'); }); this.body = 'User successfully created'; this.status = 201; return logger.info("User " + this.authenticated.email + " created user " + userData.email); } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Could not add user via the API " + e, 'error'); } }; /* * Retrieves the details of a specific user */ exports.getUser = function*(email) { var e, result; email = unescape(email); if (!authorisation.inGroup('admin', this.authenticated) && this.authenticated.email !== email) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getUser denied.", 'info'); return; } try { result = (yield User.findOne({ email: email }).exec()); if (!result) { this.body = "User with email " + email + " could not be found."; return this.status = 404; } else { return this.body = result; } } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Could not get user via the API " + e, 'error'); } }; exports.updateUser = function*(email) { var e, userData; email = unescape(email); if (!authorisation.inGroup('admin', this.authenticated) && this.authenticated.email !== email) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to updateUser denied.", 'info'); return; } userData = this.request.body; if (userData.passwordAlgorithm && userData.passwordHash && userData.passwordSalt) { userData.token = null; userData.locked = false; userData.expiry = null; } if (this.authenticated.email === email && !authorisation.inGroup('admin', this.authenticated)) { delete userData.groups; } if (userData._id) { delete userData._id; } try { (yield User.findOneAndUpdate({ email: email }, userData).exec()); this.body = "Successfully updated user."; return logger.info("User " + this.authenticated.email + " updated user " + userData.email); } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Could not update user " + email + " via the API " + e, 'error'); } }; exports.removeUser = function*(email) { var e; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to removeUser denied.", 'info'); return; } email = unescape(email); if (email === 'root@openhim.org') { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is OpenHIM root, User cannot be deleted through the API", 'info'); return; } try { (yield User.findOneAndRemove({ email: email }).exec()); this.body = "Successfully removed user with email " + email; return logger.info("User " + this.authenticated.email + " removed user " + email); } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Could not remove user " + email + " via the API " + e, 'error'); } }; exports.getUsers = function*() { var e; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getUsers denied.", 'info'); return; } try { return this.body = (yield User.find().exec()); } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Could not fetch all users via the API " + e, 'error'); } }; //# sourceMappingURL=users.js.map