Add rudimentary MediaWiki support to ExternalAuth
[lhc/web/wiklou.git] / includes / extauth / MediaWiki.php
1 <?php
2
3 # Copyright (C) 2009 Aryeh Gregor
4 #
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.
9 #
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.
14 #
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
19
20 /**
21 * This class supports authentication against an external MediaWiki database,
22 * probably any version back to 1.5 or something. Example configuration:
23 *
24 * $wgExternalAuthType = 'MediaWiki';
25 * $wgExternalAuthConf = array(
26 * 'DBtype' => 'mysql',
27 * 'DBserver' => 'localhost',
28 * 'DBname' => 'wikidb',
29 * 'DBuser' => 'quasit',
30 * 'DBpassword' => 'a5Cr:yf9u-6[{`g',
31 * 'DBprefix' => '',
32 * );
33 *
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.
39 *
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
43 * should be unlikely.
44 */
45 class ExternalUser_MediaWiki extends ExternalUser {
46 private $mRow, $mDb;
47
48 protected function initFromName( $name ) {
49 # We might not need the 'usable' bit, but let's be safe. Theoretically
50 # this might return wrong results for old versions, but it's probably
51 # good enough.
52 $name = User::getCanonicalName( $name, 'usable' );
53
54 if ( !is_string( $name ) ) {
55 return false;
56 }
57
58 return $this->initFromCond( array( 'user_name' => $name ) );
59 }
60
61 protected function initFromId( $id ) {
62 return $this->initFromCond( array( 'user_id' => $id ) );
63 }
64
65 private function initFromCond( $cond ) {
66 global $wgExternalAuthConf;
67
68 $class = 'Database' . $wgExternalAuthConf['DBtype'];
69 $this->mDb = new $class(
70 $wgExternalAuthConf['DBserver'],
71 $wgExternalAuthConf['DBuser'],
72 $wgExternalAuthConf['DBpassword'],
73 $wgExternalAuthConf['DBname'],
74 false,
75 0,
76 $wgExternalAuthConf['DBprefix']
77 );
78
79 $row = $this->mDb->selectRow(
80 'user',
81 array(
82 'user_name', 'user_id', 'user_password', 'user_email',
83 'user_email_authenticated'
84 ),
85 $cond,
86 __METHOD__
87 );
88 if ( !$row ) {
89 return false;
90 }
91 $this->mRow = $row;
92
93 return true;
94 }
95
96 # TODO: Implement initFromCookie().
97
98 public function getId() {
99 return $this->mRow->user_id;
100 }
101
102 public function getName() {
103 return $this->mRow->user_name;
104 }
105
106 public function authenticate( $password ) {
107 # This might be wrong if anyone actually uses the UserComparePasswords hook
108 # (on either end), so don't use this if you those are incompatible.
109 return User::comparePasswords( $this->mRow->user_password, $password,
110 $this->mRow->user_id );
111 }
112
113 public function getPref( $pref ) {
114 # FIXME: Return other prefs too. Lots of global-riddled code that does
115 # this normally.
116 if ( $pref === 'emailaddress'
117 && $this->row->user_email_authenticated !== null ) {
118 return $this->mRow->user_email;
119 }
120 return null;
121 }
122
123 public function getGroups() {
124 # FIXME: Untested.
125 $groups = array();
126 $res = $this->mDb->select(
127 'user_groups',
128 'ug_group',
129 array( 'ug_user' => $this->mRow->user_id ),
130 __METHOD__
131 );
132 foreach ( $res as $row ) {
133 $groups[] = $row->ug_group;
134 }
135 return $groups;
136 }
137
138 # TODO: Implement setPref().
139 }