Search in sources :

Example 11 with ErrorVO

use of org.olat.restapi.support.vo.ErrorVO in project OpenOLAT by OpenOLAT.

the class UserWebService method validateProperty.

private boolean validateProperty(User user, String value, UserPropertyHandler userPropertyHandler, List<ErrorVO> errors, UserManager um, Locale locale) {
    ValidationError error = new ValidationError();
    if (!StringHelper.containsNonWhitespace(value) && um.isMandatoryUserProperty(PROPERTY_HANDLER_IDENTIFIER, userPropertyHandler)) {
        Translator translator = new PackageTranslator("org.olat.core", locale);
        String translation = translator.translate("new.form.mandatory");
        errors.add(new ErrorVO("org.olat.core:new.form.mandatory:" + userPropertyHandler.getName(), translation));
        return false;
    }
    value = parseUserProperty(value, userPropertyHandler, locale);
    if (!userPropertyHandler.isValidValue(user, value, error, locale)) {
        String pack = userPropertyHandler.getClass().getPackage().getName();
        Translator translator = new PackageTranslator(pack, locale);
        String translation = translator.translate(error.getErrorKey(), error.getArgs());
        errors.add(new ErrorVO(pack, error.getErrorKey(), translation));
        return false;
    } else if ((userPropertyHandler.getName().equals(UserConstants.INSTITUTIONALEMAIL) && StringHelper.containsNonWhitespace(value)) || userPropertyHandler.getName().equals(UserConstants.EMAIL)) {
        if (!UserManager.getInstance().isEmailAllowed(value, user)) {
            String pack = userPropertyHandler.getClass().getPackage().getName();
            Translator translator = new PackageTranslator(pack, locale);
            String translation = translator.translate("form.name." + userPropertyHandler.getName() + ".error.exists", new String[] { value });
            translation += " (" + value + ")";
            errors.add(new ErrorVO("org.olat.user.propertyhandlers:new.form.name." + userPropertyHandler.getName() + ".exists", translation));
        }
    }
    return true;
}
Also used : PackageTranslator(org.olat.core.gui.translator.PackageTranslator) ErrorVO(org.olat.restapi.support.vo.ErrorVO) Translator(org.olat.core.gui.translator.Translator) PackageTranslator(org.olat.core.gui.translator.PackageTranslator) ValidationError(org.olat.core.gui.components.form.ValidationError)

Example 12 with ErrorVO

use of org.olat.restapi.support.vo.ErrorVO in project OpenOLAT by OpenOLAT.

the class UserWebService method update.

/**
 * Update an user
 * @response.representation.qname {http://www.example.com}userVO
 * @response.representation.mediaType application/xml, application/json
 * @response.representation.doc The user
 * @response.representation.example {@link org.olat.user.restapi.Examples#SAMPLE_USERVO}
 * @response.representation.200.qname {http://www.example.com}userVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The user
 * @response.representation.200.example {@link org.olat.user.restapi.Examples#SAMPLE_USERVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The identity not found
 * @response.representation.406.qname {http://www.example.com}errorVO
 * @response.representation.406.mediaType application/xml, application/json
 * @response.representation.406.doc The list of validation errors
 * @response.representation.406.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_ERRORVOes}
 * @param identityKey The user key identifier
 * @param user The user datas
 * @param request The HTTP request
 * @return <code>User</code> object. The operation status (success or fail)
 */
