|
|
|
|
@ -1,3 +1,4 @@ |
|
|
|
|
import type { IUser } from '@rocket.chat/core-typings'; |
|
|
|
|
import type { ServerMethods } from '@rocket.chat/ddp-client'; |
|
|
|
|
import { check } from 'meteor/check'; |
|
|
|
|
import { Meteor } from 'meteor/meteor'; |
|
|
|
|
@ -13,34 +14,38 @@ declare module '@rocket.chat/ddp-client' { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const setEmailFunction = async (email: string, user: Meteor.User | IUser) => { |
|
|
|
|
check(email, String); |
|
|
|
|
|
|
|
|
|
if (!settings.get('Accounts_AllowEmailChange')) { |
|
|
|
|
throw new Meteor.Error('error-action-not-allowed', 'Changing email is not allowed', { |
|
|
|
|
method: 'setEmail', |
|
|
|
|
action: 'Changing_email', |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (user.emails?.[0] && user.emails[0].address === email) { |
|
|
|
|
return email; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!(await setEmail(user._id, email))) { |
|
|
|
|
throw new Meteor.Error('error-could-not-change-email', 'Could not change email', { |
|
|
|
|
method: 'setEmail', |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return email; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Meteor.methods<ServerMethods>({ |
|
|
|
|
async setEmail(email) { |
|
|
|
|
check(email, String); |
|
|
|
|
|
|
|
|
|
const user = await Meteor.userAsync(); |
|
|
|
|
|
|
|
|
|
if (!user) { |
|
|
|
|
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setEmail' }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!settings.get('Accounts_AllowEmailChange')) { |
|
|
|
|
throw new Meteor.Error('error-action-not-allowed', 'Changing email is not allowed', { |
|
|
|
|
method: 'setEmail', |
|
|
|
|
action: 'Changing_email', |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (user.emails?.[0] && user.emails[0].address === email) { |
|
|
|
|
return email; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!(await setEmail(user._id, email))) { |
|
|
|
|
throw new Meteor.Error('error-could-not-change-email', 'Could not change email', { |
|
|
|
|
method: 'setEmail', |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return email; |
|
|
|
|
return setEmailFunction(email, user); |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|