MediaWiki  REL1_19
ExternalUser.php
Go to the documentation of this file.
00001 <?php
00039 abstract class ExternalUser {
00040         protected function __construct() {}
00041 
00050         public static function newFromName( $name ) {
00051                 global $wgExternalAuthType;
00052                 if ( is_null( $wgExternalAuthType ) ) {
00053                         return false;
00054                 }
00055                 $obj = new $wgExternalAuthType;
00056                 if ( !$obj->initFromName( $name ) ) {
00057                         return false;
00058                 }
00059                 return $obj;
00060         }
00061 
00066         public static function newFromId( $id ) {
00067                 global $wgExternalAuthType;
00068                 if ( is_null( $wgExternalAuthType ) ) {
00069                         return false;
00070                 }
00071                 $obj = new $wgExternalAuthType;
00072                 if ( !$obj->initFromId( $id ) ) {
00073                         return false;
00074                 }
00075                 return $obj;
00076         }
00077 
00081         public static function newFromCookie() {
00082                 global $wgExternalAuthType;
00083                 if ( is_null( $wgExternalAuthType ) ) {
00084                         return false;
00085                 }
00086                 $obj = new $wgExternalAuthType;
00087                 if ( !$obj->initFromCookie() ) {
00088                         return false;
00089                 }
00090                 return $obj;
00091         }
00092 
00103         public static function newFromUser( $user ) {
00104                 global $wgExternalAuthType;
00105                 if ( is_null( $wgExternalAuthType ) ) {
00106                         # Short-circuit to avoid database query in common case so no one
00107                         # kills me
00108                         return false;
00109                 }
00110 
00111                 $dbr = wfGetDB( DB_SLAVE );
00112                 $id = $dbr->selectField( 'external_user', 'eu_external_id',
00113                         array( 'eu_local_id' => $user->getId() ), __METHOD__ );
00114                 if ( $id === false ) {
00115                         return false;
00116                 }
00117                 return self::newFromId( $id );
00118         }
00119 
00129         protected abstract function initFromName( $name );
00130 
00139         protected abstract function initFromId( $id );
00140 
00150         protected function initFromCookie() {
00151                 return false;
00152         }
00153 
00169         abstract public function getId();
00170 
00181         abstract public function getName();
00182 
00190         abstract public function authenticate( $password );
00191 
00215         public function getPref( $pref ) {
00216                 return null;
00217         }
00218 
00229         public function getGroups() {
00230                 return array();
00231         }
00232 
00246         public static function getPrefMessage( $pref ) {
00247                 return false;
00248         }
00249 
00267         public static function setPref( $key, $value ) {
00268                 return false;
00269         }
00270 
00281         public final function linkToLocal( $id ) {
00282                 $dbw = wfGetDB( DB_MASTER );
00283                 $dbw->replace( 'external_user',
00284                         array( 'eu_local_id', 'eu_external_id' ),
00285                         array( 'eu_local_id' => $id,
00286                                    'eu_external_id' => $this->getId() ),
00287                         __METHOD__ );
00288         }
00289         
00295         public final function getLocalUser(){
00296                 $dbr = wfGetDB( DB_SLAVE );
00297                 $row = $dbr->selectRow(
00298                         'external_user',
00299                         '*',
00300                         array( 'eu_external_id' => $this->getId() )
00301                 );
00302                 return $row
00303                         ? User::newFromId( $row->eu_local_id )
00304                         : null;
00305         }
00306         
00307 }