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

39 lines
1.3 KiB

import s from 'underscore.string';
RocketChat._setEmail = function(userId, email, shouldSendVerificationEmail = true) {
10 years ago
email = s.trim(email);
if (!userId) {
Change meteor error (#2969) * Add function to handle errors * Delete message errors * handle error for hideRoom * Allow returning error instead of calling toastr.error * Handle error for leaveRoom * handle error for openRoom * handleError for toggleFavorite * handleError in updateMessage * error for samlLogout * handleError for assets * Add global handleError to eslint * handleError for addOAuthService * handleError: getUserRoles * handleError: insertOrUpdateUsere * handleError: messageDeleting * handleError: removeUserFromRoles * handleError: addPermissionToRole * handleError: addUserToRole * handleError: deleteRole * handleError: removeRoleFromPermission * handleError: removeUserFromRole * handleError: saveRole * Return ready on publish without permission * handleError: channel-settings * handleError: mailMessages * handleError: fileUpload * handleError: rocketchat-importer * handleError: addIncomingIntegration * handleError: deleteIncomingIntegration * handleError: updateIncomingIntegration * handleError: addOutgoingIntegration * handleError: deleteOutgoingIntegration * handleError: updateOutgoingIntegration * Return ready on publish without permission * handleError ldap * remove throw from client code * handleError: setEmail, slashCommand * Sort en.i18n.json * Google translated languages * Use correct error return from publishes * RateLimiter.limitFunction * Fix order of error "500" * handleError validateEmailDomain * handleError channelSettings; settings * handleError livechat * handleError: Mailer.sendMail * handleError pinMessage and unpinMessage * handleError messageStarring * handleError oauth apps * handleError: saveNotificationSettings * handleError getRoomRoles * handleError: createDirectMessage * handleError saveUserPreferences * handleError: saveUserProfile * handleError sendConfirmationEmail * Add ecmascript to root
10 years ago
throw new Meteor.Error('error-invalid-user', 'Invalid user', { function: '_setEmail' });
}
if (!email) {
Change meteor error (#2969) * Add function to handle errors * Delete message errors * handle error for hideRoom * Allow returning error instead of calling toastr.error * Handle error for leaveRoom * handle error for openRoom * handleError for toggleFavorite * handleError in updateMessage * error for samlLogout * handleError for assets * Add global handleError to eslint * handleError for addOAuthService * handleError: getUserRoles * handleError: insertOrUpdateUsere * handleError: messageDeleting * handleError: removeUserFromRoles * handleError: addPermissionToRole * handleError: addUserToRole * handleError: deleteRole * handleError: removeRoleFromPermission * handleError: removeUserFromRole * handleError: saveRole * Return ready on publish without permission * handleError: channel-settings * handleError: mailMessages * handleError: fileUpload * handleError: rocketchat-importer * handleError: addIncomingIntegration * handleError: deleteIncomingIntegration * handleError: updateIncomingIntegration * handleError: addOutgoingIntegration * handleError: deleteOutgoingIntegration * handleError: updateOutgoingIntegration * Return ready on publish without permission * handleError ldap * remove throw from client code * handleError: setEmail, slashCommand * Sort en.i18n.json * Google translated languages * Use correct error return from publishes * RateLimiter.limitFunction * Fix order of error "500" * handleError validateEmailDomain * handleError channelSettings; settings * handleError livechat * handleError: Mailer.sendMail * handleError pinMessage and unpinMessage * handleError messageStarring * handleError oauth apps * handleError: saveNotificationSettings * handleError getRoomRoles * handleError: createDirectMessage * handleError saveUserPreferences * handleError: saveUserProfile * handleError sendConfirmationEmail * Add ecmascript to root
10 years ago
throw new Meteor.Error('error-invalid-email', 'Invalid email', { function: '_setEmail' });
}
RocketChat.validateEmailDomain(email);
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 email availability
if (!RocketChat.checkEmailAvailability(email)) {
throw new Meteor.Error('error-field-unavailable', `${ email } is already in use :(`, { function: '_setEmail', field: email });
}
// Set new email
RocketChat.models.Users.setEmail(user._id, email);
user.email = email;
if (shouldSendVerificationEmail === true) {
Meteor.call('sendConfirmationEmail', user.email);
}
return user;
10 years ago
};
RocketChat.setEmail = RocketChat.RateLimiter.limitFunction(RocketChat._setEmail, 1, 60000, {
0() { return !Meteor.userId() || !RocketChat.authz.hasPermission(Meteor.userId(), 'edit-other-user-info'); } // Administrators have permission to change others emails, so don't limit those
});