Search in sources :

Example 1 with Error

use of org.keycloak.userprofile.ValidationException.Error in project keycloak by keycloak.

the class AccountRestService method updateAccount.

@Path("/")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public Response updateAccount(UserRepresentation rep) {
    auth.require(AccountRoles.MANAGE_ACCOUNT);
    event.event(EventType.UPDATE_PROFILE).client(auth.getClient()).user(auth.getUser()).detail(Details.CONTEXT, UserProfileContext.ACCOUNT.name());
    UserProfileProvider profileProvider = session.getProvider(UserProfileProvider.class);
    UserProfile profile = profileProvider.create(UserProfileContext.ACCOUNT, rep.toAttributes(), auth.getUser());
    try {
        profile.update(new EventAuditingAttributeChangeListener(profile, event));
        event.success();
        return Response.noContent().build();
    } catch (ValidationException pve) {
        List<ErrorRepresentation> errors = new ArrayList<>();
        for (Error err : pve.getErrors()) {
            errors.add(new ErrorRepresentation(err.getAttribute(), err.getMessage(), validationErrorParamsToString(err.getMessageParameters(), profile.getAttributes())));
        }
        return ErrorResponse.errors(errors, pve.getStatusCode(), false);
    } catch (ReadOnlyException e) {
        return ErrorResponse.error(Messages.READ_ONLY_USER, Response.Status.BAD_REQUEST);
    }
}
Also used : ValidationException(org.keycloak.userprofile.ValidationException) UserProfile(org.keycloak.userprofile.UserProfile) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) EventAuditingAttributeChangeListener(org.keycloak.userprofile.EventAuditingAttributeChangeListener) Error(org.keycloak.userprofile.ValidationException.Error) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ReadOnlyException(org.keycloak.storage.ReadOnlyException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 NoCache (org.jboss.resteasy.annotations.cache.NoCache)1 ErrorRepresentation (org.keycloak.representations.idm.ErrorRepresentation)1 ReadOnlyException (org.keycloak.storage.ReadOnlyException)1 EventAuditingAttributeChangeListener (org.keycloak.userprofile.EventAuditingAttributeChangeListener)1 UserProfile (org.keycloak.userprofile.UserProfile)1 UserProfileProvider (org.keycloak.userprofile.UserProfileProvider)1 ValidationException (org.keycloak.userprofile.ValidationException)1 Error (org.keycloak.userprofile.ValidationException.Error)1