Skip to content

Commit 213945a

Browse files
fixed role enums (#35)
1 parent 81b32ad commit 213945a

File tree

9 files changed

+35
-48
lines changed

9 files changed

+35
-48
lines changed

src/main/java/org/example/projektarendehantering/application/service/AuditService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,15 @@ private void requireActor(Actor actor) {
262262
}
263263

264264
private boolean isManager(Actor actor) {
265-
return actor.role() == Role.MANAGER || actor.role() == Role.ADMIN;
265+
return actor.role() == Role.MANAGER;
266266
}
267267

268268
private boolean isDoctor(Actor actor) {
269-
return actor.role() == Role.DOCTOR || actor.role() == Role.CASE_OWNER;
269+
return actor.role() == Role.DOCTOR;
270270
}
271271

272272
private boolean isNurse(Actor actor) {
273-
return actor.role() == Role.NURSE || actor.role() == Role.HANDLER;
273+
return actor.role() == Role.NURSE;
274274
}
275275
}
276276

src/main/java/org/example/projektarendehantering/application/service/CaseService.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import org.springframework.web.server.ResponseStatusException;
2020

2121
import java.time.Instant;
22+
import java.util.LinkedHashMap;
2223
import java.util.List;
24+
import java.util.Map;
2325
import java.util.Optional;
2426
import java.util.Set;
2527
import java.util.UUID;
@@ -108,12 +110,10 @@ public List<CaseDTO> getAllCases(Actor actor) {
108110
.collect(Collectors.toList());
109111
}
110112
if (isPatient(actor)) {
111-
return caseRepository.findAllByPatient_Id(actor.userId()).stream()
112-
.map(caseMapper::toDTO)
113-
.collect(Collectors.toList());
114-
}
115-
if (isOther(actor)) {
116-
return caseRepository.findAllByOtherId(actor.userId()).stream()
113+
Map<UUID, CaseEntity> byId = new LinkedHashMap<>();
114+
caseRepository.findAllByPatient_Id(actor.userId()).forEach(c -> byId.putIfAbsent(c.getId(), c));
115+
caseRepository.findAllByOtherId(actor.userId()).forEach(c -> byId.putIfAbsent(c.getId(), c));
116+
return byId.values().stream()
117117
.map(caseMapper::toDTO)
118118
.collect(Collectors.toList());
119119
}
@@ -145,15 +145,15 @@ public CaseDTO assignUsers(Actor actor, UUID caseId, CaseAssignmentDTO dto) {
145145
}
146146

147147
if (isManager(actor) && dto.getOwnerId() != null) {
148-
UUID ownerId = requireEmployeeWithRole(dto.getOwnerId(), Set.of(Role.DOCTOR, Role.CASE_OWNER), "ownerId");
148+
UUID ownerId = requireEmployeeWithRole(dto.getOwnerId(), Set.of(Role.DOCTOR), "ownerId");
149149
entity.setOwnerId(ownerId);
150150
}
151151
if (dto.getHandlerId() != null) {
152-
UUID handlerId = requireEmployeeWithRole(dto.getHandlerId(), Set.of(Role.NURSE, Role.HANDLER), "handlerId");
152+
UUID handlerId = requireEmployeeWithRole(dto.getHandlerId(), Set.of(Role.NURSE), "handlerId");
153153
entity.setHandlerId(handlerId);
154154
}
155155
if (dto.getOtherId() != null) {
156-
UUID otherId = requireEmployeeWithRole(dto.getOtherId(), Set.of(Role.OTHER), "otherId");
156+
UUID otherId = requireEmployeeWithRole(dto.getOtherId(), Set.of(Role.PATIENT), "otherId");
157157
entity.setOtherId(otherId);
158158
}
159159
return caseMapper.toDTO(caseRepository.save(entity));
@@ -181,7 +181,7 @@ private void requireCanRead(Actor actor, CaseEntity entity) {
181181
if (isPatient(actor)
182182
&& entity.getPatient() != null
183183
&& actor.userId().equals(entity.getPatient().getId())) return;
184-
if (isOther(actor) && actor.userId().equals(entity.getOtherId())) return;
184+
if (isPatient(actor) && actor.userId().equals(entity.getOtherId())) return;
185185
throw new NotAuthorizedException("Not allowed to read this case");
186186
}
187187

@@ -190,22 +190,18 @@ private boolean canCreate(Actor actor) {
190190
}
191191

192192
private boolean isManager(Actor actor) {
193-
return actor.role() == Role.MANAGER || actor.role() == Role.ADMIN;
193+
return actor.role() == Role.MANAGER;
194194
}
195195

196196
private boolean isDoctor(Actor actor) {
197-
return actor.role() == Role.DOCTOR || actor.role() == Role.CASE_OWNER;
197+
return actor.role() == Role.DOCTOR;
198198
}
199199

200200
private boolean isNurse(Actor actor) {
201-
return actor.role() == Role.NURSE || actor.role() == Role.HANDLER;
201+
return actor.role() == Role.NURSE;
202202
}
203203

204204
private boolean isPatient(Actor actor) {
205205
return actor.role() == Role.PATIENT;
206206
}
207-
208-
private boolean isOther(Actor actor) {
209-
return actor.role() == Role.OTHER;
210-
}
211207
}

