Commit 54f73051 authored by Jan Marchel's avatar Jan Marchel
Browse files

adding possibility to change user profile and get username from backend

parent c0f58d5b
Pipeline #21294 failed with stages
in 13 minutes and 9 seconds
......@@ -22,4 +22,6 @@ public interface JwtServerApi {
void unlockUserAccount(String username, String token);
void deleteUser(String username, String token);
UserResponse updateExistingUser(NewUserRequest newUserRequest, String token);
}
......@@ -80,6 +80,16 @@ public class JwtServerClientApi extends RestCommunicationService implements JwtS
getResponse(requestUrl, responseType, requestHttpEntity, ServiceName.JWT_SERVER.name, HttpMethod.DELETE);
}
@Override
public UserResponse updateExistingUser(NewUserRequest newUserRequest, String token) {
String requestUrl = guiBackendProperties.getJwtServer().getUrl() + "/auth/user";
ParameterizedTypeReference<UserResponse> responseType = new ParameterizedTypeReference<UserResponse>() {
};
HttpEntity<NewUserRequest> requestHttpEntity = createHttpEntityWithAuthorizationHeader(newUserRequest, token);
ResponseEntity<UserResponse> response = getResponse(requestUrl, responseType, requestHttpEntity, ServiceName.JWT_SERVER.name, HttpMethod.POST);
return response.getBody();
}
@Override
public void changePassword(ChangePasswordRequest changePasswordRequest, String token) {
String requestUrl = guiBackendProperties.getJwtServer().getUrl() + "/auth/user/password";
......
......@@ -29,28 +29,6 @@ public class UserController {
private JwtServerApi jwtServerApi;
private UserService userService;
@PostMapping("/user/userProfile")
@ResponseStatus(HttpStatus.CREATED)
public UserProfile getUserProfile() {
UserProfile userProfile = new UserProfile();
return userProfile;
}
@PostMapping("/user/resources")
@ResponseStatus(HttpStatus.CREATED)
public Resources getResources() {
Resources resources = new Resources();
return resources;
}
@PostMapping("/user/activities")
@ResponseStatus(HttpStatus.CREATED)
public Activities getActivities() {
Activities activities = new Activities();
return activities;
}
@PostMapping("/user/login")
@ResponseStatus(HttpStatus.CREATED)
public LoginResponse loginUser(@RequestBody UserRequest loginRequest) {
......@@ -82,6 +60,16 @@ public class UserController {
.build();
}
@PutMapping("/auth/user")
@ResponseStatus(HttpStatus.CREATED)
public UserResponse updateExistingUser(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String token,
@RequestBody NewUserRequest newUserRequest) {
log.info("POST request for create new user account: username: {} and role: {}", newUserRequest.getUsername(), newUserRequest.getUserRole());
UserResponse newUser = jwtServerApi.updateExistingUser(newUserRequest, token);
log.info("Account for user {} with role {} successfully created", newUserRequest.getUsername(), newUserRequest.getUserRole());
return newUser;
}
@DeleteMapping("/auth/user/{username}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteUserAccount(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String token,
......
......@@ -10,13 +10,15 @@ import lombok.*;
public class NewUserRequest extends NewUserAutogeneratedPasswordRequest {
private String password;
private String passwordConfirmation;
private String fullName;
private String mail;
@Builder
public NewUserRequest(String username, UserRole userRole, String password, String passwordConfirmation) {
public NewUserRequest(String username, UserRole userRole, String password, String passwordConfirmation, String fullName, String mail) {
super(username, userRole);
this.password = password;
this.passwordConfirmation = passwordConfirmation;
this.fullName = fullName;
this.mail = mail;
}
}
......@@ -14,4 +14,6 @@ import lombok.Setter;
public class UserResponse {
private String username;
private UserRole userRole;
private String mail;
private String fullName;
}
......@@ -31,8 +31,10 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.security.Principal;
import java.util.List;
@Slf4j
......@@ -68,6 +70,12 @@ public class UserController {
log.info("New user account for user {} with role {} successfully created", userRequest.getUsername(), userRequest.getUserRole());
return userResponse;
}
@GetMapping("/auth/username")
@ResponseBody
public String currentUserName(HttpServletRequest request) {
Principal principal = request.getUserPrincipal();
return principal.getName();
}
@DeleteMapping("/auth/user/{username}")
@ResponseStatus(HttpStatus.NO_CONTENT)
......@@ -88,6 +96,17 @@ public class UserController {
log.info("Password for user: {} successfully changed", changePasswordRequest.getUsername());
}
@PutMapping("/auth/user")
@ResponseStatus(HttpStatus.NO_CONTENT)
@PreAuthorize("#newUserRequest.username.equals(authentication.name)")
public void updateUser(@RequestBody @Valid NewUserRequest newUserRequest)
throws AuthenticationException {
log.info("PUT request for change user info from user: {}", newUserRequest.getUsername());
userService.updateUser(newUserRequest);
log.info("user info for user: {} successfully changed", newUserRequest.getUsername());
}
@PutMapping("/auth/user/unlock/{username}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@PreAuthorize("@PermissionComponent.isUserInAdminGroup(authentication.name)")
......
......@@ -123,6 +123,15 @@ public class UserService {
userLdapRepository.save(user);
}
public void updateUser(NewUserRequest newUserRequest) throws AuthenticationException {
User user = userLdapRepository.findByUsernameAndPassword(newUserRequest.getUsername(), digestSHA(newUserRequest.getPassword()))
.orElseThrow(UserNotFoundException::new);
user.setUsername(newUserRequest.getUsername());
user.setFullName(newUserRequest.getFullName());
user.setMail(newUserRequest.getMail());
userLdapRepository.save(user);
}
public void unlockAccount(String username) {
User user = userLdapRepository.findByUsername(username)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment