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,
|
|
|
|
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-05-06 20:11:15 -05:00
|
|
|
return canThis(this).browse.user().then(function () {
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
return dataProvider.User.findAll(options).then(function (result) {
|
2014-05-06 05:14:58 -05:00
|
|
|
return { users: result.toJSON() };
|
2014-04-08 08:40:33 -05:00
|
|
|
});
|
|
|
|
}, function () {
|
2014-05-06 05:14:58 -05:00
|
|
|
return when.reject({type: 'NoPermission', 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};
|
|
|
|
}
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
return dataProvider.User.findOne(args).then(function (result) {
|
2013-12-06 03:51:35 -05:00
|
|
|
if (result) {
|
2014-05-06 05:14:58 -05:00
|
|
|
return { users: [result.toJSON()] };
|
2013-12-06 03:51:35 -05:00
|
|
|
}
|
|
|
|
|
2014-05-05 08:51:21 -05:00
|
|
|
return when.reject({type: 'NotFound', 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-05-06 20:11:15 -05:00
|
|
|
return canThis(this).edit.user(userData.users[0].id).then(function () {
|
2014-04-28 15:42:38 -05:00
|
|
|
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) {
|
2014-05-06 05:14:58 -05:00
|
|
|
return { users: [result.toJSON()]};
|
2014-04-08 08:40:33 -05:00
|
|
|
}
|
2014-05-05 08:51:21 -05:00
|
|
|
return when.reject({type: 'NotFound', message: 'User not found.'});
|
2014-04-08 08:40:33 -05:00
|
|
|
});
|
|
|
|
}, function () {
|
2014-05-05 08:51:21 -05:00
|
|
|
return when.reject({type: 'NoPermission', message: 'You do not have permission to edit this users.'});
|
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;
|
2014-05-06 20:11:15 -05:00
|
|
|
return canThis(this).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
|
2014-05-06 20:11:15 -05:00
|
|
|
if (self.internal) {
|
2014-04-28 15:42:38 -05:00
|
|
|
self.user = 1;
|
|
|
|
}
|
|
|
|
return dataProvider.User.add(checkedUserData.users[0], {user: self.user});
|
|
|
|
}).then(function (result) {
|
|
|
|
if (result) {
|
2014-05-06 05:14:58 -05:00
|
|
|
return { users: [result.toJSON()]};
|
2014-04-28 15:42:38 -05:00
|
|
|
}
|
|
|
|
});
|
2014-04-08 08:40:33 -05:00
|
|
|
}, function () {
|
2014-05-05 08:51:21 -05:00
|
|
|
return when.reject({type: 'NoPermission', message: 'You do not have permission to add a users.'});
|
2014-04-08 08:40:33 -05:00
|
|
|
});
|
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-05-06 20:11:15 -05:00
|
|
|
return users.add.call({internal: true}, 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-05-06 19:49:25 -05:00
|
|
|
return settings.read.call({ internal: true }, 'dbHash').then(function (response) {
|
2014-04-27 18:28:50 -05:00
|
|
|
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-05-06 19:49:25 -05:00
|
|
|
return settings.read.call({ internal: true }, 'dbHash').then(function (response) {
|
2014-04-27 18:28:50 -05:00
|
|
|
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-05-06 19:49:25 -05:00
|
|
|
return settings.read.call({ internal: true }, 'dbHash').then(function (response) {
|
2014-04-27 18:28:50 -05:00
|
|
|
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() {
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 10:18:38 -05:00
|
|
|
return dataProvider.User.findAll().then(function (users) {
|
2014-04-08 08:40:33 -05:00
|
|
|
if (users.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2013-12-06 03:51:35 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = users;
|