~ubuntu-branches/ubuntu/saucy/mediawiki-extensions/saucy

« back to all changes in this revision

Viewing changes to dist/mediawiki-extensions-ldapauth/usr/share/mediawiki-extensions/ldapauth/LdapAutoAuthentication.php

  • Committer: Bazaar Package Importer
  • Author(s): Romain Beauxis
  • Date: 2010-05-04 15:13:35 UTC
  • mfrom: (0.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100504151335-54qeucg3ec108q28
Tags: 2.2
* Added Replaces:/Conflicts: to allow a proper upgrade.
Closes: #580066
* Fixed package descriptions.
Closes: #579667
* Patched mediawiki-extensions-fckeditor to make it work with
  php 5.3. The fix may not be perfect but at least it work.
  Not closing the bug (#579822) for now..

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
class LdapAutoAuthentication {
 
4
 
 
5
        /**
 
6
         * Does the web server authentication piece of the LDAP plugin.
 
7
         *
 
8
         * @access public
 
9
         */
 
10
        static function Authenticate( $user, &$result ) {
 
11
                global $wgUser;
 
12
                global $wgAuth;
 
13
                global $wgLDAPAutoAuthUsername;
 
14
                global $wgVersion;
 
15
        
 
16
                $wgAuth->printDebug( "Entering AutoAuthentication.", NONSENSITIVE );
 
17
 
 
18
                if ( version_compare( $wgVersion, '1.14.0', '<' ) ) {
 
19
                        //The following section is a hack to determine whether or not
 
20
                        //the user is logged in. We need a core fix to make this simpler.
 
21
                        if ( isset( $_SESSION['wsUserID'] ) ) {
 
22
                                $user->setID( $_SESSION['wsUserID'] );
 
23
                                if ( $user->loadFromId() ) {
 
24
                                        if ( $_SESSION['wsToken'] == $user->mToken  && $_SESSION['wsUserName'] == $user->mName ) {
 
25
                                                $wgAuth->printDebug( "User is already logged in.", NONSENSITIVE );
 
26
                                                $result = true;
 
27
                                                return true;
 
28
                                        } else {
 
29
                                                $user->loadDefaults();
 
30
                                        }
 
31
                                }
 
32
                        }
 
33
                } else {
 
34
                        if ( $user->isLoggedIn() ) {
 
35
                                $wgAuth->printDebug( "User is already logged in.", NONSENSITIVE );
 
36
                                return true;
 
37
                        }
 
38
                }
 
39
        
 
40
                $wgAuth->printDebug( "User isn't logged in, calling setup.", NONSENSITIVE );
 
41
        
 
42
                //Let regular authentication plugins configure themselves for auto
 
43
                //authentication chaining
 
44
                $wgAuth->autoAuthSetup();
 
45
        
 
46
                $wgAuth->printDebug( "Calling authenticate with username ($wgLDAPAutoAuthUsername).", NONSENSITIVE );
 
47
                //The user hasn't already been authenticated, let's check them
 
48
                $authenticated = $wgAuth->authenticate( $wgLDAPAutoAuthUsername );
 
49
                if ( !$authenticated ) {
 
50
                        //If the user doesn't exist in LDAP, there isn't much reason to
 
51
                        //go any further.
 
52
                        $wgAuth->printDebug("User wasn't found in LDAP, exiting.", NONSENSITIVE );
 
53
                        return false;
 
54
                }
 
55
        
 
56
                //We need the username that MediaWiki will always use, *not* the one we
 
57
                //get from LDAP.
 
58
                $mungedUsername = $wgAuth->getCanonicalName( $wgLDAPAutoAuthUsername );
 
59
        
 
60
                $wgAuth->printDebug( "User exists in LDAP; finding the user by name ($mungedUsername) in MediaWiki.", NONSENSITIVE );
 
61
        
 
62
                $localId = User::idFromName( $mungedUsername );
 
63
                $wgAuth->printDebug( "Got id ($localId).", NONSENSITIVE );
 
64
        
 
65
                //Is the user already in the database?
 
66
                if( !$localId ) {
 
67
                        $userAdded = self::attemptAddUser( $user, $mungedUsername );
 
68
                        if ( !$userAdded ) {
 
69
                                $result = false;
 
70
                                return false;
 
71
                        }
 
72
                } else {
 
73
                        $wgAuth->printDebug( "User exists in local database, logging in.", NONSENSITIVE );
 
74
                        $user->setID( $localId );
 
75
                        $user->loadFromId();
 
76
                        $user->setCookies();
 
77
                        $wgAuth->updateUser( $user );
 
78
                        wfSetupSession();
 
79
                        $result = true;
 
80
                }
 
81
 
 
82
                return true;
 
83
        }
 
84
 
 
85
        static function attemptAddUser( $user, $mungedUsername ) {
 
86
                global $wgAuth;
 
87
 
 
88
                if ( !$wgAuth->autoCreate() ) {
 
89
                        $wgAuth->printDebug( "Cannot automatically create accounts.", NONSENSITIVE );
 
90
                        return false;
 
91
                }
 
92
 
 
93
                $wgAuth->printDebug( "User does not exist in local database; creating.", NONSENSITIVE );
 
94
        
 
95
                // Checks passed, create the user
 
96
                $user->loadDefaults( $mungedUsername );
 
97
                $user->addToDatabase();
 
98
 
 
99
                $wgAuth->initUser( $user, true );
 
100
                $user->setCookies();
 
101
                wfSetupSession();
 
102
 
 
103
                # Update user count
 
104
                $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
 
105
                $ssUpdate->doUpdate();
 
106
 
 
107
                # Notify hooks (e.g. Newuserlog)
 
108
                wfRunHooks( 'AuthPluginAutoCreate', array( $user ) );
 
109
 
 
110
                return true;
 
111
        }
 
112
 
 
113
        /* No logout link in MW */
 
114
        static function NoLogout( &$personal_urls, $title ) {
 
115
                global $wgAuth;
 
116
                $wgAuth->printDebug( "Entering NoLogout.", NONSENSITIVE );
 
117
        
 
118
                $personal_urls['logout'] = null;
 
119
        
 
120
                return true;
 
121
        }
 
122
}