Skip to content

Loading…

Added configuration of Authentication Adapters to config and ChainFactory #163

Closed
wants to merge 23 commits into from

8 participants

@pdobrigkeit

Hi Evan,

my plan is to implement a module for a "Remember Me" Option on Login. My current idea is to just implement a Cookie Authentification Adapter and put that in the auth chain. That is why I created this Pull Request. From the architecture it seems that it was your intention to make it possible to have multiple adapters from the start.

Now I have a question about the next steps. In order to make this work the LoginAction would need to try and authenticate even if the request is not POST (because the cookie might be present none the less).

One possible approach I can think of would be refactoring the loginAction a little bit in order to run the authenticate action no matter what. Another way could be to configure options to check the request against. A third option would be to add a new route checking for POST and handling the form submission differently. Any suggestions or thoughts how to handle that?

@pdobrigkeit

A little further modifications for being able to just drop in a cookie adapter

@pdobrigkeit pdobrigkeit referenced this pull request
Closed

Remember me #136

basz and others added some commits
@basz basz fixes a problem with case sensitive class names 301f554
@jhuet jhuet Do not limit retrieval of redirect parameter to POST. 3a25a26
@jhuet jhuet Fix making redirect still retrievable by POST. c743473
@jhuet jhuet Provide same behavior for logout action. f0570e2
@atans atans Small typo fix
$locator => $serviceManager
ec9d33a
@parktrip parktrip Update src/ZfcUser/Authentication/Adapter/AbstractAdapter.php
mispelling "statisfied" in isSatisfied() method comment
622f784
@akrabat akrabat Merge pull request #141 from jakejohns/hotfix/registration-captcha
Fixed how Form\Base deals with captcha options
8c76984
@FredzL FredzL Synchronization of French translation with ZF2 570c712
@akrabat akrabat Merge pull request #194 from FredzL/master
Translation of the validation messages for AbstractRecord
ec2817c
@akrabat akrabat Merge pull request #192 from parktrip/patch-1
Update src/ZfcUser/Authentication/Adapter/AbstractAdapter.php
83475f9
@akrabat akrabat Merge pull request #186 from atans/patch-1
Small typo fix
20c603b
@akrabat akrabat Merge pull request #180 from jhuet/retrieving-redirect-from-query
Do not limit retrieval of redirect parameter to POST
35ef380
@akrabat akrabat Merge pull request #159 from basz/hotfix/zf2-develop-aware-change-for…
…-controller

accommodate ServiceLocatorAwareInterface
ee18443
@akrabat akrabat Merge pull request #185 from riceri/master
Change password and email while not logged in
84ddb23
@pdobrigkeit pdobrigkeit Added configuration of Authentication Adapters to config and ChainFac…
…tory
b5cd314
@pdobrigkeit pdobrigkeit Added logout Event and trigger
Chainable Adapter is registered to the events it offers
USer Controller calls logout on the adapters
2598f91
@pdobrigkeit pdobrigkeit Merge branch 'feature/configurableAdapterChain' of github.com:pdobrig…
…keit/ZfcUser into feature/configurableAdapterChain

Conflicts:
	src/ZfcUser/Options/ModuleOptions.php
6e98d5b
@pdobrigkeit

Hey,

just wanted to know if this has a chance of making it, or if you plan on adding something similar soon? Because it just took me a couple of hours figuring out that an update to ZF2 broke the ZfcUser, which I had to update and rebase my own changes, so that my modules keep working. And you merged a couple of PRs which caused conflicts with mine...

@pdobrigkeit

Hi, just noticed that the functionality I provided in the PR is now already part of ZfcUser, thus my other modules work out of the box. Closing.

@pdobrigkeit pdobrigkeit closed this
@pdobrigkeit

Or I was mistaken myself.

@pdobrigkeit pdobrigkeit reopened this
@akrabat akrabat added a commit that closed this pull request
@akrabat akrabat Merge branch '163'
Close #163
074e129
@akrabat akrabat closed this in 074e129
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 12, 2012
  1. @jakejohns
Commits on Oct 5, 2012
  1. @basz
  2. @basz
  3. @basz
Commits on Oct 12, 2012
  1. @pdobrigkeit
  2. @pdobrigkeit

    Added logout Event and trigger

    pdobrigkeit committed
    Chainable Adapter is registered to the events it offers
    USer Controller calls logout on the adapters
