Extension:AuthElgg

From MediaWiki.org

Jump to: navigation, search

       

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
Elgg Authentication

Release status: beta

Implementation  User identity
Description Used to authenticate against Elgg.
Author(s)  Computermacgyver
MediaWiki  1.9
License No license specified
Download see below

check usage (experimental)

Contents

[edit] Purpose

This extension shows how to edit the LocalSettings.php to authenticate against an Elgg (v. 1.5) installation on the same server.

[edit] Installation

Extension is in beta; proceed at your own risk!


  • Install your wiki on the same server as Elgg
  • Open the media wiki file "include/GlobalFunctions.php"
  • Add the line "wfRunHooks( 'BeforeSetupSession');" after "function wfSetupSession() {"
include/GlobalFunctions.php

/**
 * Initialise php session
 */
function wfSetupSession() {
	wfRunHooks( 'BeforeSetupSession');
	global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookieHttpOnly;

  • Open LocalSettings.php for the wiki and add the following at the bottom (you may also place in another file and include in LocalSettings.php)
 include_once("extensions/blacklist.php");
$wgBlacklist['*']['read'] = array("Special:UserLogin", "Special:UserLogout");
 
function fnElggAuth($user, &$result) {
    global $wgUser, $wgCookieExpiration;
 
    # Ensure we have a PHP session in place.
    //This fixes the get_loggedin_user() not defined error in previous release.
    if( session_id() == '' ) {
        wfSetupSession();
    }
 
    #Get Elgg User Object
        global $CONFIG;
        $result=1;//Abort normal Auth process
        $ElggUser=get_loggedin_user();//Elgg (sessions.php) call
        if (!(isset($ElggUser)) || !($ElggUser->guid > 0)){ //No Elgg User
            $user->loadDefaults();
            return false;
        }
 
    $username=$ElggUser->get("username");
 
    # If no account exists, autocreate one.
    # Check validity just in case.
    $name = User::getCanonicalName( strtolower( $username ), 'creatable' );
    if ( $name == false) {
        die("here1" . $name);
        $result = false;
        return true;
    }
    //$user = new User(); //!!!Do Not Call User!! After User() you cannot change mName?! //Thanks Skrypt for tracking down this error.
    $user->mName = $name;
    $userId = $user->idForName();
    if ( 0 == $userId ) {
        fnCreateElggAcct( $user, $ElggUser );    # see below
    } else {
        $user->setId( $userId );
    }
 
    # Finally, automagically login based on the corporate credentials
    $user->loadfromDatabase();
    $user->saveToCache();        # this also loads the user's group membership
    $wgUser = $user;
 
    # here, use fixed offset, but elsewhere try to align with actual corporate credentials expiry
    $wgCookieExpiration = 13 * 60 * 60;
    $wgUser->setCookies();
    $result = true;                # user is logged in
    return true;
}
 
function fnCreateElggAcct($WikiUser, $ElggUser) {
    $name=$ElggUser->get("name");
        $email=$ElggUser->get("email");
        $lang=$ElggUser->get("language");
        $guid=$ElggUser->get("guid");//New v.02
    $username=$WikiUser->mName;
 
    /**
     * Add a user to the database, return the user object
     *
     * @param $name \string Username to add
     * @param $params \type{\arrayof{\string}} Non-default parameters to save to the database:
     *   - password             The user's password. Password logins will be disabled if this is omitted.
     *   - newpassword          A temporary password mailed to the user
     *   - email                The user's email address
     *   - email_authenticated  The email authentication timestamp
     *   - real_name            The user's real name
     *   - options              An associative array of non-default options
     *   - token                Random authentication token. Do not set.
     *   - registration         Registration timestamp. Do not set.
     *
     * @return \type{User} A new User object, or null if the username already exists
     */
    $params = array(
            'email' => $email,
            'email_authenticated' => wfTimestampNow(),
            'real_name' => $name,
    );
    $WikiUser = User::createNew($username,$params);
 
}
 
function fnElggInit() {
    define('externalpage',true);
        require_once("/full/path/to/elgg/installation/engine/start.php");
        global $CONFIG;
    return true;
}
$wgHooks['UserLoadFromSession'][] = 'fnElggAuth';
$wgHooks['BeforeSetupSession'][] = 'fnElggInit';
  • Also in local settings adjust the default permissions for unregistered users as desired
e.g. 
Adding "$wgGroupPermissions['*']['edit'] = false; " will prevent unregistered users from editing pages
Adding "$wgGroupPermissions['*']['view'] = false;" will prevent unregistred users from viewing pages, etc.
  • Hide the login/logout links. Edit the css for your wiki skin (that file by default is skins/monobook/main.css)
li#pt-anonlogin, li#pt-logout, li#pt-login, li#pt-anonlogout
{
	display: none;
}

[edit] Troubleshooting

[edit] Source code

See above.