Skip to content

Commit

Permalink
Fixed user request classes. Added RegisterUserRequest.
Browse files Browse the repository at this point in the history
Signed-off-by: sugarylump <[email protected]>
  • Loading branch information
SugaryLump committed Jul 25, 2024
1 parent 4486112 commit 85deb2c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void setGroups(Set<String> groups) {
this.groups = groups;
}

public boolean isGuest() {
public boolean getGuest() {
return guest;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.roda.core.data.v2.user.requests;

import java.io.Serial;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import org.roda.core.data.common.SecureString;
import org.roda.core.data.v2.generics.MetadataValue;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
* @author António Lindo <[email protected]>
*/
@JsonInclude(JsonInclude.Include.ALWAYS)
public class RegisterUserRequest implements Serializable {

@Serial
private static final long serialVersionUID = 2288990827132952813L;

private String email;
private String name;
private String fullName;
private SecureString password;
private Set<MetadataValue> values;

public RegisterUserRequest(String email, String name, String fullName, SecureString password,
Set<MetadataValue> values) {
this.email = email;
this.name = name;
this.fullName = fullName;
this.password = password;
this.values = values;
}

public RegisterUserRequest() {
this.password = new SecureString();
this.values = new HashSet<>();
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public Set<MetadataValue> getValues() {
return values;
}

public void setValues(Set<MetadataValue> values) {
this.values = values;
}

public SecureString getPassword() {
return password;
}

public void setPassword(SecureString password) {
this.password = password;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.roda.core.data.v2.user.requests.CreateUserExtraFormFields;
import org.roda.core.data.v2.user.requests.CreateUserRequest;
import org.roda.core.data.v2.user.requests.LoginRequest;
import org.roda.core.data.v2.user.requests.RegisterUserRequest;
import org.roda.core.data.v2.user.requests.ResetPasswordRequest;
import org.roda.core.data.v2.user.requests.UpdateUserRequest;
import org.roda.core.model.utils.UserUtility;
Expand Down Expand Up @@ -713,8 +714,8 @@ public User createUser(@RequestBody CreateUserRequest createUserRequest, String
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);
LogEntryState state = LogEntryState.SUCCESS;
User user = new User(createUserRequest.getName(), createUserRequest.getName(), createUserRequest.getFullName(),
true, "", new HashSet<>(), new HashSet<>(), createUserRequest.getGroups(), null, null, null, null);

true, new HashSet<>(), new HashSet<>(), createUserRequest.getGroups(), createUserRequest.getEmail(),
createUserRequest.getGuest(), null, createUserRequest.getValues(), null, null, null, null);
try {
// check user permissions
controllerAssistant.checkRoles(requestContext.getUser());
Expand All @@ -735,25 +736,26 @@ public User createUser(@RequestBody CreateUserRequest createUserRequest, String
}

@Override
public User registerUser(@RequestBody UpdateUserRequest createUserRequest, String localeString, String captcha) {
public User registerUser(@RequestBody RegisterUserRequest registerUserRequest, String localeString, String captcha) {
ControllerAssistant controllerAssistant = new ControllerAssistant() {};
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);
LogEntryState state = LogEntryState.SUCCESS;

User user = new User(registerUserRequest.getName(), registerUserRequest.getName(),
registerUserRequest.getFullName(), true, new HashSet<>(), new HashSet<>(), new HashSet<>(),
registerUserRequest.getEmail(), false, null, registerUserRequest.getValues(), null, null, null, null);
try {
// delegate
return membersService.registerUser(request, createUserRequest.getUser(), createUserRequest.getPassword(), captcha,
createUserRequest.getValues(), localeString, request.getRequestURL().toString().split("/api")[0]);
return membersService.registerUser(request, user, registerUserRequest.getPassword(), captcha,
registerUserRequest.getValues(), localeString, request.getRequestURL().toString().split("/api")[0]);
} catch (EmailAlreadyExistsException | UserAlreadyExistsException e) {
state = LogEntryState.FAILURE;
return new User(createUserRequest.getUser());
return new User(user);
} catch (RODAException | RecaptchaException e) {
state = LogEntryState.FAILURE;
throw new RESTException(e);
} finally {
// register action
controllerAssistant.registerAction(requestContext, state, RodaConstants.CONTROLLER_USER_PARAM,
createUserRequest.getUser());
controllerAssistant.registerAction(requestContext, state, RodaConstants.CONTROLLER_USER_PARAM, user);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.roda.core.data.exceptions.UserAlreadyExistsException;
import org.roda.core.data.v2.generics.MetadataValue;
import org.roda.core.data.v2.user.User;
import org.roda.core.data.v2.user.requests.UpdateUserRequest;
import org.roda.core.data.v2.user.requests.RegisterUserRequest;
import org.roda.wui.client.common.UserLogin;
import org.roda.wui.client.common.dialogs.Dialogs;
import org.roda.wui.client.common.utils.JavascriptUtils;
Expand Down Expand Up @@ -169,7 +169,8 @@ void buttonApplyHandler(ClickEvent e) {
final String recaptcha = recaptchaResponse;

Services services = new Services("Register RODA user", "register");
UpdateUserRequest userRequest = new UpdateUserRequest(user, password, userDataPanel.getUserExtra());
RegisterUserRequest userRequest = new RegisterUserRequest(user.getEmail(), user.getName(), user.getFullName(),
password, userDataPanel.getUserExtra());
services.membersResource(s -> s.registerUser(userRequest, LocaleInfo.getCurrentLocale().getLocaleName(), recaptcha)).whenComplete((registedUser, error) -> {
if (registedUser != null) {
if (registedUser.isActive()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.roda.core.data.v2.user.requests.CreateUserExtraFormFields;
import org.roda.core.data.v2.user.requests.CreateUserRequest;
import org.roda.core.data.v2.user.requests.LoginRequest;
import org.roda.core.data.v2.user.requests.RegisterUserRequest;
import org.roda.core.data.v2.user.requests.ResetPasswordRequest;
import org.roda.core.data.v2.user.requests.UpdateUserRequest;
import org.roda.wui.api.v2.exceptions.model.ErrorResponseMessage;
Expand Down Expand Up @@ -106,10 +107,10 @@ User createUser(

@RequestMapping(path = "/users/register", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "Register user", requestBody = @RequestBody(required = true, content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = UpdateUserRequest.class))), description = "Registers a new user", responses = {
@Operation(summary = "Register user", requestBody = @RequestBody(required = true, content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = RegisterUserRequest.class))), description = "Registers a new user", responses = {
@ApiResponse(responseCode = "201", description = "Created", content = @Content(schema = @Schema(implementation = User.class)))})
User registerUser(
@Parameter(name = "user", required = true, content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE)) UpdateUserRequest userOperations,
@Parameter(name = "user", required = true, content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE)) RegisterUserRequest userOperations,
@Parameter(description = "The language to be used for internationalization") @RequestParam(name = "lang", defaultValue = "en", required = false) String localeString,
@Parameter(description = "captcha") @RequestParam(required = false, name = "captcha") String captcha);

Expand Down

0 comments on commit 85deb2c

Please sign in to comment.