Skip to content

Commit

Permalink
fix: userExists method now accepts alias as well
Browse files Browse the repository at this point in the history
Adjust all appropriate methods to take account of this
  • Loading branch information
AVVS committed Feb 22, 2016
1 parent 99db2d5 commit cedf25e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/actions/ban.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function unlockUser({ username }) {
module.exports = function banUser(opts) {
return Promise
.bind(this, opts.username)
.tap(userExists)
.return(opts)
.then(userExists)
.then(username => ({ ...opts, username }))
.then(opts.ban ? lockUser : unlockUser);
};
5 changes: 4 additions & 1 deletion src/actions/getInternalData.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const Promise = require('bluebird');
const getInternalData = require('../utils/getInternalData.js');

module.exports = function internalData(message) {
return getInternalData.call(this, message.username);
return Promise
.bind(this, message.username)
.then(getInternalData);
};
7 changes: 4 additions & 3 deletions src/actions/getMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const getMetadata = require('../utils/getMetadata.js');
const userExists = require('../utils/userExists.js');

module.exports = function getMetadataAction(message) {
const { username, audience } = message;
const { audience } = message;

return Promise
.bind(this, username)
.bind(this, message.username)
.then(userExists)
.then(() => getMetadata.call(this, username, audience));
.then(username => [username, audience])
.spread(getMetadata);
};
6 changes: 2 additions & 4 deletions src/actions/updateMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ const updateMetadata = require('../utils/updateMetadata.js');
const userExists = require('../utils/userExists.js');

module.exports = function updateMetadataAction(message) {
const { username } = message;

return Promise
.bind(this, username)
.bind(this, message.username)
.then(userExists)
.return(message)
.then(username => ({ ...message, username }))
.then(updateMetadata);
};
19 changes: 12 additions & 7 deletions src/actions/updatePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,21 @@ function usernamePasswordReset(username, password) {
* @param {String} username
* @param {String} password
*/
function setPassword(username, password) {
function setPassword(_username, password) {
const { redis } = this;

return Promise
.bind(this, username)
.bind(this, _username)
.then(userExists)
.return(password)
.then(scrypt.hash)
.then(hash => redis.hset(redisKey(username, USERS_DATA), 'password', hash))
.return(username);
.then(username => Promise.props({
username,
hash: scrypt.hash(password),
}))
.then(({ username, hash }) =>
redis
.hset(redisKey(username, USERS_DATA), 'password', hash)
.return(username)
);
}

module.exports = exports = function updatePassword(opts) {
Expand All @@ -72,7 +77,7 @@ module.exports = exports = function updatePassword(opts) {

if (remoteip) {
promise = promise.tap(function resetLock(username) {
return redis.del(redisKey(username, USERS_DATA, remoteip));
return redis.del(redisKey(username, 'ip', remoteip));
});
}

Expand Down
13 changes: 10 additions & 3 deletions src/utils/userExists.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
const Errors = require('common-errors');
const redisKey = require('../utils/key.js');
const { USERS_DATA } = require('../constants.js');
const { USERS_DATA, USERS_ALIAS_TO_LOGIN } = require('../constants.js');

module.exports = function userExists(username) {
return this
.redis
.pipeline()
.hget(USERS_ALIAS_TO_LOGIN, username)
.exists(redisKey(username, USERS_DATA))
.then(exists => {
if (!exists) {
.exec()
.spread((alias, exists) => {
if (alias[1]) {
return alias[1];
}

if (!exists[1]) {
throw new Errors.HttpStatusError(404, `"${username}" does not exists`);
}

Expand Down

0 comments on commit cedf25e

Please sign in to comment.