From 4297b0526d7691c0e8d24293156f72301d64db28 Mon Sep 17 00:00:00 2001 From: marwanehcine Date: Sun, 3 Dec 2023 16:03:01 +0100 Subject: [PATCH] Making Default Org Optional for OAuth2 users --- .../accounts/admin/ldap/LdapAccountsManager.java | 13 ++++++++++++- gateway/src/main/resources/application.yml | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gateway/src/main/java/org/georchestra/gateway/accounts/admin/ldap/LdapAccountsManager.java b/gateway/src/main/java/org/georchestra/gateway/accounts/admin/ldap/LdapAccountsManager.java index 688edd2b..38665353 100644 --- a/gateway/src/main/java/org/georchestra/gateway/accounts/admin/ldap/LdapAccountsManager.java +++ b/gateway/src/main/java/org/georchestra/gateway/accounts/admin/ldap/LdapAccountsManager.java @@ -42,6 +42,7 @@ import org.georchestra.gateway.accounts.admin.AccountManager; import org.georchestra.security.api.UsersApi; import org.georchestra.security.model.GeorchestraUser; +import org.springframework.beans.factory.annotation.Value; import org.springframework.ldap.NameNotFoundException; import lombok.NonNull; @@ -55,6 +56,7 @@ @Slf4j(topic = "org.georchestra.gateway.accounts.admin.ldap") class LdapAccountsManager extends AbstractAccountsManager { + private @Value("${georchestra.gateway.security.defaultOrganization:}") String defaultOrganization; private final @NonNull AccountDao accountDao; private final @NonNull RoleDao roleDao; @@ -148,7 +150,11 @@ private Account mapToAccountBrief(@NonNull GeorchestraUser preAuth) { Account newAccount = AccountFactory.createBrief(username, password, firstName, lastName, email, phone, title, description, oAuth2ProviderId); newAccount.setPending(false); - newAccount.setOrg(org); + if (StringUtils.isEmpty(org) && !StringUtils.isBlank(defaultOrganization)) { + newAccount.setOrg(defaultOrganization); + } else { + newAccount.setOrg(org); + } return newAccount; } @@ -165,6 +171,8 @@ private void ensureOrgExists(@NonNull Account newAccount) { List currentMembers = org.getMembers(); currentMembers.add(newAccount.getUid()); org.setMembers(currentMembers); + org.setId(orgId); + orgsDao.update(org); } catch (NameNotFoundException e) { log.info("Org {} does not exist, trying to create it", orgId); @@ -172,6 +180,9 @@ private void ensureOrgExists(@NonNull Account newAccount) { org = new Org(); org.setId(orgId); org.setName(orgId); + org.setShortName(orgId); + org.setPending(false); + org.setOrgType("default"); org.setMembers(Arrays.asList(newAccount.getUid())); orgsDao.insert(org); } diff --git a/gateway/src/main/resources/application.yml b/gateway/src/main/resources/application.yml index 7959542a..ead19e59 100644 --- a/gateway/src/main/resources/application.yml +++ b/gateway/src/main/resources/application.yml @@ -70,6 +70,7 @@ georchestra: gateway: security: create-non-existing-users-in-l-d-a-p: false + defaultOrganization: ${defaultOrganization:} header-authentication: enabled: false events: