-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
idriss.naji
committed
Jan 20, 2023
1 parent
001a24f
commit d917ddc
Showing
11 changed files
with
188 additions
and
78 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/main/java/de/caritas/cob/consultingtypeservice/api/auth/Authority.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package de.caritas.cob.consultingtypeservice.api.auth; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
import com.google.common.collect.Lists; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum Authority { | ||
TENANT_ADMIN(UserRole.TENANT_ADMIN, | ||
Lists.newArrayList( | ||
AuthorityValue.PATCH_APPLICATION_SETTINGS, | ||
AuthorityValue.CREATE_CONSULTING_TYPE)), | ||
TOPIC_ADMIN(UserRole.TOPIC_ADMIN, | ||
Lists.newArrayList( | ||
AuthorityValue.CREATE_TOPIC, | ||
AuthorityValue.UPDATE_TOPIC, | ||
AuthorityValue.GET_ALL_TOPICS_WITH_TRANSLATION, | ||
AuthorityValue.GET_TOPICS_TRANSLATION_BY_ID)); | ||
|
||
private final UserRole userRole; | ||
private final List<String> grantedAuthorities; | ||
|
||
public static List<String> getAuthoritiesByUserRole(UserRole userRole) { | ||
Optional<Authority> authorityByUserRole = | ||
Stream.of(values()).filter(authority -> authority.userRole.equals(userRole)).findFirst(); | ||
|
||
return authorityByUserRole.isPresent() | ||
? authorityByUserRole.get().getGrantedAuthorities() | ||
: emptyList(); | ||
} | ||
|
||
public static class AuthorityValue { | ||
|
||
private AuthorityValue() { | ||
} | ||
|
||
|
||
public static final String PREFIX = "AUTHORIZATION_"; | ||
public static final String PATCH_APPLICATION_SETTINGS = PREFIX + "PATCH_APPLICATION_SETTINGS"; | ||
public static final String CREATE_CONSULTING_TYPE = PREFIX + "CREATE_CONSULTING_TYPE"; | ||
public static final String CREATE_TOPIC = PREFIX + "CREATE_TOPIC"; | ||
public static final String UPDATE_TOPIC = PREFIX + "UPDATE_TOPIC"; | ||
public static final String GET_ALL_TOPICS_WITH_TRANSLATION = | ||
PREFIX + "GET_ALL_TOPICS_WITH_TRANSLATION"; | ||
public static final String GET_TOPICS_TRANSLATION_BY_ID = | ||
PREFIX + "GET_TOPICS_TRANSLATION_BY_ID"; | ||
|
||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../java/de/caritas/cob/consultingtypeservice/api/auth/RoleAuthorizationAuthorityMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.caritas.cob.consultingtypeservice.api.auth; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Own implementation of the Spring GrantedAuthoritiesMapper. | ||
*/ | ||
@Component | ||
public class RoleAuthorizationAuthorityMapper implements GrantedAuthoritiesMapper { | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> mapAuthorities( | ||
Collection<? extends GrantedAuthority> authorities) { | ||
Set<String> roleNames = | ||
authorities.stream() | ||
.map(GrantedAuthority::getAuthority) | ||
.map(String::toLowerCase) | ||
.collect(Collectors.toSet()); | ||
|
||
return mapAuthorities(roleNames); | ||
} | ||
|
||
private Set<GrantedAuthority> mapAuthorities(Set<String> roleNames) { | ||
return roleNames.parallelStream() | ||
.map(UserRole::getRoleByValue) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.map(Authority::getAuthoritiesByUserRole) | ||
.flatMap(Collection::parallelStream) | ||
.map(SimpleGrantedAuthority::new) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.