src/main/java/org/example/projektarendehantering/application/service/EmployeeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private void requireCanManageEmployees(Actor actor) {
5454
if (actor == null) {
5555
throw new NotAuthorizedException("Missing actor");
5656
}
57-
if (actor.role() == Role.MANAGER || actor.role() == Role.ADMIN) {
57+
if (actor.role() == Role.MANAGER) {
5858
return;
5959
}
6060
throw new NotAuthorizedException("Not allowed to access employees");

src/main/java/org/example/projektarendehantering/common/Role.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,8 @@
66
* Note: enum constant names are intended to be stable because infrastructure may parse them from headers.
77
*/
88
public enum Role {
9-
/**
10-
* New naming (preferred).
11-
*/
129
MANAGER,
1310
DOCTOR,
1411
NURSE,
15-
PATIENT,
16-
17-
/**
18-
* Legacy naming (kept for backward compatibility with header parsing).
19-
*/
20-
CASE_OWNER,
21-
HANDLER,
22-
ADMIN,
23-
OTHER
12+
PATIENT
2413
}

src/main/java/org/example/projektarendehantering/infrastructure/config/SecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public UserDetailsService userDetailsService() {
3838
UserDetails admin = User.builder()
3939
.username("admin")
4040
.password("{noop}password") // {noop} means no password encoding (fine for dev)
41-
.roles("ADMIN")
41+
.roles("MANAGER")
4242
.build();
4343
return new InMemoryUserDetailsManager(admin);
4444
}

src/main/java/org/example/projektarendehantering/infrastructure/security/HeaderCurrentUserAdapter.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ public Actor currentUser() {
2626
// Create a deterministic UUID based on the username/name
2727
UUID userId = UUID.nameUUIDFromBytes(authentication.getName().getBytes(StandardCharsets.UTF_8));
2828

29-
Role role = Role.OTHER;
30-
if (authentication.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))) {
31-
role = Role.ADMIN;
32-
} else if (authentication.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_HANDLER"))) {
33-
role = Role.HANDLER;
34-
} else if (authentication.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_CASE_OWNER"))) {
35-
role = Role.CASE_OWNER;
29+
Role role = Role.PATIENT;
30+
if (authentication.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_MANAGER"))) {
31+
role = Role.MANAGER;
32+
} else if (authentication.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_DOCTOR"))) {
33+
role = Role.DOCTOR;
34+
} else if (authentication.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_NURSE"))) {
35+
role = Role.NURSE;
36+
} else if (authentication.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_PATIENT"))) {
37+
role = Role.PATIENT;
3638
}
3739

3840
return new Actor(userId, role);

src/main/resources/data.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ ON CONFLICT (id) DO NOTHING;
1313

1414
-- Seed Employees
1515
INSERT INTO employees (id, display_name, role, created_at)
16-
VALUES ('770e8400-e29b-41d4-a716-446655440000', 'Admin User', 'ADMIN', CURRENT_TIMESTAMP)
16+
VALUES ('770e8400-e29b-41d4-a716-446655440000', 'Admin User', 'MANAGER', CURRENT_TIMESTAMP)
1717
ON CONFLICT (id) DO NOTHING;
1818

1919
INSERT INTO employees (id, display_name, role, created_at)
20-
VALUES ('770e8400-e29b-41d4-a716-446655440001', 'Dr. Alice Roberts', 'CASE_OWNER', CURRENT_TIMESTAMP)
20+
VALUES ('770e8400-e29b-41d4-a716-446655440001', 'Dr. Alice Roberts', 'DOCTOR', CURRENT_TIMESTAMP)
2121
ON CONFLICT (id) DO NOTHING;
2222

2323
INSERT INTO employees (id, display_name, role, created_at)
24-
VALUES ('770e8400-e29b-41d4-a716-446655440002', 'Nurse Bob Jones', 'HANDLER', CURRENT_TIMESTAMP)
24+
VALUES ('770e8400-e29b-41d4-a716-446655440002', 'Nurse Bob Jones', 'NURSE', CURRENT_TIMESTAMP)
2525
ON CONFLICT (id) DO NOTHING;
2626

2727
-- Seed Cases

src/test/java/org/example/projektarendehantering/ProjektArendehanteringApplicationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void contextLoads() {
2828
}
2929

3030
@Test
31-
@WithMockUser(username = "handler1", roles = {"HANDLER"})
31+
@WithMockUser(username = "handler1", roles = {"NURSE"})
3232
void uiRequest_createsAuditEvent() throws Exception {
3333
MockMvc mockMvc = webAppContextSetup(webApplicationContext)
3434
.apply(springSecurity())

src/test/resources/data-test.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ VALUES ('550e8400-e29b-41d4-a716-446655440002', 'Anders', 'Andersson', '19780315
1010

1111
-- Seed Employees
1212
INSERT INTO employees (id, display_name, role, created_at)
13-
VALUES ('770e8400-e29b-41d4-a716-446655440000', 'Admin User', 'ADMIN', CURRENT_TIMESTAMP);
13+
VALUES ('770e8400-e29b-41d4-a716-446655440000', 'Admin User', 'MANAGER', CURRENT_TIMESTAMP);
1414

1515
INSERT INTO employees (id, display_name, role, created_at)
16-
VALUES ('770e8400-e29b-41d4-a716-446655440001', 'Dr. Alice Roberts', 'CASE_OWNER', CURRENT_TIMESTAMP);
16+
VALUES ('770e8400-e29b-41d4-a716-446655440001', 'Dr. Alice Roberts', 'DOCTOR', CURRENT_TIMESTAMP);
1717

1818
INSERT INTO employees (id, display_name, role, created_at)
19-
VALUES ('770e8400-e29b-41d4-a716-446655440002', 'Nurse Bob Jones', 'HANDLER', CURRENT_TIMESTAMP);
19+
VALUES ('770e8400-e29b-41d4-a716-446655440002', 'Nurse Bob Jones', 'NURSE', CURRENT_TIMESTAMP);
2020

2121
-- Seed Cases
2222
INSERT INTO cases (id, title, description, status, patient_id, owner_id, handler_id, created_at)

0 commit comments

Comments
 (0)