Commits on Nov 13, 2012
  1. @basz
Commits on Nov 14, 2012
  1. @jhuet
Commits on Nov 16, 2012
  1. @jhuet
  2. @jhuet
Commits on Nov 21, 2012
  1. @atans

    Small typo fix

    atans committed
    $locator => $serviceManager
Commits on Nov 30, 2012
  1. @parktrip

    Update src/ZfcUser/Authentication/Adapter/AbstractAdapter.php

    parktrip committed
    mispelling "statisfied" in isSatisfied() method comment
Commits on Dec 4, 2012
  1. @akrabat

    Merge pull request #141 from jakejohns/hotfix/registration-captcha

    akrabat committed
    Fixed how Form\Base deals with captcha options
Commits on Dec 5, 2012
  1. @FredzL
  2. @akrabat

    Merge pull request #194 from FredzL/master

    akrabat committed
    Translation of the validation messages for AbstractRecord
  3. @akrabat

    Merge pull request #192 from parktrip/patch-1

    akrabat committed
    Update src/ZfcUser/Authentication/Adapter/AbstractAdapter.php
  4. @akrabat

    Merge pull request #186 from atans/patch-1

    akrabat committed
    Small typo fix
  5. @akrabat

    Merge pull request #180 from jhuet/retrieving-redirect-from-query

    akrabat committed
    Do not limit retrieval of redirect parameter to POST
  6. @akrabat

    Merge pull request #159 from basz/hotfix/zf2-develop-aware-change-for…

    akrabat committed
    …-controller
    
    accommodate ServiceLocatorAwareInterface
  7. @akrabat

    Merge pull request #185 from riceri/master

    akrabat committed
    Change password and email while not logged in
Commits on Dec 7, 2012
  1. @pdobrigkeit
  2. @pdobrigkeit

    Added logout Event and trigger

    pdobrigkeit committed
    Chainable Adapter is registered to the events it offers
    USer Controller calls logout on the adapters
  3. @pdobrigkeit

    Merge branch 'feature/configurableAdapterChain' of github.com:pdobrig…

    pdobrigkeit committed
    …keit/ZfcUser into feature/configurableAdapterChain
    
    Conflicts:
    	src/ZfcUser/Options/ModuleOptions.php
View
17 Module.php
@@ -31,6 +31,23 @@ public function getConfig($env = null)
return include __DIR__ . '/config/module.config.php';
}
+ public function getControllerPluginConfig()
+ {
+ return array(
+ 'factories' => array(
+ 'zfcUserAuthentication' => function ($sm) {
+ $serviceLocator = $sm->getServiceLocator();
+ $authService = $serviceLocator->get('zfcuser_auth_service');
+ $authAdapter = $serviceLocator->get('ZfcUser\Authentication\Adapter\AdapterChain');
+ $controllerPlugin = new Controller\Plugin\ZfcUserAuthentication;
+ $controllerPlugin->setAuthService($authService);
+ $controllerPlugin->setAuthAdapter($authAdapter);
+ return $controllerPlugin;
+ },
+ ),
+ );
+ }
+
public function getViewHelperConfig()
{
return array(
View
5 config/module.config.php
@@ -10,11 +10,6 @@
'zfcuser' => 'ZfcUser\Controller\UserController',
),
),
- 'controller_plugins' => array(
- 'invokables' => array(
- 'zfcuserauthentication' => 'ZfcUser\Controller\Plugin\ZfcUserAuthentication',
- ),
- ),
'service_manager' => array(
'aliases' => array(
'zfcuser_zend_db_adapter' => 'Zend\Db\Adapter\Adapter',
View
20 config/zfcuser.global.php.dist
@@ -42,6 +42,16 @@ $settings = array(
*/
//'enable_username' => false,
+ /**
+ * Authentication Adapters
+ *
+ * Specify the adapters that will be used to try and authenticate the user
+ *
+ * Default value: array containing 'ZfcUser\Authentication\Adapter\Db' with priority 100
+ * Accepted values: array containing services that implement 'ZfcUser\Authentication\Adapter\ChainableAdapter'
+ */
+ 'auth_adapters' => array( 100 => 'ZfcUser\Authentication\Adapter\Db' ),
+
/**
* Enable Display Name
*
@@ -120,7 +130,7 @@ $settings = array(
/**
* Use Redirect Parameter If Present
*
- * Upon successful authentication, check for a 'redirect' POST parameter
+ * Upon successful authentication, check for a 'redirect' POST or GET parameter
*
* Accepted values: boolean true or false
*/
@@ -136,15 +146,15 @@ $settings = array(
/**
* Login Redirect Route
- *
+ *
* Upon successful login the user will be redirected to the entered route
- *
+ *
* Default value: 'zfcuser'
* Accepted values: A valid route name within your application
- *
+ *
*/
//'login_redirect_route' => 'zfcuser',
-
+
/**
* Logout Redirect Route
*
View
2 src/ZfcUser/Authentication/Adapter/AbstractAdapter.php
@@ -40,7 +40,7 @@ public function setStorage(Storage\StorageInterface $storage)
}
/**
- * Check if this adapter is satsified or not
+ * Check if this adapter is satisfied or not
*
* @return bool
*/
View
11 src/ZfcUser/Authentication/Adapter/AdapterChain.php
@@ -80,6 +80,17 @@ public function resetAdapters()
}
/**
+ * logoutAdapters
+ *
+ * @return AdapterChain
+ */
+ public function logoutAdapters()
+ {
+ //Adapters might need to perform additional cleanup after logout
+ $this->getEventManager()->trigger('logout', $this->getEvent());
+ }
+
+ /**
* Get the auth event
*
* @return AdapterChainEvent
View
20 src/ZfcUser/Authentication/Adapter/AdapterChainServiceFactory.php
@@ -10,8 +10,22 @@ class AdapterChainServiceFactory implements FactoryInterface
public function createService(ServiceLocatorInterface $serviceLocator)
{
$chain = new AdapterChain;
- $adapter = $serviceLocator->get('ZfcUser\Authentication\Adapter\Db');
- $chain->getEventManager()->attach('authenticate', array($adapter, 'authenticate'));
+
+ $options = $serviceLocator->get('zfcuser_module_options');
+
+ //iterate and attach multiple adapters and events if offered
+ foreach($options->getAuthAdapters() as $priority => $adapterName) {
+ $adapter = $serviceLocator->get($adapterName);
+
+ if(is_callable(array($adapter, 'authenticate'))) {
+ $chain->getEventManager()->attach('authenticate', array($adapter, 'authenticate'), $priority);
+ }
+
+ if(is_callable(array($adapter, 'logout'))) {
+ $chain->getEventManager()->attach('logout', array($adapter, 'logout'), $priority);
+ }
+ }
+
return $chain;
}
-}
+}
View
37 src/ZfcUser/Controller/Plugin/ZfcUserAuthentication.php
@@ -4,11 +4,10 @@
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Authentication\AuthenticationService;
-use Zend\ServiceManager\ServiceManagerAwareInterface;
-use Zend\ServiceManager\ServiceManager;
+use Zend\ServiceManager\ServiceLocatorInterface;
use ZfcUser\Authentication\Adapter\AdapterChain as AuthAdapter;
-class ZfcUserAuthentication extends AbstractPlugin implements ServiceManagerAwareInterface
+class ZfcUserAuthentication extends AbstractPlugin
{
/**
* @var AuthAdapter
@@ -21,10 +20,10 @@ class ZfcUserAuthentication extends AbstractPlugin implements ServiceManagerAwar
protected $authService;
/**
- * @var ServiceManager
+ * @var ServiceLocatorInterface
*/
- protected $serviceManager;
-
+ protected $serviceLocator;
+
/**
* Proxy convenience method
*
@@ -52,9 +51,6 @@ public function getIdentity()
*/
public function getAuthAdapter()
{
- if (null === $this->authAdapter) {
- $this->authAdapter = $this->getServiceManager()->get('ZfcUser\Authentication\Adapter\AdapterChain');
- }
return $this->authAdapter;
}
@@ -76,9 +72,6 @@ public function setAuthAdapter(AuthAdapter $authAdapter)
*/
public function getAuthService()
{
- if (null === $this->authService) {
- $this->authService = $this->getServiceManager()->get('zfcuser_auth_service');
- }
return $this->authService;
}
@@ -93,24 +86,4 @@ public function setAuthService(AuthenticationService $authService)
return $this;
}
- /**
- * Retrieve service manager instance
- *
- * @return ServiceManager
- */
- public function getServiceManager()
- {
- return $this->serviceManager->getServiceLocator();
- }
-
- /**
- * Set service manager instance
- *
- * @param ServiceManager $locator
- * @return void
- */
- public function setServiceManager(ServiceManager $serviceManager)
- {
- $this->serviceManager = $serviceManager;
- }
}
View
8 src/ZfcUser/Controller/UserController.php
@@ -98,9 +98,10 @@ public function loginAction()
public function logoutAction()
{
$this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
+ $this->zfcUserAuthentication()->getAuthAdapter()->logoutAdapters();
$this->zfcUserAuthentication()->getAuthService()->clearIdentity();
- $redirect = ($this->getRequest()->getPost()->get('redirect')) ? $this->getRequest()->getPost()->get('redirect') : false;
+ $redirect = $this->params()->fromPost('redirect', $this->params()->fromQuery('redirect', false));
if ($this->getOptions()->getUseRedirectParameterIfPresent() && $redirect) {
return $this->redirect()->toUrl($redirect);
@@ -117,11 +118,10 @@ public function authenticateAction()
if ($this->zfcUserAuthentication()->getAuthService()->hasIdentity()) {
return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
}
- $request = $this->getRequest();
$adapter = $this->zfcUserAuthentication()->getAuthAdapter();
- $redirect = $request->getPost()->get('redirect') ? $request->getPost()->get('redirect') : false;
+ $redirect = $this->params()->fromPost('redirect', $this->params()->fromQuery('redirect', false));
- $result = $adapter->prepareForAuthentication($request);
+ $result = $adapter->prepareForAuthentication($this->getRequest());
// Return early if an adapter returned a response
if ($result instanceof Response) {
View
4 src/ZfcUser/Form/Base.php
@@ -68,9 +68,7 @@ public function __construct()
'type' => 'Zend\Form\Element\Captcha',
'options' => array(
'label' => 'Please type the following text',
- ),
- 'attributes' => array(
- 'captcha' => $this->getOptions()->getFormCaptchaOptions(),
+ 'captcha' => $this->getRegistrationOptions()->getFormCaptchaOptions(),
),
));
}
View
29 src/ZfcUser/Options/ModuleOptions.php
@@ -61,6 +61,11 @@ class ModuleOptions extends AbstractOptions implements
/**
* @var array
*/
+ protected $authAdapters = array( 100 => 'ZfcUser\Authentication\Adapter\Db' );
+
+ /**
+ * @var array
+ */
protected $authIdentityFields = array( 'email' );
/**
@@ -197,7 +202,7 @@ public function getUserLoginWidgetViewTemplate()
{
return $this->userLoginWidgetViewTemplate;
}
-
+
/**
* set enable user registration
*
@@ -353,6 +358,28 @@ public function setAllowedLoginStates(Array $states)
}
/**
+ * set auth adapters
+ *
+ * @param array $authAdapterss
+ * @return ModuleOptions
+ */
+ public function setAuthAdapters($authAdapters)
+ {
+ $this->authAdapters = $authAdapters;
+ return $this;
+ }
+
+ /**
+ * get auth adapters
+ *
+ * @return array
+ */
+ public function getAuthAdapters()
+ {
+ return $this->authAdapters;
+ }
+
+ /**
* set auth identity fields
*
* @param array $authIdentityFields
View
2 src/ZfcUser/Service/User.php
@@ -273,7 +273,7 @@ public function getServiceManager()
/**
* Set service manager instance
*
- * @param ServiceManager $locator
+ * @param ServiceManager $serviceManager
* @return User
*/
public function setServiceManager(ServiceManager $serviceManager)
View
4 src/ZfcUser/Validator/AbstractRecord.php
@@ -17,8 +17,8 @@
* @var array Message templates
*/
protected $messageTemplates = array(
- self::ERROR_NO_RECORD_FOUND => "No record matching '%value%' was found",
- self::ERROR_RECORD_FOUND => "A record matching '%value%' was found",
+ self::ERROR_NO_RECORD_FOUND => "No record matching the input was found",
+ self::ERROR_RECORD_FOUND => "A record matching the input was found",
);
/**
Something went wrong with that request. Please try again.