How to Authenticate Users From Moodle Into WordPress MU

Introduction

As I was exploring which social networks to use next year, I tried out WordPress MU with Buddypress to complement Moodle, my course management system, on the advice of Christopher Rogers. Another teacher, Colin Matheson, and I both wanted to implement this but we also required a single-sign on, if possible, between the two so that we could have an easier time managing users and it would be less that the kids have to remember. After much research and experimenting, I figured out a solution that works for me. It may or may not work for your particular requirements.

What I was able to do is relatively simple. I set it so that I create student accounts in Moodle and disabled user registration. This will keep the course to only my students. When student accounts are created in Moodle, identical accounts are created in WPMU. Students accounts in WPMU are created as subscribers and they can then choose what to call their blogs in WPMU. Creating an account does not create an account in WPMU because I want students to be able to make a blog at http://blogs.jasontbedell.com/[title] as opposed to http://blogs.jasontbedell.com/[StudentName]. Account information is only editable in Moodle. I currently take the first name, last name, email, username, password, and description from Moodle and import it immediately into WPMU. It is not a true single-sign on since students have to sign on to both separately. I simply use the Moodle database and only create users there.

Step 1

You need to mirror your WordPress administrator account on Moodle because WPMU will not look at it’s own accounts once we’re done. So, if your WPMU username is Administrator and your password is securepassword, you nee to setup an administrator account on Moodle with the same credentials.

Step 2

You need either direct server access or ftp access to your Moodle installation. I use Filezilla on Windows and Linux. Cyberduck works great on Mac. Whatever you use, go to the main folder of your Moodle installation. Download or edit your config.php. Write down the following fields:

  • dbhost =
  • dbname =
  • dbuser =
  • dbpass =

You will need those fields for WPMU to gain access to Moodle’s database.

While you are in config.php, you need to turn off password salting. This is somewhat risky because you are removing an extra security layer, but I believe it should not be issue. There are 3 lines you need to edit. Find the line that begins with passwordsaltmain. There will be a very long, random text string. You need to edit out this line.

Beginning:

$CFG->passwordsaltmain = ‘xl&JnOK,~56tgdfyytd6/hhT.)u7uH,’;

End

/*

$CFG->passwordsaltmain = ‘xl&JnOK,~56tgdfyytd6/hhT.)u7uH,’;

*/

Now, you need to put that text sting in passwordsaltalt1 which should be the next line and make a new line called passwordsaltmain (Remember, you are editing php code. By adding */ and */ around a line, you basically make it invisible.) and leave it empty. So, the code should look like this in the end.

  • */
  • $CFG->passwordsaltmain = ‘xl&JnOK,~E;&SRi8NzpgHT.)uj0H,’;
  • */
  • $CFG->passwordsaltalt1 = ‘xl&JnOK,~E;&SRi8NzpgHT.)uj0H,’;
  • $CFG->passwordsaltmain = ”;

Save your config.php and upload it to your Moodle server.

Step 3

It is best if you are starting with clean installations of Moodle and WPMU because this step will take less work. Go to you PHPMyAdmin. If you are using a database other than MySQL, it will be slightly different. Go to the user table in your Moodle database, probably mdl_user. You need to take your administrator password, go to this website, and run it through their MD5 hash generator. Take the output and put it in place of the password field of the administrator account in the database. This will make sense in a few steps.

Step 4

We are done with Moodle for now. Log into your WordPress or WPMU installation. You need to install a plugin called External DB authentication.There’s more information here: http://www.ploofle.com/tag/ext_db_auth/. Download, install, and activate the plugin.

Step 5

Under Settings on the WPMU administration page, go to External DB Settings. You should have all the info you need written down. For the External Database source fields, you should be able to get all of them from PHPMyAdmin. Make sure you choose MD5 as the password encryption method. What I used was:

Final Thoughts

This works well for me. I would like to get password salting working again. There is an option to use other for the password encryption and provide php code. We need to set $password2. $password2 = $password ; was in as an example. I thought that the following code would work.

  • $password2 = md5($password);
  • $salt = ‘xl&JnOK,~E;&SRi8NzpgHT.)uj0H,’;
  • echo $password2;
  • echo $salt;

