3 # Copyright (C) 2009 Aryeh Gregor
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * This class supports authentication against an external MediaWiki database,
22 * probably any version back to 1.5 or something. Example configuration:
24 * $wgExternalAuthType = 'ExternalUser_MediaWiki';
25 * $wgExternalAuthConf = array(
26 * 'DBtype' => 'mysql',
27 * 'DBserver' => 'localhost',
28 * 'DBname' => 'wikidb',
29 * 'DBuser' => 'quasit',
30 * 'DBpassword' => 'a5Cr:yf9u-6[{`g',
34 * All fields must be present. These mean the same things as $wgDBtype,
35 * $wgDBserver, etc. This implementation is quite crude; it could easily
36 * support multiple database servers, for instance, and memcached, and it
37 * probably has bugs. Kind of hard to reuse code when things might rely on who
38 * knows what configuration globals.
40 * If either wiki uses the UserComparePasswords hook, password authentication
41 * might fail unexpectedly unless they both do the exact same validation.
42 * There may be other corner cases like this where this will fail, but it
45 * @ingroup ExternalUser
47 class ExternalUser_MediaWiki
extends ExternalUser
{
50 protected function initFromName( $name ) {
51 # We might not need the 'usable' bit, but let's be safe. Theoretically
52 # this might return wrong results for old versions, but it's probably
54 $name = User
::getCanonicalName( $name, 'usable' );
56 if ( !is_string( $name ) ) {
60 return $this->initFromCond( array( 'user_name' => $name ) );
63 protected function initFromId( $id ) {
64 return $this->initFromCond( array( 'user_id' => $id ) );
67 private function initFromCond( $cond ) {
68 global $wgExternalAuthConf;
70 $class = 'Database' . $wgExternalAuthConf['DBtype'];
71 $this->mDb
= new $class(
72 $wgExternalAuthConf['DBserver'],
73 $wgExternalAuthConf['DBuser'],
74 $wgExternalAuthConf['DBpassword'],
75 $wgExternalAuthConf['DBname'],
78 $wgExternalAuthConf['DBprefix']
81 $row = $this->mDb
->selectRow(
84 'user_name', 'user_id', 'user_password', 'user_email',
85 'user_email_authenticated'
98 # TODO: Implement initFromCookie().
100 public function getId() {
101 return $this->mRow
->user_id
;
104 public function getName() {
105 return $this->mRow
->user_name
;
108 public function authenticate( $password ) {
109 # This might be wrong if anyone actually uses the UserComparePasswords hook
110 # (on either end), so don't use this if you those are incompatible.
111 return User
::comparePasswords( $this->mRow
->user_password
, $password,
112 $this->mRow
->user_id
);
115 public function getPref( $pref ) {
116 # FIXME: Return other prefs too. Lots of global-riddled code that does
118 if ( $pref === 'emailaddress'
119 && $this->row
->user_email_authenticated
!== null ) {
120 return $this->mRow
->user_email
;
125 public function getGroups() {
128 $res = $this->mDb
->select(
131 array( 'ug_user' => $this->mRow
->user_id
),
134 foreach ( $res as $row ) {
135 $groups[] = $row->ug_group
;
140 # TODO: Implement setPref().