LDAP Feeds example usage

Last updated on
9 March 2017

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Introduction

Feeds is a general architecture for moving data where an importer consists of a fetcher, parser, and processor. Ldap Feeds supplies the fetcher and parser such that any processor can be used (node, user, taxonomy term, and any of the processors at: http://drupal.org/node/856644). Example use-cases:

  • Move course or faculty staff info into drupal nodes for directories.
    Rough Recipe: FeedsLdapQueryFetcher for ldap query, FeedsLdapEntryParser for parsing it into feeds format, Node Processor for creating/synching nodes.
  • Synch ldap attributes with user profile data
    Rough Recipe: FeedsDrupalUserLdapEntryFetcher for gettling ldap data, FeedsLdapEntryParser for parsing it into feeds format, User Processor for creating/synching with drupal users.
  • Provision Drupal Users with ldap query

Detailed example

Using LDAP Feeds "LDAP Query Fetcher" to bring in user data from LDAP to create New Drupal Users.

  1. Configure the ldap server. Make sure fields related to ldap and drupal users are filled out.
  2. Enable the following modules: ldap_query, ldap_feeds, feeds, job_scheduler
  3. Add user account fields (admin/config/people/accounts/fields)
            [Example]
               First Name Field:  First Name field_fname Text  Text field
               Last Name Field:  Last Name  field_lname Text  Text field
            [/Example]
          

    Include other fields as per your needs further ahead.

  4. Create new LDAP Query : admin/config/people/ldap/query/add
            [Example]    
              Machine name for this query configuration : ecm_users (Give Unique Name)
              Name : ECM Users (Human readable name for the query)
              LDAP Server used for query : select your LDAP server
              Enabled : checked
              Base DNs to search in query : CN=Users,DC=hogwarts,DC=com
              Filter : (&(objectClass=user)(memberOf=CN=give_specific_group_name_if_needed,CN=Users,DC=hogwarts,DC=com))
            [/Example]
          

    If you want all of users and not of specific group, you can skip section memberOf completely.
    Filter then becomes (&(objectClass=user))

            [Example] 
              Attributes to return : DN,SN,GIVENNAME,USERPRINCIPALNAME,MAILNICKNAME                    
            [/Example]        
          

    Feel free to add your needed Atrributes in here.
    "Save" this LDAP Query.

  5. Create new Feed importer: admin/structure/feeds/create
            [Example]
               Name: LDAP Data to User Data
               Machine-Readable name: ldap_data_to_user_data
               Description: admin/structure/feeds/ldap_data_to_user_data
            [/Example]        
          

    "Save" this Feed Importer.

  6. Configure Basic Settings: admin/structure/feeds/ldap_data_to_user_data/settings
            [Example]
              Name : LDAP Data to User Data
              Description : admin/structure/feeds/ldap_data_to_user_data
              Attach to content type: Use standalone form
              Periodic import : 15 min
              Import on Submission: unchecked
              Processed in background:  checked.
            [/Example]        
          

    "Save" this Basic Settings.

  7. Configure Fetcher: admin/structure/feeds/ldap_data_to_user_data/fetcher
    Set to "LDAP Query Fetcher". No further specific settings in here.
    "Save" this Fetcher.
  8. Fetcher Settings: admin/structure/feeds/ldap_data_to_user_data/settings/FeedsLdapQueryFetcher
    Select "LDAP Query" in here. In this case, "ECM Users"
    "Save" this Fetcher Settings.
  9. Configure Parser: admin/structure/feeds/ldap_data_to_user_data/parser
    Set to "LDAP Entry Parser for Feeds". No further specific settings in here.
    "Save" this Fetcher.
  10. Configure Processor: admin/structure/feeds/ldap_data_to_user_data/processor
    Set to "User processor". No further specific settings in here.
    "Save" this Processor.
  11. Configure User processor: admin/structure/feeds/ldap_data_to_user_data/settings/FeedsUserProcessor
          [Example]
            Insert new users : Checked
            Update existing users : Update existing users
            Text format : Plain Text
            Action to take when previously imported users are missing in the feed : Skip non-existent users
            Status : Active
            Additional roles : Select extra roles to assign to users upon import.
            Defuse e-mail addresses : Unchecked
          [/Example]      
        

    "Save" this User processor.

  12. Mapping for User processor : admin/structure/feeds/ldap_data_to_user_data/mapping
          [Example]
            Make a note, SOURCE are fields from LDAP  and TARGET are the fields from Drupal User Account
            SOURCE              TARGET                      TARGET CONFIGURATION 
            MAILNICKNAME        User name (name)            Used as unique.
            USERPRINCIPALNAME   Email address (mail)        Used as unique.
            GIVENNAME           First Name (field_fname)
            SN                  Last Name (field_lname)
          [/Example]      
        

    "Save" this User Mapping.

One can test the LDAP Query @ admin/config/people/ldap/query under OPERATIONS

So far, we've created a LDAP QUERY now. Lets see how can we schedule this query to execute periodically to import
users from LDAP to Drupal.

Assuming we have created a module named "import_data".

A. Implement hook_cronapi()

        function import_data_cronapi($op, $job = NULL) {
          return array(
            'import_data_cronjob_1' => array(
              'title' => 'Import LDAP Users',
              'callback' => 'import_data_ldap_users_callback',
              'enabled' => TRUE,
              'scheduler' => array(
                'name' => 'crontab',
                'crontab' => array(
                  'rules' => array('0+@ */12 * * *'), // Schedule for import once in 12 hours
                ),
              ),
            ),
          );
        }
      

B. Write Function to actually import

      function import_data_ldap_users_callback($job) {
        $vars = array();
        if (function_exists('feeds_source')){
          while (FEEDS_BATCH_COMPLETE != feeds_source('ldap_data_to_user_data')->import());
          watchdog('Cron LDAP Users Import', t(' LDAP Users Imported Successfully.'), $vars, WATCHDOG_INFO,NULL);
        } else {
          watchdog('Cron LDAP Users Import', t('Function : feeds_source not found.'), $vars, WATCHDOG_ERROR,NULL);
        }
      }       
      

Done ! You have successfully scheduled a cron to import LDAP Users into Drupal

Caveats:
- feeds User Processor is a little fuzzy on update behavior for users. See issue #1300764
- mapping must include either "name" and "mail" fields as "Unique Targets" to affect existing users. GUIDs such as dn used as unique identifiers won't affect existing users.

Help improve this page

Page status: No known problems

You can: