Search in sources :

Example 1 with Attributes

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

the class UserProfileTest method testGetProfileAttributes.

private static void testGetProfileAttributes(KeycloakSession session) {
    RealmModel realm = session.getContext().getRealm();
    UserModel user = session.users().addUser(realm, org.keycloak.models.utils.KeycloakModelUtils.generateId());
    UserProfileProvider provider = getDynamicUserProfileProvider(session);
    provider.setConfiguration("{\"attributes\": [{\"name\": \"address\", \"required\": {}, \"permissions\": {\"edit\": [\"user\"]}}]}");
    UserProfile profile = provider.create(UserProfileContext.ACCOUNT, user);
    Attributes attributes = profile.getAttributes();
    assertThat(attributes.nameSet(), containsInAnyOrder(UserModel.USERNAME, UserModel.EMAIL, UserModel.FIRST_NAME, UserModel.LAST_NAME, "address"));
    try {
        profile.validate();
        Assert.fail("Should fail validation");
    } catch (ValidationException ve) {
        // username is mandatory
        assertTrue(ve.isAttributeOnError("address"));
    }
    assertNotNull(attributes.getFirstValue(UserModel.USERNAME));
    assertNull(attributes.getFirstValue(UserModel.EMAIL));
    assertNull(attributes.getFirstValue(UserModel.FIRST_NAME));
    assertNull(attributes.getFirstValue(UserModel.LAST_NAME));
    assertNull(attributes.getFirstValue("address"));
    user.setAttribute("address", Arrays.asList("fixed-address"));
    profile = provider.create(UserProfileContext.ACCOUNT, user);
    attributes = profile.getAttributes();
    profile.validate();
    assertNotNull(attributes.getFirstValue("address"));
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ComponentValidationException(org.keycloak.component.ComponentValidationException) ValidationException(org.keycloak.userprofile.ValidationException) UserProfile(org.keycloak.userprofile.UserProfile) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) Attributes(org.keycloak.userprofile.Attributes)

Example 2 with Attributes

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

the class UserProfileTest method testGetProfileAttributeGroups.

private static void testGetProfileAttributeGroups(KeycloakSession session) {
    RealmModel realm = session.getContext().getRealm();
    UserModel user = session.users().addUser(realm, org.keycloak.models.utils.KeycloakModelUtils.generateId());
    UserProfileProvider provider = getDynamicUserProfileProvider(session);
    String configuration = "{\n" + "  \"attributes\": [\n" + "    {\n" + "      \"name\": \"address\",\n" + "      \"group\": \"companyaddress\"\n" + "    },\n" + "    {\n" + "      \"name\": \"second\",\n" + "      \"group\": \"groupwithanno" + "\"\n" + "    }\n" + "  ],\n" + "  \"groups\": [\n" + "    {\n" + "      \"name\": \"companyaddress\",\n" + "      \"displayHeader\": \"header\",\n" + "      \"displayDescription\": \"description\"\n" + "    },\n" + "    {\n" + "      \"name\": \"groupwithanno\",\n" + "      \"annotations\": {\n" + "        \"anno1\": \"value1\",\n" + "        \"anno2\": \"value2\"\n" + "      }\n" + "    }\n" + "  ]\n" + "}\n";
    provider.setConfiguration(configuration);
    UserProfile profile = provider.create(UserProfileContext.ACCOUNT, user);
    Attributes attributes = profile.getAttributes();
    assertThat(attributes.nameSet(), containsInAnyOrder(UserModel.USERNAME, UserModel.EMAIL, UserModel.FIRST_NAME, UserModel.LAST_NAME, "address", "second"));
    AttributeGroupMetadata companyAddressGroup = attributes.getMetadata("address").getAttributeGroupMetadata();
    assertEquals("companyaddress", companyAddressGroup.getName());
    assertEquals("header", companyAddressGroup.getDisplayHeader());
    assertEquals("description", companyAddressGroup.getDisplayDescription());
    assertNull(companyAddressGroup.getAnnotations());
    AttributeGroupMetadata groupwithannoGroup = attributes.getMetadata("second").getAttributeGroupMetadata();
    assertEquals("groupwithanno", groupwithannoGroup.getName());
    assertNull(groupwithannoGroup.getDisplayHeader());
    assertNull(groupwithannoGroup.getDisplayDescription());
    Map<String, Object> annotations = groupwithannoGroup.getAnnotations();
    assertEquals(2, annotations.size());
    assertEquals("value1", annotations.get("anno1"));
    assertEquals("value2", annotations.get("anno2"));
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) AttributeGroupMetadata(org.keycloak.userprofile.AttributeGroupMetadata) UserProfile(org.keycloak.userprofile.UserProfile) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) Attributes(org.keycloak.userprofile.Attributes)

Aggregations

RealmModel (org.keycloak.models.RealmModel)2 UserModel (org.keycloak.models.UserModel)2 Attributes (org.keycloak.userprofile.Attributes)2 DeclarativeUserProfileProvider (org.keycloak.userprofile.DeclarativeUserProfileProvider)2 UserProfile (org.keycloak.userprofile.UserProfile)2 UserProfileProvider (org.keycloak.userprofile.UserProfileProvider)2 ComponentValidationException (org.keycloak.component.ComponentValidationException)1 AttributeGroupMetadata (org.keycloak.userprofile.AttributeGroupMetadata)1 ValidationException (org.keycloak.userprofile.ValidationException)1