1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JwPersistentUser; |
4
|
|
|
|
5
|
|
|
use JwPersistentUser\Listener\WriteTokenToCookie, |
6
|
|
|
JwPersistentUser\Service\CookieAuthenticationService; |
7
|
|
|
|
8
|
|
|
use Zend\ModuleManager\Feature, |
9
|
|
|
Zend\EventManager\EventManager, |
10
|
|
|
Zend\EventManager\EventInterface; |
11
|
|
|
|
12
|
|
|
class Module implements |
13
|
|
|
Feature\ConfigProviderInterface, |
14
|
|
|
Feature\BootstrapListenerInterface, |
15
|
|
|
Feature\AutoloaderProviderInterface |
16
|
|
|
{ |
17
|
|
|
public function getConfig() |
18
|
|
|
{ |
19
|
|
|
return include __DIR__ . '/config/module.config.php'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function onBootstrap(EventInterface $e) |
23
|
|
|
{ |
24
|
|
|
/** @var EventManager $em */ |
25
|
|
|
$em = $e->getApplication()->getEventManager(); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$request = $e->getApplication()->getRequest(); |
|
|
|
|
28
|
|
|
$response = $e->getApplication()->getResponse(); |
|
|
|
|
29
|
|
|
|
30
|
|
|
// Write token to cookie after valid authentication |
31
|
|
|
$placeCookie = new WriteTokenToCookie; |
32
|
|
|
$placeCookie->setRequest($request); |
33
|
|
|
$placeCookie->setResponse($response); |
34
|
|
|
$placeCookie->setServiceLocator($e->getApplication()->getServiceManager()); |
|
|
|
|
35
|
|
|
$em->getSharedManager()->attachAggregate($placeCookie); |
|
|
|
|
36
|
|
|
|
37
|
|
|
// Try to login from Cookie if applicable |
38
|
|
|
$service = new CookieAuthenticationService; |
39
|
|
|
$service->setServiceLocator($e->getApplication()->getServiceManager()); |
|
|
|
|
40
|
|
|
$service->loginFrom($request, $response); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getAutoloaderConfig() |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
'Zend\Loader\StandardAutoloader' => [ |
47
|
|
|
'namespaces' => [ |
48
|
|
|
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: