00001 <?php
00002
00003 # Copyright (C) 2009 Aryeh Gregor
00004 #
00005 # This program is free software; you can redistribute it and/or modify
00006 # it under the terms of the GNU General Public License as published by
00007 # the Free Software Foundation; either version 2 of the License, or
00008 # (at your option) any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 # GNU General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program; if not, write to the Free Software Foundation, Inc.,
00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 # http://www.gnu.org/copyleft/gpl.html
00019
00036 abstract class ExternalUser {
00037 protected function __construct() {}
00038
00047 public static function newFromName( $name ) {
00048 global $wgExternalAuthType;
00049 if ( is_null( $wgExternalAuthType ) ) {
00050 return false;
00051 }
00052 $obj = new $wgExternalAuthType;
00053 if ( !$obj->initFromName( $name ) ) {
00054 return false;
00055 }
00056 return $obj;
00057 }
00058
00063 public static function newFromId( $id ) {
00064 global $wgExternalAuthType;
00065 if ( is_null( $wgExternalAuthType ) ) {
00066 return false;
00067 }
00068 $obj = new $wgExternalAuthType;
00069 if ( !$obj->initFromId( $id ) ) {
00070 return false;
00071 }
00072 return $obj;
00073 }
00074
00078 public static function newFromCookie() {
00079 global $wgExternalAuthType;
00080 if ( is_null( $wgExternalAuthType ) ) {
00081 return false;
00082 }
00083 $obj = new $wgExternalAuthType;
00084 if ( !$obj->initFromCookie() ) {
00085 return false;
00086 }
00087 return $obj;
00088 }
00089
00100 public static function newFromUser( $user ) {
00101 global $wgExternalAuthType;
00102 if ( is_null( $wgExternalAuthType ) ) {
00103 # Short-circuit to avoid database query in common case so no one
00104 # kills me
00105 return false;
00106 }
00107
00108 $dbr = wfGetDB( DB_SLAVE );
00109 $id = $dbr->selectField( 'external_user', 'eu_external_id',
00110 array( 'eu_local_id' => $user->getId() ), __METHOD__ );
00111 if ( $id === false ) {
00112 return false;
00113 }
00114 return self::newFromId( $id );
00115 }
00116
00126 protected abstract function initFromName( $name );
00127
00136 protected abstract function initFromId( $id );
00137
00147 protected function initFromCookie() {
00148 return false;
00149 }
00150
00166 abstract public function getId();
00167
00178 abstract public function getName();
00179
00187 abstract public function authenticate( $password );
00188
00212 public function getPref( $pref ) {
00213 return null;
00214 }
00215
00226 public function getGroups() {
00227 return array();
00228 }
00229
00243 public static function getPrefMessage( $pref ) {
00244 return false;
00245 }
00246
00264 public static function setPref( $key, $value ) {
00265 return false;
00266 }
00267
00278 public final function linkToLocal( $id ) {
00279 $dbw = wfGetDB( DB_MASTER );
00280 $dbw->replace( 'external_user',
00281 array( 'eu_local_id', 'eu_external_id' ),
00282 array( 'eu_local_id' => $id,
00283 'eu_external_id' => $this->getId() ),
00284 __METHOD__ );
00285 }
00286
00292 public final function getLocalUser(){
00293 $dbr = wfGetDb( DB_SLAVE );
00294 $row = $dbr->selectRow(
00295 'external_user',
00296 '*',
00297 array( 'eu_external_id' => $this->getId() )
00298 );
00299 return $row
00300 ? User::newFromId( $row->eu_local_id )
00301 : null;
00302 }
00303
00304 }