More convenient format for $wgExternalAuthType
[lhc/web/wiklou.git] / includes / extauth / Hardcoded.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 external authentication from a literal array dumped in
22 * LocalSettings.php. It's mostly useful for testing. Example configuration:
23 *
24 * $wgExternalAuthType = 'ExternalUser_Hardcoded';
25 * $wgExternalAuthConf = array(
26 * 'Bob Smith' => array(
27 * 'password' => 'literal string',
28 * 'emailaddress' => 'bob@example.com',
29 * ),
30 * );
31 *
32 * Multiple names may be provided. The keys of the inner arrays can be either
33 * 'password', or the name of any preference.
34 */
35 class ExternalUser_Hardcoded extends ExternalUser {
36 private $mName;
37
38 protected function initFromName( $name ) {
39 global $wgExternalAuthConf;
40
41 if ( isset( $wgExternalAuthConf[$name] ) ) {
42 $this->mName = $name;
43 return true;
44 }
45 return false;
46 }
47
48 protected function initFromId( $id ) {
49 return $this->initFromName( $id );
50 }
51
52 public function getId() {
53 return $this->mName;
54 }
55
56 public function getName() {
57 return $this->mName;
58 }
59
60 public function authenticate( $password ) {
61 global $wgExternalAuthConf;
62
63 return isset( $wgExternalAuthConf[$this->mName]['password'] )
64 && $wgExternalAuthConf[$this->mName]['password'] == $password;
65 }
66
67 public function getPref( $pref ) {
68 global $wgExternalAuthConf;
69
70 if ( isset( $wgExternalAuthConf[$this->mName][$pref] ) ) {
71 return $wgExternalAuthConf[$this->mName][$pref];
72 }
73 return null;
74 }
75
76 # TODO: Implement setPref() via regex on LocalSettings. (Just kidding.)
77 }