Begin work on allowing extauth login from cookies
[mediawiki.git] / includes / ExternalUser.php
blobe89c205da72aa578ab84b4b07e18e720565a80b4
1 <?php
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
20 /**
21 * A class intended to supplement, and perhaps eventually replace, AuthPlugin.
22 * See: http://www.mediawiki.org/wiki/ExternalAuth
24 * The class represents a user whose data is in a foreign database. The
25 * database may have entirely different conventions from MediaWiki, but it's
26 * assumed to at least support the concept of a user id (possibly not an
27 * integer), a user name (possibly not meeting MediaWiki's username
28 * requirements), and a password.
30 abstract class ExternalUser {
31 protected function __construct() {}
33 /**
34 * Wrappers around initFrom*().
37 /**
38 * @param $name string
39 * @return mixed ExternalUser, or false on failure
41 public static function newFromName( $name ) {
42 global $wgExternalAuthType;
43 if ( is_null( $wgExternalAuthType ) ) {
44 return false;
46 $obj = new $wgExternalAuthType;
47 if ( !$obj->initFromName( $name ) ) {
48 return false;
50 return $obj;
53 /**
54 * @param $id string
55 * @return mixed ExternalUser, or false on failure
57 public static function newFromId( $id ) {
58 global $wgExternalAuthType;
59 if ( is_null( $wgExternalAuthType ) ) {
60 return false;
62 $obj = new $wgExternalAuthType;
63 if ( !$obj->initFromId( $id ) ) {
64 return false;
66 return $obj;
69 /**
70 * @param $cookie string
71 * @return mixed ExternalUser, or false on failure
73 public static function newFromCookie() {
74 global $wgExternalAuthType;
75 if ( is_null( $wgExternalAuthType ) ) {
76 return false;
78 $obj = new $wgExternalAuthType;
79 if ( !$obj->initFromCookie() ) {
80 return false;
82 return $obj;
85 /**
86 * Creates the object corresponding to the given User object, assuming the
87 * user exists on the wiki and is linked to an external account. If either
88 * of these is false, this will return false.
90 * This is a wrapper around newFromId().
92 * @param $user User
93 * @return mixed ExternalUser or false
95 public static function newFromUser( $user ) {
96 global $wgExternalAuthType;
97 if ( is_null( $wgExternalAuthType ) ) {
98 # Short-circuit to avoid database query in common case so no one
99 # kills me
100 return false;
103 $dbr = wfGetDB( DB_SLAVE );
104 $id = $dbr->selectField( 'external_user', 'eu_external_id',
105 array( 'eu_local_id' => $user->getId() ), __METHOD__ );
106 if ( $id === false ) {
107 return false;
109 return self::newFromId( $id );
113 * Given a name, which is a string exactly as input by the user in the
114 * login form but with whitespace stripped, initialize this object to be
115 * the corresponding ExternalUser. Return true if successful, otherwise
116 * false.
118 * @param $name string
119 * @return bool Success?
121 protected abstract function initFromName( $name );
124 * Given an id, which was at some previous point in history returned by
125 * getId(), initialize this object to be the corresponding ExternalUser.
126 * Return true if successful, false otherwise.
128 * @param $id string
129 * @return bool Success?
131 protected abstract function initFromId( $id );
134 * Try to magically initialize the user from cookies or similar information
135 * so he or she can be logged in on just viewing the wiki. If this is
136 * impossible to do, just return false.
138 * TODO: Actually use this.
140 * @return bool Success?
142 protected function initFromCookie() {
143 return false;
147 * This must return some identifier that stably, uniquely identifies the
148 * user. In a typical web application, this could be an integer
149 * representing the "user id". In other cases, it might be a string. In
150 * any event, the return value should be a string between 1 and 255
151 * characters in length; must uniquely identify the user in the foreign
152 * database; and, if at all possible, should be permanent.
154 * This will only ever be used to reconstruct this ExternalUser object via
155 * newFromId(). The resulting object in that case should correspond to the
156 * same user, even if details have changed in the interim (e.g., renames or
157 * preference changes).
159 * @return string
161 abstract public function getId();
164 * This must return the name that the user would normally use for login to
165 * the external database. It is subject to no particular restrictions
166 * beyond rudimentary sanity, and in particular may be invalid as a
167 * MediaWiki username. It's used to auto-generate an account name that
168 * *is* valid for MediaWiki, either with or without user input, but
169 * basically is only a hint.
171 * @return string
173 abstract public function getName();
176 * Is the given password valid for the external user? The password is
177 * provided in plaintext.
179 * @param $password string
180 * @return bool
182 abstract public function authenticate( $password );
185 * Retrieve the value corresponding to the given preference key. The most
186 * important values are:
188 * - emailaddress
189 * - language
191 * The value must meet MediaWiki's requirements for values of this type,
192 * and will be checked for validity before use. If the preference makes no
193 * sense for the backend, or it makes sense but is unset for this user, or
194 * is unrecognized, return null.
196 * $pref will never equal 'password', since passwords are usually hashed
197 * and cannot be directly retrieved. authenticate() is used for this
198 * instead.
200 * TODO: Currently this is only called for 'emailaddress'; generalize! Add
201 * some config option to decide which values are grabbed on user
202 * initialization.
204 * @param $pref string
205 * @return mixed
207 public function getPref( $pref ) {
208 return null;
212 * Return an array of identifiers for all the foreign groups that this user
213 * has. The identifiers are opaque objects that only need to be
214 * specifiable by the administrator in LocalSettings.php when configuring
215 * $wgAutopromote. They may be, for instance, strings or integers.
217 * TODO: Support this in $wgAutopromote.
219 * @return array
221 public function getGroups() {
222 return array();
226 * Given a preference key (e.g., 'emailaddress'), provide an HTML message
227 * telling the user how to change it in the external database. The
228 * administrator has specified that this preference cannot be changed on
229 * the wiki, and may only be changed in the foreign database. If no
230 * message is available, such as for an unrecognized preference, return
231 * false.
233 * TODO: Use this somewhere.
235 * @param $pref string
236 * @return mixed String or false
238 public static function getPrefMessage( $pref ) {
239 return false;
243 * Set the given preference key to the given value. Two important
244 * preference keys that you might want to implement are 'password' and
245 * 'emailaddress'. If the set fails, such as because the preference is
246 * unrecognized or because the external database can't be changed right
247 * now, return false. If it succeeds, return true.
249 * If applicable, you should make sure to validate the new value against
250 * any constraints the external database may have, since MediaWiki may have
251 * more limited constraints (e.g., on password strength).
253 * TODO: Untested.
255 * @param $key string
256 * @param $value string
257 * @return bool Success?
259 public static function setPref( $key, $value ) {
260 return false;
264 * Create a link for future reference between this object and the provided
265 * user_id. If the user was already linked, the old link will be
266 * overwritten.
268 * This is part of the core code and is not overridable by specific
269 * plugins. It's in this class only for convenience.
271 * @param $id int user_id
273 public final function linkToLocal( $id ) {
274 $dbw = wfGetDB( DB_MASTER );
275 $dbw->replace( 'external_user',
276 array( 'eu_local_id', 'eu_external_id' ),
277 array( 'eu_local_id' => $id,
278 'eu_external_id' => $this->getId() ),
279 __METHOD__ );
283 * Check whether this external user id is already linked with
284 * a local user.
285 * @return Mixed User if the account is linked, Null otherwise.
287 public final function getLocalUser(){
288 $dbr = wfGetDb( DB_SLAVE );
289 $row = $dbr->selectRow(
290 'external_user',
291 '*',
292 array( 'eu_external_id' => $this->getId() )
294 return $row
295 ? User::newFromId( $row->eu_local_id )
296 : null;