Skip to content

Commit

Permalink
Making Default Org Optional for OAuth2 users
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanehcine authored and emmdurin committed Dec 21, 2023
1 parent c7c345b commit 4297b05
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}

Expand All @@ -165,13 +171,18 @@ private void ensureOrgExists(@NonNull Account newAccount) {
List<String> 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);
// org does not exist yet, create it
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);
}
Expand Down
1 change: 1 addition & 0 deletions gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ georchestra:
gateway:
security:
create-non-existing-users-in-l-d-a-p: false
defaultOrganization: ${defaultOrganization:}
header-authentication:
enabled: false
events:
Expand Down

0 comments on commit 4297b05

Please sign in to comment.