Search in sources :

Example 16 with UserProfileProvider

use of org.keycloak.userprofile.UserProfileProvider in project keycloak by keycloak.

the class UserProfileResource method update.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
public Response update(String text) {
    auth.realm().requireManageRealm();
    UserProfileProvider t = session.getProvider(UserProfileProvider.class);
    try {
        t.setConfiguration(text);
    } catch (ComponentValidationException e) {
        // show validation result containing details about error
        return ErrorResponse.error(e.getMessage(), Response.Status.BAD_REQUEST);
    }
    return Response.ok(t.getConfiguration()).type(MediaType.APPLICATION_JSON).build();
}
Also used : ComponentValidationException(org.keycloak.component.ComponentValidationException) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 17 with UserProfileProvider

use of org.keycloak.userprofile.UserProfileProvider in project keycloak by keycloak.

the class AbstractUserProfileBean method init.

/**
 * Subclass have to call this method at the end of constructor to init user profile data.
 *
 * @param session
 * @param writeableOnly if true then only writeable (no read-only) attributes are put into template, if false then all readable attributes are there
 */
protected void init(KeycloakSession session, boolean writeableOnly) {
    UserProfileProvider provider = session.getProvider(UserProfileProvider.class);
    this.profile = createUserProfile(provider);
    this.attributes = toAttributes(profile.getAttributes().getReadable(), writeableOnly);
    if (this.attributes != null)
        this.attributesByName = attributes.stream().collect(Collectors.toMap((a) -> a.getName(), (a) -> a));
}
Also used : MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UserProfile(org.keycloak.userprofile.UserProfile) List(java.util.List) Stream(java.util.stream.Stream) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) Map(java.util.Map) KeycloakSession(org.keycloak.models.KeycloakSession) AttributeMetadata(org.keycloak.userprofile.AttributeMetadata) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) AttributeValidatorMetadata(org.keycloak.userprofile.AttributeValidatorMetadata) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider)

Example 18 with UserProfileProvider

use of org.keycloak.userprofile.UserProfileProvider in project keycloak by keycloak.

the class AccountRestService method account.

/**
 * Get account information.
 *
 * @return
 */
@Path("/")
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public UserRepresentation account(@QueryParam("userProfileMetadata") final Boolean userProfileMetadata) {
    auth.requireOneOf(AccountRoles.MANAGE_ACCOUNT, AccountRoles.VIEW_PROFILE);
    UserModel user = auth.getUser();
    UserRepresentation rep = new UserRepresentation();
    rep.setId(user.getId());
    rep.setUsername(user.getUsername());
    rep.setFirstName(user.getFirstName());
    rep.setLastName(user.getLastName());
    rep.setEmail(user.getEmail());
    rep.setEmailVerified(user.isEmailVerified());
    UserProfileProvider provider = session.getProvider(UserProfileProvider.class);
    UserProfile profile = provider.create(UserProfileContext.ACCOUNT, user);
    rep.setAttributes(profile.getAttributes().getReadable(false));
    if (userProfileMetadata == null || userProfileMetadata.booleanValue())
        rep.setUserProfileMetadata(createUserProfileMetadata(profile));
    return rep;
}
Also used : UserModel(org.keycloak.models.UserModel) UserProfile(org.keycloak.userprofile.UserProfile) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 19 with UserProfileProvider

use of org.keycloak.userprofile.UserProfileProvider 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)

Example 20 with UserProfileProvider

use of org.keycloak.userprofile.UserProfileProvider in project keycloak by keycloak.

the class DeclarativeUserTest method testDefaultUserProfileProviderIsActive.

@Test
public void testDefaultUserProfileProviderIsActive() {
    getTestingClient().server(TEST_REALM_NAME).run(session -> {
        Set<UserProfileProvider> providers = session.getAllProviders(UserProfileProvider.class);
        assertThat(providers, notNullValue());
        assertThat(providers.isEmpty(), is(false));
        UserProfileProvider provider = session.getProvider(UserProfileProvider.class);
        assertThat(provider, notNullValue());
        assertThat(DeclarativeUserProfileProvider.class.getName(), is(provider.getClass().getName()));
        assertThat(provider, instanceOf(DeclarativeUserProfileProvider.class));
    });
}
Also used : DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) Test(org.junit.Test)

Aggregations

UserProfileProvider (org.keycloak.userprofile.UserProfileProvider)30 UserProfile (org.keycloak.userprofile.UserProfile)24 ValidationException (org.keycloak.userprofile.ValidationException)15 UserModel (org.keycloak.models.UserModel)13 DeclarativeUserProfileProvider (org.keycloak.userprofile.DeclarativeUserProfileProvider)13 HashMap (java.util.HashMap)9 List (java.util.List)9 ComponentValidationException (org.keycloak.component.ComponentValidationException)7 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Consumes (javax.ws.rs.Consumes)4 RealmModel (org.keycloak.models.RealmModel)4 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 NoCache (org.jboss.resteasy.annotations.cache.NoCache)3 EventBuilder (org.keycloak.events.EventBuilder)3 KeycloakSession (org.keycloak.models.KeycloakSession)3 EventAuditingAttributeChangeListener (org.keycloak.userprofile.EventAuditingAttributeChangeListener)3 LinkedList (java.util.LinkedList)2