Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
DC-#125: merge main into branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Lica200 committed May 5, 2024
2 parents 97cb7cc + 1c7fd17 commit bfa71ae
Show file tree
Hide file tree
Showing 80 changed files with 3,398 additions and 1,284 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public SecurityFilterChain filterChain(
"/webjars/**",
"/oauth2/authorization/google",
"/login",
"/landingpage",
"/swagger-ui",
"/swagger-ui/**",
"/v3/api-docs/**").permitAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public Authentication authenticate(Authentication authentication) throws Authent
return null;
}
authentication.setAuthenticated(true);
log.debug("Authenticated: " + authentication);
return authentication;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
@RequestMapping("/event")
public class EventController {

private EventService eventService;
private final EventService eventService;

public EventController(EventService eventService) {
EventController(EventService eventService) {
this.eventService = eventService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@
import com.dhbw.get2gether.backend.event.model.Event;
import com.dhbw.get2gether.backend.event.model.EventCreateCommand;
import com.dhbw.get2gether.backend.event.model.EventUpdateCommand;
import com.dhbw.get2gether.backend.event.model.EventWidgetUpdateCommand;
import com.dhbw.get2gether.backend.exceptions.EntityNotFoundException;
import com.dhbw.get2gether.backend.user.application.UserService;
import com.dhbw.get2gether.backend.user.model.User;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.core.env.Environment;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class EventService {
private final Environment env;
Expand All @@ -28,7 +31,7 @@ public class EventService {
private final EventRepository eventRepository;
private final UserService userService;

public EventService(
EventService(
EventMapper eventMapper, EventRepository eventRepository, UserService userService, Environment env) {
this.eventMapper = eventMapper;
this.eventRepository = eventRepository;
Expand Down Expand Up @@ -96,17 +99,10 @@ public Event updateEvent(AuthenticatedPrincipal principal, String eventId, Event
}

@PreAuthorize("hasRole('USER')")
public Event addParticipantToEvent(AuthenticatedPrincipal principal, String invitationLink) {
Optional<Event> event = eventRepository.findByInvitationLink(invitationLink);
return event.map(presentEvent -> {
Optional<User> user = this.userService.findUserFromPrincipal(principal);
return user.map(presentUser -> {
presentEvent.addParticipant(presentUser.getId());
return eventRepository.save(presentEvent);
})
.orElseThrow(() -> new EntityNotFoundException("User not found"));
})
.orElseThrow(() -> new EntityNotFoundException("Event not found"));
public Event updateEventWidgets(AuthenticatedPrincipal principal, String eventId, EventWidgetUpdateCommand eventWidgetUpdateCommand) {
Event oldEvent = getEventIfUserIsParticipant(principal, eventId);
Event newEvent = eventMapper.updateEvent(oldEvent, eventWidgetUpdateCommand);
return eventRepository.save(newEvent);
}

@PreAuthorize("hasRole('USER')")
Expand All @@ -117,12 +113,17 @@ public Event generateInvitationLink(AuthenticatedPrincipal principal, String eve
}

@PreAuthorize("hasRole('GUEST')")
public Optional<String> findRouteFromInvitationLink(AuthenticatedPrincipal principal, String invitationLink) {
public Optional<String> openEventFromInvitationLink(AuthenticatedPrincipal principal, String invitationLink) {
Optional<Event> event = findEventByInvitationLink(invitationLink);
if (event.isPresent() && principal instanceof GuestAuthenticationPrincipal guestPrincipal) {
guestPrincipal.grantAccessToEvent(event.get().getId());
}
return event.map(presentEvent -> env.getProperty("frontend.url") + "/event/" + presentEvent.getId());
return event.map(presentEvent -> {
if (principal instanceof GuestAuthenticationPrincipal guestPrincipal) {
guestPrincipal.grantAccessToEvent(presentEvent.getId());
} else if (principal instanceof OAuth2User) {
// add user as participant
addUserToParticipantsIfNotParticipating(principal, presentEvent);
}
return env.getProperty("frontend.url") + "/event/" + presentEvent.getId();
});
}

private Event getEventById(String eventId) {
Expand All @@ -144,4 +145,11 @@ private Event getEventIfUserIsParticipant(AuthenticatedPrincipal principal, Stri
private Optional<Event> findEventByInvitationLink(String invitationLink) {
return eventRepository.findByInvitationLink(invitationLink);
}

private Event addUserToParticipantsIfNotParticipating(AuthenticatedPrincipal principal, Event event) {
User user = userService.getUserByPrincipal(principal);
if (event.getParticipantIds().contains(user.getId())) return event;
event.addParticipant(user.getId());
return eventRepository.save(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dhbw.get2gether.backend.event.model.Event;
import com.dhbw.get2gether.backend.event.model.EventCreateCommand;
import com.dhbw.get2gether.backend.event.model.EventUpdateCommand;
import com.dhbw.get2gether.backend.event.model.EventWidgetUpdateCommand;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
Expand All @@ -18,4 +19,6 @@ public interface EventMapper {
Event toEvent(EventCreateCommand eventCreateCommand);

Event updateEvent(@MappingTarget Event event, EventUpdateCommand eventUpdateCommand);

Event updateEvent(@MappingTarget Event event, EventWidgetUpdateCommand eventWidgetUpdateCommand);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.dhbw.get2gether.backend.event.model;

import com.dhbw.get2gether.backend.widget.model.Widget;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;

import java.time.LocalDateTime;
import java.util.List;

@AllArgsConstructor
@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.dhbw.get2gether.backend.event.model;

import com.dhbw.get2gether.backend.widget.model.Widget;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Builder
@Getter
@AllArgsConstructor
public class EventWidgetUpdateCommand {
private List<Widget> widgets;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Entity not found")
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class EntityNotFoundException extends RuntimeException {

public EntityNotFoundException() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dhbw.get2gether.backend.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public class OperationNotAllowedException extends RuntimeException {

public OperationNotAllowedException() {}

public OperationNotAllowedException(String message) {
super(message);
}

public OperationNotAllowedException(String message, Throwable cause) {
super(message, cause);
}

public OperationNotAllowedException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public String getInvitationLink(
@AuthenticationPrincipal AuthenticatedPrincipal principal,
HttpServletResponse httpServletResponse,
@PathVariable String invitationLink) {
Optional<String> locationUrl = eventService.findRouteFromInvitationLink(principal, invitationLink);
Optional<String> locationUrl = eventService.openEventFromInvitationLink(principal, invitationLink);
if (locationUrl.isPresent()) {
httpServletResponse.setHeader("Location", locationUrl.get());
httpServletResponse.setStatus(HttpServletResponse.SC_FOUND);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package com.dhbw.get2gether.backend.user.adapter.in;

import com.dhbw.get2gether.backend.user.application.UserService;
import com.dhbw.get2gether.backend.user.model.UpdateUserCommand;
import com.dhbw.get2gether.backend.user.model.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

@Autowired
UserService userService;
private final UserService userService;

UserController(UserService userService) {
this.userService = userService;
}

@GetMapping("/error")
public String error(HttpServletRequest request) {
Expand All @@ -32,4 +35,9 @@ public User user(@AuthenticationPrincipal AuthenticatedPrincipal principal) {
public User user(@PathVariable String id) {
return userService.getUserById(id);
}

@PutMapping("user/self")
public User updateUser(@AuthenticationPrincipal AuthenticatedPrincipal principal, @RequestBody UpdateUserCommand updateUserCommand){
return userService.updateUser(principal, updateUserCommand);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dhbw.get2gether.backend.user.application;

import com.dhbw.get2gether.backend.user.model.CreateUserCommand;
import com.dhbw.get2gether.backend.user.model.UpdateUserCommand;
import com.dhbw.get2gether.backend.user.model.SyncUserCommand;
import com.dhbw.get2gether.backend.user.model.User;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -16,7 +16,7 @@ public class OAuthUserService extends DefaultOAuth2UserService {

private final UserService userService;

public OAuthUserService(@Autowired UserService userService) {
OAuthUserService(UserService userService) {
this.userService = userService;
}

Expand All @@ -34,17 +34,17 @@ private void processOAuthUser(OAuth2User oAuth2User) {
user -> updateUser(oAuth2User, createUpdateCommand(user, oAuth2User)), () -> createNewUser(oAuth2User));
}

private UpdateUserCommand createUpdateCommand(User user, OAuth2User oAuth2User) {
return UpdateUserCommand.builder()
private SyncUserCommand createUpdateCommand(User user, OAuth2User oAuth2User) {
return SyncUserCommand.builder()
.firstName(oAuth2User.getAttribute("given_name"))
.lastName(oAuth2User.getAttribute("family_name"))
.email(oAuth2User.getAttribute("email"))
.profilePictureUrl(oAuth2User.getAttribute("picture"))
.build();
}

private void updateUser(OAuth2User principal, UpdateUserCommand updateUserCommand) {
userService.updateUser(principal, updateUserCommand);
private void updateUser(OAuth2User principal, SyncUserCommand syncUserCommand) {
userService.syncUser(principal, syncUserCommand);
}

private void createNewUser(OAuth2User oAuth2User) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@
import com.dhbw.get2gether.backend.exceptions.EntityNotFoundException;
import com.dhbw.get2gether.backend.user.adapter.out.UserRepository;
import com.dhbw.get2gether.backend.user.application.mapper.UserMapper;
import com.dhbw.get2gether.backend.user.model.CreateUserCommand;
import com.dhbw.get2gether.backend.user.model.Guest;
import com.dhbw.get2gether.backend.user.model.UpdateUserCommand;
import com.dhbw.get2gether.backend.user.model.User;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import com.dhbw.get2gether.backend.user.model.*;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;

@Service
public class UserService {
private final UserRepository userRepository;
private final UserMapper userMapper;

public UserService(@Autowired UserRepository userRepository, UserMapper userMapper) {
UserService(UserRepository userRepository, UserMapper userMapper) {
this.userRepository = userRepository;
this.userMapper = userMapper;
}
Expand Down Expand Up @@ -53,17 +50,27 @@ public User getUserByPrincipal(AuthenticatedPrincipal principal) {
return findUserFromPrincipal(principal).orElseThrow(() -> new EntityNotFoundException("User not found"));
}

@PreAuthorize("hasRole('USER')")
public User updateUser(AuthenticatedPrincipal principal, UpdateUserCommand updateUserCommand) {
User oldUser = getUserByPrincipal(principal);
User newUser = userMapper.mapSettingsCommandToUser(oldUser, updateUserCommand);
return userRepository.save(newUser);
}

protected User createUser(CreateUserCommand command) {
User user = userMapper.mapToUser(command).toBuilder()
.id(UUID.randomUUID().toString())
.creationDate(LocalDateTime.now())
.settings(Settings.builder()
.colorMode(ColorMode.LIGHT)
.build())
.build();
return userRepository.insert(user);
}

protected User updateUser(OAuth2User principal, UpdateUserCommand updateUserCommand) {
protected User syncUser(OAuth2User principal, SyncUserCommand syncUserCommand) {
User oldUser = getUserByPrincipal(principal);
User newUser = userMapper.updateUser(oldUser, updateUserCommand);
User newUser = userMapper.mapSyncCommandToUser(oldUser, syncUserCommand);
return userRepository.save(newUser);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

import com.dhbw.get2gether.backend.user.model.CreateUserCommand;
import com.dhbw.get2gether.backend.user.model.UpdateUserCommand;
import com.dhbw.get2gether.backend.user.model.SyncUserCommand;
import com.dhbw.get2gether.backend.user.model.User;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

@Mapper(componentModel = "spring")
public interface UserMapper {
UserMapper MAPPER = Mappers.getMapper(UserMapper.class);

@Mapping(target = "id", ignore = true)
User mapToUser(CreateUserCommand command);

User updateUser(@MappingTarget User user, UpdateUserCommand command);

User mapSyncCommandToUser(@MappingTarget User user, SyncUserCommand command);

User mapSettingsCommandToUser(@MappingTarget User user, UpdateUserCommand command);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dhbw.get2gether.backend.user.model;

public enum ColorMode {
LIGHT,
DARK,
MODERN


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Guest extends User {
private final boolean isGuest = true;

public Guest(String id, LocalDateTime creationDate) {
super(id, creationDate, "Guest", "", "", getProfilePictureUrl(id));
super(id, creationDate, "Guest", "", "", getProfilePictureUrl(id), new Settings(ColorMode.LIGHT));
}

private static String getProfilePictureUrl(String hash) {
Expand Down
Loading

0 comments on commit bfa71ae

Please sign in to comment.