forked from Onlineberatung/onlineBeratung-messageService
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
10 changed files
with
227 additions
and
153 deletions.
There are no files selected for viewing
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
79 changes: 79 additions & 0 deletions
79
src/main/java/de/caritas/cob/messageservice/api/authorization/Authorities.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,79 @@ | ||
package de.caritas.cob.messageservice.api.authorization; | ||
|
||
import static de.caritas.cob.messageservice.api.authorization.Authorities.Authority.ANONYMOUS_DEFAULT; | ||
import static de.caritas.cob.messageservice.api.authorization.Authorities.Authority.ASSIGN_CONSULTANT_TO_ENQUIRY; | ||
import static de.caritas.cob.messageservice.api.authorization.Authorities.Authority.ASSIGN_CONSULTANT_TO_SESSION; | ||
import static de.caritas.cob.messageservice.api.authorization.Authorities.Authority.CONSULTANT_DEFAULT; | ||
import static de.caritas.cob.messageservice.api.authorization.Authorities.Authority.TECHNICAL_DEFAULT; | ||
import static de.caritas.cob.messageservice.api.authorization.Authorities.Authority.USER_DEFAULT; | ||
import static de.caritas.cob.messageservice.api.authorization.Authorities.Authority.USE_FEEDBACK; | ||
import static de.caritas.cob.messageservice.api.authorization.Authorities.Authority.VIEW_AGENCY_CONSULTANTS; | ||
import static de.caritas.cob.messageservice.api.authorization.Authorities.Authority.VIEW_ALL_FEEDBACK_SESSIONS; | ||
import static de.caritas.cob.messageservice.api.authorization.Authorities.Authority.VIEW_ALL_PEER_SESSIONS; | ||
import static java.util.Arrays.asList; | ||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.singletonList; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
/** | ||
* Definition of all authorities and of the role-authority-mapping. | ||
*/ | ||
@Getter | ||
@AllArgsConstructor | ||
public enum Authorities { | ||
|
||
ANONYMOUS(Role.ANONYMOUS, singletonList(ANONYMOUS_DEFAULT)), | ||
USER(Role.USER, singletonList(USER_DEFAULT)), | ||
CONSULTANT(Role.CONSULTANT, singletonList(CONSULTANT_DEFAULT)), | ||
U25_CONSULTANT(Role.U25_CONSULTANT, singletonList(USE_FEEDBACK)), | ||
U25_MAIN_CONSULTANT(Role.U25_MAIN_CONSULTANT, | ||
asList(VIEW_ALL_FEEDBACK_SESSIONS, VIEW_ALL_PEER_SESSIONS, ASSIGN_CONSULTANT_TO_SESSION, | ||
ASSIGN_CONSULTANT_TO_ENQUIRY, VIEW_AGENCY_CONSULTANTS)), | ||
TECHNICAL(Role.TECHNICAL, singletonList(TECHNICAL_DEFAULT)); | ||
|
||
private final Role userRole; | ||
private final List<String> grantedAuthorities; | ||
|
||
/** | ||
* Get all authorities for a specific role. | ||
* | ||
* @param userRole the user role | ||
* @return the related authorities | ||
*/ | ||
public static List<String> getAuthoritiesByUserRole(Role userRole) { | ||
Optional<Authorities> authorityByUserRole = Stream.of(values()) | ||
.filter(authority -> authority.userRole.equals(userRole)) | ||
.findFirst(); | ||
|
||
return authorityByUserRole.isPresent() ? authorityByUserRole.get().getGrantedAuthorities() | ||
: emptyList(); | ||
} | ||
|
||
public static class Authority { | ||
|
||
private Authority() { | ||
} | ||
|
||
public static final String PREFIX = "AUTHORIZATION_"; | ||
|
||
public static final String CONSULTANT_DEFAULT = PREFIX + "CONSULTANT_DEFAULT"; | ||
public static final String USER_DEFAULT = PREFIX + "USER_DEFAULT"; | ||
public static final String USE_FEEDBACK = PREFIX + "USE_FEEDBACK"; | ||
public static final String VIEW_ALL_FEEDBACK_SESSIONS = PREFIX + "VIEW_ALL_FEEDBACK_SESSIONS"; | ||
public static final String VIEW_ALL_PEER_SESSIONS = PREFIX + "VIEW_ALL_PEER_SESSIONS"; | ||
public static final String ASSIGN_CONSULTANT_TO_SESSION = | ||
PREFIX + "ASSIGN_CONSULTANT_TO_SESSION"; | ||
public static final String ASSIGN_CONSULTANT_TO_ENQUIRY = | ||
PREFIX + "ASSIGN_CONSULTANT_TO_ENQUIRY"; | ||
public static final String VIEW_AGENCY_CONSULTANTS = PREFIX + "VIEW_AGENCY_CONSULTANTS"; | ||
public static final String TECHNICAL_DEFAULT = PREFIX + "TECHNICAL_DEFAULT"; | ||
public static final String ANONYMOUS_DEFAULT = PREFIX + "ANONYMOUS_DEFAULT"; | ||
|
||
} | ||
|
||
} |
60 changes: 0 additions & 60 deletions
60
src/main/java/de/caritas/cob/messageservice/api/authorization/Authority.java
This file was deleted.
Oops, something went wrong.
31 changes: 22 additions & 9 deletions
31
src/main/java/de/caritas/cob/messageservice/api/authorization/Role.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 |
---|---|---|
@@ -1,17 +1,30 @@ | ||
package de.caritas.cob.messageservice.api.authorization; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
/** | ||
* | ||
* All used Keycloak roles | ||
* | ||
* All used Keycloak roles. | ||
*/ | ||
@Getter | ||
@AllArgsConstructor | ||
public enum Role { | ||
|
||
TECHNICAL("technical"), | ||
USER("user"), | ||
CONSULTANT("consultant"), | ||
U25_CONSULTANT("u25-consultant"), | ||
U25_MAIN_CONSULTANT("u25-main-consultant"), | ||
ANONYMOUS("anonymous"); | ||
|
||
public class Role { | ||
private final String roleName; | ||
|
||
public static String TECHNICAL = "technical"; | ||
public static String USER = "user"; | ||
public static String CONSULTANT = "consultant"; | ||
public static String U25_CONSULTANT = "u25-consultant"; | ||
public static String U25_MAIN_CONSULTANT = "u25-main-consultant"; | ||
public static Optional<Role> getRoleByName(String roleName) { | ||
return Arrays.stream(values()) | ||
.filter(userRole -> userRole.roleName.equals(roleName)) | ||
.findFirst(); | ||
} | ||
|
||
} |
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.