Skip to content

Commit

Permalink
feat: provide new authorization role for user admin
Browse files Browse the repository at this point in the history
  • Loading branch information
mebo4b committed Nov 20, 2020
1 parent 831c2e5 commit 1962b75
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 208 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package de.caritas.cob.userservice.api.authorization;

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.
*/
@AllArgsConstructor
@Getter
public enum Authorities {

CONSULTANT(UserRole.CONSULTANT, singletonList(Authority.CONSULTANT_DEFAULT)),
USER(UserRole.USER, singletonList(Authority.USER_DEFAULT)),
U25_CONSULTANT(UserRole.U25_CONSULTANT, singletonList(Authority.USE_FEEDBACK)),
U25_MAIN_CONSULTANT(UserRole.U25_MAIN_CONSULTANT, asList(
Authority.VIEW_ALL_FEEDBACK_SESSIONS, Authority.VIEW_ALL_PEER_SESSIONS,
Authority.ASSIGN_CONSULTANT_TO_SESSION, Authority.ASSIGN_CONSULTANT_TO_ENQUIRY,
Authority.VIEW_AGENCY_CONSULTANTS)),
TECHNICAL(UserRole.TECHNICAL, singletonList(Authority.TECHNICAL_DEFAULT)),
KREUZBUND_CONSULTANT(UserRole.KREUZBUND_CONSULTANT, asList(
Authority.CONSULTANT_DEFAULT, Authority.CREATE_NEW_CHAT,
Authority.START_CHAT, Authority.STOP_CHAT, Authority.UPDATE_CHAT)),
USER_ADMIN(UserRole.USER_ADMIN, singletonList(Authority.USER_ADMIN));

private final UserRole userRole;
private final List<String> auhorities;

public static List<String> getAuthoritiesByUserRole(UserRole userRole) {
Optional<Authorities> authorityByUserRole = Stream.of(values())
.filter(authority -> authority.userRole.equals(userRole))
.findFirst();

return authorityByUserRole.isPresent() ?
authorityByUserRole.get().getAuhorities() : 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 CREATE_NEW_CHAT = PREFIX + "CREATE_NEW_CHAT";
public static final String START_CHAT = PREFIX + "START_CHAT";
public static final String STOP_CHAT = PREFIX + "STOP_CHAT";
public static final String UPDATE_CHAT = PREFIX + "UPDATE_CHAT";
public static final String USER_ADMIN = PREFIX + "USER_ADMIN";
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package de.caritas.cob.userservice.api.authorization;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -14,7 +11,7 @@

/**
*
* Own implementation of the Spring GrantedAuthoritiesMapper
* Own implementation of the Spring GrantedAuthoritiesMapper.
*
*/
@Component
Expand All @@ -23,26 +20,23 @@ public class RoleAuthorizationAuthorityMapper implements GrantedAuthoritiesMappe
@Override
public Collection<? extends GrantedAuthority> mapAuthorities(
Collection<? extends GrantedAuthority> authorities) {
Set<String> roleNames = authorities.stream().map(GrantedAuthority::getAuthority)
.map(String::toLowerCase).collect(Collectors.toSet());
Set<String> roleNames = authorities.stream()
.map(GrantedAuthority::getAuthority)
.map(String::toLowerCase)
.collect(Collectors.toSet());

HashSet<GrantedAuthority> mapped = new HashSet<>();
mapped.addAll(mapAuthorities(roleNames));

return mapped;
return mapAuthorities(roleNames);
}

private Set<GrantedAuthority> mapAuthorities(Set<String> roleNames) {
List<SimpleGrantedAuthority> grantendAuthorities = new ArrayList<SimpleGrantedAuthority>();
roleNames.forEach(roleName -> {
Optional<UserRole> userRoleOptional = UserRole.getRoleByValue(roleName);
if (userRoleOptional.isPresent()) {
grantendAuthorities.addAll(Authority.getAuthoritiesByUserRole(userRoleOptional.get())
.stream().map(authority -> new SimpleGrantedAuthority(authority))
.collect(Collectors.toList()));
}
});
return new HashSet<>(grantendAuthorities);
return roleNames.parallelStream()
.map(UserRole::getRoleByValue)
.filter(Optional::isPresent)
.map(Optional::get)
.map(Authorities::getAuthoritiesByUserRole)
.flatMap(Collection::parallelStream)
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toSet());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
@AllArgsConstructor
@Getter
public enum UserRole {
USER("user"), CONSULTANT("consultant"), TECHNICAL("technical"), U25_CONSULTANT(
"u25-consultant"), U25_MAIN_CONSULTANT(
"u25-main-consultant"), KREUZBUND_CONSULTANT("kreuzbund-consultant");

USER("user"),
CONSULTANT("consultant"),
TECHNICAL("technical"),
U25_CONSULTANT("u25-consultant"),
U25_MAIN_CONSULTANT("u25-main-consultant"),
KREUZBUND_CONSULTANT("kreuzbund-consultant"),
USER_ADMIN("user-admin");

private final String value;

public static Optional<UserRole> getRoleByValue(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static java.util.Objects.requireNonNull;
import static org.apache.commons.collections.CollectionUtils.isNotEmpty;

import de.caritas.cob.userservice.api.authorization.Authority;
import de.caritas.cob.userservice.api.authorization.Authorities.Authority;
import de.caritas.cob.userservice.api.container.RocketChatCredentials;
import de.caritas.cob.userservice.api.container.SessionListQueryParameter;
import de.caritas.cob.userservice.api.controller.validation.MinValue;
Expand All @@ -26,7 +26,6 @@
import de.caritas.cob.userservice.api.helper.AuthenticatedUser;
import de.caritas.cob.userservice.api.helper.AuthenticatedUserHelper;
import de.caritas.cob.userservice.api.model.AbsenceDTO;
import de.caritas.cob.userservice.api.model.chat.ChatDTO;
import de.caritas.cob.userservice.api.model.ChatInfoResponseDTO;
import de.caritas.cob.userservice.api.model.ChatMembersResponseDTO;
import de.caritas.cob.userservice.api.model.ConsultantResponseDTO;
Expand All @@ -35,18 +34,19 @@
import de.caritas.cob.userservice.api.model.CreateUserResponseDTO;
import de.caritas.cob.userservice.api.model.EnquiryMessageDTO;
import de.caritas.cob.userservice.api.model.MasterKeyDTO;
import de.caritas.cob.userservice.api.model.monitoring.MonitoringDTO;
import de.caritas.cob.userservice.api.model.NewMessageNotificationDTO;
import de.caritas.cob.userservice.api.model.registration.NewRegistrationDto;
import de.caritas.cob.userservice.api.model.NewRegistrationResponseDto;
import de.caritas.cob.userservice.api.model.PasswordDTO;
import de.caritas.cob.userservice.api.model.UpdateChatResponseDTO;
import de.caritas.cob.userservice.api.model.registration.UserDTO;
import de.caritas.cob.userservice.api.model.user.UserDataResponseDTO;
import de.caritas.cob.userservice.api.model.UserSessionListResponseDTO;
import de.caritas.cob.userservice.api.model.UserSessionResponseDTO;
import de.caritas.cob.userservice.api.model.chat.ChatDTO;
import de.caritas.cob.userservice.api.model.keycloak.KeycloakCreateUserResponseDTO;
import de.caritas.cob.userservice.api.model.keycloak.login.LoginResponseDTO;
import de.caritas.cob.userservice.api.model.monitoring.MonitoringDTO;
import de.caritas.cob.userservice.api.model.registration.NewRegistrationDto;
import de.caritas.cob.userservice.api.model.registration.UserDTO;
import de.caritas.cob.userservice.api.model.user.UserDataResponseDTO;
import de.caritas.cob.userservice.api.repository.chat.Chat;
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
import de.caritas.cob.userservice.api.repository.session.Session;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static java.util.Objects.nonNull;

import de.caritas.cob.userservice.api.authorization.Authority;
import de.caritas.cob.userservice.api.authorization.Authorities.Authority;
import de.caritas.cob.userservice.api.container.CreateEnquiryExceptionInformation;
import de.caritas.cob.userservice.api.container.RocketChatCredentials;
import de.caritas.cob.userservice.api.exception.CreateEnquiryException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static java.util.Objects.nonNull;

import de.caritas.cob.userservice.api.authorization.Authority;
import de.caritas.cob.userservice.api.authorization.Authorities.Authority;
import de.caritas.cob.userservice.api.exception.UpdateSessionException;
import de.caritas.cob.userservice.api.exception.httpresponses.InternalServerErrorException;
import de.caritas.cob.userservice.api.exception.keycloak.KeycloakException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,26 @@

import static org.apache.commons.lang3.BooleanUtils.isTrue;

import de.caritas.cob.userservice.api.exception.httpresponses.InternalServerErrorException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatRemoveSystemMessagesException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import de.caritas.cob.userservice.api.authorization.Authority;
import de.caritas.cob.userservice.api.authorization.Authorities.Authority;
import de.caritas.cob.userservice.api.container.CreateEnquiryExceptionInformation;
import de.caritas.cob.userservice.api.container.RocketChatCredentials;
import de.caritas.cob.userservice.api.exception.AgencyServiceHelperException;
import de.caritas.cob.userservice.api.exception.ImportException;
import de.caritas.cob.userservice.api.exception.SaveUserException;
import de.caritas.cob.userservice.api.exception.httpresponses.InternalServerErrorException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatCreateGroupException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatLoginException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatRemoveSystemMessagesException;
import de.caritas.cob.userservice.api.helper.Helper;
import de.caritas.cob.userservice.api.helper.MonitoringHelper;
import de.caritas.cob.userservice.api.helper.RocketChatHelper;
import de.caritas.cob.userservice.api.helper.UserHelper;
import de.caritas.cob.userservice.api.manager.consultingType.ConsultingTypeManager;
import de.caritas.cob.userservice.api.manager.consultingType.ConsultingTypeSettings;
import de.caritas.cob.userservice.api.model.AgencyDTO;
import de.caritas.cob.userservice.api.model.keycloak.KeycloakCreateUserResponseDTO;
import de.caritas.cob.userservice.api.model.monitoring.MonitoringDTO;
import de.caritas.cob.userservice.api.model.registration.UserDTO;
import de.caritas.cob.userservice.api.model.keycloak.KeycloakCreateUserResponseDTO;
import de.caritas.cob.userservice.api.model.rocketchat.login.LoginResponseDTO;
import de.caritas.cob.userservice.api.repository.consultant.Consultant;
import de.caritas.cob.userservice.api.repository.consultantAgency.ConsultantAgency;
Expand All @@ -60,8 +33,35 @@
import de.caritas.cob.userservice.api.service.helper.AgencyServiceHelper;
import de.caritas.cob.userservice.api.service.helper.KeycloakAdminClientHelper;
import de.caritas.cob.userservice.api.service.helper.MessageServiceHelper;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
* Imports the askers from the created CSV file of the old Caritas system.
Expand Down
Loading

0 comments on commit 1962b75

Please sign in to comment.