User Profile Fields update on Auth, plugin only

User Profile Fields update on Auth, plugin only

Matt Porritt發表於
Number of replies: 1

Hi,
I've been developing an authentication plugin that is essentially an extension of the LDAP auth plugin.  As part of this plugin I wanted to do 3 things, ALL without changing any core moodle code. That is I want the plugin to do everything, that way I can distribute it as a plugin not a patch.  The three things I wanted to do are.

1. Change the auth plugin config page (config.html) to divs not tables
2. Be able to map LDAP fields to ALL standard user profile fields (ICQ, MSN, SKYPE, etc)
3. Be able to map LDAP fields to the custom (already defined) user profile fields.

I've been able to accomplish 1 & 2 of the list above but only part of 3. 
The part of 3 I've done is: I can extract the defined custom user profile fields and display them on the plugins config page.  I can also get the mappings to save to the corresponding LDAP fields.  What I have not been able to figure out is how to update the Moodle database with the LDAP data, WITHOUT modifying \lib\moodlelib.php for the custom user profile fields.

The tracker MDL-16982 has given some clues as how I might accomplish this, but I can't figure out a way to have the code to update/add the custom user profile fields run at the correct point of execution from the plugins code without modifying moodlelib.php

Does anyone have any ideas how/if this can be accomplished?

 Thanks,

評比平均分數: -
In reply to Matt Porritt

Re: User Profile Fields update on Auth, plugin only

Olumuyiwa Taiwo發表於
Plugin developers的相片

Matt

I had to do something similar some time ago on Moodle 1.9.x, but I think the method I used may still be applicable in 2.x.

The "trick" lay in being able to tell your auth plugin's get_userinfo() function to ignore the custom user profile fields if it's been called from moodlelib.php, but include them if it's been called from within the plugin's auth.php. One way I found to do that check was to use PHP's debug_backtrace() function in the auth plugin's auth.php as follows:

        <snip>

        $search_attribs = array();

///////   START: Don't pass custom profile fields to moodlelib.php
        $calledbymoodlelib = true;
        $trace = debug_backtrace();
        $caller = $trace[1];
        if (isset($caller['class'])) { // caller doesn't have a class if it's a function in lib/moodlelib.php
            $calledbymoodlelib = false;
        }
        foreach ($attrmap as $key => $values) {
            if ($calledbymoodlelib) {
                // lose custom profile fields - moodlelib.php doesn't like them 傷心
                if (strstr($key, 'profile_field_')) {
                    unset($attrmap[$key]);
                }
            }
            ////// END: Don't pass custom profile fields to moodlelib.php

            if (!is_array($values)) {
                $values = array($values);
            }
            foreach ($values as $value) {
                if (!in_array($value, $search_attribs)) {
                    array_push($search_attribs, $value);
                }
            }
        }

        $user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);

        </snip>

I added the code between the ////// START and ////// END comments to get_userinfo() in auth.php.

Hope that helps.

Muyi