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
00047 class ExternalUser_MediaWiki extends ExternalUser {
00048 private $mRow, $mDb;
00049
00050 protected function initFromName( $name ) {
00051 # We might not need the 'usable' bit, but let's be safe. Theoretically
00052 # this might return wrong results for old versions, but it's probably
00053 # good enough.
00054 $name = User::getCanonicalName( $name, 'usable' );
00055
00056 if ( !is_string( $name ) ) {
00057 return false;
00058 }
00059
00060 return $this->initFromCond( array( 'user_name' => $name ) );
00061 }
00062
00063 protected function initFromId( $id ) {
00064 return $this->initFromCond( array( 'user_id' => $id ) );
00065 }
00066
00067 private function initFromCond( $cond ) {
00068 global $wgExternalAuthConf;
00069
00070 $class = 'Database' . $wgExternalAuthConf['DBtype'];
00071 $this->mDb = new $class(
00072 $wgExternalAuthConf['DBserver'],
00073 $wgExternalAuthConf['DBuser'],
00074 $wgExternalAuthConf['DBpassword'],
00075 $wgExternalAuthConf['DBname'],
00076 false,
00077 0,
00078 $wgExternalAuthConf['DBprefix']
00079 );
00080
00081 $row = $this->mDb->selectRow(
00082 'user',
00083 array(
00084 'user_name', 'user_id', 'user_password', 'user_email',
00085 'user_email_authenticated'
00086 ),
00087 $cond,
00088 __METHOD__
00089 );
00090 if ( !$row ) {
00091 return false;
00092 }
00093 $this->mRow = $row;
00094
00095 return true;
00096 }
00097
00098 # TODO: Implement initFromCookie().
00099
00100 public function getId() {
00101 return $this->mRow->user_id;
00102 }
00103
00104 public function getName() {
00105 return $this->mRow->user_name;
00106 }
00107
00108 public function authenticate( $password ) {
00109 # This might be wrong if anyone actually uses the UserComparePasswords hook
00110 # (on either end), so don't use this if you those are incompatible.
00111 return User::comparePasswords( $this->mRow->user_password, $password,
00112 $this->mRow->user_id );
00113 }
00114
00115 public function getPref( $pref ) {
00116 # FIXME: Return other prefs too. Lots of global-riddled code that does
00117 # this normally.
00118 if ( $pref === 'emailaddress'
00119 && $this->row->user_email_authenticated !== null ) {
00120 return $this->mRow->user_email;
00121 }
00122 return null;
00123 }
00124
00125 public function getGroups() {
00126 # FIXME: Untested.
00127 $groups = array();
00128 $res = $this->mDb->select(
00129 'user_groups',
00130 'ug_group',
00131 array( 'ug_user' => $this->mRow->user_id ),
00132 __METHOD__
00133 );
00134 foreach ( $res as $row ) {
00135 $groups[] = $row->ug_group;
00136 }
00137 return $groups;
00138 }
00139
00140 # TODO: Implement setPref().
00141 }