Skip to content

Commit

Permalink
feat: write down banned reason
Browse files Browse the repository at this point in the history
  • Loading branch information
AVVS committed Feb 2, 2016
1 parent 95efa16 commit 287d9c9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
32 changes: 22 additions & 10 deletions src/actions/ban.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
const Promise = require('bluebird');
const mapValues = require('lodash/mapValues');
const stringify = JSON.stringify.bind(JSON);

const redisKey = require('../utils/key.js');
const userExists = require('../utils/userExists.js');
const { USERS_DATA, USERS_METADATA, USERS_BANNED_FLAG, USERS_TOKENS } = require('../constants.js');
const stringify = JSON.stringify.bind(JSON);
const {
USERS_DATA, USERS_METADATA,
USERS_BANNED_FLAG, USERS_TOKENS, USERS_BANNED_DATA,
} = require('../constants.js');

function lockUser(username) {
function lockUser({ username, reason, whom, remoteip }) {
const { redis, config } = this;
const { jwt: { defaultAudience } } = config;
const data = {
banned: true,
[USERS_BANNED_DATA]: {
reason: reason || '',
whom: whom || '',
remoteip: remoteip || '',
},
};

return redis
.pipeline()
.hset(redisKey(username, USERS_DATA), USERS_BANNED_FLAG, 'true')
// set .banned on metadata for filtering & sorting users by that field
.hset(redisKey(username, USERS_METADATA, defaultAudience), 'banned', stringify(true))
.hmset(redisKey(username, USERS_METADATA, defaultAudience), mapValues(data, stringify))
.del(redisKey(username, USERS_TOKENS))
.exec();
}

function unlockUser(username) {
function unlockUser({ username }) {
const { redis, config } = this;
const { jwt: { defaultAudience } } = config;

return redis
.pipeline()
.hdel(redisKey(username, USERS_DATA), USERS_BANNED_FLAG)
// remove .banned on metadata for filtering & sorting users by that field
.hdel(redisKey(username, USERS_METADATA, defaultAudience), 'banned')
.hdel(redisKey(username, USERS_METADATA, defaultAudience), 'banned', USERS_BANNED_DATA)
.exec();
}

Expand All @@ -35,10 +48,9 @@ function unlockUser(username) {
* @return {Promise}
*/
module.exports = function banUser(opts) {
const { username, ban } = opts;

return Promise
.bind(this, username)
.bind(this, opts.username)
.tap(userExists)
.then(ban ? lockUser : unlockUser);
.return(opts)
.then(opts.ban ? lockUser : unlockUser);
};
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ module.exports = {
USERS_ACTIVE_FLAG: 'active',
USERS_ADMIN_ROLE: 'admin',
USERS_TESTER_ROLE: 'tester',
USERS_BANNED_DATA: 'bannedData',
};
4 changes: 2 additions & 2 deletions test/suites/ban.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('#ban', function banSuite() {
.then(inspectPromise())
.then(ban => {
expect(ban[0][1]).to.be.eq(1);
expect(ban[1][1]).to.be.eq(1);
expect(ban[1][1]).to.be.eq('OK');
});
});

Expand All @@ -52,7 +52,7 @@ describe('#ban', function banSuite() {
.then(inspectPromise())
.then(ban => {
expect(ban[0][1]).to.be.eq(1);
expect(ban[1][1]).to.be.eq(1);
expect(ban[1][1]).to.be.eq(2);
});
});
});
Expand Down

0 comments on commit 287d9c9

Please sign in to comment.