@POST
@Path("{identityKey}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response update(@PathParam("identityKey") Long identityKey, UserVO user, @Context HttpServletRequest request) {
    try {
        if (user == null) {
            return Response.serverError().status(Status.NO_CONTENT).build();
        }
        if (!isUserManager(request)) {
            return Response.serverError().status(Status.UNAUTHORIZED).build();
        }
        BaseSecurity baseSecurity = BaseSecurityManager.getInstance();
        Identity retrievedIdentity = baseSecurity.loadIdentityByKey(identityKey, false);
        if (retrievedIdentity == null) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
        User retrievedUser = retrievedIdentity.getUser();
        List<ErrorVO> errors = validateUser(retrievedUser, user, request);
        if (errors.isEmpty()) {
            if (StringHelper.containsNonWhitespace(user.getExternalId()) && !user.getExternalId().equals(retrievedIdentity.getExternalId())) {
                retrievedIdentity = baseSecurity.setExternalId(retrievedIdentity, user.getExternalId());
                retrievedUser = retrievedIdentity.getUser();
            }
            String oldEmail = retrievedUser.getEmail();
            post(retrievedUser, user, getLocale(request));
            UserManager.getInstance().updateUser(retrievedUser);
            BaseSecurityManager.getInstance().deleteInvalidAuthenticationsByEmail(oldEmail);
            return Response.ok(get(retrievedIdentity, true, true)).build();
        }
        // content not ok
        ErrorVO[] errorVos = new ErrorVO[errors.size()];
        errors.toArray(errorVos);
        return Response.ok(errorVos).status(Status.NOT_ACCEPTABLE).build();
    } catch (Exception e) {
        log.error("Error updating an user", e);
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : ErrorVO(org.olat.restapi.support.vo.ErrorVO) User(org.olat.core.id.User) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) WebApplicationException(javax.ws.rs.WebApplicationException) BaseSecurity(org.olat.basesecurity.BaseSecurity) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 13 with ErrorVO

use of org.olat.restapi.support.vo.ErrorVO in project OpenOLAT by OpenOLAT.

the class UserMgmtTest method testCreateUserWithValidationError.

@Test
public void testCreateUserWithValidationError() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    String login = "rest-809-" + UUID.randomUUID();
    UserVO vo = new UserVO();
    vo.setLogin(login);
    vo.setFirstName("John");
    vo.setLastName("Smith");
    vo.setEmail("");
    vo.putProperty("gender", "lu");
    URI request = UriBuilder.fromUri(getContextURI()).path("users").build();
    HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(method, vo);
    HttpResponse response = conn.execute(method);
    assertEquals(406, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    List<ErrorVO> errors = parseErrorArray(body);
    assertNotNull(errors);
    assertFalse(errors.isEmpty());
    assertTrue(errors.size() >= 2);
    assertNotNull(errors.get(0).getCode());
    assertNotNull(errors.get(0).getTranslation());
    assertNotNull(errors.get(1).getCode());
    assertNotNull(errors.get(1).getTranslation());
    Identity savedIdent = BaseSecurityManager.getInstance().findIdentityByName(login);
    assertNull(savedIdent);
    conn.shutdown();
}
Also used : ErrorVO(org.olat.restapi.support.vo.ErrorVO) ManagedUserVO(org.olat.user.restapi.ManagedUserVO) UserVO(org.olat.user.restapi.UserVO) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) Identity(org.olat.core.id.Identity) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 14 with ErrorVO

use of org.olat.restapi.support.vo.ErrorVO in project openolat by klemens.

the class ObjectFactory method get.

public static ErrorVO get(ValidationError error) {
    ErrorVO vo = new ErrorVO();
    vo.setCode("unkown" + ":" + error.getErrorKey());
    vo.setTranslation("Hello");
    return vo;
}
Also used : ErrorVO(org.olat.restapi.support.vo.ErrorVO)

Example 15 with ErrorVO

use of org.olat.restapi.support.vo.ErrorVO in project openolat by klemens.

the class UserWebService method validateUser.

private List<ErrorVO> validateUser(User user, UserVO userVo, HttpServletRequest request) {
    UserManager um = UserManager.getInstance();
    Locale locale = getLocale(request);
    List<ErrorVO> errors = new ArrayList<>();
    List<UserPropertyHandler> propertyHandlers = um.getUserPropertyHandlersFor(PROPERTY_HANDLER_IDENTIFIER, false);
    validateProperty(user, UserConstants.FIRSTNAME, userVo.getFirstName(), propertyHandlers, errors, um, locale);
    validateProperty(user, UserConstants.LASTNAME, userVo.getLastName(), propertyHandlers, errors, um, locale);
    validateProperty(user, UserConstants.EMAIL, userVo.getEmail(), propertyHandlers, errors, um, locale);
    for (UserPropertyHandler propertyHandler : propertyHandlers) {
        if (!UserConstants.FIRSTNAME.equals(propertyHandler.getName()) && !UserConstants.LASTNAME.equals(propertyHandler.getName()) && !UserConstants.EMAIL.equals(propertyHandler.getName())) {
            validateProperty(user, userVo, propertyHandler, errors, um, locale);
        }
    }
    return errors;
}
Also used : Locale(java.util.Locale) RestSecurityHelper.getLocale(org.olat.restapi.security.RestSecurityHelper.getLocale) ErrorVO(org.olat.restapi.support.vo.ErrorVO) UserManager(org.olat.user.UserManager) RestSecurityHelper.isUserManager(org.olat.restapi.security.RestSecurityHelper.isUserManager) ArrayList(java.util.ArrayList) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler)

Aggregations

ErrorVO (org.olat.restapi.support.vo.ErrorVO)20 Identity (org.olat.core.id.Identity)10 Locale (java.util.Locale)6 Consumes (javax.ws.rs.Consumes)6 Produces (javax.ws.rs.Produces)6 Translator (org.olat.core.gui.translator.Translator)6 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)6 URI (java.net.URI)4 PUT (javax.ws.rs.PUT)4 HttpResponse (org.apache.http.HttpResponse)4 HttpPut (org.apache.http.client.methods.HttpPut)4 Test (org.junit.Test)4 Authentication (org.olat.basesecurity.Authentication)4 BaseSecurity (org.olat.basesecurity.BaseSecurity)4 PackageTranslator (org.olat.core.gui.translator.PackageTranslator)4 User (org.olat.core.id.User)4 RestSecurityHelper.getLocale (org.olat.restapi.security.RestSecurityHelper.getLocale)4 AuthenticationVO (org.olat.restapi.support.vo.AuthenticationVO)4 UserManager (org.olat.user.UserManager)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2