Fix Email sent everytime a admin owns an Entity during startup (#10697)

* Fix Email sent everytime a admin owns an Entity during startup

* fix
This commit is contained in:
Mohit Yadav 2023-03-23 11:58:54 +05:30 committed by GitHub
parent 3d8e7e6d41
commit acb0e71a3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View File

@ -179,8 +179,9 @@ public class UserResource extends EntityResource<User, UserRepository> {
this.authenticationConfiguration = config.getAuthenticationConfiguration(); this.authenticationConfiguration = config.getAuthenticationConfiguration();
SmtpSettings smtpSettings = config.getSmtpSettings(); SmtpSettings smtpSettings = config.getSmtpSettings();
this.isEmailServiceEnabled = smtpSettings != null && smtpSettings.getEnableSmtpServer(); this.isEmailServiceEnabled = smtpSettings != null && smtpSettings.getEnableSmtpServer();
this.dao.initializeUsers(config); // Keep this before initializeUsers, else getUpdater() will fail
SubjectCache.initialize(); SubjectCache.initialize();
this.dao.initializeUsers(config);
} }
public static class UserList extends ResultList<User> { public static class UserList extends ResultList<User> {

View File

@ -23,7 +23,6 @@ import static org.openmetadata.schema.auth.SSOAuthMechanism.SsoServiceType.OKTA;
import static org.openmetadata.schema.entity.teams.AuthenticationMechanism.AuthType.JWT; import static org.openmetadata.schema.entity.teams.AuthenticationMechanism.AuthType.JWT;
import static org.openmetadata.schema.entity.teams.AuthenticationMechanism.AuthType.SSO; import static org.openmetadata.schema.entity.teams.AuthenticationMechanism.AuthType.SSO;
import static org.openmetadata.service.Entity.ADMIN_USER_NAME; import static org.openmetadata.service.Entity.ADMIN_USER_NAME;
import static org.openmetadata.service.resources.teams.UserResource.USER_PROTECTED_FIELDS;
import at.favre.lib.crypto.bcrypt.BCrypt; import at.favre.lib.crypto.bcrypt.BCrypt;
import java.io.IOException; import java.io.IOException;
@ -76,24 +75,24 @@ public final class UserUtil {
public static void addUserForBasicAuth(String username, String pwd, String domain) throws IOException { public static void addUserForBasicAuth(String username, String pwd, String domain) throws IOException {
UserRepository userRepository = (UserRepository) Entity.getEntityRepository(Entity.USER); UserRepository userRepository = (UserRepository) Entity.getEntityRepository(Entity.USER);
User originalUser;
try { try {
List<String> fields = userRepository.getAllowedFieldsCopy(); List<String> fields = List.of("profile", "roles", "teams", "authenticationMechanism", "isEmailVerified");
fields.add(USER_PROTECTED_FIELDS); User originalUser = userRepository.getByName(null, username, new EntityUtil.Fields(fields));
originalUser = userRepository.getByName(null, username, new EntityUtil.Fields(fields, String.join(",", fields)));
if (originalUser.getAuthenticationMechanism() == null) { if (originalUser.getAuthenticationMechanism() == null) {
updateUserWithHashedPwd(originalUser, pwd); updateBasicAuthUser(originalUser, pwd);
} }
addOrUpdateUser(originalUser);
} catch (EntityNotFoundException e) { } catch (EntityNotFoundException e) {
// TODO: Not the best way ! :(
User user = user(username, domain, username).withIsAdmin(true).withIsEmailVerified(true); User user = user(username, domain, username).withIsAdmin(true).withIsEmailVerified(true);
updateUserWithHashedPwd(user, pwd); updateBasicAuthUser(user, pwd);
addOrUpdateUser(user);
EmailUtil.sendInviteMailToAdmin(user, pwd);
} }
} }
private static void updateBasicAuthUser(User user, String pwd) {
updateUserWithHashedPwd(user, pwd);
addOrUpdateUser(user);
EmailUtil.sendInviteMailToAdmin(user, pwd);
}
public static void updateUserWithHashedPwd(User user, String pwd) { public static void updateUserWithHashedPwd(User user, String pwd) {
String hashedPwd = BCrypt.withDefaults().hashToString(12, pwd.toCharArray()); String hashedPwd = BCrypt.withDefaults().hashToString(12, pwd.toCharArray());
user.setAuthenticationMechanism( user.setAuthenticationMechanism(