Skip to content

Commit

Permalink
#705 set isWritable for team correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
janikEndtner committed Jan 3, 2024
1 parent a64d716 commit 013df44
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion backend/src/main/java/ch/puzzle/okr/dto/TeamDto.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.puzzle.okr.dto;

public record TeamDto(Long id, int version, String name) {
public record TeamDto(Long id, int version, String name, boolean isWriteable) {
}
2 changes: 1 addition & 1 deletion backend/src/main/java/ch/puzzle/okr/mapper/TeamMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public TeamMapper() {
}

public TeamDto toDto(Team team) {
return new TeamDto(team.getId(), team.getVersion(), team.getName());
return new TeamDto(team.getId(), team.getVersion(), team.getName(), team.isWriteable());
}

public Team toTeam(TeamDto teamDto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,33 @@ public void deleteEntity(Long id) {
teamBusinessService.deleteTeam(id);
}

public void addUsersToTeam(long entityId, List<Long> userIdList) {
checkUserAuthorization(OkrResponseStatusException.of(ErrorKey.NOT_AUTHORIZED_TO_WRITE, TEAM), entityId);
teamBusinessService.addUsersToTeam(entityId, userIdList);
}

private void checkUserAuthorization(OkrResponseStatusException exception, Long teamId) {
if (isUserWriteAllowed(teamId)) {
return;
}
throw exception;
}

private boolean isUserWriteAllowed(Long teamId) {
AuthorizationUser authorizationUser = authorizationService.getAuthorizationUser();
if (hasRoleWriteAndReadAll(authorizationUser)) {
return;
return true;
}
if (authorizationUser.isUserAdminInTeam(teamId)) {
return;
return true;
}
throw exception;
return false;
}

public List<Team> getAllTeams() {
AuthorizationUser authorizationUser = authorizationService.getAuthorizationUser();
boolean isWritable = hasRoleWriteAndReadAll(authorizationUser);
List<Team> allTeams = teamBusinessService.getAllTeams(authorizationUser);
allTeams.forEach(team -> team.setWriteable(isWritable));
allTeams.forEach(team -> team.setWriteable(isUserWriteAllowed(team.getId())));
return allTeams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class TeamControllerIT {
static Team teamPuzzle = Team.Builder.builder().withId(5L).withName(PUZZLE).build();
static Team teamOKR = Team.Builder.builder().withId(7L).withName("OKR").build();
static List<Team> teamList = Arrays.asList(teamPuzzle, teamOKR);
static TeamDto teamPuzzleDto = new TeamDto(5L, 3, PUZZLE);
static TeamDto teamOkrDto = new TeamDto(7L, 4, "OKR");
static TeamDto teamPuzzleDto = new TeamDto(5L, 3, PUZZLE, false);
static TeamDto teamOkrDto = new TeamDto(7L, 4, "OKR", false);

private static final String CREATE_NEW_TEAM = """
{
Expand Down Expand Up @@ -126,7 +126,7 @@ void shouldReturnResponseStatusExceptionWhenCreatingObjectiveWithNullValues() th

@Test
void shouldReturnUpdatedTeam() throws Exception {
TeamDto teamDto = new TeamDto(1L, 0, "OKR-Team");
TeamDto teamDto = new TeamDto(1L, 0, "OKR-Team", false);
Team team = Team.Builder.builder().withId(1L).withName("OKR-Team").build();

BDDMockito.given(teamMapper.toDto(any())).willReturn(teamDto);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/app/shared/types/model/Team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface Team {
version: number;
name: string;
filterIsActive: boolean;
isWriteable: boolean;
}

0 comments on commit 013df44

Please sign in to comment.