84c8705a0ac3e93cd51f2746d9282c765c0e52a9
3 * External authentication with external MediaWiki database.
5 * Copyright © 2009 Aryeh Gregor
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
26 * This class supports authentication against an external MediaWiki database,
27 * probably any version back to 1.5 or something. Example configuration:
29 * $wgExternalAuthType = 'ExternalUser_MediaWiki';
30 * $wgExternalAuthConf = array(
31 * 'DBtype' => 'mysql',
32 * 'DBserver' => 'localhost',
33 * 'DBname' => 'wikidb',
34 * 'DBuser' => 'quasit',
35 * 'DBpassword' => 'a5Cr:yf9u-6[{`g',
39 * All fields must be present. These mean the same things as $wgDBtype,
40 * $wgDBserver, etc. This implementation is quite crude; it could easily
41 * support multiple database servers, for instance, and memcached, and it
42 * probably has bugs. Kind of hard to reuse code when things might rely on who
43 * knows what configuration globals.
45 * If either wiki uses the UserComparePasswords hook, password authentication
46 * might fail unexpectedly unless they both do the exact same validation.
47 * There may be other corner cases like this where this will fail, but it
50 * @ingroup ExternalUser
52 class ExternalUser_MediaWiki
extends ExternalUser
{
64 protected function initFromName( $name ) {
65 # We might not need the 'usable' bit, but let's be safe. Theoretically
66 # this might return wrong results for old versions, but it's probably
68 $name = User
::getCanonicalName( $name, 'usable' );
70 if ( !is_string( $name ) ) {
74 return $this->initFromCond( array( 'user_name' => $name ) );
81 protected function initFromId( $id ) {
82 return $this->initFromCond( array( 'user_id' => $id ) );
89 private function initFromCond( $cond ) {
90 global $wgExternalAuthConf;
92 $this->mDb
= DatabaseBase
::newFromType( $wgExternalAuthConf['DBtype'],
94 'host' => $wgExternalAuthConf['DBserver'],
95 'user' => $wgExternalAuthConf['DBuser'],
96 'password' => $wgExternalAuthConf['DBpassword'],
97 'dbname' => $wgExternalAuthConf['DBname'],
98 'tableprefix' => $wgExternalAuthConf['DBprefix'],
102 $row = $this->mDb
->selectRow(
105 'user_name', 'user_id', 'user_password', 'user_email',
106 'user_email_authenticated'
119 # TODO: Implement initFromCookie().
121 public function getId() {
122 return $this->mRow
->user_id
;
128 public function getName() {
129 return $this->mRow
->user_name
;
132 public function authenticate( $password ) {
133 # This might be wrong if anyone actually uses the UserComparePasswords hook
134 # (on either end), so don't use this if you those are incompatible.
135 return User
::comparePasswords( $this->mRow
->user_password
, $password,
136 $this->mRow
->user_id
);
139 public function getPref( $pref ) {
140 # @todo FIXME: Return other prefs too. Lots of global-riddled code that does
142 if ( $pref === 'emailaddress'
143 && $this->row
->user_email_authenticated
!== null ) {
144 return $this->mRow
->user_email
;
152 public function getGroups() {
153 # @todo FIXME: Untested.
155 $res = $this->mDb
->select(
158 array( 'ug_user' => $this->mRow
->user_id
),
161 foreach ( $res as $row ) {
162 $groups[] = $row->ug_group
;
167 # TODO: Implement setPref().