Skip to content

Commit

Permalink
Uses fetchTenant whenever possible
Browse files Browse the repository at this point in the history
which uses the internal cache instead of relying on the value set at the reference

Fixes: SIRI-1039
  • Loading branch information
idlira committed Dec 16, 2024
1 parent 35cdb77 commit 9399897
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/main/java/sirius/biz/tenants/TenantUserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ private UserInfo verifyIpRange(WebContext webContext, UserInfo info) {
return defaultUser;
}

T tenant = account.getTenant().fetchValue();
T tenant = fetchTenant(account.getTenant().getIdAsString());

if (tenant != null && !tenant.getTenantData().matchesIPRange(webContext)) {
return createUserWithLimitedRoles(info, tenant.getTenantData().getRolesToKeepAsSet());
Expand Down Expand Up @@ -606,7 +606,8 @@ private UserInfo asUserWithRoles(U account, Set<String> roles, @Nullable Supplie
return UserInfo.Builder.createUser(account.getUniqueName())
.withUsername(account.getUserAccountData().getLogin().getUsername())
.withTenantId(String.valueOf(account.getTenant().getId()))
.withTenantName(account.getTenant().fetchValue().getTenantData().getName())
.withTenantName(fetchTenant(account.getTenant().getIdAsString()).getTenantData()
.getName())
.withLanguage(computeLanguage(null, account.getUniqueName()))
.withPermissions(roles)
.withSettingsSupplier(user -> getUserSettings(getScopeSettings(), user))
Expand Down Expand Up @@ -639,13 +640,12 @@ public UserInfo findUserByCredentials(@Nullable WebContext webContext, String us
}

U account = result.getUserObject(getUserClass());
T tenant = fetchTenant(account.getTenant().getIdAsString());
if (account.getUserAccountData().isExternalLoginRequired() && !isWithinInterval(account.getUserAccountData()
.getLogin()
.getLastExternalLogin(),
account.getTenant()
.fetchValue()
.getTenantData()
.getExternalLoginIntervalDays())) {
tenant.getTenantData()
.getExternalLoginIntervalDays())) {
completeAuditLogForUser(auditLog.negative("AuditLog.externalLoginRequired"), account);
throw Exceptions.createHandled().withNLSKey("UserAccount.externalLoginMustBePerformed").handle();
}
Expand Down Expand Up @@ -674,8 +674,7 @@ public UserInfo findUserByCredentials(@Nullable WebContext webContext, String us

auditLog.negative("AuditLog.loginRejected")
.forUser(account.getUniqueName(), account.getUserAccountData().getLogin().getUsername())
.forTenant(String.valueOf(account.getTenant().getId()),
account.getTenant().fetchValue().getTenantData().getName())
.forTenant(tenant.getIdAsString(), tenant.getTenantData().getName())
.log();

return null;
Expand Down Expand Up @@ -720,7 +719,7 @@ protected void completeAuditLogForUser(AuditLog.AuditLogBuilder builder, U accou
builder.causedByUser(account.getUniqueName(), account.getUserAccountData().getLogin().getUsername())
.forUser(account.getUniqueName(), account.getUserAccountData().getLogin().getUsername())
.forTenant(String.valueOf(account.getTenant().getId()),
account.getTenant().fetchValue().getTenantData().getName())
fetchTenant(account.getTenant().getIdAsString()).getTenantData().getName())
.log();
}

Expand Down Expand Up @@ -793,7 +792,7 @@ protected U getUserObject(UserInfo userInfo) {
protected UserSettings getUserSettings(UserSettings scopeSettings, UserInfo userInfo) {
U user = userInfo.getUserObject(getUserClass());
Config userAccountConfig = user.getUserAccountData().getPermissions().getConfig();
Config tenantConfig = user.getTenant().fetchValue().getTenantData().getConfig();
Config tenantConfig = fetchTenant(user.getTenant().getIdAsString()).getTenantData().getConfig();

if (userAccountConfig == null) {
if (tenantConfig == null) {
Expand Down Expand Up @@ -854,7 +853,7 @@ protected boolean isUserStillValid(String userId, WebContext webContext) {
}

LoginData loginData = user.getUserAccountData().getLogin();
TenantData tenantData = user.getTenant().fetchValue().getTenantData();
TenantData tenantData = fetchTenant(user.getTenant().getIdAsString()).getTenantData();

if (loginData.isAccountLocked()) {
return false;
Expand Down Expand Up @@ -968,7 +967,7 @@ protected Set<String> computeRoles(WebContext webContext, String accountUniqueNa
}

Set<String> roles = computeRoles(user,
user.getTenant().fetchValue(),
fetchTenant(user.getTenant().getIdAsString()),
Strings.areEqual(systemTenant, String.valueOf(user.getTenant().getId())));

rolesCache.put(accountUniqueName, Tuple.create(roles, user.getTenant().getUniqueObjectName()));
Expand Down Expand Up @@ -1043,7 +1042,7 @@ protected String computeLanguage(WebContext webContext, String userId) {
return NLS.getDefaultLanguage();
}
return Strings.firstFilled(userAccount.getUserAccountData().getLanguage().getValue(),
userAccount.getTenant().fetchValue().getTenantData().getLanguage().getValue(),
fetchTenant(userAccount.getTenant().getIdAsString()).getTenantData().getLanguage().getValue(),
NLS.getDefaultLanguage());
}
}

0 comments on commit 9399897

Please sign in to comment.