I also tried it without the echo statements to no avail. I get odd errors on the page about unexpected “=” on line 1. I would love some help if anyone has any ideas. Thank you.

  • http://moodlerific.org melissa

    Can you describe what this “looks” like?

    How do users get to their blogs dashboard? Do they sign into Moodle and then there is a link/block on Moodle which takes them to the blogs?

    Or is this purely for managing users in 1 spot? We use active directory for Moodle and WPMU already so users are managed through AD. Would this integration help me out in any way or have we just accomplished the same thing?

    Thanks!

  • http://jasontbedell.com Jason Bedell

    Melissa,
    Congrats on getting Active D to work with Moodle and WPMU. We’ve accomplished pretty much the same thing. Where you manage Moodle and WPMU users through your Active D, I manage my WPMU through my Moodle database.
    Right now, students still have to sign into both. I’m working on getting it setup like Mahara where students can sign into Moodle and their will be a “Blog” or “WPMU” button under networks that will take them straight to their dashboard, but I am really a novice at programming. I will have to wait until the network APIs for Moodle 2.0 are all nicely added to the Moodle Docs page.
    Thanks,
    Jason.

  • http://elearning.unipd.it/moodle/scipol/ Emil

    Hi Jason,

    I’m doing the same thing right now.
    I want to integrate moodle for the courses and wordpress for the homepage.
    Till now i’ve made the same thing, using wordpress plugin for external authentication in my moodle database.
    But this is not a sso. I’m working on it.
    I would like to contact you. We can help each other.
    Please send me a mail.
    emilpilafi@gmail.com

  • http://jasontbedell.com Jason Bedell

    Emil,
    I would love to get a true SSO working. I’m not sure I have the skills, but I’ll send you an email. Hopefully we can work it out. I’m jason ++at++ jasontbedell dot com.

  • http://moodlerific.org melissa

    Please update us on your progress! Let me know if I can help.

  • http://jasontbedell.com Jason Bedell

    Melissa,
    I will. I believe Emil above wants to work together to create a true SSO so that there will be integration under networks in Moodle, the same way that Mahara works. I am a VERY novice programmer, so if you want to help or can offer any guidance, that would be great.
    .-= Jason Bedell´s last blog ..Edible Book Contest =-.

  • http://communityhigh.net safia yonker

    I have worked extensively coding php for wordpress and have just stumbled upon moodle. I would really like to get the two working together as well. I happy to offer my services. safiayonker@gmail.com

  • http://www.hylson.com Hylson

    I used this:

    $password2 = md5($password.’salt_phrase_in_your_config’);

    It works!

  • Dinesh

    Hi,

    I am running a wordpress blog and moodle, want to implement SSO. I am trying to manage my WPMU through my Moodle database. I followed all given steps. Right now, admin is able to sign in ,but other users still not able to login in wordpress through my Moodle database.

    Kindly Advice,
    Thanks in advance

  • Pingback: How to implement SSO with moodle and wordpress blog? | deepinphp.com

  • http://isrokids.com/wp/ David Rajah

    I want to integrate moodle for the courses and wordpress for the homepage. Can i use the wordpress plugin for external authentication in my wordpress database. any Tricks Kindly Reply here or send a Mail to davidrajah.s@gmail.com
    David Rajah´s last blog post ..David Rajah wrote a new blog post- Planet Pursuit Game

  • Andre Kruger

    Hi Jason, have you managed to implement the SSO between WordPress and Moodle, I’m also very interested to get this working. Would appreciate your input.

  • http://jasontbedell.com Jason Bedell

    Andre,

    I haven’t looked at this in a while; still using WordPress but not Moodle at the moment. I was able to authenticate both applications with a single database, but it isn’t true SSO as they still have to login to both (even though it is the same username and password).

  • Andre Kruger

    Thanks Jason. I will keep looking around and update this post if I manage to get it right.

  • http://twitter.com/ckiyan Carlos Kiyan Tsunami

    Hi Jason, I had configured the config.php.(tested with moodle 2.3.1, wordpress 3.4.1 as shown:
    ++++++++++++++++++++++++++

    dbtype = ‘mysqli’;
    $CFG->dblibrary = ‘native’;
    $CFG->dbhost = ‘localhost’;
    $CFG->dbname = ‘moodle’;
    $CFG->dbuser = ‘root’;
    $CFG->dbpass = ‘root’;
    $CFG->prefix = ‘mdl_’;
    $CFG->dboptions = array (
    ‘dbpersist’ => 0,
    ‘dbsocket’ => 0,
    );

    $CFG->wwwroot = ‘http://localhost:8888/moodle’;
    $CFG->dataroot = ‘/Applications/MAMP/moodledata’;
    $CFG->admin = ‘admin’;

    $CFG->directorypermissions = 0777;

    $CFG->passwordsaltmain = ‘xg,y}lQWkcy+WY%BXq-..NG2′;

    require_once(dirname(__FILE__) . ‘/lib/setup.php’);

    dbhost =
    dbname =
    dbuser =
    dbpass =

    */
    $CFG->passwordsaltmain = ‘xl&JnOK,~E;&SRi8NzpgHT.)uj0H,’;
    */
    $CFG->passwordsaltalt1 = ‘xl&JnOK,~E;&SRi8NzpgHT.)uj0H,’;
    $CFG->passwordsaltmain = “;

    +++++++++++++++++++
    1)what is wrong with the config.php code?

    Then i follow all the steps that you were mentioning above. I tried to login at wordpress and i got this message “ERROR: Invalid password”

    2)Do you know how can i solve this problem

    Thanks in advance