Index: trunk/phase3/includes/AutoLoader.php |
— | — | @@ -76,6 +76,7 @@ |
77 | 77 | 'ExternalStore' => 'includes/ExternalStore.php', |
78 | 78 | 'ExternalUser' => 'includes/ExternalUser.php', |
79 | 79 | 'ExternalUser_Hardcoded' => 'includes/extauth/Hardcoded.php', |
| 80 | + 'ExternalUser_MediaWiki' => 'includes/extauth/MediaWiki.php', |
80 | 81 | 'ExternalUser_vB' => 'includes/extauth/vB.php', |
81 | 82 | 'FatalError' => 'includes/Exception.php', |
82 | 83 | 'FakeTitle' => 'includes/FakeTitle.php', |
Index: trunk/phase3/includes/extauth/MediaWiki.php |
— | — | @@ -0,0 +1,139 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +# Copyright (C) 2009 Aryeh Gregor |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation; either version 2 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License along |
| 17 | +# with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | +# http://www.gnu.org/copyleft/gpl.html |
| 20 | + |
| 21 | +/** |
| 22 | + * This class supports authentication against an external MediaWiki database, |
| 23 | + * probably any version back to 1.5 or something. Example configuration: |
| 24 | + * |
| 25 | + * $wgExternalAuthType = 'MediaWiki'; |
| 26 | + * $wgExternalAuthConf = array( |
| 27 | + * 'DBtype' => 'mysql', |
| 28 | + * 'DBserver' => 'localhost', |
| 29 | + * 'DBname' => 'wikidb', |
| 30 | + * 'DBuser' => 'quasit', |
| 31 | + * 'DBpassword' => 'a5Cr:yf9u-6[{`g', |
| 32 | + * 'DBprefix' => '', |
| 33 | + * ); |
| 34 | + * |
| 35 | + * All fields must be present. These mean the same things as $wgDBtype, |
| 36 | + * $wgDBserver, etc. This implementation is quite crude; it could easily |
| 37 | + * support multiple database servers, for instance, and memcached, and it |
| 38 | + * probably has bugs. Kind of hard to reuse code when things might rely on who |
| 39 | + * knows what configuration globals. |
| 40 | + * |
| 41 | + * If either wiki uses the UserComparePasswords hook, password authentication |
| 42 | + * might fail unexpectedly unless they both do the exact same validation. |
| 43 | + * There may be other corner cases like this where this will fail, but it |
| 44 | + * should be unlikely. |
| 45 | + */ |
| 46 | +class ExternalUser_MediaWiki extends ExternalUser { |
| 47 | + private $mRow, $mDb; |
| 48 | + |
| 49 | + protected function initFromName( $name ) { |
| 50 | + # We might not need the 'usable' bit, but let's be safe. Theoretically |
| 51 | + # this might return wrong results for old versions, but it's probably |
| 52 | + # good enough. |
| 53 | + $name = User::getCanonicalName( $name, 'usable' ); |
| 54 | + |
| 55 | + if ( !is_string( $name ) ) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + return $this->initFromCond( array( 'user_name' => $name ) ); |
| 60 | + } |
| 61 | + |
| 62 | + protected function initFromId( $id ) { |
| 63 | + return $this->initFromCond( array( 'user_id' => $id ) ); |
| 64 | + } |
| 65 | + |
| 66 | + private function initFromCond( $cond ) { |
| 67 | + global $wgExternalAuthConf; |
| 68 | + |
| 69 | + $class = 'Database' . $wgExternalAuthConf['DBtype']; |
| 70 | + $this->mDb = new $class( |
| 71 | + $wgExternalAuthConf['DBserver'], |
| 72 | + $wgExternalAuthConf['DBuser'], |
| 73 | + $wgExternalAuthConf['DBpassword'], |
| 74 | + $wgExternalAuthConf['DBname'], |
| 75 | + false, |
| 76 | + 0, |
| 77 | + $wgExternalAuthConf['DBprefix'] |
| 78 | + ); |
| 79 | + |
| 80 | + $row = $this->mDb->selectRow( |
| 81 | + 'user', |
| 82 | + array( |
| 83 | + 'user_name', 'user_id', 'user_password', 'user_email', |
| 84 | + 'user_email_authenticated' |
| 85 | + ), |
| 86 | + $cond, |
| 87 | + __METHOD__ |
| 88 | + ); |
| 89 | + if ( !$row ) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + $this->mRow = $row; |
| 93 | + |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + # TODO: Implement initFromCookie(). |
| 98 | + |
| 99 | + public function getId() { |
| 100 | + return $this->mRow->user_id; |
| 101 | + } |
| 102 | + |
| 103 | + public function getName() { |
| 104 | + return $this->mRow->user_name; |
| 105 | + } |
| 106 | + |
| 107 | + public function authenticate( $password ) { |
| 108 | + # This might be wrong if anyone actually uses the UserComparePasswords hook |
| 109 | + # (on either end), so don't use this if you those are incompatible. |
| 110 | + return User::comparePasswords( $this->mRow->user_password, $password, |
| 111 | + $this->mRow->user_id ); |
| 112 | + } |
| 113 | + |
| 114 | + public function getPref( $pref ) { |
| 115 | + # FIXME: Return other prefs too. Lots of global-riddled code that does |
| 116 | + # this normally. |
| 117 | + if ( $pref === 'emailaddress' |
| 118 | + && $this->row->user_email_authenticated !== null ) { |
| 119 | + return $this->mRow->user_email; |
| 120 | + } |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + public function getGroups() { |
| 125 | + # FIXME: Untested. |
| 126 | + $groups = array(); |
| 127 | + $res = $this->mDb->select( |
| 128 | + 'user_groups', |
| 129 | + 'ug_group', |
| 130 | + array( 'ug_user' => $this->mRow->user_id ), |
| 131 | + __METHOD__ |
| 132 | + ); |
| 133 | + foreach ( $res as $row ) { |
| 134 | + $groups[] = $row->ug_group; |
| 135 | + } |
| 136 | + return $groups; |
| 137 | + } |
| 138 | + |
| 139 | + # TODO: Implement setPref(). |
| 140 | +} |
Property changes on: trunk/phase3/includes/extauth/MediaWiki.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 141 | + native |