2013-12-06 03:51:35 -05:00
|
|
|
var when = require('when'),
|
2014-02-05 03:40:30 -05:00
|
|
|
_ = require('lodash'),
|
2013-12-06 03:51:35 -05:00
|
|
|
dataProvider = require('../models'),
|
|
|
|
settings = require('./settings'),
|
2014-04-08 08:40:33 -05:00
|
|
|
canThis = require('../permissions').canThis,
|
2013-12-06 03:51:35 -05:00
|
|
|
ONE_DAY = 86400000,
|
2014-04-28 15:42:38 -05:00
|
|
|
filteredAttributes = ['password'],
|
2013-12-06 03:51:35 -05:00
|
|
|
users;
|
|
|
|
|
2014-04-28 15:42:38 -05:00
|
|
|
|
|
|
|
function checkUserData(userData) {
|
|
|
|
if (_.isEmpty(userData) || _.isEmpty(userData.users) || _.isEmpty(userData.users[0])) {
|
|
|
|
return when.reject({code: 400, message: 'No root key (\'users\') provided.'});
|
|
|
|
}
|
|
|
|
return when.resolve(userData);
|
|
|
|
}
|
2013-12-06 03:51:35 -05:00
|
|
|
// ## Users
|
|
|
|
users = {
|
|
|
|
|
2014-02-26 12:51:01 -05:00
|
|
|
// #### Browse
|
2013-12-06 03:51:35 -05:00
|
|
|
// **takes:** options object
|
|
|
|
browse: function browse(options) {
|
|
|
|
// **returns:** a promise for a collection of users in a json object
|
2014-04-08 08:40:33 -05:00
|
|
|
return canThis(this.user).browse.user().then(function () {
|
|
|
|
return dataProvider.User.browse(options).then(function (result) {
|
2014-04-28 15:42:38 -05:00
|
|
|
var omitted = {},
|
|
|
|
i;
|
2013-12-06 03:51:35 -05:00
|
|
|
|
2014-04-08 08:40:33 -05:00
|
|
|
if (result) {
|
|
|
|
omitted = result.toJSON();
|
|
|
|
}
|
2013-12-06 03:51:35 -05:00
|
|
|
|
2014-04-08 08:40:33 -05:00
|
|
|
for (i = 0; i < omitted.length; i = i + 1) {
|
|
|
|
omitted[i] = _.omit(omitted[i], filteredAttributes);
|
|
|
|
}
|
2013-12-06 03:51:35 -05:00
|
|
|
|
2014-04-28 15:42:38 -05:00
|
|
|
return { users: omitted };
|
2014-04-08 08:40:33 -05:00
|
|
|
});
|
|
|
|
}, function () {
|
|
|
|
return when.reject({code: 403, message: 'You do not have permission to browse users.'});
|
2013-12-06 03:51:35 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// #### Read
|
|
|
|
// **takes:** an identifier (id or slug?)
|
|
|
|
read: function read(args) {
|
|
|
|
// **returns:** a promise for a single user in a json object
|
|
|
|
if (args.id === 'me') {
|
|
|
|
args = {id: this.user};
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataProvider.User.read(args).then(function (result) {
|
|
|
|
if (result) {
|
|
|
|
var omitted = _.omit(result.toJSON(), filteredAttributes);
|
2014-04-28 15:42:38 -05:00
|
|
|
return { users: [omitted] };
|
2013-12-06 03:51:35 -05:00
|
|
|
}
|
|
|
|
|
2014-02-26 12:51:01 -05:00
|
|
|
return when.reject({code: 404, message: 'User not found'});
|
2013-12-06 03:51:35 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// #### Edit
|
|
|
|
// **takes:** a json object representing a user
|
|
|
|
edit: function edit(userData) {
|
|
|
|
// **returns:** a promise for the resulting user in a json object
|
2014-04-08 08:40:33 -05:00
|
|
|
var self = this;
|
2014-04-28 15:42:38 -05:00
|
|
|
return canThis(this.user).edit.user(userData.users[0].id).then(function () {
|
|
|
|
return checkUserData(userData).then(function (checkedUserData) {
|
|
|
|
return dataProvider.User.edit(checkedUserData.users[0], {user: self.user});
|
|
|
|
}).then(function (result) {
|
2014-04-08 08:40:33 -05:00
|
|
|
if (result) {
|
|
|
|
var omitted = _.omit(result.toJSON(), filteredAttributes);
|
2014-04-28 15:42:38 -05:00
|
|
|
return { users: [omitted]};
|
2014-04-08 08:40:33 -05:00
|
|
|
}
|
|
|
|
return when.reject({code: 404, message: 'User not found'});
|
|
|
|
});
|
|
|
|
}, function () {
|
2014-04-28 15:42:38 -05:00
|
|
|
return when.reject({code: 403, message: 'You do not have permission to edit this user.'});
|
2013-12-06 03:51:35 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// #### Add
|
|
|
|
// **takes:** a json object representing a user
|
|
|
|
add: function add(userData) {
|
|
|
|
// **returns:** a promise for the resulting user in a json object
|
2014-04-08 08:40:33 -05:00
|
|
|
var self = this;
|
|
|
|
return canThis(this.user).add.user().then(function () {
|
2014-04-28 15:42:38 -05:00
|
|
|
return checkUserData(userData).then(function (checkedUserData) {
|
|
|
|
// if the user is created by users.register(), use id: 1
|
|
|
|
// as the creator for now
|
|
|
|
if (self.user === 'internal') {
|
|
|
|
self.user = 1;
|
|
|
|
}
|
|
|
|
return dataProvider.User.add(checkedUserData.users[0], {user: self.user});
|
|
|
|
}).then(function (result) {
|
|
|
|
if (result) {
|
|
|
|
var omitted = _.omit(result.toJSON(), filteredAttributes);
|
|
|
|
return { users: [omitted]};
|
|
|
|
}
|
|
|
|
});
|
2014-04-08 08:40:33 -05:00
|
|
|
}, function () {
|
|
|
|
return when.reject({code: 403, message: 'You do not have permission to add a users.'});
|
|
|
|
});
|
2014-04-03 08:03:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// #### Register
|
|
|
|
// **takes:** a json object representing a user
|
|
|
|
register: function register(userData) {
|
|
|
|
// TODO: if we want to prevent users from being created with the signup form
|
|
|
|
// this is the right place to do it
|
2014-04-08 08:40:33 -05:00
|
|
|
return users.add.call({user: 'internal'}, userData);
|
2013-12-06 03:51:35 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// #### Check
|
|
|
|
// Checks a password matches the given email address
|
|
|
|
|
|
|
|
// **takes:** a json object representing a user
|
|
|
|
check: function check(userData) {
|
|
|
|
// **returns:** on success, returns a promise for the resulting user in a json object
|
|
|
|
return dataProvider.User.check(userData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// #### Change Password
|
|
|
|
// **takes:** a json object representing a user
|
|
|
|
changePassword: function changePassword(userData) {
|
|
|
|
// **returns:** on success, returns a promise for the resulting user in a json object
|
|
|
|
return dataProvider.User.changePassword(userData);
|
|
|
|
},
|
|
|
|
|
|
|
|
generateResetToken: function generateResetToken(email) {
|
|
|
|
var expires = Date.now() + ONE_DAY;
|
2014-04-27 18:28:50 -05:00
|
|
|
return settings.read('dbHash').then(function (response) {
|
|
|
|
var dbHash = response.settings[0].value;
|
2013-12-06 03:51:35 -05:00
|
|
|
return dataProvider.User.generateResetToken(email, expires, dbHash);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
validateToken: function validateToken(token) {
|
2014-04-27 18:28:50 -05:00
|
|
|
return settings.read('dbHash').then(function (response) {
|
|
|
|
var dbHash = response.settings[0].value;
|
2013-12-06 03:51:35 -05:00
|
|
|
return dataProvider.User.validateToken(token, dbHash);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
resetPassword: function resetPassword(token, newPassword, ne2Password) {
|
2014-04-27 18:28:50 -05:00
|
|
|
return settings.read('dbHash').then(function (response) {
|
|
|
|
var dbHash = response.settings[0].value;
|
2013-12-06 03:51:35 -05:00
|
|
|
return dataProvider.User.resetPassword(token, newPassword, ne2Password, dbHash);
|
|
|
|
});
|
2014-04-08 08:40:33 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
doesUserExist: function doesUserExist() {
|
|
|
|
return dataProvider.User.browse().then(function (users) {
|
|
|
|
if (users.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2013-12-06 03:51:35 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = users;
|
|
|
|
module.exports.filteredAttributes = filteredAttributes;
|