federation/ldap/src/main/java/org/keycloak/federation/ldap/LDAPFederationProviderFactory.java
// Best effort to create appropriate mappers according to our LDAP config

Best effort to create appropriate mappers according to our LDAP config
Best effort to create appropriate mappers according to our LDAP config
@Override
    public void onProviderModelCreated(RealmModel realm, UserFederationProviderModel newProviderModel) {
        LDAPConfig ldapConfig = new LDAPConfig(newProviderModel.getConfig());
        boolean activeDirectory = ldapConfig.isActiveDirectory();
        UserFederationProvider.EditMode editMode = ldapConfig.getEditMode();
        String readOnly = String.valueOf(editMode==UserFederationProvider.EditMode.READ_ONLY || editMode== UserFederationProvider.EditMode.UNSYNCED);
        String usernameLdapAttribute = ldapConfig.getUsernameLdapAttribute();
        UserFederationMapperModel mapperModel;
        mapperModel = KeycloakModelUtils.createUserFederationMapperModel("username", newProviderModel.getId(), UserAttributeLDAPFederationMapperFactory.PROVIDER_ID,
                UserAttributeLDAPFederationMapper.USER_MODEL_ATTRIBUTE, UserModel.USERNAME,
                UserAttributeLDAPFederationMapper.LDAP_ATTRIBUTE, usernameLdapAttribute,
                UserAttributeLDAPFederationMapper.READ_ONLY, readOnly);
        realm.addUserFederationMapper(mapperModel);
        if (ldapConfig.getRdnLdapAttribute().equalsIgnoreCase(LDAPConstants.CN)) {
            if (usernameLdapAttribute.equalsIgnoreCase(LDAPConstants.CN)) {
                mapperModel = KeycloakModelUtils.createUserFederationMapperModel("first name", newProviderModel.getId(), UserAttributeLDAPFederationMapperFactory.PROVIDER_ID,
                        UserAttributeLDAPFederationMapper.USER_MODEL_ATTRIBUTE, UserModel.FIRST_NAME,
                        UserAttributeLDAPFederationMapper.LDAP_ATTRIBUTE, LDAPConstants.GIVENNAME,
                        UserAttributeLDAPFederationMapper.READ_ONLY, readOnly);
                realm.addUserFederationMapper(mapperModel);
            } else {
                if (editMode == UserFederationProvider.EditMode.WRITABLE) {
                    mapperModel = KeycloakModelUtils.createUserFederationMapperModel("first name", newProviderModel.getId(), UserAttributeLDAPFederationMapperFactory.PROVIDER_ID,
                            UserAttributeLDAPFederationMapper.USER_MODEL_ATTRIBUTE, UserModel.FIRST_NAME,
                            UserAttributeLDAPFederationMapper.LDAP_ATTRIBUTE, LDAPConstants.GIVENNAME,
                            UserAttributeLDAPFederationMapper.READ_ONLY, readOnly);
                    realm.addUserFederationMapper(mapperModel);
                    mapperModel = KeycloakModelUtils.createUserFederationMapperModel("username-cn", newProviderModel.getId(), UserAttributeLDAPFederationMapperFactory.PROVIDER_ID,
                            UserAttributeLDAPFederationMapper.USER_MODEL_ATTRIBUTE, UserModel.USERNAME,
                            UserAttributeLDAPFederationMapper.LDAP_ATTRIBUTE, LDAPConstants.CN,
                            UserAttributeLDAPFederationMapper.READ_ONLY, readOnly);
                    realm.addUserFederationMapper(mapperModel);
                } else {
                    mapperModel = KeycloakModelUtils.createUserFederationMapperModel("full name", newProviderModel.getId(), FullNameLDAPFederationMapperFactory.PROVIDER_ID,
                            FullNameLDAPFederationMapper.LDAP_FULL_NAME_ATTRIBUTE, LDAPConstants.CN,
                            UserAttributeLDAPFederationMapper.READ_ONLY, readOnly);
                    realm.addUserFederationMapper(mapperModel);
                }
            }
        } else {
            mapperModel = KeycloakModelUtils.createUserFederationMapperModel("first name", newProviderModel.getId(), UserAttributeLDAPFederationMapperFactory.PROVIDER_ID,
                    UserAttributeLDAPFederationMapper.USER_MODEL_ATTRIBUTE, UserModel.FIRST_NAME,
                    UserAttributeLDAPFederationMapper.LDAP_ATTRIBUTE, LDAPConstants.CN,
                    UserAttributeLDAPFederationMapper.READ_ONLY, readOnly);
            realm.addUserFederationMapper(mapperModel);
        }
        mapperModel = KeycloakModelUtils.createUserFederationMapperModel("last name", newProviderModel.getId(), UserAttributeLDAPFederationMapperFactory.PROVIDER_ID,
                UserAttributeLDAPFederationMapper.USER_MODEL_ATTRIBUTE, UserModel.LAST_NAME,
                UserAttributeLDAPFederationMapper.LDAP_ATTRIBUTE, LDAPConstants.SN,
                UserAttributeLDAPFederationMapper.READ_ONLY, readOnly);
        realm.addUserFederationMapper(mapperModel);
        mapperModel = KeycloakModelUtils.createUserFederationMapperModel("email", newProviderModel.getId(), UserAttributeLDAPFederationMapperFactory.PROVIDER_ID,
                UserAttributeLDAPFederationMapper.USER_MODEL_ATTRIBUTE, UserModel.EMAIL,
                UserAttributeLDAPFederationMapper.LDAP_ATTRIBUTE, LDAPConstants.EMAIL,
                UserAttributeLDAPFederationMapper.READ_ONLY, readOnly);
        realm.addUserFederationMapper(mapperModel);
        String createTimestampLdapAttrName = activeDirectory ? "whenCreated" : LDAPConstants.CREATE_TIMESTAMP;
        String modifyTimestampLdapAttrName = activeDirectory ? "whenChanged" : LDAPConstants.MODIFY_TIMESTAMP;
        mapperModel = KeycloakModelUtils.createUserFederationMapperModel("creation date", newProviderModel.getId(), UserAttributeLDAPFederationMapperFactory.PROVIDER_ID,
                UserAttributeLDAPFederationMapper.USER_MODEL_ATTRIBUTE, LDAPConstants.CREATE_TIMESTAMP,
                UserAttributeLDAPFederationMapper.LDAP_ATTRIBUTE, createTimestampLdapAttrName,
                UserAttributeLDAPFederationMapper.READ_ONLY, "true");
        realm.addUserFederationMapper(mapperModel);
        mapperModel = KeycloakModelUtils.createUserFederationMapperModel("modify date", newProviderModel.getId(), UserAttributeLDAPFederationMapperFactory.PROVIDER_ID,
                UserAttributeLDAPFederationMapper.USER_MODEL_ATTRIBUTE, LDAPConstants.MODIFY_TIMESTAMP,
                UserAttributeLDAPFederationMapper.LDAP_ATTRIBUTE, modifyTimestampLdapAttrName,
                UserAttributeLDAPFederationMapper.READ_ONLY, "true");
        realm.addUserFederationMapper(mapperModel);
    }
