Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add view helper and controller plugin to pull the current identity from ... #2650

Closed
wants to merge 2 commits into from

5 participants

@superdweebie

...an AuthenticationService

@superdweebie superdweebie referenced this pull request in doctrine/DoctrineModule
Merged

Add authentication storage factory #95

@Ocramius
Collaborator

Nice to see that nothing got wasted :)
Anyway, I think you can just use mock objects instead of adding assets. Either way is fine by me anyway...

@superdweebie

Yeah, I'm still getting the hang of mock objects.

@bakura10 bakura10 commented on the diff
library/Zend/Mvc/Controller/Plugin/Identity.php
((10 lines not shown))
+
+namespace Zend\Mvc\Controller\Plugin;
+
+use Zend\Authentication\AuthenticationService;
+use Zend\Mvc\Exception;
+
+/**
+ * Controller plugin to fetch the authenticated identity.
+ *
+ * @category Zend
+ * @package Zend_Mvc
+ * @subpackage Controller
+ */
+class Identity extends AbstractPlugin
+{
+
@bakura10
bakura10 added a note

Remove this empty line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bakura10 bakura10 commented on the diff
library/Zend/Mvc/Controller/Plugin/Identity.php
((12 lines not shown))
+
+use Zend\Authentication\AuthenticationService;
+use Zend\Mvc\Exception;
+
+/**
+ * Controller plugin to fetch the authenticated identity.
+ *
+ * @category Zend
+ * @package Zend_Mvc
+ * @subpackage Controller
+ */
+class Identity extends AbstractPlugin
+{
+
+ /**
+ *
@bakura10
bakura10 added a note

Remove this empty line (and all the others for the properties and functions, we don't have an initial empty lines in CS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bakura10 bakura10 commented on the diff
library/Zend/Mvc/Controller/Plugin/Identity.php
((28 lines not shown))
+ * @var \Zend\Authentication\AuthenticationService
+ */
+ protected $authenticationService;
+
+ /**
+ *
+ * @return \Zend\Authentication\AuthenticationService
+ */
+ public function getAuthenticationService()
+ {
+ return $this->authenticationService;
+ }
+
+ /**
+ *
+ * @param \Zend\Authentication\AuthenticationService $authetnicationService
@bakura10
bakura10 added a note

Typo in the name (you wrote autheTNication). Moreover, as you added the use statement, you can simply use "AuthenticationService" as well in each docblock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney

This should also include a factor in the HelperPluginManager for injecting the AuthenticationService instance. (I'll likely add this on merge). Otherwise, looks very good!

@bakura10

I thought about that Matthew (injecting the AuthenticationService in the constructor as it is a hard depencency). However how can you fetch the AuthenticationService in the ServiceManager ? Is there a standard key for it ?

@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#2650] CS fixes
- Removed unnecessary empty lines and empty docblock lines
- Removed unnecessary imports (and resolved class names relative to existing
  imports or current namespace)
- Ensured all docblocks were present
- Minor flow fixes
7f86eac
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#2650] Added factories for identity plugins
- In View HelperPluginManager and Mvc Controller\PluginManager
- Includes tests for expected behavior
9150bcc
@weierophinney

@bakura10 Typically, if the service is not a core MVC service, we define it using the FQCN (we do this with translator, db adapter, and navigation already). I did the same here -- I have it check for Zend\Authentication\AuthenticationService, and, if present, inject it.

@bakura10
@internalsystemerror

Can I request an amendment to allow controllers to easily call clearIdentity() by using the controller plugin?. What I'm thinking, in the controller plugin class, is:

public function __invoke($clear = false)
{
    /** check for authenticationService ommited **/
    if (!$this->authenticationService->hasIdentity()) {
        return null; // Should this return false if $this->identity() will be used to check for valid id?
    }
    if (true === clear) {
        return $this->authenticationService()->clearIdentity();
    }
    return $this->authenticationService()->getIdentity();
}

So I've included an optional boolean to check whether the id should be cleared or returned. Thoughts?

ise

@superdweebie

It's not a bad idea, but it think it might be better to use another method, rather than __invoke, for clarity. @internalsystemerror if we use your suggestion, then this would clear the identity:

$this->identity(true)

Which I think it confusing. I would prefer:

$this->identity()->clear()

Also, if this change is made, the controller plugin should also be updated.

@internalsystemerror

@superdweebie I agree. But with using __invoke() and returning an entity from getIdentity(), this will require another layer, hence why i thought to avoid that? I can fork develop and make that change if required?

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#2650] CS fixes
- Removed unnecessary empty lines and empty docblock lines
- Removed unnecessary imports (and resolved class names relative to existing
  imports or current namespace)
- Ensured all docblocks were present
- Minor flow fixes
0cef3a3
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#2650] Added factories for identity plugins
- In View HelperPluginManager and Mvc Controller\PluginManager
- Includes tests for expected behavior
5e83a48
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-view
@weierophinney weierophinney [zendframework/zf2#2650] CS fixes
- Removed unnecessary empty lines and empty docblock lines
- Removed unnecessary imports (and resolved class names relative to existing
  imports or current namespace)
- Ensured all docblocks were present
- Minor flow fixes
db5d423
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-view
@weierophinney weierophinney [zendframework/zf2#2650] Added factories for identity plugins
- In View HelperPluginManager and Mvc Controller\PluginManager
- Includes tests for expected behavior
a7503a6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 3, 2012
  1. @superdweebie
  2. @superdweebie

    Fix EOF

    superdweebie authored
This page is out of date. Refresh to see the latest.
View
65 library/Zend/Mvc/Controller/Plugin/Identity.php
@@ -0,0 +1,65 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace Zend\Mvc\Controller\Plugin;
+
+use Zend\Authentication\AuthenticationService;
+use Zend\Mvc\Exception;
+
+/**
+ * Controller plugin to fetch the authenticated identity.
+ *
+ * @category Zend
+ * @package Zend_Mvc
+ * @subpackage Controller
+ */
+class Identity extends AbstractPlugin
+{
+
@bakura10
bakura10 added a note

Remove this empty line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ /**
+ *
@bakura10
bakura10 added a note

Remove this empty line (and all the others for the properties and functions, we don't have an initial empty lines in CS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * @var \Zend\Authentication\AuthenticationService
+ */
+ protected $authenticationService;
+
+ /**
+ *
+ * @return \Zend\Authentication\AuthenticationService
+ */
+ public function getAuthenticationService()
+ {
+ return $this->authenticationService;
+ }
+
+ /**
+ *
+ * @param \Zend\Authentication\AuthenticationService $authetnicationService
@bakura10
bakura10 added a note

Typo in the name (you wrote autheTNication). Moreover, as you added the use statement, you can simply use "AuthenticationService" as well in each docblock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ */
+ public function setAuthenticationService(AuthenticationService $authenticationService)
+ {
+ $this->authenticationService = $authenticationService;
+ }
+
+ /**
+ *
+ * @return mixed | null
+ * @throws Exception\RuntimeException
+ */
+ public function __invoke()
+ {
+ if ( ! $this->authenticationService instanceof AuthenticationService){
+ throw new Exception\RuntimeException('No AuthenticationService instance provided');
+ }
+ if ($this->authenticationService->hasIdentity()) {
+ return $this->authenticationService->getIdentity();
+ }
+ return null;
+ }
+}
View
65 library/Zend/View/Helper/Identity.php
@@ -0,0 +1,65 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_View
+ */
+
+namespace Zend\View\Helper;
+
+use Zend\Authentication\AuthenticationService;
+use Zend\View\Exception;
+
+/**
+ * View helper plugin to fetch the authenticated identity.
+ *
+ * @category Zend
+ * @package Zend_View
+ * @subpackage Helper
+ */
+class Identity extends AbstractHelper
+{
+
+ /**
+ *
+ * @var \Zend\Authentication\AuthenticationService
+ */
+ protected $authenticationService;
+
+ /**
+ *
+ * @return \Zend\Authentication\AuthenticationService
+ */
+ public function getAuthenticationService()
+ {
+ return $this->authenticationService;
+ }
+
+ /**
+ *
+ * @param \Zend\Authentication\AuthenticationService $authetnicationService
+ */
+ public function setAuthenticationService(AuthenticationService $authenticationService)
+ {
+ $this->authenticationService = $authenticationService;
+ }
+
+ /**
+ *
+ * @return mixed | null
+ * @throws Exception\RuntimeException
+ */
+ public function __invoke()
+ {
+ if ( ! $this->authenticationService instanceof AuthenticationService){
+ throw new Exception\RuntimeException('No AuthenticationService instance provided');
+ }
+ if ($this->authenticationService->hasIdentity()) {
+ return $this->authenticationService->getIdentity();
+ }
+ return null;
+ }
+}
View
51 tests/ZendTest/Mvc/Controller/Plugin/IdentityTest.php
@@ -0,0 +1,51 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_View
+ */
+
+namespace ZendTest\Mvc\Controller\Plugin;
+
+use Zend\Authentication\AuthenticationService;
+use Zend\Authentication\Storage\NonPersistent as NonPersistentStorage;
+use Zend\Mvc\Controller\Plugin\Identity as IdentityPlugin;
+use ZendTest\Mvc\Controller\Plugin\TestAsset\IdentityObject;
+use ZendTest\Mvc\Controller\Plugin\TestAsset\AuthenticationAdapter;
+
+/**
+ * Tests Identity plugin
+ *
+ * @category Zend
+ * @package Zend_Mvc
+ * @subpackage UnitTests
+ */
+class IdentityTest extends \PHPUnit_Framework_TestCase
+{
+
+ public function testGetIdentity()
+ {
+
+ $identity = new IdentityObject();
+ $identity->setUsername('a username');
+ $identity->setPassword('a password');
+
+ $authenticationService = new AuthenticationService(new NonPersistentStorage, new AuthenticationAdapter);
+
+ $identityPlugin = new IdentityPlugin;
+ $identityPlugin->setAuthenticationService($authenticationService);
+
+ $this->assertNull($identityPlugin());
+
+ $this->assertFalse($authenticationService->hasIdentity());
+
+ $authenticationService->getAdapter()->setIdentity($identity);
+ $result = $authenticationService->authenticate();
+ $this->assertTrue($result->isValid());
+
+ $this->assertEquals($identity, $identityPlugin());
+ }
+}
View
30 tests/ZendTest/Mvc/Controller/Plugin/TestAsset/AuthenticationAdapter.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_View
+ */
+
+namespace ZendTest\Mvc\Controller\Plugin\TestAsset;
+
+use Zend\Authentication\Adapter\AdapterInterface;
+use Zend\Authentication\Result;
+
+class AuthenticationAdapter implements AdapterInterface
+{
+
+ protected $identity;
+
+ public function setIdentity($identity)
+ {
+ $this->identity = $identity;
+ }
+
+ public function authenticate()
+ {
+ return new Result(Result::SUCCESS, $this->identity);
+ }
+}
View
56 tests/ZendTest/Mvc/Controller/Plugin/TestAsset/IdentityObject.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_View
+ */
+
+namespace ZendTest\Mvc\Controller\Plugin\TestAsset;
+
+class IdentityObject
+{
+ /**
+ * @var string|null
+ */
+ protected $username;
+
+ /**
+ * @var string|null
+ */
+ protected $password;
+
+ /**
+ * @param string $password
+ */
+ public function setPassword($password)
+ {
+ $this->password = (string) $password;
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getPassword()
+ {
+ return $this->password;
+ }
+
+ /**
+ * @param string $username
+ */
+ public function setUsername($username)
+ {
+ $this->username = (string) $username;
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getUsername()
+ {
+ return $this->username;
+ }
+}
View
57 tests/ZendTest/View/Helper/IdentityTest.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_View
+ */
+
+namespace ZendTest\View\Helper;
+
+use Zend\Authentication\AuthenticationService;
+use Zend\Authentication\Storage\NonPersistent as NonPersistentStorage;
+use Zend\View\Helper\Identity as IdentityHelper;
+use ZendTest\View\Helper\TestAsset\IdentityObject;
+use ZendTest\View\Helper\TestAsset\AuthenticationAdapter;
+
+/**
+ * Zend_View_Helper_IdentityTest
+ *
+ * Tests Identity helper
+ *
+ * @category Zend
+ * @package Zend_View
+ * @subpackage UnitTests
+ * @group Zend_View
+ * @group Zend_View_Helper
+ */
+
+class IdentityTest extends \PHPUnit_Framework_TestCase
+{
+
+ public function testGetIdentity()
+ {
+
+ $identity = new IdentityObject();
+ $identity->setUsername('a username');
+ $identity->setPassword('a password');
+
+
+ $authenticationService = new AuthenticationService(new NonPersistentStorage, new AuthenticationAdapter);
+
+ $identityHelper = new IdentityHelper;
+ $identityHelper->setAuthenticationService($authenticationService);
+
+ $this->assertNull($identityHelper());
+
+ $this->assertFalse($authenticationService->hasIdentity());
+
+ $authenticationService->getAdapter()->setIdentity($identity);
+ $result = $authenticationService->authenticate();
+ $this->assertTrue($result->isValid());
+
+ $this->assertEquals($identity, $identityHelper());
+ }
+}
View
30 tests/ZendTest/View/Helper/TestAsset/AuthenticationAdapter.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_View
+ */
+
+namespace ZendTest\View\Helper\TestAsset;
+
+use Zend\Authentication\Adapter\AdapterInterface;
+use Zend\Authentication\Result;
+
+class AuthenticationAdapter implements AdapterInterface
+{
+
+ protected $identity;
+
+ public function setIdentity($identity)
+ {
+ $this->identity = $identity;
+ }
+
+ public function authenticate()
+ {
+ return new Result(Result::SUCCESS, $this->identity);
+ }
+}
View
56 tests/ZendTest/View/Helper/TestAsset/IdentityObject.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_View
+ */
+
+namespace ZendTest\View\Helper\TestAsset;
+
+class IdentityObject
+{
+ /**
+ * @var string|null
+ */
+ protected $username;
+
+ /**
+ * @var string|null
+ */
+ protected $password;
+
+ /**
+ * @param string $password
+ */
+ public function setPassword($password)
+ {
+ $this->password = (string) $password;
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getPassword()
+ {
+ return $this->password;
+ }
+
+ /**
+ * @param string $username
+ */
+ public function setUsername($username)
+ {
+ $this->username = (string) $username;
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getUsername()
+ {
+ return $this->username;
+ }
+}
Something went wrong with that request. Please try again.