Skip to content

Commit 03e88d5

Browse files
committed
✨ rewrite backend to use email instead of lhmExtId and extract mail directly from JWT token
1 parent 15fa642 commit 03e88d5

File tree

17 files changed

+238
-157
lines changed

17 files changed

+238
-157
lines changed

personalization-service/src/main/java/de/muenchen/dbs/personalization/checklist/ChecklistController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public class ChecklistController {
2626
private final ChecklistMapper checklistMapper;
2727

2828
@GetMapping
29-
@Operation(summary = "Get all checklists by user.", description = "Returns all checklists of an user by lhmExtId")
29+
@Operation(summary = "Get all checklists by user.", description = "Returns all checklists of an user (identified by JWT-Token)")
3030
@ResponseStatus(HttpStatus.OK)
31-
public List<ChecklistReadDTO> getChecklists(@RequestHeader("lhmExtID") final String lhmExtID) {
32-
final List<Checklist> checklists = checklistService.getChecklists(lhmExtID);
31+
public List<ChecklistReadDTO> getChecklists() {
32+
final List<Checklist> checklists = checklistService.getChecklists();
3333
return checklists.stream().map(checklistMapper::toReadDTO).toList();
3434
}
3535

personalization-service/src/main/java/de/muenchen/dbs/personalization/checklist/ChecklistRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
@Repository
1010
public interface ChecklistRepository extends JpaRepository<Checklist, UUID> {
1111

12-
List<Checklist> findChecklistByLhmExtId(String lhmExtId);
12+
List<Checklist> findChecklistByEmail(String email);
1313

1414
}

personalization-service/src/main/java/de/muenchen/dbs/personalization/checklist/ChecklistService.java

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@
1010
import java.util.UUID;
1111
import lombok.RequiredArgsConstructor;
1212
import lombok.extern.slf4j.Slf4j;
13+
import org.apache.commons.lang3.StringUtils;
14+
import org.apache.hc.client5.http.HttpResponseException;
15+
import org.springframework.http.HttpStatus;
1316
import org.springframework.security.access.prepost.PreAuthorize;
17+
import org.springframework.security.core.Authentication;
18+
import org.springframework.security.core.context.SecurityContextHolder;
19+
import org.springframework.security.oauth2.jwt.Jwt;
20+
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
1421
import org.springframework.stereotype.Service;
22+
import org.springframework.web.server.ResponseStatusException;
1523

1624
@Service
1725
@Slf4j
@@ -20,36 +28,49 @@ public class ChecklistService {
2028

2129
private final ChecklistRepository checklistRepository;
2230

23-
@PreAuthorize(Authorities.CHECKLIST_CREATE)
2431
public Checklist createChecklist(final Checklist checklist) {
25-
log.debug("Create Checklist {}", checklist);
32+
String userMail = getUserMailFromAuthenticationOrThrow();
33+
log.debug("Create Checklist {} for {}", checklist, userMail);
34+
checklist.setEmail(userMail);
2635
return checklistRepository.save(checklist);
2736
}
2837

29-
@PreAuthorize(Authorities.CHECKLIST_GET_ALL)
30-
public List<Checklist> getChecklists(final String userId) {
31-
log.debug("Get all checklists of {}", userId);
32-
return checklistRepository.findChecklistByLhmExtId(userId);
38+
public List<Checklist> getChecklists() {
39+
String userMail = getUserMailFromAuthenticationOrThrow();
40+
log.debug("Get all checklists of {}", userMail);
41+
return checklistRepository.findChecklistByEmail(userMail);
3342
}
3443

35-
@PreAuthorize(Authorities.CHECKLIST_GET)
3644
public Checklist getChecklist(final UUID checklistId) {
37-
log.debug("Get checklist with ID {}", checklistId);
38-
return getChecklistOrThrowException(checklistId);
45+
String userMail = getUserMailFromAuthenticationOrThrow();
46+
log.debug("Get checklist with ID {} for {}", checklistId, userMail);
47+
Checklist checklistOrThrowException = getChecklistOrThrowException(checklistId);
48+
49+
isChecklistOwnerOrThrow(checklistOrThrowException, userMail);
50+
51+
return checklistOrThrowException;
3952
}
4053

41-
@PreAuthorize(Authorities.CHECKLIST_UPDATE)
4254
public Checklist updateChecklist(final Checklist checklist, final UUID checklistId) {
55+
String userMail = getUserMailFromAuthenticationOrThrow();
56+
log.debug("Update checklist with ID {} for {}", checklistId, userMail);
4357
final Checklist foundChecklist = getChecklistOrThrowException(checklistId);
58+
59+
isChecklistOwnerOrThrow(foundChecklist, userMail);
60+
4461
foundChecklist.setChecklistItems(checklist.getChecklistItems());
4562
foundChecklist.setLastUpdate(ZonedDateTime.now());
4663
log.debug("Update Checklist {}", foundChecklist);
4764
return checklistRepository.save(foundChecklist);
4865
}
4966

50-
@PreAuthorize(Authorities.CHECKLIST_DELETE)
5167
public void deleteChecklist(final UUID checklistId) {
52-
log.debug("Delete Checklist with ID {}", checklistId);
68+
String userMail = getUserMailFromAuthenticationOrThrow();
69+
log.debug("Delete Checklist with ID {} for {}", checklistId, userMail);
70+
71+
final Checklist foundChecklist = getChecklistOrThrowException(checklistId);
72+
isChecklistOwnerOrThrow(foundChecklist, userMail);
73+
5374
checklistRepository.deleteById(checklistId);
5475
}
5576

@@ -58,4 +79,21 @@ private Checklist getChecklistOrThrowException(final UUID checklistId) {
5879
.findById(checklistId)
5980
.orElseThrow(() -> new NotFoundException(String.format(MSG_NOT_FOUND, checklistId)));
6081
}
82+
83+
private String getUserMailFromAuthenticationOrThrow() {
84+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
85+
if(authentication.getPrincipal() instanceof Jwt jwt) {
86+
String email = jwt.getClaims().get("email").toString();
87+
if(!StringUtils.isBlank(email)) {
88+
return email;
89+
}
90+
}
91+
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
92+
}
93+
94+
private void isChecklistOwnerOrThrow(Checklist checklist, String userMail) {
95+
if(!checklist.getEmail().equals(userMail)) {
96+
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User does not own the checklist");
97+
}
98+
}
6199
}

personalization-service/src/main/java/de/muenchen/dbs/personalization/checklist/domain/Checklist.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public class Checklist extends BaseEntity {
2525
@Serial
2626
private static final long serialVersionUID = 1L;
2727

28-
@Column(name = "lhm_ext_id", nullable = false)
28+
@Column(name = "email", nullable = false)
2929
@NotNull
30-
private String lhmExtId;
30+
private String email;
3131

3232
@Column(name = "title")
3333
@NotNull

personalization-service/src/main/java/de/muenchen/dbs/personalization/checklist/domain/ChecklistCreateDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import jakarta.validation.constraints.NotNull;
44
import java.util.List;
55

6-
public record ChecklistCreateDTO(@NotNull String lhmExtId, @NotNull String title, @NotNull List<ChecklistItemDTO> checklistItems) {
6+
public record ChecklistCreateDTO(@NotNull String title, @NotNull List<ChecklistItemDTO> checklistItems) {
77

88
}

personalization-service/src/main/java/de/muenchen/dbs/personalization/checklist/domain/ChecklistMapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public interface ChecklistMapper {
1010
ChecklistReadDTO toReadDTO(Checklist checklist);
1111

1212
@Mapping(target = "id", ignore = true)
13+
@Mapping(target = "email", ignore = true)
1314
Checklist toCreateChecklist(ChecklistCreateDTO checklistUpdateDTO);
1415

1516
@Mapping(target = "id", ignore = true)

personalization-service/src/main/java/de/muenchen/dbs/personalization/checklist/domain/ChecklistReadDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.List;
66
import java.util.UUID;
77

8-
public record ChecklistReadDTO(@NotNull UUID id, @NotNull String lhmExtId, @NotNull String title, @NotNull ZonedDateTime lastUpdate,
8+
public record ChecklistReadDTO(@NotNull UUID id, @NotNull String email, @NotNull String title, @NotNull ZonedDateTime lastUpdate,
99
@NotNull List<ChecklistItemDTO> checklistItems) {
1010

1111
}

personalization-service/src/main/java/de/muenchen/dbs/personalization/checklist/domain/ChecklistUpdateDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
import java.util.List;
55
import java.util.UUID;
66

7-
public record ChecklistUpdateDTO(@NotNull UUID id, @NotNull String lhmExtId, @NotNull String title, @NotNull List<ChecklistItemDTO> checklistItems) {
7+
public record ChecklistUpdateDTO(@NotNull UUID id, @NotNull String email, @NotNull String title, @NotNull List<ChecklistItemDTO> checklistItems) {
88

99
}

personalization-service/src/main/resources/db/migration/schema/V001__Checklist_schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
create table checklist (
2-
lhm_ext_id varchar(255) not null,
2+
email varchar(255) not null,
33
title varchar(255) not null,
44
last_update timestamp with time zone,
55
id uuid not null,
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
truncate checklist CASCADE;
22
truncate checklist_item CASCADE;
33

4-
INSERT INTO checklist (lhm_ext_id, title, last_update, id) VALUES
5-
('user1', 'title1', NOW(), '123e4567-e89b-12d3-a456-426614174000'),
6-
('user2', 'title2', NOW(), '123e4567-e89b-12d3-a456-426614174001'),
7-
('user3', 'title3', NOW(), '123e4567-e89b-12d3-a456-426614174002');
4+
INSERT INTO checklist (email, title, last_update, id) VALUES
5+
('user1@example.com', 'title1', NOW(), '123e4567-e89b-12d3-a456-426614174000'),
6+
('user2@example.com', 'title2', NOW(), '123e4567-e89b-12d3-a456-426614174001'),
7+
('user3@example.com', 'title3', NOW(), '123e4567-e89b-12d3-a456-426614174002');
88

99
INSERT INTO checklist_item (service_id, checked, title, note, required, checklist_id) VALUES
10-
('service1', NOW(), 'Item 1', 'Note for Item 1', TRUE, (select id from checklist where lhm_ext_id='user1')),
11-
('service2', NOW(), 'Item 2', 'Note for Item 2', FALSE, (select id from checklist where lhm_ext_id='user1')),
12-
('service3', NOW(), 'Item 3', 'Note for Item 3', FALSE, (select id from checklist where lhm_ext_id='user2')),
13-
('service4', NOW(), 'Item 4', 'Note for Item 4', TRUE, (select id from checklist where lhm_ext_id='user3')),
14-
('service5', NOW(), 'Item 5', 'Note for Item 5', TRUE, (select id from checklist where lhm_ext_id='user3')),
15-
('service6', NOW(), 'Item 6', 'Note for Item 6', FALSE, (select id from checklist where lhm_ext_id='user3'));
10+
('service1', NOW(), 'Item 1', 'Note for Item 1', TRUE, (select id from checklist where email='user1@example.com')),
11+
('service2', NOW(), 'Item 2', 'Note for Item 2', FALSE, (select id from checklist where email='user1@example.com')),
12+
('service3', NOW(), 'Item 3', 'Note for Item 3', FALSE, (select id from checklist where email='user2@example.com')),
13+
('service4', NOW(), 'Item 4', 'Note for Item 4', TRUE, (select id from checklist where email='user3@example.com')),
14+
('service5', NOW(), 'Item 5', 'Note for Item 5', TRUE, (select id from checklist where email='user3@example.com')),
15+
('service6', NOW(), 'Item 6', 'Note for Item 6', FALSE, (select id from checklist where email='user3@example.com'));

0 commit comments

Comments
 (0)