Skip to content
Snippets Groups Projects
Commit 22661cbb authored by Marcelo Schmidt's avatar Marcelo Schmidt
Browse files

Functions should throw errors instead of returning false;

Switched new files to .js instead of .coffee;
parent 07141ff9
No related branches found
No related tags found
No related merge requests found
......@@ -55,11 +55,11 @@ Package.onUse(function(api) {
// SERVER FUNCTIONS
api.addFiles('server/functions/checkUsernameAvailability.coffee', 'server');
api.addFiles('server/functions/checkEmailAvailability.coffee', 'server');
api.addFiles('server/functions/checkEmailAvailability.js', 'server');
api.addFiles('server/functions/sendMessage.coffee', 'server');
api.addFiles('server/functions/settings.coffee', 'server');
api.addFiles('server/functions/setUsername.coffee', 'server');
api.addFiles('server/functions/setEmail.coffee', 'server');
api.addFiles('server/functions/setEmail.js', 'server');
api.addFiles('server/functions/Notifications.coffee', 'server');
// SERVER METHODS
......@@ -75,7 +75,7 @@ Package.onUse(function(api) {
api.addFiles('server/methods/setAdminStatus.coffee', 'server');
api.addFiles('server/methods/setRealName.coffee', 'server');
api.addFiles('server/methods/setUsername.coffee', 'server');
api.addFiles('server/methods/setEmail.coffee', 'server');
api.addFiles('server/methods/setEmail.js', 'server');
api.addFiles('server/methods/updateUser.coffee', 'server');
api.addFiles('server/methods/restartServer.coffee', 'server');
......
RocketChat.checkEmailAvailability = (email) ->
return not Meteor.users.findOne({ "emails.address": { $regex : new RegExp("^" + s.trim(s.escapeRegExp(email)) + "$", "i") } })
RocketChat.checkEmailAvailability = function(email) {
return !Meteor.users.findOne({ "emails.address": { $regex : new RegExp("^" + s.trim(s.escapeRegExp(email)) + "$", "i") } })
}
RocketChat._setEmail = (userId, email) ->
email = s.trim email
if not userId or not email
return false
emailValidation = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
if not emailValidation.test email
return false
user = RocketChat.models.Users.findOneById userId
# User already has desired username, return
if user.emails?[0]?.address is email
return user
# Check e-mail availability
unless RocketChat.checkEmailAvailability email
return false
# Set new email
RocketChat.models.Users.setEmail user._id, email
user.email = email
return user
RocketChat.setEmail = RocketChat.RateLimiter.limitFunction RocketChat._setEmail, 1, 60000,
0: (userId) -> return not RocketChat.authz.hasPermission(userId, 'edit-other-user-info') # Administrators have permission to change others emails, so don't limit those
RocketChat._setEmail = function(userId, email) {
email = s.trim(email)
if (!userId) {
throw new Meteor.Error('invalid-user', "[methods] setEmail -> Invalid user");
}
if (!email) {
throw new Meteor.Error('invalid-email', "[methods] setEmail -> Invalid email");
}
emailValidation = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
if (!emailValidation.test(email)) {
throw new Meteor.Error('email-invalid', "#{email} is not a valid e-mail");
}
user = RocketChat.models.Users.findOneById(userId);
// User already has desired username, return
if (user.emails && user.emails[0] && user.emails[0].address === email) {
return user;
}
// Check e-mail availability
if (!RocketChat.checkEmailAvailability(email)) {
throw new Meteor.Error('email-unavailable', "#{email} is already in use :(");
}
// Set new email
RocketChat.models.Users.setEmail(user._id, email);
user.email = email;
return user;
}
RocketChat.setEmail = RocketChat.RateLimiter.limitFunction(RocketChat._setEmail, 1, 60000, {
0: function(userId) { return !RocketChat.authz.hasPermission(userId, 'edit-other-user-info') } // Administrators have permission to change others emails, so don't limit those
});
Meteor.methods
setEmail: (email) ->
if not Meteor.userId()
throw new Meteor.Error('invalid-user', "[methods] setEmail -> Invalid user")
user = Meteor.user()
if not RocketChat.settings.get("Accounts_AllowEmailChange")
throw new Meteor.Error(403, "[methods] setEmail -> E-mail change not allowed")
if user.emails?[0]?.address is email
return email
emailValidation = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
if not emailValidation.test email
throw new Meteor.Error 'email-invalid', "#{email} is not a valid e-mail"
if not RocketChat.checkEmailAvailability email
throw new Meteor.Error 'email-unavailable', "#{email} is already in use :("
unless RocketChat.setEmail user._id, email
throw new Meteor.Error 'could-not-change-email', "Could not change email"
return email
RocketChat.RateLimiter.limitMethod 'setEmail', 1, 1000,
userId: (userId) -> return true
Meteor.methods({
setEmail: function(email) {
if (!Meteor.userId()) {
throw new Meteor.Error('invalid-user', "[methods] setEmail -> Invalid user");
}
user = Meteor.user();
if (!RocketChat.settings.get("Accounts_AllowEmailChange")) {
throw new Meteor.Error(403, "[methods] setEmail -> E-mail change not allowed");
}
if (user.emails && user.emails[0] && user.emails[0].address === email) {
return email;
}
emailValidation = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
if (!emailValidation.test(email)) {
throw new Meteor.Error('email-invalid', "#{email} is not a valid e-mail");
}
if (!RocketChat.checkEmailAvailability(email)) {
throw new Meteor.Error('email-unavailable', "#{email} is already in use :(");
}
if (!RocketChat.setEmail(user._id, email)) {
throw new Meteor.Error('could-not-change-email', "Could not change email");
}
return email;
}
});
RocketChat.RateLimiter.limitMethod('setEmail', 1, 1000, {
userId: function(userId) { return true }
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment