-
Notifications
You must be signed in to change notification settings - Fork 9
/
Module.php
58 lines (50 loc) · 1.97 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace LdcZfcUserOAuth2;
use Zend\Mvc\MvcEvent;
use ZF\MvcAuth\MvcAuthEvent;
use ZF\MvcAuth\Identity\AuthenticatedIdentity;
use ZfcUser\Service\User as ZfcUserService;
use ZfcUser\Entity\UserInterface as ZfcUserEntity;
class Module
{
public function onBootstrap(MvcEvent $e)
{
// Short-circuit ZfcUser's inbuilt user session storage mechanism
// as we're relying solely on zf-mvc-auth to handle that for us
$storage = new \Zend\Authentication\Storage\NonPersistent();
$sm = $e->getApplication()->getServiceManager();
$sm->get('ZfcUser\Authentication\Storage\Db')->setStorage($storage);
$sm->get('ZfcUser\Authentication\Adapter\Db')->setStorage($storage);
// Inject authenticated user from zf-mvc-auth into ZfcUser so it's
// built-in session and user checking still function properly
$zfcUserService = $sm->get('zfcuser_user_service');
$em = $e->getApplication()->getEventManager();
$em->attach(MvcAuthEvent::EVENT_AUTHENTICATION_POST, function (MvcAuthEvent $e) use ($zfcUserService, $storage) {
$identity = $e->getIdentity();
if ( ! $identity instanceof AuthenticatedIdentity ) {
return;
}
$token = $identity->getAuthenticationIdentity();
$uid = $token['user_id'];
$user = $zfcUserService->getUserMapper()->findById($uid);
if ( ! $user instanceof ZfcUserEntity ) {
return;
}
$storage->write($user->getId());
});
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src'
),
),
);
}
}