Skip to content

Commit

Permalink
[kbss-cvut/23ava-distribution#18] Add getByUsername endpoint back to …
Browse files Browse the repository at this point in the history
…OIDC-based users REST controller.

It is needed for impersonate functionality, as it needs to open the user detail.
  • Loading branch information
ledsoft committed Nov 23, 2023
1 parent b1695f2 commit e370a5e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/cz/cvut/kbss/study/rest/OidcUserController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.cvut.kbss.study.rest;

import cz.cvut.kbss.study.exception.NotFoundException;
import cz.cvut.kbss.study.model.Institution;
import cz.cvut.kbss.study.model.User;
import cz.cvut.kbss.study.security.SecurityConstants;
Expand All @@ -9,6 +10,7 @@
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -40,6 +42,17 @@ public User getCurrent() {
return userService.getCurrentUser();
}

@PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or #username == authentication.name or " +
"hasRole('" + SecurityConstants.ROLE_USER + "') and @securityUtils.areFromSameInstitution(#username)")
@GetMapping(value = "/{username}", produces = MediaType.APPLICATION_JSON_VALUE)
public User getByUsername(@PathVariable("username") String username) {
final User user = userService.findByUsername(username);
if (user == null) {
throw NotFoundException.create("User", username);
}
return user;
}

@PreAuthorize(
"hasRole('" + SecurityConstants.ROLE_ADMIN + "') " +
"or hasRole('" + SecurityConstants.ROLE_USER + "') and @securityUtils.isMemberOfInstitution(#institutionKey)")
Expand Down

0 comments on commit e370a5e

Please sign in to comment.