Support » Plugin: wpDirAuth » Custom field

  • dzielinger

    (@dzielinger)


    Hi,
    First thanks for your plugin, it works great.
    I would like to use the plugin for an intranet and il would like to get the phone value from AD to create the value in budypress.
    Is this possible to do ?

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Paul Gilzow

    (@gilzow)

    Currently, no. but that doesn’t mean we can’t add something. I could add a filter after the user creation process that you could hook into and grab the phone number.

    the filter could pass you the UserID, and the full record returned for the user from ldap_get_entries(). Have you ever dealt with ldap records in PHP?

    Plugin Author Paul Gilzow

    (@gilzow)

    just following up again. Would providing you with a filter you could hook into work for your situation? If so, I’ll send you a version you can test. if it works, i’ll incorporate those changes into the next version of the plugin.

    Hi thanks for your fast answer !
    No i never dealed with ldap records in PHP.
    The field i want to get is the field “telephoneNumber”.
    I will be happy to test the new version and to tell you if it works.

    Plugin Author Paul Gilzow

    (@gilzow)

    Sorry it took a little bit. There were a couple of sections I wanted to refactor to make all of this work more smoothly. If you’ve never dealt with ldap search results, it can be a bit… frustrating. It’s a multi-dimensional nested array. I’ve included a sample of what it looks like at the bottom of this post. The PHP Documentation will help as well.

    Beta version for you to test: https://gist.github.com/gilzow/a051e5ace829c9b1e99d37d7f25a800c

    There are now 2 actions and 1 filter you can hook into:

    Filter
    wpdirauth_ldapreturnkeys
    Fires before making an ldap_search. Will pass your function a single argument: an array of extra keys that should be included in the ldap return. Your function should add to this array and then return the array.

    Example:

    add_filter('wpdirauth_ldapreturnkeys',function($aryKeys){
        $myKeys = array('department','telephoneNumber');
        return array_merge($aryKeys,$myKeys);
    },10,1);

    Actions:
    wpdirauth_usercreated
    Fires right after a new AD-authenticated user has been created in WordPress. This occurs either right after an ad-authed user has authenticated and Auto Register is enabled in the plugin, or when adding an AD-authenticated user from the Users panel. Will pass your function two parameters: WordPress User ID and an array of user details (see example below). One key in the array will be ldap_entry and will be the search result from the ldap search. Function does not need to return a value.

    Example:

    /**
     * Add an AD-authenticated user's department and phone number to their WordPress User meta data
     */
    add_action('wpdirauth_usercreated',function($intUserID,$aryUserDetails){
        if(isset($aryUserDetails['ldap_entry']['telephonenumber'][0]) && '' != $aryUserDetails['ldap_entry']['telephonenumber'][0] ){
            add_user_meta($intUserID,'telephone',$aryUserDetails['ldap_entry']['telephonenumber'][0]);
        }
    
        if(isset($aryUserDetails['ldap_entry']['department'][0]) && '' != $aryUserDetails['ldap_entry']['department'][0] ){
            add_user_meta($intUserID,'department',$aryUserDetails['ldap_entry']['department'][0]);
        }
    },10,2);

    wpdirauth_userauthenticated
    Fires right after an ad-authenticated user has authenticated. Will pass your function two parameters: WordPress User ID, and an array of user details (see example below). One key in the array will be ldap_entry and will be the search result from the ldap search. Function does not need to return a value.

    Example:

    /**
     * Update a user's phone number if they've updated it in AD, or add it here if they've added it in AD 
     */
    add_action('wpdirauth_userauthenticated',function($intUserID,$aryUserDetails){
        $strOldPhone = get_user_meta($intUserID,'telephone',true);
        if(isset($aryUserDetails['ldap_entry']['telephonenumber'][0]) && '' != $aryUserDetails['ldap_entry']['telephonenumber'][0] ){
            if($strOldPhone != $aryUserDetails['ldap_entry']['telephonenumber'][0]){
                update_user_meta($intUserID,'telephone',$aryUserDetails['ldap_entry']['telephonenumber'][0],$strOldPhone);   
            }
        }
    },10,2);

    Example of user details array

    array (
      'email' => 'GilzowP@domain.tld',
      'last_name' => 'Gilzow',
      'first_name' => 'Paul',
      'ldap_entry' => 
      array (
        'sn' => 
        array (
          'count' => 1,
          0 => 'Gilzow',
        ),
        0 => 'sn',
        'telephonenumber' => 
        array (
          'count' => 1,
          0 => '(555) 555-1212',
        ),
        1 => 'telephonenumber',
        'givenname' => 
        array (
          'count' => 1,
          0 => 'Paul',
        ),
        2 => 'givenname',
        'department' => 
        array (
          'count' => 1,
          0 => 'Some Department',
        ),
        3 => 'department',
        'mail' => 
        array (
          'count' => 1,
          0 => 'GilzowP@domain.tld',
        ),
        4 => 'mail',
        'count' => 5,
        'dn' => 'CN=Gilzow\\, Paul,CN=Users,DC=domain,DC=tld',
      ),
    )
    • This reply was modified 7 months, 3 weeks ago by  Paul Gilzow. Reason: left my email address in there. doh
    Plugin Author Paul Gilzow

    (@gilzow)

    WordPress forums keep eating my replies. Let’s break this into a couple of replies.

    Sorry it took a little bit. There were a couple of sections I wanted to refactor to make all of this work more smoothly. If you’ve never dealt with ldap search results, it can be a bit… frustrating. It’s a multi-dimensional nested array. I’ve included a sample of what it looks like at the bottom of this post. The PHP Documentation will help as well.

    Beta version for you to test: https://gist.github.com/gilzow/a051e5ace829c9b1e99d37d7f25a800c

    Plugin Author Paul Gilzow

    (@gilzow)

    There are now 2 actions and 1 filter you can hook into:

    Filter
    wpdirauth_ldapreturnkeys
    Fires before making an ldap_search. Will pass your function a single argument: an array of extra keys that should be included in the ldap return. Your function should add to this array and then return the array.

    Example:

    add_filter('wpdirauth_ldapreturnkeys',function($aryKeys){
        $myKeys = array('department','telephoneNumber');
        return array_merge($aryKeys,$myKeys);
    },10,1);
    Plugin Author Paul Gilzow

    (@gilzow)

    Actions:
    wpdirauth_usercreated
    Fires right after a new AD-authenticated user has been created in WordPress. This occurs either right after an ad-authed user has authenticated and Auto Register is enabled in the plugin, or when adding an AD-authenticated user from the Users panel. Will pass your function two parameters: WordPress User ID and an array of user details (see example below). One key in the array will be ldap_entry and will be the search result from the ldap search. Function does not need to return a value.

    Example:

    /**
     * Add an AD-authenticated user's department and phone number to their WordPress User meta data
     */
    add_action('wpdirauth_usercreated',function($intUserID,$aryUserDetails){
        if(isset($aryUserDetails['ldap_entry']['telephonenumber'][0]) && '' != $aryUserDetails['ldap_entry']['telephonenumber'][0] ){
            add_user_meta($intUserID,'telephone',$aryUserDetails['ldap_entry']['telephonenumber'][0]);
        }
    
        if(isset($aryUserDetails['ldap_entry']['department'][0]) && '' != $aryUserDetails['ldap_entry']['department'][0] ){
            add_user_meta($intUserID,'department',$aryUserDetails['ldap_entry']['department'][0]);
        }
    },10,2);

    wpdirauth_userauthenticated
    Fires right after an ad-authenticated user has authenticated. Will pass your function two parameters: WordPress User ID, and an array of user details (see example below). One key in the array will be ldap_entry and will be the search result from the ldap search. Function does not need to return a value.

    Example:

    /**
     * Update a user's phone number if they've updated it in AD, or add it here if they've added it in AD 
     */
    add_action('wpdirauth_userauthenticated',function($intUserID,$aryUserDetails){
        $strOldPhone = get_user_meta($intUserID,'telephone',true);
        if(isset($aryUserDetails['ldap_entry']['telephonenumber'][0]) && '' != $aryUserDetails['ldap_entry']['telephonenumber'][0] ){
            if($strOldPhone != $aryUserDetails['ldap_entry']['telephonenumber'][0]){
                update_user_meta($intUserID,'telephone',$aryUserDetails['ldap_entry']['telephonenumber'][0],$strOldPhone);   
            }
        }
    },10,2);
    Plugin Author Paul Gilzow

    (@gilzow)

    Example of user details array

    array (
      'email' => 'GilzowP@domain.tld',
      'last_name' => 'Gilzow',
      'first_name' => 'Paul',
      'ldap_entry' => 
      array (
        'sn' => 
        array (
          'count' => 1,
          0 => 'Gilzow',
        ),
        0 => 'sn',
        'telephonenumber' => 
        array (
          'count' => 1,
          0 => '(555) 555-1212',
        ),
        1 => 'telephonenumber',
        'givenname' => 
        array (
          'count' => 1,
          0 => 'Paul',
        ),
        2 => 'givenname',
        'department' => 
        array (
          'count' => 1,
          0 => 'Some Department',
        ),
        3 => 'department',
        'mail' => 
        array (
          'count' => 1,
          0 => 'GilzowP@domain.tld’,
        ),
        4 => 'mail',
        'count' => 5,
        'dn' => 'CN=Gilzow\\, Paul,CN=Users,DC=domain,DC=tld',
      ),
    )

    Nice additions Paul – could be very useful 🙂

    Plugin Author Paul Gilzow

    (@gilzow)

    thanks! i’m actually really excited about it as it gives me the ability to make plugins that extend wpDirAuth’s functionality without having to build in excess complexity into wpDirAuth.

    Thanks, i will test that as soon as i have a little time.

    Plugin Author Paul Gilzow

    (@gilzow)

    another updated version if you can test. Same functionality as the version you are testing, but i’ve also added ability to change cookie expiration times as well as a couple of other bug fixes.

    https://gist.github.com/gilzow/febf6c36fd46dd24dba7b2bbbbd7c375

    • This reply was modified 7 months, 2 weeks ago by  Paul Gilzow. Reason: actually include the link
    Plugin Author Paul Gilzow

    (@gilzow)

    version 1.9.3 is now out with all of the above hooks (actions+filter) baked in. If you can, test and let me know if this addresses your issue.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Custom field’ is closed to new replies.