4 * @file PKPLoginHandler.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class PKPLoginHandler
10 * @ingroup pages_login
12 * @brief Handle login/logout requests.
15 // $Id: PKPLoginHandler.inc.php,v 1.17 2009/09/22 21:18:27 asmecher Exp $
18 import('handler.Handler');
20 class PKPLoginHandler
extends Handler
{
23 * Display user login form.
24 * Redirect to user index page if user is already validated.
28 $this->setupTemplate();
29 if (Validation
::isLoggedIn()) {
30 PKPRequest
::redirect(null, 'user');
33 if (Config
::getVar('security', 'force_login_ssl') && Request
::getProtocol() != 'https') {
34 // Force SSL connections for login
35 PKPRequest
::redirectSSL();
38 $sessionManager =& SessionManager
::getManager();
39 $session =& $sessionManager->getUserSession();
41 $templateMgr =& TemplateManager
::getManager();
43 // If the user wasn't expecting a login page, i.e. if they're new to the
44 // site and want to submit a paper, it helps to explain why they need to
46 if(Request
::getUserVar('loginMessage'))
47 $templateMgr->assign('loginMessage', Request
::getUserVar('loginMessage'));
49 $templateMgr->assign('username', $session->getSessionVar('username'));
50 $templateMgr->assign('remember', Request
::getUserVar('remember'));
51 $templateMgr->assign('source', Request
::getUserVar('source'));
52 $templateMgr->assign('showRemember', Config
::getVar('general', 'session_lifetime') > 0);
53 $templateMgr->display('user/login.tpl');
57 * Handle login when implicitAuth is enabled.
58 * If the user came in on a non-ssl url - then redirect back to the ssl url
60 function implicitAuthLogin() {
61 if (Request
::getProtocol() != 'https')
62 PKPRequest
::redirectSSL();
64 $wayf_url = Config
::getVar("security", "implicit_auth_wayf_url");
67 die("Error in implicit authentication. WAYF URL not set in config file.");
69 $url = $wayf_url . "?target=https://" . Request
::getServerHost() . Request
::getBasePath() . '/index.php/index/login/implicitAuthReturn';
71 PKPRequest
::redirectUrl($url);
75 * This is the function that Shibboleth redirects to - after the user has authenticated.
77 function implicitAuthReturn() {
80 if (Validation
::isLoggedIn()) {
81 PKPRequest
::redirect(null, 'user');
84 // Login - set remember to false
85 $user = Validation
::login(Request
::getUserVar('username'), Request
::getUserVar('password'), $reason, false);
87 PKPRequest
::redirect(null, 'user');
91 * Validate a user's credentials and log the user in.
95 $this->setupTemplate();
96 if (Validation
::isLoggedIn()) {
97 PKPRequest
::redirect(null, 'user');
100 if (Config
::getVar('security', 'force_login_ssl') && Request
::getProtocol() != 'https') {
101 // Force SSL connections for login
102 PKPRequest
::redirectSSL();
105 $user = Validation
::login(Request
::getUserVar('username'), Request
::getUserVar('password'), $reason, Request
::getUserVar('remember') == null ?
false : true);
106 if ($user !== false) {
107 if (Config
::getVar('security', 'force_login_ssl') && !Config
::getVar('security', 'force_ssl')) {
108 // Redirect back to HTTP if forcing SSL for login only
109 PKPRequest
::redirectNonSSL();
111 } else if ($user->getMustChangePassword()) {
112 // User must change their password in order to log in
113 Validation
::logout();
114 PKPRequest
::redirect(null, null, 'changePassword', $user->getUsername());
117 $source = Request
::getUserVar('source');
118 if (isset($source) && !empty($source)) {
119 PKPRequest
::redirectUrl(Request
::getProtocol() . '://' . Request
::getServerHost() . $source, false);
121 Request
::redirectHome();
126 $sessionManager =& SessionManager
::getManager();
127 $session =& $sessionManager->getUserSession();
129 $templateMgr =& TemplateManager
::getManager();
130 $templateMgr->assign('username', Request
::getUserVar('username'));
131 $templateMgr->assign('remember', Request
::getUserVar('remember'));
132 $templateMgr->assign('source', Request
::getUserVar('source'));
133 $templateMgr->assign('showRemember', Config
::getVar('general', 'session_lifetime') > 0);
134 $templateMgr->assign('error', $reason===null?
'user.login.loginError':($reason===''?
'user.login.accountDisabled':'user.login.accountDisabledWithReason'));
135 $templateMgr->assign('reason', $reason);
136 $templateMgr->display('user/login.tpl');
145 $this->setupTemplate();
146 if (Validation
::isLoggedIn()) {
147 Validation
::logout();
150 $source = Request
::getUserVar('source');
151 if (isset($source) && !empty($source)) {
152 PKPRequest
::redirectUrl(Request
::getProtocol() . '://' . Request
::getServerHost() . $source, false);
154 PKPRequest
::redirect(null, Request
::getRequestedPage());
159 * Display form to reset a user's password.
161 function lostPassword() {
163 $this->setupTemplate();
164 $templateMgr =& TemplateManager
::getManager();
165 $templateMgr->display('user/lostPassword.tpl');
169 * Send a request to reset a user's password
171 function requestResetPassword() {
173 $this->setupTemplate();
174 $templateMgr =& TemplateManager
::getManager();
176 $email = Request
::getUserVar('email');
177 $userDao =& DAORegistry
::getDAO('UserDAO');
178 $user =& $userDao->getUserByEmail($email);
180 if ($user == null ||
($hash = Validation
::generatePasswordResetHash($user->getId())) == false) {
181 $templateMgr->assign('error', 'user.login.lostPassword.invalidUser');
182 $templateMgr->display('user/lostPassword.tpl');
185 $site =& Request
::getSite();
187 // Send email confirming password reset
188 import('mail.MailTemplate');
189 $mail = new MailTemplate('PASSWORD_RESET_CONFIRM');
190 $this->_setMailFrom($mail);
191 $mail->assignParams(array(
192 'url' => PKPRequest
::url(null, 'login', 'resetPassword', $user->getUsername(), array('confirm' => $hash)),
193 'siteTitle' => $site->getLocalizedTitle()
195 $mail->addRecipient($user->getEmail(), $user->getFullName());
197 $templateMgr->assign('pageTitle', 'user.login.resetPassword');
198 $templateMgr->assign('message', 'user.login.lostPassword.confirmationSent');
199 $templateMgr->assign('backLink', PKPRequest
::url(null, Request
::getRequestedPage()));
200 $templateMgr->assign('backLinkLabel', 'user.login');
201 $templateMgr->display('common/message.tpl');
206 * Reset a user's password
207 * @param $args array first param contains the username of the user whose password is to be reset
209 function resetPassword($args) {
211 $this->setupTemplate();
213 $username = isset($args[0]) ?
$args[0] : null;
214 $userDao =& DAORegistry
::getDAO('UserDAO');
215 $confirmHash = Request
::getUserVar('confirm');
217 if ($username == null ||
($user =& $userDao->getUserByUsername($username)) == null) {
218 PKPRequest
::redirect(null, null, 'lostPassword');
222 $templateMgr =& TemplateManager
::getManager();
224 $hash = Validation
::generatePasswordResetHash($user->getId());
225 if ($hash == false ||
$confirmHash != $hash) {
226 $templateMgr->assign('errorMsg', 'user.login.lostPassword.invalidHash');
227 $templateMgr->assign('backLink', PKPRequest
::url(null, null, 'lostPassword'));
228 $templateMgr->assign('backLinkLabel', 'user.login.resetPassword');
229 $templateMgr->display('common/error.tpl');
233 $newPassword = Validation
::generatePassword();
235 if ($user->getAuthId()) {
236 $authDao =& DAORegistry
::getDAO('AuthSourceDAO');
237 $auth =& $authDao->getPlugin($user->getAuthId());
241 $auth->doSetUserPassword($user->getUsername(), $newPassword);
242 $user->setPassword(Validation
::encryptCredentials($user->getId(), Validation
::generatePassword())); // Used for PW reset hash only
244 $user->setPassword(Validation
::encryptCredentials($user->getUsername(), $newPassword));
247 $user->setMustChangePassword(1);
248 $userDao->updateObject($user);
250 // Send email with new password
251 $site =& Request
::getSite();
252 import('mail.MailTemplate');
253 $mail = new MailTemplate('PASSWORD_RESET');
254 $this->_setMailFrom($mail);
255 $mail->assignParams(array(
256 'username' => $user->getUsername(),
257 'password' => $newPassword,
258 'siteTitle' => $site->getLocalizedTitle()
260 $mail->addRecipient($user->getEmail(), $user->getFullName());
262 $templateMgr->assign('pageTitle', 'user.login.resetPassword');
263 $templateMgr->assign('message', 'user.login.lostPassword.passwordSent');
264 $templateMgr->assign('backLink', PKPRequest
::url(null, Request
::getRequestedPage()));
265 $templateMgr->assign('backLinkLabel', 'user.login');
266 $templateMgr->display('common/message.tpl');
271 * Display form to change user's password.
272 * @param $args array first argument may contain user's username
274 function changePassword($args = array()) {
276 $this->setupTemplate();
278 import('user.form.LoginChangePasswordForm');
280 $passwordForm = new LoginChangePasswordForm();
281 $passwordForm->initData();
282 if (isset($args[0])) {
283 $passwordForm->setData('username', $args[0]);
285 $passwordForm->display();
289 * Save user's new password.
291 function savePassword() {
293 $this->setupTemplate();
295 import('user.form.LoginChangePasswordForm');
297 $passwordForm = new LoginChangePasswordForm();
298 $passwordForm->readInputData();
300 if ($passwordForm->validate()) {
301 if ($passwordForm->execute()) {
302 $user = Validation
::login($passwordForm->getData('username'), $passwordForm->getData('password'), $reason);
304 PKPRequest
::redirect(null, 'user');
307 $passwordForm->display();
312 * Helper function - set mail From
313 * can be overriden by child classes
314 * @param MailTemplate $mail
316 function _setMailFrom(&$mail) {
317 $site =& Request
::getSite();
318 $mail->setFrom($site->getLocalizedContactEmail(), $site->getLocalizedContactName());