Add a way for packagers to override some installation details
[lhc/web/wiklou.git] / includes / ExternalUser.php
index a5f9322..9a01deb 100644 (file)
@@ -1,21 +1,30 @@
 <?php
+/**
+ * Authentication with a foreign database
+ *
+ * Copyright © 2009 Aryeh Gregor
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
 
-# Copyright (C) 2009 Aryeh Gregor
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-# http://www.gnu.org/copyleft/gpl.html
+/**
+ * @defgroup ExternalUser ExternalUser
+ */
 
 /**
  * A class intended to supplement, and perhaps eventually replace, AuthPlugin.
@@ -26,6 +35,8 @@
  * assumed to at least support the concept of a user id (possibly not an
  * integer), a user name (possibly not meeting MediaWiki's username
  * requirements), and a password.
+ *
+ * @ingroup ExternalUser
  */
 abstract class ExternalUser {
        protected function __construct() {}
@@ -43,8 +54,7 @@ abstract class ExternalUser {
                if ( is_null( $wgExternalAuthType ) ) {
                        return false;
                }
-               $class = "ExternalUser_$wgExternalAuthType";
-               $obj = new $class;
+               $obj = new $wgExternalAuthType;
                if ( !$obj->initFromName( $name ) ) {
                        return false;
                }
@@ -60,8 +70,7 @@ abstract class ExternalUser {
                if ( is_null( $wgExternalAuthType ) ) {
                        return false;
                }
-               $class = "ExternalUser_$wgExternalAuthType";
-               $obj = new $class;
+               $obj = new $wgExternalAuthType;
                if ( !$obj->initFromId( $id ) ) {
                        return false;
                }
@@ -69,17 +78,15 @@ abstract class ExternalUser {
        }
 
        /**
-        * @param $cookie string
         * @return mixed ExternalUser, or false on failure
         */
-       public static function newFromCookie( $cookie ) {
+       public static function newFromCookie() {
                global $wgExternalAuthType;
                if ( is_null( $wgExternalAuthType ) ) {
                        return false;
                }
-               $class = "ExternalUser_$wgExternalAuthType";
-               $obj = new $class;
-               if ( !$obj->initFromCookie( $cookie ) ) {
+               $obj = new $wgExternalAuthType;
+               if ( !$obj->initFromCookie() ) {
                        return false;
                }
                return $obj;
@@ -93,7 +100,7 @@ abstract class ExternalUser {
         * This is a wrapper around newFromId().
         *
         * @param $user User
-        * @return mixed ExternalUser or false
+        * @return ExternalUser|bool False on failure
         */
        public static function newFromUser( $user ) {
                global $wgExternalAuthType;
@@ -105,7 +112,7 @@ abstract class ExternalUser {
 
                $dbr = wfGetDB( DB_SLAVE );
                $id = $dbr->selectField( 'external_user', 'eu_external_id',
-                       array( 'eu_wiki_id' => $user->getId() ), __METHOD__ );
+                       array( 'eu_local_id' => $user->getId() ), __METHOD__ );
                if ( $id === false ) {
                        return false;
                }
@@ -134,18 +141,15 @@ abstract class ExternalUser {
        protected abstract function initFromId( $id );
 
        /**
-        * Given the user's cookie, initialize this object to the correct user if
-        * the cookie indicates that the user is logged into the external database.
-        * If successful, return true.  If the external database doesn't support
-        * cookie-based authentication, or if the cookies don't belong to a
-        * logged-in user, return false.
+        * Try to magically initialize the user from cookies or similar information
+        * so he or she can be logged in on just viewing the wiki.  If this is
+        * impossible to do, just return false.
         *
         * TODO: Actually use this.
         *
-        * @param $cookie string
         * @return bool Success?
         */
-       protected function initFromCookie( $cookie ) {
+       protected function initFromCookie() {
                return false;
        }
 
@@ -180,8 +184,7 @@ abstract class ExternalUser {
 
        /**
         * Is the given password valid for the external user?  The password is
-        * provided in plaintext, with whitespace stripped but not otherwise
-        * modified.
+        * provided in plaintext.
         *
         * @param $password string
         * @return bool
@@ -242,7 +245,7 @@ abstract class ExternalUser {
         * @param $pref string
         * @return mixed String or false
         */
-       public static function prefMessage( $pref ) {
+       public static function getPrefMessage( $pref ) {
                return false;
        }
 
@@ -277,12 +280,30 @@ abstract class ExternalUser {
         *
         * @param $id int user_id
         */
-       public final function link( $id ) {
+       public final function linkToLocal( $id ) {
                $dbw = wfGetDB( DB_MASTER );
                $dbw->replace( 'external_user',
-                       array( 'eu_wiki_id', 'eu_external_id' ),
-                       array( 'eu_wiki_id' => $id,
-                              'eu_external_id' => $this->getId() ),
-                   __METHOD__ );
+                       array( 'eu_local_id', 'eu_external_id' ),
+                       array( 'eu_local_id' => $id,
+                                  'eu_external_id' => $this->getId() ),
+                       __METHOD__ );
+       }
+       
+       /**
+        * Check whether this external user id is already linked with
+        * a local user.
+        * @return Mixed User if the account is linked, Null otherwise.
+        */
+       public final function getLocalUser(){
+               $dbr = wfGetDB( DB_SLAVE );
+               $row = $dbr->selectRow(
+                       'external_user',
+                       '*',
+                       array( 'eu_external_id' => $this->getId() )
+               );
+               return $row
+                       ? User::newFromId( $row->eu_local_id )
+                       : null;
        }
+       
 }