The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/packages/rocketchat-lib/server/functions/setEmail.js

37 lines
1.3 KiB

RocketChat._setEmail = function(userId, email) {
10 years ago
email = s.trim(email);
if (!userId) {
10 years ago
throw new Meteor.Error('invalid-user', '[methods] setEmail -> Invalid user');
}
if (!email) {
10 years ago
throw new Meteor.Error('invalid-email', '[methods] setEmail -> Invalid email');
}
10 years ago
const 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)) {
10 years ago
throw new Meteor.Error('email-invalid', email + ' is not a valid e-mail');
}
10 years ago
const 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)) {
10 years ago
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;
10 years ago
};
RocketChat.setEmail = RocketChat.RateLimiter.limitFunction(RocketChat._setEmail, 1, 60000, {
10 years ago
0: function(userId) { return !RocketChat.authz.hasPermission(userId, 'edit-other-user-info'); } // Administrators have permission to change others emails, so don't limit those
});