parent
c6d1814d81
commit
134ed42360
@ -0,0 +1,2 @@ |
||||
RocketChat.checkEmailAvailability = (email) -> |
||||
return not Meteor.users.findOne({ "emails.address": { $regex : new RegExp("^" + s.trim(email) + "$", "i") } }) |
@ -0,0 +1,26 @@ |
||||
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 |
@ -0,0 +1,27 @@ |
||||
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 |
Loading…
Reference in new issue