Package de.trustable.ca3s.core.web.rest
Class UserResource
- java.lang.Object
-
- de.trustable.ca3s.core.web.rest.UserResource
-
@RestController @RequestMapping("/api") public class UserResource extends ObjectREST controller for managing users.This class accesses the
Userentity, and needs to fetch its collection of authorities.For a normal use-case, it would be better to have an eager relationship between User and Authority, and send everything to the client side: there would be no View Model and DTO, a lot less code, and an outer-join which would be good for performance.
We use a View Model and a DTO for 3 reasons:
- We want to keep a lazy association between the user and the authorities, because people will quite often do relationships with the user, and we don't want them to get the authorities all the time for nothing (for performance reasons). This is the #1 goal: we should not impact our users' application because of this use-case.
- Not having an outer join causes n+1 requests to the database. This is not a real issue as we have by default a second-level cache. This means on the first HTTP call we do the n+1 requests, but then all authorities come from the cache, so in fact it's much better than doing an outer join (which will get lots of data from the database, for each HTTP call).
- As this manages users, for security reasons, we'd rather have a DTO layer.
Another option would be to have a specific JPA entity graph to handle this case.
-
-
Constructor Summary
Constructors Constructor Description UserResource(UserService userService, UserRepository userRepository, TenantRepository tenantRepository, boolean enforceEmailUniqueness, MailService mailService)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description org.springframework.http.ResponseEntity<User>createUser(@Valid UserDTO userDTO)POST /users: Creates a new user.org.springframework.http.ResponseEntity<Void>deleteUser(String login)DELETE /users/:login: delete the "login" User.org.springframework.http.ResponseEntity<List<UserDTO>>getAllUsers(org.springframework.data.domain.Pageable pageable)GET /users: get all users.List<String>getAuthorities()Gets a list of all roles.org.springframework.http.ResponseEntity<UserDTO>getUser(String login)GET /users/:login: get the "login" user.org.springframework.http.ResponseEntity<List<UserDTO>>getUsersByRole(String role)GET /users/role/:role: get the user with a given role.org.springframework.http.ResponseEntity<UserDTO>updateUser(@Valid UserDTO userDTO)PUT /users: Updates an existing User.
-
-
-
Constructor Detail
-
UserResource
public UserResource(UserService userService, UserRepository userRepository, TenantRepository tenantRepository, @Value("${ca3s.ui.user.email.enforceUnique:false}") boolean enforceEmailUniqueness, MailService mailService)
-
-
Method Detail
-
createUser
@PostMapping("/users") @PreAuthorize("hasRole(\"ROLE_ADMIN\")") @Transactional public org.springframework.http.ResponseEntity<User> createUser(@Valid @RequestBody @Valid UserDTO userDTO) throws URISyntaxException, javax.mail.MessagingExceptionPOST /users: Creates a new user.Creates a new user if the login and email are not already used, and sends an mail with an activation link. The user needs to be activated on creation.
- Parameters:
userDTO- the user to create.- Returns:
- the
ResponseEntitywith status201 (Created)and with body the new user, or with status400 (Bad Request)if the login or email is already in use. - Throws:
URISyntaxException- if the Location URI syntax is incorrect.BadRequestAlertException-400 (Bad Request)if the login or email is already in use.javax.mail.MessagingException
-
updateUser
@PutMapping("/users") @PreAuthorize("hasRole(\"ROLE_ADMIN\")") @Transactional public org.springframework.http.ResponseEntity<UserDTO> updateUser(@Valid @RequestBody @Valid UserDTO userDTO)PUT /users: Updates an existing User.- Parameters:
userDTO- the user to update.- Returns:
- the
ResponseEntitywith status200 (OK)and with body the updated user. - Throws:
EmailAlreadyUsedException-400 (Bad Request)if the email is already in use.LoginAlreadyUsedException-400 (Bad Request)if the login is already in use.
-
getAllUsers
@GetMapping("/users") @PreAuthorize("hasRole(\"ROLE_ADMIN\")") public org.springframework.http.ResponseEntity<List<UserDTO>> getAllUsers(org.springframework.data.domain.Pageable pageable)GET /users: get all users.- Parameters:
pageable- the pagination information.- Returns:
- the
ResponseEntitywith status200 (OK)and with body all users.
-
getAuthorities
@GetMapping("/users/authorities") @PreAuthorize("hasRole(\"ROLE_ADMIN\")") public List<String> getAuthorities()Gets a list of all roles.- Returns:
- a string list of all roles.
-
getUser
@GetMapping("/users/{login:^[_.@A-Za-z0-9-]*$}") @Transactional(readOnly=true) public org.springframework.http.ResponseEntity<UserDTO> getUser(@PathVariable String login)GET /users/:login: get the "login" user.- Parameters:
login- the login of the user to find.- Returns:
- the
ResponseEntitywith status200 (OK)and with body the "login" user, or with status404 (Not Found).
-
getUsersByRole
@GetMapping("/users/role/{role}") @PreAuthorize("hasRole(\"ROLE_ADMIN\")") @Transactional(readOnly=true) public org.springframework.http.ResponseEntity<List<UserDTO>> getUsersByRole(@PathVariable String role)GET /users/role/:role: get the user with a given role.- Parameters:
role- the role to be retrieved.- Returns:
- the
ResponseEntitywith status200 (OK)and with body the users of given role, or with status404 (Not Found).
-
deleteUser
@DeleteMapping("/users/{login:^[_.@A-Za-z0-9-]*$}") @PreAuthorize("hasRole(\"ROLE_ADMIN\")") @Transactional public org.springframework.http.ResponseEntity<Void> deleteUser(@PathVariable String login)DELETE /users/:login: delete the "login" User.- Parameters:
login- the login of the user to delete.- Returns:
- the
ResponseEntitywith status204 (NO_CONTENT).
-
-