Skip to content
Snippets Groups Projects
Commit 6a58a335 authored by Demian Katz's avatar Demian Katz
Browse files

Began wiring up account manager (still work in progress).

parent b4c56d13
No related merge requests found
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
* @link http://www.vufind.org Main Page * @link http://www.vufind.org Main Page
*/ */
namespace VuFind\Account; 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; Zend\Registry, Zend\Session, Zend\Session\Container as SessionContainer;
/** /**
...@@ -51,11 +51,9 @@ class Manager ...@@ -51,11 +51,9 @@ class Manager
public function __construct() public function __construct()
{ {
$this->config = ConfigReader::getConfig(); $this->config = ConfigReader::getConfig();
/* TODO: $this->auth = AuthFactory::getAuth(
$this->auth = VF_Auth_Factory::getAuth(
$this->config->Authentication->method, $this->config $this->config->Authentication->method, $this->config
); );
*/
$this->session = new SessionContainer('Account'); $this->session = new SessionContainer('Account');
} }
...@@ -139,6 +137,7 @@ class Manager ...@@ -139,6 +137,7 @@ class Manager
} }
return !$catalog->loginIsHidden(); return !$catalog->loginIsHidden();
*/ */
return true;
} }
/** /**
......
<?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
<?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
<?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
...@@ -26,7 +26,8 @@ ...@@ -26,7 +26,8 @@
* @link http://vufind.org Main Site * @link http://vufind.org Main Site
*/ */
namespace VuFind; namespace VuFind;
use VuFind\Config\Reader as ConfigReader, use VuFind\Account\Manager as AccountManager,
VuFind\Config\Reader as ConfigReader,
VuFind\Theme\Initializer as ThemeInitializer, VuFind\Theme\Initializer as ThemeInitializer,
Zend\Mvc\MvcEvent; Zend\Mvc\MvcEvent;
/** /**
...@@ -42,6 +43,7 @@ class Bootstrap ...@@ -42,6 +43,7 @@ class Bootstrap
{ {
protected $config; protected $config;
protected $event; protected $event;
protected $events;
/** /**
* Constructor * Constructor
...@@ -52,6 +54,7 @@ class Bootstrap ...@@ -52,6 +54,7 @@ class Bootstrap
{ {
$this->config = ConfigReader::getConfig(); $this->config = ConfigReader::getConfig();
$this->event = $event; $this->event = $event;
$this->events = $event->getApplication()->events();
} }
/** /**
...@@ -61,9 +64,25 @@ class Bootstrap ...@@ -61,9 +64,25 @@ class Bootstrap
*/ */
public function bootstrap() public function bootstrap()
{ {
$this->initAccount();
$this->initTheme(); $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. * Set up theme handling.
* *
...@@ -71,10 +90,8 @@ class Bootstrap ...@@ -71,10 +90,8 @@ class Bootstrap
*/ */
protected function initTheme() protected function initTheme()
{ {
$events = $this->event->getApplication()->events();
// Attach template injection configuration to the route event: // Attach template injection configuration to the route event:
$events->attach( $this->events->attach(
'route', array('VuFind\Theme\Initializer', 'configureTemplateInjection') 'route', array('VuFind\Theme\Initializer', 'configureTemplateInjection')
); );
...@@ -84,6 +101,6 @@ class Bootstrap ...@@ -84,6 +101,6 @@ class Bootstrap
$theme = new ThemeInitializer($config, $event); $theme = new ThemeInitializer($config, $event);
$theme->init(); $theme->init();
}; };
$events->attach('dispatch', $callback); $this->events->attach('dispatch', $callback);
} }
} }
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment