Skip to content
Snippets Groups Projects
Unverified Commit 483b4a39 authored by Marcos Spessatto Defendi's avatar Marcos Spessatto Defendi Committed by GitHub
Browse files

refactor: remove calls for setEmail Meteor method (server) (#34889)

parent 8c51b5e9
No related branches found
No related tags found
No related merge requests found
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);
},
});
......
......@@ -11,6 +11,7 @@ import { saveCustomFields } from '../../app/lib/server/functions/saveCustomField
import { validateUserEditing } from '../../app/lib/server/functions/saveUser';
import { saveUserIdentity } from '../../app/lib/server/functions/saveUserIdentity';
import { passwordPolicy } from '../../app/lib/server/lib/passwordPolicy';
import { setEmailFunction } from '../../app/lib/server/methods/setEmail';
import { settings as rcSettings } from '../../app/settings/server';
import { setUserStatusMethod } from '../../app/user-status/server/methods/setUserStatus';
import { compareUserPassword } from '../lib/compareUserPassword';
......@@ -107,8 +108,8 @@ async function saveUserProfile(
await Users.setNickname(user._id, settings.nickname.trim());
}
if (settings.email) {
await Meteor.callAsync('setEmail', settings.email);
if (user && settings.email) {
await setEmailFunction(settings.email, user);
}
const canChangePasswordForOAuth = rcSettings.get<boolean>('Accounts_AllowPasswordChangeForOAuthUsers');
......
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