Skip to content

Commit

Permalink
Rename UserTenantAware to TenantAwareUser (#1668)
Browse files Browse the repository at this point in the history
in order to be compatible with other TenantAware entities

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
  • Loading branch information
avgustinmm committed Feb 27, 2024
1 parent 24d7082 commit a0db5ff
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
Expand Up @@ -21,7 +21,7 @@
import org.eclipse.hawkbit.im.authentication.MultitenancyIndicator;
import org.eclipse.hawkbit.im.authentication.PermissionUtils;
import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails;
import org.eclipse.hawkbit.im.authentication.UserTenantAware;
import org.eclipse.hawkbit.im.authentication.TenantAwareUser;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand Down Expand Up @@ -57,10 +57,10 @@ public class InMemoryUserManagementAutoConfiguration extends GlobalAuthenticatio

InMemoryUserManagementAutoConfiguration(
final SecurityProperties securityProperties,
final TenantAwareUserProperties userTenantAwareProperties,
final TenantAwareUserProperties tenantAwareUserProperties,
final Optional<PasswordEncoder> passwordEncoder) {
userDetailsService = userDetailsService(
securityProperties, userTenantAwareProperties, passwordEncoder.orElse(null));
securityProperties, tenantAwareUserProperties, passwordEncoder.orElse(null));
}

@Override
Expand All @@ -72,11 +72,11 @@ public void configure(final AuthenticationManagerBuilder auth) {

private static UserDetailsService userDetailsService(
final SecurityProperties securityProperties,
final TenantAwareUserProperties userTenantAwareProperties,
final TenantAwareUserProperties tenantAwareUserProperties,
final PasswordEncoder passwordEncoder) {
final List<User> userPrincipals = new ArrayList<>();
userTenantAwareProperties.getUsers().forEach((username, user) -> {
final UserTenantAware userPrincipal = new UserTenantAware(
tenantAwareUserProperties.getUsers().forEach((username, user) -> {
final TenantAwareUser userPrincipal = new TenantAwareUser(
username, password(user.getPassword(), passwordEncoder),
createAuthorities(user.getRoles(), Collections::emptyList),
ObjectUtils.isEmpty(user.getTenant()) ? DEFAULT_TENANT : user.getTenant());
Expand All @@ -87,7 +87,7 @@ username, password(user.getPassword(), passwordEncoder),
// the default user from spring security properties as super DEFAULT tenant user
if (userPrincipals.isEmpty()) {
userPrincipals
.add(new UserTenantAware(
.add(new TenantAwareUser(
securityProperties.getUser().getName(),
password(securityProperties.getUser().getPassword(), passwordEncoder),
createAuthorities(
Expand Down Expand Up @@ -153,9 +153,9 @@ public UserDetails loadUserByUsername(final String username) {
}

private static User clone(final User user) {
if (user instanceof UserTenantAware) {
return new UserTenantAware(user.getUsername(), user.getPassword(), user.getAuthorities(),
((UserTenantAware)user).getTenant());
if (user instanceof TenantAwareUser) {
return new TenantAwareUser(user.getUsername(), user.getPassword(), user.getAuthorities(),
((TenantAwareUser)user).getTenant());
} else {
return new User(user.getUsername(), user.getPassword(), user.getAuthorities());
}
Expand Down
Expand Up @@ -74,17 +74,17 @@ public ContextAware contextAware(
* resolving user authorities/roles.
*
* @param securityProperties The Spring {@link SecurityProperties} for the security user
* @param userTenantAwareProperties The {@link TenantAwareUserProperties} for the managed users
* @param tenantAwareUserProperties The {@link TenantAwareUserProperties} for the managed users
* @return an {@link InMemoryUserAuthoritiesResolver} bean
*/
@Bean
@ConditionalOnMissingBean
public UserAuthoritiesResolver inMemoryAuthoritiesResolver(final SecurityProperties securityProperties,
final TenantAwareUserProperties userTenantAwareProperties) {
final Map<String, User> userTenantAwares = userTenantAwareProperties.getUsers();
final TenantAwareUserProperties tenantAwareUserProperties) {
final Map<String, User> tenantAwareUsers = tenantAwareUserProperties.getUsers();
final Map<String, List<String>> usersToPermissions;
if (!CollectionUtils.isEmpty(userTenantAwares)) {
usersToPermissions = userTenantAwares.entrySet().stream().collect(
if (!CollectionUtils.isEmpty(tenantAwareUsers)) {
usersToPermissions = tenantAwareUsers.entrySet().stream().collect(
Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getRoles()));
} else {
usersToPermissions = Collections.singletonMap(securityProperties.getUser().getName(),
Expand Down
Expand Up @@ -18,7 +18,7 @@

import org.eclipse.hawkbit.im.authentication.SpPermission;
import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails;
import org.eclipse.hawkbit.im.authentication.UserTenantAware;
import org.eclipse.hawkbit.im.authentication.TenantAwareUser;
import org.eclipse.hawkbit.repository.model.helper.SystemManagementHolder;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -163,7 +163,7 @@ public Authentication getAuthentication() {
authorities = annotation.authorities();
}
final TestingAuthenticationToken testingAuthenticationToken = new TestingAuthenticationToken(
new UserTenantAware(annotation.principal(), annotation.tenantId()),
new TenantAwareUser(annotation.principal(), annotation.tenantId()),
annotation.credentials(), authorities);
testingAuthenticationToken.setDetails(
new TenantAwareAuthenticationDetails(annotation.tenantId(), annotation.controller()));
Expand Down
Expand Up @@ -27,7 +27,7 @@
@Getter
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class UserTenantAware extends User {
public class TenantAwareUser extends User {

@Serial
private static final long serialVersionUID = 1L;
Expand All @@ -40,7 +40,7 @@ public class UserTenantAware extends User {
* @param authorities the authorities which the user has
* @param tenant the tenant of the user
*/
public UserTenantAware(final String username, final String password,
public TenantAwareUser(final String username, final String password,
final Collection<? extends GrantedAuthority> authorities, final String tenant) {
super(username, password, authorities == null ? Collections.emptyList() : authorities);
this.tenant = tenant;
Expand All @@ -52,7 +52,7 @@ public UserTenantAware(final String username, final String password,
* @param username the username of the user
* @param tenant the tenant of the user
*/
public UserTenantAware(final String username, String tenant) {
public TenantAwareUser(final String username, String tenant) {
this(username, "***", null, tenant);
}

Expand Down
Expand Up @@ -21,7 +21,7 @@
import org.eclipse.hawkbit.ContextAware;
import org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions;
import org.eclipse.hawkbit.im.authentication.TenantAwareAuthenticationDetails;
import org.eclipse.hawkbit.im.authentication.UserTenantAware;
import org.eclipse.hawkbit.im.authentication.TenantAwareUser;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.eclipse.hawkbit.tenancy.UserAuthoritiesResolver;
import org.springframework.lang.Nullable;
Expand All @@ -39,7 +39,6 @@
* from the {@link SecurityContext#getAuthentication()}
* {@link Authentication#getDetails()} which holds the
* {@link TenantAwareAuthenticationDetails} object.
*
*/
public class SecurityContextTenantAware implements ContextAware {

Expand Down Expand Up @@ -85,8 +84,8 @@ public String getCurrentTenant() {
final Object principal = context.getAuthentication().getPrincipal();
if (context.getAuthentication().getDetails() instanceof TenantAwareAuthenticationDetails) {
return ((TenantAwareAuthenticationDetails) context.getAuthentication().getDetails()).getTenant();
} else if (principal instanceof UserTenantAware) {
return ((UserTenantAware) principal).getTenant();
} else if (principal instanceof TenantAwareUser) {
return ((TenantAwareUser) principal).getTenant();
}
}
return null;
Expand Down Expand Up @@ -191,14 +190,14 @@ private static final class AuthenticationDelegate implements Authentication {

private final Authentication delegate;

private final UserTenantAware principal;
private final TenantAwareUser principal;

private final TenantAwareAuthenticationDetails tenantAwareAuthenticationDetails;

private AuthenticationDelegate(final Authentication delegate, final String tenant, final String username,
final Collection<? extends GrantedAuthority> authorities) {
this.delegate = delegate;
this.principal = new UserTenantAware(username, username, authorities, tenant);
this.principal = new TenantAwareUser(username, username, authorities, tenant);
tenantAwareAuthenticationDetails = new TenantAwareAuthenticationDetails(tenant, false);
}

Expand Down

0 comments on commit a0db5ff

Please sign in to comment.