@ Override public void onProviderModelCreated ( RealmModel realm , UserFederationProviderModel newProviderModel ) { LDAPConfig ldapConfig = new LDAPConfig ( newProviderModel . getConfig ( ) ) ; boolean activeDirectory = ldapConfig . isActiveDirectory ( ) ; UserFederationProvider . EditMode editMode = ldapConfig . getEditMode ( ) ; String readOnly = String . valueOf ( editMode == UserFederationProvider . EditMode . READ_ONLY || editMode == UserFederationProvider . EditMode . UNSYNCED ) ; String usernameLdapAttribute = ldapConfig . getUsernameLdapAttribute ( ) ; UserFederationMapperModel mapperModel ; mapperModel = KeycloakModelUtils . createUserFederationMapperModel ( "username" , newProviderModel . getId ( ) , UserAttributeLDAPFederationMapperFactory . PROVIDER_ID , UserAttributeLDAPFederationMapper . USER_MODEL_ATTRIBUTE , UserModel . USERNAME , UserAttributeLDAPFederationMapper . LDAP_ATTRIBUTE , usernameLdapAttribute , UserAttributeLDAPFederationMapper . READ_ONLY , readOnly ) ; realm . addUserFederationMapper ( mapperModel ) ; if ( ldapConfig . getRdnLdapAttribute ( ) . equalsIgnoreCase ( LDAPConstants . CN ) ) { if ( usernameLdapAttribute . equalsIgnoreCase ( LDAPConstants . CN ) ) { mapperModel = KeycloakModelUtils . createUserFederationMapperModel ( "first name" , newProviderModel . getId ( ) , UserAttributeLDAPFederationMapperFactory . PROVIDER_ID , UserAttributeLDAPFederationMapper . USER_MODEL_ATTRIBUTE , UserModel . FIRST_NAME , UserAttributeLDAPFederationMapper . LDAP_ATTRIBUTE , LDAPConstants . GIVENNAME , UserAttributeLDAPFederationMapper . READ_ONLY , readOnly ) ; realm . addUserFederationMapper ( mapperModel ) ; } else { if ( editMode == UserFederationProvider . EditMode . WRITABLE ) { mapperModel = KeycloakModelUtils . createUserFederationMapperModel ( "first name" , newProviderModel . getId ( ) , UserAttributeLDAPFederationMapperFactory . PROVIDER_ID , UserAttributeLDAPFederationMapper . USER_MODEL_ATTRIBUTE , UserModel . FIRST_NAME , UserAttributeLDAPFederationMapper . LDAP_ATTRIBUTE , LDAPConstants . GIVENNAME , UserAttributeLDAPFederationMapper . READ_ONLY , readOnly ) ; realm . addUserFederationMapper ( mapperModel ) ; mapperModel = KeycloakModelUtils . createUserFederationMapperModel ( "username-cn" , newProviderModel . getId ( ) , UserAttributeLDAPFederationMapperFactory . PROVIDER_ID , UserAttributeLDAPFederationMapper . USER_MODEL_ATTRIBUTE , UserModel . USERNAME , UserAttributeLDAPFederationMapper . LDAP_ATTRIBUTE , LDAPConstants . CN , UserAttributeLDAPFederationMapper . READ_ONLY , readOnly ) ; realm . addUserFederationMapper ( mapperModel ) ; } else { mapperModel = KeycloakModelUtils . createUserFederationMapperModel ( "full name" , newProviderModel . getId ( ) , FullNameLDAPFederationMapperFactory . PROVIDER_ID , FullNameLDAPFederationMapper . LDAP_FULL_NAME_ATTRIBUTE , LDAPConstants . CN , UserAttributeLDAPFederationMapper . READ_ONLY , readOnly ) ; realm . addUserFederationMapper ( mapperModel ) ; } } } else { mapperModel = KeycloakModelUtils . createUserFederationMapperModel ( "first name" , newProviderModel . getId ( ) , UserAttributeLDAPFederationMapperFactory . PROVIDER_ID , UserAttributeLDAPFederationMapper . USER_MODEL_ATTRIBUTE , UserModel . FIRST_NAME , UserAttributeLDAPFederationMapper . LDAP_ATTRIBUTE , LDAPConstants . CN , UserAttributeLDAPFederationMapper . READ_ONLY , readOnly ) ; realm . addUserFederationMapper ( mapperModel ) ; } mapperModel = KeycloakModelUtils . createUserFederationMapperModel ( "last name" , newProviderModel . getId ( ) , UserAttributeLDAPFederationMapperFactory . PROVIDER_ID , UserAttributeLDAPFederationMapper . USER_MODEL_ATTRIBUTE , UserModel . LAST_NAME , UserAttributeLDAPFederationMapper . LDAP_ATTRIBUTE , LDAPConstants . SN , UserAttributeLDAPFederationMapper . READ_ONLY , readOnly ) ; realm . addUserFederationMapper ( mapperModel ) ; mapperModel = KeycloakModelUtils . createUserFederationMapperModel ( "email" , newProviderModel . getId ( ) , UserAttributeLDAPFederationMapperFactory . PROVIDER_ID , UserAttributeLDAPFederationMapper . USER_MODEL_ATTRIBUTE , UserModel . EMAIL , UserAttributeLDAPFederationMapper . LDAP_ATTRIBUTE , LDAPConstants . EMAIL , UserAttributeLDAPFederationMapper . READ_ONLY , readOnly ) ; realm . addUserFederationMapper ( mapperModel ) ; String createTimestampLdapAttrName = activeDirectory ? "whenCreated" : LDAPConstants . CREATE_TIMESTAMP ; String modifyTimestampLdapAttrName = activeDirectory ? "whenChanged" : LDAPConstants . MODIFY_TIMESTAMP ; mapperModel = KeycloakModelUtils . createUserFederationMapperModel ( "creation date" , newProviderModel . getId ( ) , UserAttributeLDAPFederationMapperFactory . PROVIDER_ID , UserAttributeLDAPFederationMapper . USER_MODEL_ATTRIBUTE , LDAPConstants . CREATE_TIMESTAMP , UserAttributeLDAPFederationMapper . LDAP_ATTRIBUTE , createTimestampLdapAttrName , UserAttributeLDAPFederationMapper . READ_ONLY , "true" ) ; realm . addUserFederationMapper ( mapperModel ) ; mapperModel = KeycloakModelUtils . createUserFederationMapperModel ( "modify date" , newProviderModel . getId ( ) , UserAttributeLDAPFederationMapperFactory . PROVIDER_ID , UserAttributeLDAPFederationMapper . USER_MODEL_ATTRIBUTE , LDAPConstants . MODIFY_TIMESTAMP , UserAttributeLDAPFederationMapper . LDAP_ATTRIBUTE , modifyTimestampLdapAttrName , UserAttributeLDAPFederationMapper . READ_ONLY , "true" ) ; realm . addUserFederationMapper ( mapperModel ) ; }
Best effort to create appropriate mappers according to our LDAP config
Best effort to create appropriate mappers according to our LDAP config
// CN is typically used as RDN for Active Directory deployments
 // For AD deployments with "cn" as username, we will map "givenName" to first name
 // For AD deployments with "sAMAccountName" as username and writable, we need to map "cn" as username as well (this is needed so we can register new users from KC into LDAP) and we will map "givenName" to first name.
 // For read-only LDAP, we map "cn" as full name
 // map createTimeStamp as read-only
 // map modifyTimeStamp as read-only
