Merge "Send correct HTML when reporting a MWException object and the OuputPage object...
[lhc/web/wiklou.git] / includes / AuthPlugin.php
1 <?php
2 /**
3 * Authentication plugin interface
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 /**
27 * Authentication plugin interface. Instantiate a subclass of AuthPlugin
28 * and set $wgAuth to it to authenticate against some external tool.
29 *
30 * The default behavior is not to do anything, and use the local user
31 * database for all authentication. A subclass can require that all
32 * accounts authenticate externally, or use it only as a fallback; also
33 * you can transparently create internal wiki accounts the first time
34 * someone logs in who can be authenticated externally.
35 */
36 class AuthPlugin {
37
38 /**
39 * @var string
40 */
41 protected $domain;
42
43 /**
44 * Check whether there exists a user account with the given name.
45 * The name will be normalized to MediaWiki's requirements, so
46 * you might need to munge it (for instance, for lowercase initial
47 * letters).
48 *
49 * @param $username String: username.
50 * @return bool
51 */
52 public function userExists( $username ) {
53 # Override this!
54 return false;
55 }
56
57 /**
58 * Check if a username+password pair is a valid login.
59 * The name will be normalized to MediaWiki's requirements, so
60 * you might need to munge it (for instance, for lowercase initial
61 * letters).
62 *
63 * @param $username String: username.
64 * @param $password String: user password.
65 * @return bool
66 */
67 public function authenticate( $username, $password ) {
68 # Override this!
69 return false;
70 }
71
72 /**
73 * Modify options in the login template.
74 *
75 * @param $template UserLoginTemplate object.
76 * @param $type String 'signup' or 'login'. Added in 1.16.
77 */
78 public function modifyUITemplate( &$template, &$type ) {
79 # Override this!
80 $template->set( 'usedomain', false );
81 }
82
83 /**
84 * Set the domain this plugin is supposed to use when authenticating.
85 *
86 * @param $domain String: authentication domain.
87 */
88 public function setDomain( $domain ) {
89 $this->domain = $domain;
90 }
91
92 /**
93 * Get the user's domain
94 *
95 * @return string
96 */
97 public function getDomain() {
98 if ( isset( $this->domain ) ) {
99 return $this->domain;
100 } else {
101 return 'invaliddomain';
102 }
103 }
104
105 /**
106 * Check to see if the specific domain is a valid domain.
107 *
108 * @param $domain String: authentication domain.
109 * @return bool
110 */
111 public function validDomain( $domain ) {
112 # Override this!
113 return true;
114 }
115
116 /**
117 * When a user logs in, optionally fill in preferences and such.
118 * For instance, you might pull the email address or real name from the
119 * external user database.
120 *
121 * The User object is passed by reference so it can be modified; don't
122 * forget the & on your function declaration.
123 *
124 * @param $user User object
125 * @return bool
126 */
127 public function updateUser( &$user ) {
128 # Override this and do something
129 return true;
130 }
131
132 /**
133 * Return true if the wiki should create a new local account automatically
134 * when asked to login a user who doesn't exist locally but does in the
135 * external auth database.
136 *
137 * If you don't automatically create accounts, you must still create
138 * accounts in some way. It's not possible to authenticate without
139 * a local account.
140 *
141 * This is just a question, and shouldn't perform any actions.
142 *
143 * @return Boolean
144 */
145 public function autoCreate() {
146 return false;
147 }
148
149 /**
150 * Allow a property change? Properties are the same as preferences
151 * and use the same keys. 'Realname' 'Emailaddress' and 'Nickname'
152 * all reference this.
153 *
154 * @param $prop string
155 *
156 * @return Boolean
157 */
158 public function allowPropChange( $prop = '' ) {
159 if ( $prop == 'realname' && is_callable( array( $this, 'allowRealNameChange' ) ) ) {
160 return $this->allowRealNameChange();
161 } elseif ( $prop == 'emailaddress' && is_callable( array( $this, 'allowEmailChange' ) ) ) {
162 return $this->allowEmailChange();
163 } elseif ( $prop == 'nickname' && is_callable( array( $this, 'allowNickChange' ) ) ) {
164 return $this->allowNickChange();
165 } else {
166 return true;
167 }
168 }
169
170 /**
171 * Can users change their passwords?
172 *
173 * @return bool
174 */
175 public function allowPasswordChange() {
176 return true;
177 }
178
179 /**
180 * Set the given password in the authentication database.
181 * As a special case, the password may be set to null to request
182 * locking the password to an unusable value, with the expectation
183 * that it will be set later through a mail reset or other method.
184 *
185 * Return true if successful.
186 *
187 * @param $user User object.
188 * @param $password String: password.
189 * @return bool
190 */
191 public function setPassword( $user, $password ) {
192 return true;
193 }
194
195 /**
196 * Update user information in the external authentication database.
197 * Return true if successful.
198 *
199 * @param $user User object.
200 * @return Boolean
201 */
202 public function updateExternalDB( $user ) {
203 return true;
204 }
205
206 /**
207 * Check to see if external accounts can be created.
208 * Return true if external accounts can be created.
209 * @return Boolean
210 */
211 public function canCreateAccounts() {
212 return false;
213 }
214
215 /**
216 * Add a user to the external authentication database.
217 * Return true if successful.
218 *
219 * @param $user User: only the name should be assumed valid at this point
220 * @param $password String
221 * @param $email String
222 * @param $realname String
223 * @return Boolean
224 */
225 public function addUser( $user, $password, $email = '', $realname = '' ) {
226 return true;
227 }
228
229 /**
230 * Return true to prevent logins that don't authenticate here from being
231 * checked against the local database's password fields.
232 *
233 * This is just a question, and shouldn't perform any actions.
234 *
235 * @return Boolean
236 */
237 public function strict() {
238 return false;
239 }
240
241 /**
242 * Check if a user should authenticate locally if the global authentication fails.
243 * If either this or strict() returns true, local authentication is not used.
244 *
245 * @param $username String: username.
246 * @return Boolean
247 */
248 public function strictUserAuth( $username ) {
249 return false;
250 }
251
252 /**
253 * When creating a user account, optionally fill in preferences and such.
254 * For instance, you might pull the email address or real name from the
255 * external user database.
256 *
257 * The User object is passed by reference so it can be modified; don't
258 * forget the & on your function declaration.
259 *
260 * @param $user User object.
261 * @param $autocreate Boolean: True if user is being autocreated on login
262 */
263 public function initUser( &$user, $autocreate = false ) {
264 # Override this to do something.
265 }
266
267 /**
268 * If you want to munge the case of an account name before the final
269 * check, now is your chance.
270 * @param $username string
271 * @return string
272 */
273 public function getCanonicalName( $username ) {
274 return $username;
275 }
276
277 /**
278 * Get an instance of a User object
279 *
280 * @param $user User
281 *
282 * @return AuthPluginUser
283 */
284 public function getUserInstance( User &$user ) {
285 return new AuthPluginUser( $user );
286 }
287
288 /**
289 * Get a list of domains (in HTMLForm options format) used.
290 *
291 * @return array
292 */
293 public function domainList() {
294 return array();
295 }
296 }
297
298 class AuthPluginUser {
299 function __construct( $user ) {
300 # Override this!
301 }
302
303 public function getId() {
304 # Override this!
305 return -1;
306 }
307
308 public function isLocked() {
309 # Override this!
310 return false;
311 }
312
313 public function isHidden() {
314 # Override this!
315 return false;
316 }
317
318 public function resetAuthToken() {
319 # Override this!
320 return true;
321 }
322 }