org.keycloak.userprofile

## Class DefaultAttributes

- java.lang.Object
- - java.util.AbstractMap<K,V>
  - - java.util.HashMap<String,List<String>>
    - - org.keycloak.userprofile.DefaultAttributes

- All Implemented Interfaces:
    Serializable, Cloneable, Map<String,List<String>>, Attributes
  Direct Known Subclasses:
    LegacyAttributes

      public class DefaultAttributes
      extends HashMap<String,List<String>>
      implements Attributes

  The default implementation for `Attributes`. Should be reused as much as possible by the different implementations of `UserProfileProvider`.

  One of the main aspects of this implementation is to allow normalizing attributes accordingly to the profile configuration and current context. As such, it provides some common normalization to common profile attributes (e.g.: username, email, first and last names, dynamic read-only attributes).

  This implementation is not specific to any user profile implementation.

  Author:
    Pedro Igor
  See Also:
    Serialized Form

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class java.util.AbstractMap

      `AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>`

    - ### Nested classes/interfaces inherited from interface java.util.Map

      `Map.Entry<K,V>`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected UserProfileContext` | `context`
    `static String` | `READ_ONLY_ATTRIBUTE_KEY` To reference dynamic attributes that can be configured as read-only when setting up the provider.
    `protected UserModel` | `user`

    - ### Fields inherited from interface org.keycloak.userprofile.Attributes

      `EMPTY_VALUE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DefaultAttributes(UserProfileContext context, Map<String,?> attributes, UserModel user, UserProfileMetadata profileMetadata, KeycloakSession session)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Set<Map.Entry<String,List<String>>>` | `attributeSet()` Returns all attributes defined.
    `boolean` | `contains(String name)` Checks whether an attribute with the given `name` is defined.
    `protected AttributeContext` | `createAttributeContext(AttributeMetadata metadata)`
    `AttributeMetadata` | `getMetadata(String name)` Returns the metadata associated with the attribute with the given `name`.
    `Map<String,List<String>>` | `getReadable()` Returns only the attributes that have read/write permissions.
    `List<String>` | `getValues(String name)` Returns all values for an attribute with the given `name`.
    `protected boolean` | `isIncludeAttributeIfNotProvided(AttributeMetadata metadata)`
    `boolean` | `isReadOnly(String attributeName)` Checks whether an attribute is read-only.
    `protected boolean` | `isReadOnlyFromMetadata(String attributeName)` Checks whether an attribute is marked as read only by looking at its metadata.
    `protected boolean` | `isReadOnlyInternalAttribute(String attributeName)` Returns whether an attribute is read only based on the provider configuration (using provider config), usually related to internal attributes managed by the server.
    `boolean` | `isRequired(String name)` Returns whether the attribute with the given `name` is required.
    `protected boolean` | `isSupportedAttribute(String name)` Checks whether an attribute is support by the profile configuration and the current context.
    `Set<String>` | `nameSet()` Returns the names of all defined attributes.
    `Map<String,List<String>>` | `toMap()`
    `boolean` | `validate(String name, Consumer<ValidationError>... listeners)` Validates the attribute with the given `name`.

    - ### Methods inherited from class java.util.HashMap

      `clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values`

    - ### Methods inherited from class java.util.AbstractMap

      `equals, hashCode, toString`

    - ### Methods inherited from class java.lang.Object

      `finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.keycloak.userprofile.Attributes

      `getFirstValue, getReadable, isRootAttribute`

    - ### Methods inherited from interface java.util.Map

      `equals, hashCode`

