Skip to content

Commit

Permalink
Add GET /clients/v1/users/{id} and fix GET /clients/v1/superGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
Portals committed Jun 20, 2024
1 parent 14b9801 commit 48101c7
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,23 @@ List<ClientV1User> getUsersForClient() {
return this.userFacade.getAllByClientAccepting().stream().map(ClientV1User::new).toList();
}

@GetMapping("/users/{id}")
ClientV1User getUser(@PathVariable("id") UUID id) {
return this.userFacade
.get(id)
.map(ClientV1User::new)
.orElseThrow(
() ->
new ResponseStatusException(
HttpStatus.NOT_FOUND, "User Not Found Or Unauthorized"));
}

@GetMapping("/groups/for/{id}")
List<ClientV1UserGroup> getUsersForGroup(@PathVariable("id") UUID id) {
List<ClientV1UserGroup> getGroupsForUser(@PathVariable("id") UUID id) {
Optional<UserFacade.UserWithGroupsDTO> maybeUser;

try {
maybeUser = this.userFacade.get(id);
maybeUser = this.userFacade.getWithGroups(id);
} catch (AccessGuard.AccessDeniedException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "User Not Found Or Unauthorized");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public InfoV1ApiController(SuperGroupFacade superGroupFacade, UserFacade userFac

@GetMapping("/users/{id}")
public UserFacade.UserWithGroupsDTO getUser(@PathVariable("id") UUID id) {
return this.userFacade.get(id).orElseThrow(UserNotFoundResponse::new);
return this.userFacade.getWithGroups(id).orElseThrow(UserNotFoundResponse::new);
}

@GetMapping("/blob")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public ModelAndView createAuthority(
throw new RuntimeException(e);
}

users.add(this.userFacade.get(user).orElseThrow().user());
users.add(this.userFacade.getWithGroups(user).orElseThrow().user());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@
@Controller
public class GammaErrorController implements ErrorController {

@GetMapping("/error")
public ModelAndView handleRuntimeException(@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest, HttpServletResponse response) {
response.addHeader("HX-Retarget", "body");
response.addHeader("HX-Reswap", "innerHTML");
@GetMapping("/error")
public ModelAndView handleRuntimeException(
@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest,
HttpServletResponse response) {
response.addHeader("HX-Retarget", "body");
response.addHeader("HX-Reswap", "innerHTML");

ModelAndView mv = new ModelAndView();
if (htmxRequest) {
mv.setViewName("pages/error");
} else {
mv.setViewName("index");
mv.addObject("page", "pages/error");
}

return mv;
ModelAndView mv = new ModelAndView();
if (htmxRequest) {
mv.setViewName("pages/error");
} else {
mv.setViewName("index");
mv.addObject("page", "pages/error");
}

return mv;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ public ModelAndView handleMaxSizeException(HttpServletResponse response) {
public void handleAccessDeniedException(HttpServletResponse response) throws IOException {
response.sendRedirect("/");
}

@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView handleRuntimeException(RuntimeException ex, HttpServletResponse response) {
response.addHeader("HX-Retarget", "body");
response.addHeader("HX-Reswap", "innerHTML");

return new ModelAndView("pages/error");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public ModelAndView getUser(
mv.addObject("email", u.user().email());
mv.addObject("locked", u.user().locked());
} else {
Optional<UserFacade.UserWithGroupsDTO> user = this.userFacade.get(UUID.fromString(userId));
Optional<UserFacade.UserWithGroupsDTO> user =
this.userFacade.getWithGroups(UUID.fromString(userId));

if (user.isEmpty()) {
return createUserNotFound(userId, htmxRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void deleteSuperGroup(UUID superGroupId)
}

public List<SuperGroupDTO> getAll() {
accessGuard.requireEither(isAdmin(), isSignedIn());
accessGuard.requireEither(isAdmin(), isClientApi(), isSignedIn());

return this.superGroupRepository.getAll().stream().map(SuperGroupDTO::new).toList();
}
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/it/chalmers/gamma/app/user/UserFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ public UserFacade(
this.clientApprovalsRepository = clientApprovalsRepository;
}

public Optional<UserWithGroupsDTO> get(UUID id) {
public Optional<UserDTO> get(UUID id) {
UserId userId = new UserId(id);
accessGuard.requireEither(isSignedIn(), userHasAcceptedClient(userId), isApi(ApiKeyType.INFO));

return this.userRepository.get(userId).map(UserDTO::new);
}

public Optional<UserWithGroupsDTO> getWithGroups(UUID id) {
UserId userId = new UserId(id);
accessGuard.requireEither(isSignedIn(), userHasAcceptedClient(userId), isApi(ApiKeyType.INFO));

Expand Down

0 comments on commit 48101c7

Please sign in to comment.