builtinauthenticationprovider.inc.php
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/**
* $Id$
*
* The contents of this file are subject to the KnowledgeTree Public
* License Version 1.1.2 ("License"); You may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.knowledgetree.com/KPL
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights and
* limitations under the License.
*
* All copies of the Covered Code must include on each user interface screen:
* (i) the "Powered by KnowledgeTree" logo and
* (ii) the KnowledgeTree copyright notice
* in the same form as they appear in the distribution. See the License for
* requirements.
*
* The Original Code is: KnowledgeTree Open Source
*
* The Initial Developer of the Original Code is The Jam Warehouse Software
* (Pty) Ltd, trading as KnowledgeTree.
* Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
* (C) 2007 The Jam Warehouse Software (Pty) Ltd;
* All Rights Reserved.
* Contributor( s): ______________________________________
*
*/
require_once(KT_LIB_DIR . '/authentication/authenticationprovider.inc.php');
require_once(KT_LIB_DIR . '/authentication/Authenticator.inc');
require_once(KT_LIB_DIR . '/authentication/DBAuthenticator.inc');
class KTBuiltinAuthenticationProvider extends KTAuthenticationProvider {
var $sNamespace = 'ktcore.authentication.builtin';
function KTBuiltinAuthenticationProvider() {
$this->sName = _kt('Built-in authentication provider');
parent::KTAuthenticationProvider();
}
function &getAuthenticator($oSource) {
// $oSource is null, since the built-in authentication provider
// only has a single, non-registered, instance.
return new BuiltinAuthenticator;
}
function showUserSource($oUser, $oSource) {
$sQuery = sprintf('action=editUserSource&user_id=%d', $oUser->getId());
$sUrl = KTUtil::addQueryString($_SERVER['PHP_SELF'], $sQuery);
return '<p class="descriptiveText"><a href="' . $sUrl . '">' . sprintf(_kt('Change %s\'s password'), $oUser->getName()) . '</a></p>';
}
function do_editUserSource() {
$this->redispatch('subaction', 'editUserSource');
exit(0);
}
function editUserSource_main() {
$this->oPage->setBreadcrumbDetails(_kt('Change User Password'));
$this->oPage->setTitle(_kt('Change User Password'));
$user_id = KTUtil::arrayGet($_REQUEST, 'user_id');
$oUser =& User::get($user_id);
if (PEAR::isError($oUser) || $oUser == false) {
$this->errorRedirectToMain(_kt('Please select a user first.'));
exit(0);
}
$edit_fields = array();
$edit_fields[] = new KTPasswordWidget(_kt('Password'), _kt('Specify an initial password for the user.'), 'password', null, $this->oPage, true); $edit_fields[] = new KTPasswordWidget(_kt('Confirm Password'), _kt('Confirm the password specified above.'), 'confirm_password', null, $this->oPage, true);
$oTemplating =& KTTemplating::getSingleton();
$oTemplate = $oTemplating->loadTemplate('ktcore/principals/updatepassword');
$aTemplateData = array(
'context' => $this,
'edit_fields' => $edit_fields,
'edit_user' => $oUser,
);
return $oTemplate->render($aTemplateData);
}
function editUserSource_forcePasswordChange() {
$aErrorOptions = array(
'redirect_to' => array('main'),
);
$oUser =& $this->oValidator->validateUser($_REQUEST['user_id'], $aErrorOptions);
$oUser->setAuthenticationDetailsBool1(true);
$res = $oUser->update();
$aErrorOptions = array(
'redirect_to' => array('editUserSource', sprintf('user_id=%d', $oUser->getId())),
'message' => _kt('Failed to update user'),
);
$this->oValidator->notErrorFalse($res, $aErrorOptions);
$this->commitTransaction();
$this->successRedirectTo('editUser', _kt('User will need to change password on next login.'), sprintf('user_id=%d', $oUser->getId()));
}
function editUserSource_updatePassword() {
$aErrorOptions = array(
'redirect_to' => array('main'),
);
$oUser =& $this->oValidator->validateUser($_REQUEST['user_id'], $aErrorOptions);
$aErrorOptions = array(
'redirect_to' => array('editUserSource', sprintf('user_id=%d', $oUser->getId())),
);
$sPassword = $this->oValidator->validatePasswordMatch($_REQUEST['password'], $_REQUEST['confirm_password'], $aErrorOptions);
$KTConfig =& KTConfig::getSingleton();
$minLength = ((int) $KTConfig->get('user_prefs/passwordLength', 6));
$restrictAdmin = ((bool) $KTConfig->get('user_prefs/restrictAdminPasswords', false));
if ($restrictAdmin && (strlen($sPassword) < $minLength)) {
$this->errorRedirectToMain(sprintf(_kt('The password must be at least %d characters long.'), $minLength));
}
$this->startTransaction();
// FIXME this almost certainly has side-effects. do we _really_ want
$oUser->setPassword(md5($sPassword)); //
$res = $oUser->update();
if (PEAR::isError($res) || ($res == false)) {
$this->errorRedirectTo('editUser', _kt('Failed to update user.'), sprintf('user_id=%d', $oUser->getId()));
}
$this->commitTransaction();
$this->successRedirectTo('editUser', _kt('User information updated.'), sprintf('user_id=%d', $oUser->getId()));
}
function login($oUser) {
$oConfig =& KTConfig::getSingleton();
$iDays = $oConfig->get('builtinauth/password_change_interval');
if ($iDays) {
$dLastPasswordChange = $oUser->getAuthenticationDetailsDate1();
if (empty($dLastPasswordChange)) {
$oUser->setAuthenticationDetailsDate1(formatDateTime(time()));
$oUser->update();
}
$sTable = KTUtil::getTableName('users');
$dNoLaterThan = formatDateTime(time() - ($iDays * 24 * 60 * 60));
$aSql = array("SELECT id FROM $sTable WHERE id = ? and authentication_details_d1 < ?",
array($oUser->getId(), $dNoLaterThan),
);
$iRes = DBUtil::getOneResultKey($aSql, 'id');
if (!empty($iRes)) {
$_SESSION['mustChangePassword'] = true;
}
}
if ($oUser->getAuthenticationDetailsBool1()) {
$_SESSION['mustChangePassword'] = true;
}
}
function verify($oUser) {
if (isset($_SESSION['mustChangePassword'])) {
$url = generateControllerUrl('login', 'action=providerVerify&type=1');
$this->addErrorMessage(_kt('Your password has expired'));
redirect($url);
exit(0);
}
}
function do_providerVerify() {
$this->redispatch('subaction', 'providerVerify');
exit(0);
}
function providerVerify_main() {
$oTemplate =& $this->oValidator->validateTemplate('ktcore/authentication/force_change_password');
$edit_fields = array();
$edit_fields[] = new KTPasswordWidget(_kt('Password'), _kt('Enter a new password for the account.'), 'password', null, $this->oPage, true);
$edit_fields[] = new KTPasswordWidget(_kt('Confirm Password'), _kt('Confirm the password specified above.'), 'confirm_password', null, $this->oPage, true);
$aTemplateData = array(
'user' => $this->oUser,
'edit_fields' => $edit_fields,
);
return $oTemplate->render($aTemplateData);
}
function providerVerify_return() {
$url = KTUtil::arrayGet($_SESSION, 'providerVerifyReturnUrl');
if (empty($url)) {
$url = generateControllerUrl('login');
}
redirect($url);
exit(0);
}
function providerVerify_updatePassword() {
$aErrorOptions = array(
'redirect_to' => array('providerVerify'),
);
$sPassword = $this->oValidator->validatePasswordMatch($_REQUEST['password'], $_REQUEST['confirm_password'], $aErrorOptions);
$KTConfig =& KTConfig::getSingleton();
$minLength = (int) $KTConfig->get('user_prefs/passwordLength', 6);
if (strlen($sPassword) < $minLength) {
$this->errorRedirectTo('providerVerify', sprintf(_kt('The password must be at least %d characters long.'), $minLength));
}
$sNewMD5 = md5($sPassword);
$sOldMD5 = $this->oUser->getPassword();
if ($sNewMD5 == $sOldMD5) {
$this->errorRedirectTo('providerVerify', _kt('Can not use the same password as before.'));
}
// FIXME more validation would be useful.
// validated and ready..
$this->startTransaction();
$this->oUser->setPassword($sNewMD5);
$this->oUser->setAuthenticationDetailsDate1(formatDateTime(time()));
$this->oUser->setAuthenticationDetailsBool1(false);
$res = $this->oUser->update();
$aErrorOptions = array(
'redirect_to' => array('providerVerify'),
);
$this->oValidator->notErrorFalse($res, $aErrorOptions);
$this->commitTransaction();
unset($_SESSION['mustChangePassword']);
$this->successRedirectTo('providerVerify', _kt('Password changed'), 'subaction=return');
}
}
class BuiltinAuthenticator extends Authenticator {
/**
* Checks the user's password against the database
*
* @param string the name of the user to check
* @param string the password to check
* @return boolean true if the password is correct, else false
*/
function checkPassword($oUser, $password) {
global $default;
$userName = $oUser->getUserName();
$sTable = KTUtil::getTableName('users');
$sQuery = "SELECT count(*) AS match_count FROM $sTable WHERE username = ? AND password = ?";
$aParams = array($userName, md5($password));
$res = DBUtil::getOneResultKey(array($sQuery, $aParams), 'match_count');
if (PEAR::isError($res)) { return false; }
else {
return ($res == 1);
}
}
/**
* Searches the directory for a specific user
*
* @param string the username to search for
* @param array the attributes to return from the search
* @return array containing the users found
*/
function getUser($sUserName, $aAttributes) {
$sTable = KTUtil::getTableName('users');
$sQuery = 'SELECT ';/*ok*/
$sQuery .= implode(', ', $aAttributes);
$sQuery .= " FROM $sTable WHERE username = ?";
$aParams = array($sUserName);
$res = DBUtil::getResultArray(array($sQuery, $aParams));
if (PEAR::isError($res)) {
return false;
}
$aUserResults = array();
foreach ($res as $aRow) {
foreach ($aAttributes as $sAttrName) {
$aUserResults[$sUserName][$sAttrName] = $aRow[$sAttrName];
}
}
return $aUserResults;
}
/**
* Searches the user store for users matching the supplied search string.
*
* @param string the username to search for
* @param array the attributes to return from the search
* @return array containing the users found
*/
function searchUsers($sUserNameSearch, $aAttributes) {
$sTable = KTUtil::getTableName('users');
$sQuery = 'SELECT '; /*ok*/
$sQuery .= implode(', ', $aAttributes);
$sQuery .= " FROM $sTable where username like '%" . DBUtil::escapeSimple($sUserNameSearch) . "%'";
$res = DBUtil::getResultArray(array($sQuery, array()));
if (PEAR::isError($res)) {
return false; // return $res;
}
$aUserResults = array();
foreach ($res as $aRow) {
$sUserName = $aRow['username'];
foreach ($aAttributes as $sAttrName) {
$aUserResults[$sUserName][$sAttrName] = $aRow[$sAttrName];
}
}
return $aUserResults;
}
}