- - ### Field Detail

    - #### READ_ONLY_ATTRIBUTE_KEY

          public static final String READ_ONLY_ATTRIBUTE_KEY

      To reference dynamic attributes that can be configured as read-only when setting up the provider. We should probably remove that once we remove the legacy provider, because this will come from the configuration.
      See Also:
        Constant Field Values

    - #### context

          protected final UserProfileContext context

    - #### user

          protected final UserModel user

  - ### Constructor Detail

    - #### DefaultAttributes

          public DefaultAttributes(UserProfileContext context,
                                   Map<String,?> attributes,
                                   UserModel user,
                                   UserProfileMetadata profileMetadata,
                                   KeycloakSession session)

  - ### Method Detail

    - #### isReadOnly

          public boolean isReadOnly(String attributeName)

      Description copied from interface: `Attributes`
      Checks whether an attribute is read-only.
      Specified by:
        `isReadOnly` in interface `Attributes`
      Returns:

    - #### isReadOnlyFromMetadata

          protected boolean isReadOnlyFromMetadata(String attributeName)

      Checks whether an attribute is marked as read only by looking at its metadata.
      Parameters:
        `attributeName` - the attribute name
      Returns:
        @return `true` if the attribute is readonly. Otherwise, returns `false`

    - #### isRequired

          public boolean isRequired(String name)

      Description copied from interface: `Attributes`
      Returns whether the attribute with the given `name` is required.
      Specified by:
        `isRequired` in interface `Attributes`
      Parameters:
        `name` - the attribute name
      Returns:
        `true` if the attribute is required. Otherwise, `false`.

    - #### validate

          public boolean validate(String name,
                                  Consumer<ValidationError>... listeners)

      Description copied from interface: `Attributes`
      Validates the attribute with the given `name`.
      Specified by:
        `validate` in interface `Attributes`
      Parameters:
        `name` - the name of the attribute
        `listeners` - the listeners for listening for errors. `ValidationError.inputHint` contains name of the attribute in error.
      Returns:
        `true` if validation is successful. Otherwise, `false`. In case there is no attribute with the given `name`, `false` is also returned but without triggering listeners

    - #### getValues

          public List<String> getValues(String name)

      Description copied from interface: `Attributes`
      Returns all values for an attribute with the given `name`.
      Specified by:
        `getValues` in interface `Attributes`
      Parameters:
        `name` - the name of the attribute
      Returns:
        the attribute values

    - #### contains

          public boolean contains(String name)

      Description copied from interface: `Attributes`
      Checks whether an attribute with the given `name` is defined.
      Specified by:
        `contains` in interface `Attributes`
      Parameters:
        `name` - the name of the attribute
      Returns:
        `true` if the attribute is defined. Otherwise, `false`

    - #### nameSet

          public Set<String> nameSet()

      Description copied from interface: `Attributes`
      Returns the names of all defined attributes.
      Specified by:
        `nameSet` in interface `Attributes`
      Returns:
        the set of attribute names

    - #### attributeSet

          public Set<Map.Entry<String,List<String>>> attributeSet()

      Description copied from interface: `Attributes`
      Returns all attributes defined.
      Specified by:
        `attributeSet` in interface `Attributes`
      Returns:
        the attributes

    - #### getMetadata

          public AttributeMetadata getMetadata(String name)

      Description copied from interface: `Attributes`

      Returns the metadata associated with the attribute with the given `name`.

      The `AttributeMetadata` is a copy of the original metadata. The original metadata keeps immutable.

      Specified by:
        `getMetadata` in interface `Attributes`
      Parameters:
        `name` - the attribute name
      Returns:
        the metadata

    - #### getReadable

          public Map<String,List<String>> getReadable()

      Description copied from interface: `Attributes`
      Returns only the attributes that have read/write permissions.
      Specified by:
        `getReadable` in interface `Attributes`
      Returns:
        the attributes with read/write permission.

    - #### toMap

          public Map<String,List<String>> toMap()

      Specified by:
        `toMap` in interface `Attributes`

    - #### createAttributeContext

          protected AttributeContext createAttributeContext(AttributeMetadata metadata)

    - #### isIncludeAttributeIfNotProvided

          protected boolean isIncludeAttributeIfNotProvided(AttributeMetadata metadata)

    - #### isSupportedAttribute

          protected boolean isSupportedAttribute(String name)

      Checks whether an attribute is support by the profile configuration and the current context.

      This method can be used to avoid unexpected attributes from being added as an attribute because the attribute source is a regular `Map` and not normalized.

      Parameters:
        `name` - the name of the attribute
      Returns:

    - #### isReadOnlyInternalAttribute

          protected boolean isReadOnlyInternalAttribute(String attributeName)

      Returns whether an attribute is read only based on the provider configuration (using provider config), usually related to internal attributes managed by the server.

      For user-defined attributes, it should be preferable to use the user profile configuration.

      Parameters:
        `attributeName` - the attribute name
      Returns:
        `true` if the attribute is readonly. Otherwise, returns `false`

Copyright © 2021 JBoss by Red Hat. All rights reserved.