MediaWiki  REL1_20
ExternalUser.php
Go to the documentation of this file.
00001 <?php
00041 abstract class ExternalUser {
00042         protected function __construct() {}
00043 
00052         public static function newFromName( $name ) {
00053                 global $wgExternalAuthType;
00054                 if ( is_null( $wgExternalAuthType ) ) {
00055                         return false;
00056                 }
00057                 $obj = new $wgExternalAuthType;
00058                 if ( !$obj->initFromName( $name ) ) {
00059                         return false;
00060                 }
00061                 return $obj;
00062         }
00063 
00068         public static function newFromId( $id ) {
00069                 global $wgExternalAuthType;
00070                 if ( is_null( $wgExternalAuthType ) ) {
00071                         return false;
00072                 }
00073                 $obj = new $wgExternalAuthType;
00074                 if ( !$obj->initFromId( $id ) ) {
00075                         return false;
00076                 }
00077                 return $obj;
00078         }
00079 
00083         public static function newFromCookie() {
00084                 global $wgExternalAuthType;
00085                 if ( is_null( $wgExternalAuthType ) ) {
00086                         return false;
00087                 }
00088                 $obj = new $wgExternalAuthType;
00089                 if ( !$obj->initFromCookie() ) {
00090                         return false;
00091                 }
00092                 return $obj;
00093         }
00094 
00105         public static function newFromUser( $user ) {
00106                 global $wgExternalAuthType;
00107                 if ( is_null( $wgExternalAuthType ) ) {
00108                         # Short-circuit to avoid database query in common case so no one
00109                         # kills me
00110                         return false;
00111                 }
00112 
00113                 $dbr = wfGetDB( DB_SLAVE );
00114                 $id = $dbr->selectField( 'external_user', 'eu_external_id',
00115                         array( 'eu_local_id' => $user->getId() ), __METHOD__ );
00116                 if ( $id === false ) {
00117                         return false;
00118                 }
00119                 return self::newFromId( $id );
00120         }
00121 
00131         protected abstract function initFromName( $name );
00132 
00141         protected abstract function initFromId( $id );
00142 
00152         protected function initFromCookie() {
00153                 return false;
00154         }
00155 
00171         abstract public function getId();
00172 
00183         abstract public function getName();
00184 
00192         abstract public function authenticate( $password );
00193 
00217         public function getPref( $pref ) {
00218                 return null;
00219         }
00220 
00231         public function getGroups() {
00232                 return array();
00233         }
00234 
00248         public static function getPrefMessage( $pref ) {
00249                 return false;
00250         }
00251 
00269         public static function setPref( $key, $value ) {
00270                 return false;
00271         }
00272 
00283         public final function linkToLocal( $id ) {
00284                 $dbw = wfGetDB( DB_MASTER );
00285                 $dbw->replace( 'external_user',
00286                         array( 'eu_local_id', 'eu_external_id' ),
00287                         array( 'eu_local_id' => $id,
00288                                    'eu_external_id' => $this->getId() ),
00289                         __METHOD__ );
00290         }
00291         
00297         public final function getLocalUser(){
00298                 $dbr = wfGetDB( DB_SLAVE );
00299                 $row = $dbr->selectRow(
00300                         'external_user',
00301                         '*',
00302                         array( 'eu_external_id' => $this->getId() )
00303                 );
00304                 return $row
00305                         ? User::newFromId( $row->eu_local_id )
00306                         : null;
00307         }
00308         
00309 }