Class LDAPUtils


  • public class LDAPUtils
    extends Object
    Allow to directly call some operations against LDAPIdentityStore.
    Author:
    Marek Posolda
    • Constructor Detail

      • LDAPUtils

        public LDAPUtils()
    • Method Detail

      • addUserToLDAP

        public static LDAPObject addUserToLDAP​(LDAPStorageProvider ldapProvider,
                                               RealmModel realm,
                                               UserModel user)
        Method to crate a user in the LDAP. The user will be created when all mandatory attributes specified by the mappers are set. The method onRegisterUserToLDAP is first called in each mapper to set any default or initial value.
        Parameters:
        ldapProvider - The ldap provider
        realm - The realm of the user
        user - The user model
        Returns:
        The LDAPObject created or to be created when mandatory attributes are filled
      • addUserToLDAP

        public static LDAPObject addUserToLDAP​(LDAPStorageProvider ldapProvider,
                                               RealmModel realm,
                                               UserModel user,
                                               Consumer<LDAPObject> consumerOnCreated)
        Method that creates a user in the LDAP when all the attributes marked as mandatory by the mappers are set. The method onRegisterUserToLDAP is first called in each mapper to set any default or initial value. When the user is finally created the passed consumerOnCreated parameter is executed (can be null).
        Parameters:
        ldapProvider - The ldap provider
        realm - The realm of the user
        user - The user model
        consumerOnCreated - The consumer to execute when the user is created
        Returns:
        The LDAPObject created or to be created when mandatory attributes are filled
      • addMember

        public static void addMember​(LDAPStorageProvider ldapProvider,
                                     MembershipType membershipType,
                                     String memberAttrName,
                                     String memberChildAttrName,
                                     LDAPObject ldapParent,
                                     LDAPObject ldapChild)
        Add ldapChild as member of ldapParent and save ldapParent to LDAP.
        Parameters:
        ldapProvider -
        membershipType - how is 'member' attribute saved (full DN or just uid)
        memberAttrName - usually 'member'
        memberChildAttrName - used just if membershipType is UID. Usually 'uid'
        ldapParent - role or group
        ldapChild - usually user (or child group or child role)
      • deleteMember

        public static void deleteMember​(LDAPStorageProvider ldapProvider,
                                        MembershipType membershipType,
                                        String memberAttrName,
                                        String memberChildAttrName,
                                        LDAPObject ldapParent,
                                        LDAPObject ldapChild)
        Remove ldapChild as member of ldapParent and save ldapParent to LDAP.
        Parameters:
        ldapProvider -
        membershipType - how is 'member' attribute saved (full DN or just uid)
        memberAttrName - usually 'member'
        memberChildAttrName - used just if membershipType is UID. Usually 'uid'
        ldapParent - role or group
        ldapChild - usually user (or child group or child role)
      • getExistingMemberships

        public static Set<String> getExistingMemberships​(LDAPStorageProvider ldapProvider,
                                                         String memberAttrName,
                                                         LDAPObject ldapRole)
        Return all existing memberships (values of attribute 'member' ) from the given ldapRole or ldapGroup
        Parameters:
        ldapProvider - The ldap provider
        memberAttrName - usually 'member'
        ldapRole -
        Returns:
      • getMemberValueOfChildObject

        public static String getMemberValueOfChildObject​(LDAPObject ldapUser,
                                                         MembershipType membershipType,
                                                         String memberChildAttrName)
        Get value to be used as attribute 'member' or 'memberUid' in some parent ldapObject
      • loadAllLDAPObjects

        public static List<LDAPObject> loadAllLDAPObjects​(LDAPQuery ldapQuery,
                                                          LDAPStorageProvider ldapProvider)
        Load all LDAP objects corresponding to given query. We will load them paginated, so we allow to bypass the limitation of 1000 maximum loaded objects in single query in MSAD
        Parameters:
        ldapQuery - LDAP query to be used. The caller should close it after calling this method
        ldapProvider -
        Returns:
      • fillRangedAttribute

        public static void fillRangedAttribute​(LDAPStorageProvider ldapProvider,
                                               LDAPObject ldapObject,
                                               String name)
        Performs iterative searches over an LDAPObject to return an attribute that is ranged.
        Parameters:
        ldapProvider - The provider to use
        ldapObject - The current object with the ranged attribute not complete
        name - The attribute name
      • getUserModelProperties

        public static Map<String,​Property<Object>> getUserModelProperties()
        Return a map of the user model properties from the getter methods Map key are the attributes names in lower case
      • setLDAPHostnameToKeycloakSession

        public static void setLDAPHostnameToKeycloakSession​(KeycloakSession session,
                                                            LDAPConfig ldapConfig)