diff --git a/module/VuFind/src/VuFind/Account/Manager.php b/module/VuFind/src/VuFind/Account/Manager.php index 5e153da5af522618f9113643b4dd02e178cb2307..f2ebed66d7feb58fe3a22d15d86e366931421050 100644 --- a/module/VuFind/src/VuFind/Account/Manager.php +++ b/module/VuFind/src/VuFind/Account/Manager.php @@ -26,7 +26,7 @@ * @link http://www.vufind.org Main Page */ namespace VuFind\Account; -use VuFind\Config\Reader as ConfigReader, +use VuFind\Auth\Factory as AuthFactory, VuFind\Config\Reader as ConfigReader, Zend\Registry, Zend\Session, Zend\Session\Container as SessionContainer; /** @@ -51,11 +51,9 @@ class Manager public function __construct() { $this->config = ConfigReader::getConfig(); - /* TODO: - $this->auth = VF_Auth_Factory::getAuth( + $this->auth = AuthFactory::getAuth( $this->config->Authentication->method, $this->config ); - */ $this->session = new SessionContainer('Account'); } @@ -139,6 +137,7 @@ class Manager } return !$catalog->loginIsHidden(); */ + return true; } /** diff --git a/module/VuFind/src/VuFind/Auth/AbstractBase.php b/module/VuFind/src/VuFind/Auth/AbstractBase.php new file mode 100644 index 0000000000000000000000000000000000000000..f30c0911b011c44c818fd44edf174f28a8a0b12c --- /dev/null +++ b/module/VuFind/src/VuFind/Auth/AbstractBase.php @@ -0,0 +1,131 @@ +<?php +/** + * Abstract authentication base class + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Authentication + * @author Franck Borel <franck.borel@gbv.de> + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://www.vufind.org Main Page + */ +namespace VuFind\Auth; +use VuFind\Config\Reader as ConfigReader, + VuFind\Exception\Auth as AuthException; + +/** + * Abstract authentication base class + * + * @category VuFind2 + * @package Authentication + * @author Franck Borel <franck.borel@gbv.de> + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://www.vufind.org Main Page + */ +abstract class AbstractBase +{ + protected $config; + + /** + * Constructor + * + * @param object $config Optional configuration object to pass through (loads + * default configuration if none specified). + */ + public function __construct($config = null) + { + $this->config = is_null($config) ? ConfigReader::getConfig() : $config; + } + + /** + * Attempt to authenticate the current user. Throws exception if login fails. + * + * @param Zend_Controller_Request_Abstract $request Request object containing + * account credentials. + * + * @throws VF_Exception_Auth + * @return Zend_Db_Table_Row_Abstract Object representing logged-in user. + */ + abstract public function authenticate($request); + + /** + * Has the user's login expired? + * + * @return bool + */ + public function isExpired() + { + // By default, logins do not expire: + return false; + } + + /** + * Create a new user account from the request. + * + * @param Zend_Controller_Request_Abstract $request Request object containing + * new account details. + * + * @throws AuthException + * @return Zend_Db_Table_Row_Abstract New user row. + */ + public function create($request) + { + throw new AuthException( + 'Account creation not supported by ' . get_class($this) + ); + } + + /** + * Get the URL to establish a session (needed when the internal VuFind login + * form is inadequate). Returns false when no session initiator is needed. + * + * @return bool|string + */ + public function getSessionInitiator() + { + return false; + } + + /** + * Perform cleanup at logout time. + * + * @param string $url URL to redirect user to after logging out. + * + * @return string Redirect URL (usually same as $url, but modified in + * some authentication modules). + */ + public function logout($url) + { + // No special cleanup or URL modification needed by default. + return $url; + } + + /** + * Does this authentication method support account creation? + * + * @return bool + */ + public function supportsCreation() + { + // By default, account creation is not supported. + return false; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Auth/Database.php b/module/VuFind/src/VuFind/Auth/Database.php new file mode 100644 index 0000000000000000000000000000000000000000..159be808e6b5e38f7c4f868a5f09ee078a8e5861 --- /dev/null +++ b/module/VuFind/src/VuFind/Auth/Database.php @@ -0,0 +1,151 @@ +<?php +/** + * Database authentication class + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Authentication + * @author Chris Hallberg <challber@villanova.edu> + * @author Franck Borel <franck.borel@gbv.de> + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/building_an_authentication_handler Wiki + */ +namespace VuFind\Auth; +use VuFind\Exception\Auth as AuthException; + +/** + * Database authentication class + * + * @category VuFind2 + * @package Authentication + * @author Chris Hallberg <challber@villanova.edu> + * @author Franck Borel <franck.borel@gbv.de> + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/building_an_authentication_handler Wiki + */ +class Database extends AbstractBase +{ + protected $username; + protected $password; + + /** + * Attempt to authenticate the current user. Throws exception if login fails. + * + * @param Zend_Controller_Request_Abstract $request Request object containing + * account credentials. + * + * @throws AuthException + * @return Zend_Db_Table_Row_Abstract Object representing logged-in user. + */ + public function authenticate($request) + { + // Make sure the credentials are non-blank: + $this->username = trim($request->getParam('username')); + $this->password = trim($request->getParam('password')); + if ($this->username == '' || $this->password == '') { + throw new AuthException('authentication_error_blank'); + } + + // Validate the credentials: + $user = VuFind_Model_Db_User::getByUsername($this->username, false); + if (!is_object($user) || !$user->checkPassword($this->password)) { + throw new AuthException('authentication_error_invalid'); + } + + // If we got this far, the login was successful: + return $user; + } + + /** + * Create a new user account from the request. + * + * @param Zend_Controller_Request_Abstract $request Request object containing + * new account details. + * + * @throws AuthException + * @return Zend_Db_Table_Row_Abstract New user row. + */ + public function create($request) + { + // Ensure that all expected parameters are populated to avoid notices + // in the code below. + $params = array( + 'firstname' => '', 'lastname' => '', 'username' => '', + 'password' => '', 'password2' => '', 'email' => '' + ); + foreach ($params as $param => $junk) { + $params[$param] = $request->getParam($param, ''); + } + + // Validate Input + // Needs a username + if (trim($params['username']) == '') { + throw new AuthException('Username cannot be blank'); + } + // Needs a password + if (trim($params['password']) == '') { + throw new AuthException('Password cannot be blank'); + } + // Passwords don't match + if ($params['password'] != $params['password2']) { + throw new AuthException('Passwords do not match'); + } + // Invalid Email Check + $validator = new Zend_Validate_EmailAddress(); + if (!$validator->isValid($params['email'])) { + throw new AuthException('Email address is invalid'); + } + + // Make sure we have a unique username + $table = new VuFind_Model_Db_User(); + if ($table->getByUsername($params['username'], false)) { + throw new AuthException('That username is already taken'); + } + // Make sure we have a unique email + if ($table->getByEmail($params['email'])) { + throw new AuthException('That email address is already used'); + } + + // If we got this far, we're ready to create the account: + $data = array( + 'username' => $params['username'], + 'password' => $params['password'], + 'firstname' => $params['firstname'], + 'lastname' => $params['lastname'], + 'email' => $params['email'], + 'created' => date('Y-m-d h:i:s') + ); + + // Create the row and send it back to the caller: + $table->insert($data); + return $table->getByUsername($params['username'], false); + } + + /** + * Does this authentication method support account creation? + * + * @return bool + */ + public function supportsCreation() + { + return true; + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Auth/Factory.php b/module/VuFind/src/VuFind/Auth/Factory.php new file mode 100644 index 0000000000000000000000000000000000000000..70dab59171abed5e0715f6166873d3e9206e7485 --- /dev/null +++ b/module/VuFind/src/VuFind/Auth/Factory.php @@ -0,0 +1,74 @@ +<?php +/** + * Factory class for constructing authentication modules. + * + * PHP version 5 + * + * Copyright (C) Villanova University 2010. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @category VuFind2 + * @package Authentication + * @author Franck Borel <franck.borel@gbv.de> + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/building_an_authentication_handler Wiki + */ +namespace VuFind\Auth; +use VuFind\Exception\Auth as AuthException; + +/** + * Factory class for constructing authentication modules. + * + * @category VuFind2 + * @package Authentication + * @author Franck Borel <franck.borel@gbv.de> + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link http://vufind.org/wiki/building_an_authentication_handler Wiki + */ +class Factory +{ + /** + * Initialize an authentication module. + * + * @param string $authNHandler The name of the module to initialize. + * @param object $config Optional configuration object to pass through + * (loads default configuration if none specified). + * + * @throws AuthException + * @return object + */ + static function getAuth($authNHandler, $config = null) + { + // Special handling for authentication classes that don't conform to the + // standard pattern (for legacy support): + if ($authNHandler == 'DB') { + $authNHandler = 'Database'; + } else if ($authNHandler == 'SIP') { + $authNHandler = 'SIP2'; + } + + // Load up the handler if a legal name has been supplied. + $className = 'VuFind\\Auth\\' . $authNHandler; + if (@class_exists($className)) { + return new $className($config); + } else { + throw new AuthException( + 'Authentication handler ' . $authNHandler . ' does not exist!' + ); + } + } +} \ No newline at end of file diff --git a/module/VuFind/src/VuFind/Bootstrap.php b/module/VuFind/src/VuFind/Bootstrap.php index 2b5d899111f16dd28324ef75af9860db5dae6c95..1c9da23e849a8ac314b14b38f1bf9cdef9feb9fe 100644 --- a/module/VuFind/src/VuFind/Bootstrap.php +++ b/module/VuFind/src/VuFind/Bootstrap.php @@ -26,7 +26,8 @@ * @link http://vufind.org Main Site */ namespace VuFind; -use VuFind\Config\Reader as ConfigReader, +use VuFind\Account\Manager as AccountManager, + VuFind\Config\Reader as ConfigReader, VuFind\Theme\Initializer as ThemeInitializer, Zend\Mvc\MvcEvent; /** @@ -42,6 +43,7 @@ class Bootstrap { protected $config; protected $event; + protected $events; /** * Constructor @@ -52,6 +54,7 @@ class Bootstrap { $this->config = ConfigReader::getConfig(); $this->event = $event; + $this->events = $event->getApplication()->events(); } /** @@ -61,9 +64,25 @@ class Bootstrap */ public function bootstrap() { + $this->initAccount(); $this->initTheme(); } + /** + * Make account manager available to views. + * + * @return void + */ + protected function initAccount() + { + $callback = function($event) { + $serviceManager = $event->getApplication()->getServiceManager(); + $viewModel = $serviceManager->get('viewmanager')->getViewModel(); + $viewModel->setVariable('account', AccountManager::getInstance()); + }; + $this->events->attach('dispatch', $callback); + } + /** * Set up theme handling. * @@ -71,10 +90,8 @@ class Bootstrap */ protected function initTheme() { - $events = $this->event->getApplication()->events(); - // Attach template injection configuration to the route event: - $events->attach( + $this->events->attach( 'route', array('VuFind\Theme\Initializer', 'configureTemplateInjection') ); @@ -84,6 +101,6 @@ class Bootstrap $theme = new ThemeInitializer($config, $event); $theme->init(); }; - $events->attach('dispatch', $callback); + $this->events->attach('dispatch', $callback); } } \ No newline at end of file