Skip to content

Commit

Permalink
Merge pull request #69 from Onlineberatung/develop
Browse files Browse the repository at this point in the history
[pull] develop from Onlineberatung:develop
  • Loading branch information
pull[bot] authored Sep 13, 2022
2 parents 06c14b1 + a0e0e0f commit 4996218
Show file tree
Hide file tree
Showing 30 changed files with 146 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ public Optional<Map<String, Object>> patchUser(Map<String, Object> patchMap) {
return userMap.isEmpty() ? Optional.empty() : Optional.of(userMap);
}

@Override
public boolean existsAdviceSeeker(String id) {
return userRepository.findByUserIdAndDeleteDateIsNull(id).isPresent();
}

@Override
public Optional<Map<String, Object>> findAdviceSeeker(String id) {
var userMap = new HashMap<String, Object>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static de.caritas.cob.userservice.api.exception.httpresponses.customheader.HttpStatusExceptionReason.EMAIL_NOT_AVAILABLE;
import static de.caritas.cob.userservice.api.exception.httpresponses.customheader.HttpStatusExceptionReason.USERNAME_NOT_AVAILABLE;
import static de.caritas.cob.userservice.api.helper.RequestHelper.getAuthorizedHttpHeaders;
import static de.caritas.cob.userservice.api.helper.RequestHelper.getFormHttpHeaders;
import static java.lang.Boolean.TRUE;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
Expand Down Expand Up @@ -49,6 +47,7 @@
import org.keycloak.representations.idm.UserRepresentation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -67,10 +66,7 @@
@RequiredArgsConstructor
public class KeycloakService implements IdentityClient {

private static final String KEYCLOAK_GRANT_TYPE_PW = "password";
private static final String KEYCLOAK_GRANT_TYPE_REFRESH_TOKEN = "refresh_token";
private static final String BODY_KEY_USERNAME = "username";
private static final String BODY_KEY_PASSWORD = "password";
private static final String BODY_KEY_CLIENT_ID = "client_id";
private static final String BODY_KEY_GRANT_TYPE = "grant_type";
private static final String ENDPOINT_OPENID_CONNECT_LOGIN = "/token";
Expand Down Expand Up @@ -135,21 +131,11 @@ public void changeLanguage(final String userId, final String locale) {
* @return {@link KeycloakLoginResponseDTO}
*/
public KeycloakLoginResponseDTO loginUser(final String userName, final String password) {

MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add(BODY_KEY_USERNAME, userName);
map.add(BODY_KEY_PASSWORD, password);
map.add(BODY_KEY_CLIENT_ID, keycloakClientId);
map.add(BODY_KEY_GRANT_TYPE, KEYCLOAK_GRANT_TYPE_PW);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, getFormHttpHeaders());
var entity = loginRequest(userName, password);
var url = identityClientConfig.getOpenIdConnectUrl(ENDPOINT_OPENID_CONNECT_LOGIN);

try {
return restTemplate
.postForEntity(
identityClientConfig.getOpenIdConnectUrl(ENDPOINT_OPENID_CONNECT_LOGIN),
request,
KeycloakLoginResponseDTO.class)
.getBody();
return restTemplate.postForEntity(url, entity, KeycloakLoginResponseDTO.class).getBody();

} catch (RestClientResponseException exception) {
throw new BadRequestException(
Expand All @@ -159,15 +145,22 @@ public KeycloakLoginResponseDTO loginUser(final String userName, final String pa
}
}

@Override
public boolean verifyIgnoringOtp(String username, String password) {
private HttpEntity<MultiValueMap<String, String>> loginRequest(String userName, String password) {
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add(BODY_KEY_USERNAME, username);
map.add(BODY_KEY_PASSWORD, password);
map.add("username", userName);
map.add("password", password);
map.add(BODY_KEY_CLIENT_ID, keycloakClientId);
map.add(BODY_KEY_GRANT_TYPE, KEYCLOAK_GRANT_TYPE_PW);
map.add(BODY_KEY_GRANT_TYPE, "password");

var httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

var entity = new HttpEntity<>(map, getFormHttpHeaders());
return new HttpEntity<>(map, httpHeaders);
}

@Override
public boolean verifyIgnoringOtp(String username, String password) {
var entity = loginRequest(username, password);
var url = identityClientConfig.getOpenIdConnectUrl(ENDPOINT_OPENID_CONNECT_LOGIN);

ResponseEntity<KeycloakLoginResponseDTO> loginResponse;
Expand All @@ -194,14 +187,14 @@ public boolean verifyIgnoringOtp(String username, String password) {
* @return true if logout was successful
*/
public boolean logoutUser(final String refreshToken) {

var httpHeaders =
getAuthorizedHttpHeaders(
authenticatedUser.getAccessToken(), MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add(BODY_KEY_CLIENT_ID, keycloakClientId);
map.add(BODY_KEY_GRANT_TYPE, KEYCLOAK_GRANT_TYPE_REFRESH_TOKEN);
map.add(KEYCLOAK_GRANT_TYPE_REFRESH_TOKEN, refreshToken);

var httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
httpHeaders.add("Authorization", "Bearer " + authenticatedUser.getAccessToken());
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, httpHeaders);

var url = identityClientConfig.getOpenIdConnectUrl(ENDPOINT_OPENID_CONNECT_LOGOUT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,7 @@ public ResponseEntity<Void> deleteSessionAndInactiveUser(@PathVariable Long sess
sessionService
.getSession(sessionId)
.orElseThrow(
() ->
new NotFoundException(
String.format("A session with an id %s does not exist.", sessionId)));
() -> new NotFoundException("A session with an id %s does not exist.", sessionId));

var user = session.getUser();
if (user.getSessions().size() == 1) {
Expand Down Expand Up @@ -527,9 +525,7 @@ public ResponseEntity<Void> updateConsultantData(UpdateConsultantDTO updateConsu
consultantService
.getConsultant(consultantId)
.orElseThrow(
() ->
new NotFoundException(
String.format("Consultant with id %s not found", consultantId)));
() -> new NotFoundException("Consultant with id %s not found", consultantId));

var updateAdminConsultantDTO =
consultantDtoMapper.updateAdminConsultantOf(updateConsultantDTO, consultant);
Expand Down Expand Up @@ -874,15 +870,12 @@ public ResponseEntity<Void> removeFromSession(Long sessionId, UUID consultantId)
accountManager
.findConsultant(consultantId.toString())
.orElseThrow(
() ->
new NotFoundException(
String.format("Consultant (%s) not found", consultantId)));
() -> new NotFoundException("Consultant (%s) not found", consultantId.toString()));

var sessionMap =
messenger
.findSession(sessionId)
.orElseThrow(
() -> new NotFoundException(String.format("Session (%s) not found", sessionId)));
.orElseThrow(() -> new NotFoundException("Session (%s) not found", sessionId));

var chatId = consultantDtoMapper.chatIdOf(sessionMap);
var chatUserId = userDtoMapper.chatUserIdOf(consultantMap);
Expand Down Expand Up @@ -1376,14 +1369,13 @@ public ResponseEntity<Void> deactivateTwoFactorAuthByApp() {
*/
@Override
public ResponseEntity<ConsultantResponseDTO> getConsultantPublicData(UUID consultantId) {
var consultantIdString = consultantId.toString();
var consultant =
consultantService
.getConsultant(consultantId.toString())
.getConsultant(consultantIdString)
.orElseThrow(
() ->
new NotFoundException(
String.format("Consultant with id %s not found", consultantId)));
var agencies = consultantAgencyService.getAgenciesOfConsultant(consultantId.toString());
() -> new NotFoundException("Consultant with id %s not found", consultantIdString));
var agencies = consultantAgencyService.getAgenciesOfConsultant(consultantIdString);
var consultantDto = consultantDtoMapper.consultantResponseDtoOf(consultant, agencies, false);

return new ResponseEntity<>(consultantDto, HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

/** User model */
@AllArgsConstructor
Expand Down Expand Up @@ -125,7 +126,7 @@ public class UserDTO implements UserRegistrationDTO {
private LanguageCode preferredLanguage;

public Integer getUserAge() {
return age == null ? null : Integer.valueOf(age);
return StringUtils.isNumeric(age) ? Integer.valueOf(age) : null;
}

public UserDTO(String email) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ public void markAskerForDeletion(String userId) {
User user =
userService
.getUser(userId)
.orElseThrow(
() ->
new NotFoundException(
String.format("Asker with id %s does not exist", userId)));
.orElseThrow(() -> new NotFoundException("Asker with id %s does not exist", userId));

if (nonNull(user.getDeleteDate())) {
throw new ConflictException(
Expand All @@ -51,10 +48,7 @@ public AskerResponseDTO getAsker(String userId) {
User user =
userService
.getUser(userId)
.orElseThrow(
() ->
new NotFoundException(
String.format("Asker with id %s does not exist", userId)));
.orElseThrow(() -> new NotFoundException("Asker with id %s does not exist", userId));
AskerResponseDTO asker = new AskerResponseDTO();
asker.setId(user.getUserId());
asker.setUsername(this.usernameTranscoder.decodeUsername(user.getUsername()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public void markConsultantForDeletion(String consultantId) {
this.consultantRepository
.findByIdAndDeleteDateIsNull(consultantId)
.orElseThrow(
() ->
new NotFoundException(
String.format("Consultant with id %s does not exist", consultantId)));
() -> new NotFoundException("Consultant with id %s does not exist", consultantId));

this.consultantPreDeletionService.performPreDeletionSteps(consultant);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ public class AcceptAnonymousEnquiryFacade {
*/
public void acceptAnonymousEnquiry(Long sessionId) {
var session =
this.sessionService
sessionService
.getSession(sessionId)
.orElseThrow(
() ->
new NotFoundException(
String.format("Session with id %s does not exist", sessionId)));
() -> new NotFoundException("Session with id %s does not exist", sessionId));

var consultant = this.userAccountProvider.retrieveValidatedConsultant();
this.assignEnquiryFacade.assignAnonymousEnquiry(session, consultant);
this.liveEventNotificationService.sendAcceptAnonymousEnquiryEventToUser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public void finishConversation(Long sessionId) {
this.sessionService
.getSession(sessionId)
.orElseThrow(
() ->
new NotFoundException(
String.format("Session with id %s does not exist", sessionId)));
() -> new NotFoundException("Session with id %s does not exist", sessionId));

this.actionsRegistry
.buildContainerForType(User.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public NotFoundException(String message, String arg1, Long arg2) {
super(String.format(message, arg1, arg2), LogService::logWarn);
}

public NotFoundException(String message, Long arg1, String arg2) {
super(String.format(message, arg1, arg2), LogService::logWarn);
}

/**
* Not found exception.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ public void assignChat(Long chatId, AuthenticatedUser authenticatedUser) {
private Chat getChat(Long chatId) {
return chatService
.getChat(chatId)
.orElseThrow(
() -> new NotFoundException(String.format("Chat with id %s not found", chatId)));
.orElseThrow(() -> new NotFoundException("Chat with id %s not found", chatId));
}

private User getUser(AuthenticatedUser authenticatedUser) {
return userService
.getUserViaAuthenticatedUser(authenticatedUser)
.orElseThrow(
() ->
new NotFoundException(
String.format("User with id %s not found", authenticatedUser.getUserId())));
new NotFoundException("User with id %s not found", authenticatedUser.getUserId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public ChatInfoResponseDTO getChat(Long chatId) {
Chat chat =
chatService
.getChat(chatId)
.orElseThrow(
() -> new NotFoundException(String.format("Chat with id %s not found.", chatId)));
.orElseThrow(() -> new NotFoundException("Chat with id %s not found.", chatId));

this.chatPermissionVerifier.verifyPermissionForChat(chat);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public ChatMembersResponseDTO getChatMembers(Long chatId) {
Chat chat =
chatService
.getChat(chatId)
.orElseThrow(
() -> new NotFoundException(String.format("Chat with id %s not found", chatId)));
.orElseThrow(() -> new NotFoundException("Chat with id %s not found", chatId));

verifyActiveStatus(chat);
this.chatPermissionVerifier.verifyPermissionForChat(chat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ private Chat getChat(Long chatId) {
Chat chat =
chatService
.getChat(chatId)
.orElseThrow(
() -> new NotFoundException(String.format("Chat with id %s not found", chatId)));
.orElseThrow(() -> new NotFoundException("Chat with id %s not found", chatId));

if (isFalse(chat.isActive())) {
throw new ConflictException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ private void verifyConsultantPermissionForChat(Chat chat) {
.orElseThrow(
() ->
new NotFoundException(
String.format(
"Consultant with id %s not " + "found",
authenticatedUser.getUserId())));
"Consultant with id %s not found", authenticatedUser.getUserId()));

if (!hasSameAgencyAssigned(chat, consultant)) {
throw new ForbiddenException(
Expand Down Expand Up @@ -131,7 +129,7 @@ private void verifyUserPermissionForChat(Chat chat) {
.orElseThrow(
() ->
new NotFoundException(
String.format("User with id %s not found", authenticatedUser.getUserId())));
"User with id %s not found", authenticatedUser.getUserId()));

if (!hasChatUserAssignment(chat, user) && !hasSameAgencyAssigned(chat, user)) {
throw new ForbiddenException(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public interface AccountManaging {

Optional<Map<String, Object>> patchUser(Map<String, Object> patchMap);

boolean existsAdviceSeeker(String id);

Optional<Map<String, Object>> findAdviceSeeker(String id);

Optional<User> findAdviceSeekerByChatUserId(String chatId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IdentityManaging {

Map<String, String> validateOneTimePassword(String username, String code);

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
boolean validatePasswordIgnoring2fa(String username, String password);

boolean changePassword(String userId, String password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public interface ConsultantAgencyRepository extends CrudRepository<ConsultantAge

List<ConsultantAgency> findByConsultantIdAndDeleteDateIsNull(String consultantId);

List<ConsultantAgency> findByConsultantIdInAndDeleteDateIsNull(Set<String> consultantId);

List<ConsultantAgency> findByAgencyIdAndDeleteDateIsNullOrderByConsultantFirstNameAsc(
Long agencyId);

Expand Down
Loading

0 comments on commit 4996218

Please sign in to comment.