Skip to content

Commit

Permalink
Correct postflush because authorization->getIdentity changes over tim…
Browse files Browse the repository at this point in the history
…e. Use the authorization to get the identity and not the identity at the time the postFlush is created
  • Loading branch information
TomHAnderson committed Oct 10, 2017
1 parent 1c44460 commit 2235c97
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
21 changes: 11 additions & 10 deletions src/EventListener/PostFlush.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace ZF\Doctrine\Audit\EventListener;

use Zend\Permissions\Rbac\AbstractRole as AbstractRbacRole;
use Zend\Authentication\AuthenticationService;

use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Query\ResultSetMapping;
use ZF\OAuth2\Doctrine\Identity\AuthenticatedIdentity as OAuth2AuthenticatedIdentity;
Expand All @@ -23,14 +24,14 @@
*/
final class PostFlush
{
private $identity;
private $authenticationService;
private $revisionComment;
private $enable = true;

public function __construct(RevisionComment $revisionComment, AbstractRbacRole $identity = null)
public function __construct(RevisionComment $revisionComment, AuthenticationService $authenticationService = null)
{
$this->revisionComment = $revisionComment;
$this->identity = $identity;
$this->authenticationService = $authenticationService;
}

public function postFlush(PostFlushEventArgs $args)
Expand All @@ -39,8 +40,8 @@ public function postFlush(PostFlushEventArgs $args)
$userName = 'guest';
$userEmail = '';

if ($this->identity instanceof OAuth2AuthenticatedIdentity) {
$user = $this->identity->getUser();
if ($this->authenticationService->getIdentity() instanceof OAuth2AuthenticatedIdentity) {
$user = $this->authenticationService->getIdentity()->getUser();

if (method_exists($user, 'getId')) {
$userId = $user->getId();
Expand All @@ -53,10 +54,10 @@ public function postFlush(PostFlushEventArgs $args)
if (method_exists($user, 'getEmail')) {
$userEmail = $user->getEmail();
}
} elseif ($this->identity instanceof AuthenticatedIdentity) {
$userId = $this->identity->getAuthenticationIdentity()['user_id'];
$userName = $this->identity->getName();
} elseif ($this->identity instanceof GuestIdentity) {
} elseif ($this->authenticationService->getIdentity() instanceof AuthenticatedIdentity) {
$userId = $this->authenticationService->getIdentity()->getAuthenticationIdentity()['user_id'];
$userName = $this->authenticationService->getIdentity()->getName();
} elseif ($this->authenticationService->getIdentity() instanceof GuestIdentity) {
} else {
// Is null or other identity
}
Expand Down
10 changes: 2 additions & 8 deletions src/EventListener/PostFlushFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ class PostFlushFactory implements
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
try {
$authentication = $container->get('authentication');
$identity = $authentication->getIdentity();
} catch (ServiceNotFoundException $e) {
$identity = null;
}

$authentication = $container->get('authentication');
$revisionComment = $container->get(RevisionComment::class);

return new $requestedName($revisionComment, $identity);
return new $requestedName($revisionComment, $authentication);
}
}

0 comments on commit 2235c97

Please sign in to comment.