-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from TomHAnderson/feature/revision-audit-tool
Created RevisionAuditTool
- Loading branch information
Showing
9 changed files
with
179 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace ZF\Doctrine\Audit\Persistence; | ||
|
||
use ZF\Doctrine\Audit\Tools\RevisionAuditTool; | ||
|
||
interface RevisionAuditToolAwareInterface | ||
{ | ||
public function setRevisionAuditTool(RevisionAuditTool $revisionAuditTool); | ||
public function getRevisionAuditTool(): RevisionAuditTool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace ZF\Doctrine\Audit\Persistence; | ||
|
||
use ZF\Doctrine\Audit\Tools\RevisionAuditTool; | ||
|
||
trait RevisionAuditToolAwareTrait | ||
{ | ||
protected $revisionAuditTool; | ||
|
||
public function setRevisionAuditTool(RevisionAuditTool $revisionAuditTool) | ||
{ | ||
$this->revisionAuditTool = $revisionAuditTool; | ||
|
||
return $this; | ||
} | ||
|
||
public function getRevisionAuditTool(): RevisionAuditTool | ||
{ | ||
return $this->revisionAuditTool; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace ZF\Doctrine\Audit\Tools; | ||
|
||
use Zend\View\Renderer\RendererInterface; | ||
use Zend\Authentication\AuthenticationService; | ||
use Doctrine\Common\Persistence\ObjectManager; | ||
use Doctrine\ORM\Query\ResultSetMapping; | ||
use ZF\OAuth2\Doctrine\Identity\AuthenticatedIdentity as OAuth2AuthenticatedIdentity; | ||
use ZF\MvcAuth\Identity\AuthenticatedIdentity; | ||
use ZF\MvcAuth\Identity\GuestIdentity; | ||
use ZF\Doctrine\Audit\Persistence; | ||
use ZF\Doctrine\Audit\AuditOptions; | ||
use ZF\Doctrine\Audit\RevisionComment; | ||
|
||
final class RevisionAuditTool implements | ||
Persistence\ObjectManagerAwareInterface, | ||
Persistence\RevisionCommentAwareInterface | ||
{ | ||
use Persistence\ObjectManagerAwareTrait; | ||
use Persistence\RevisionCommentAwareTrait; | ||
|
||
private $authenticationService; | ||
|
||
public function __construct( | ||
ObjectManager $objectManager, | ||
RevisionComment $revisionComment, | ||
AuthenticationService $authenticationService = null | ||
) { | ||
$this->setObjectManager($objectManager); | ||
$this->setRevisionComment($revisionComment); | ||
$this->authenticationService = $authenticationService; | ||
} | ||
|
||
/** | ||
* Close a revision - can be done manually or automatically via postFlush | ||
*/ | ||
public function close() | ||
{ | ||
$userId = 0; | ||
$userName = 'guest'; | ||
$userEmail = ''; | ||
|
||
if ($this->authenticationService) { | ||
if ($this->authenticationService->getIdentity() instanceof OAuth2AuthenticatedIdentity) { | ||
$user = $this->authenticationService->getIdentity()->getUser(); | ||
|
||
if (method_exists($user, 'getId')) { | ||
$userId = $user->getId(); | ||
} | ||
|
||
if (method_exists($user, 'getDisplayName')) { | ||
$userName = $user->getDisplayName(); | ||
} | ||
|
||
if (method_exists($user, 'getEmail')) { | ||
$userEmail = $user->getEmail(); | ||
} | ||
} 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 | ||
} | ||
} | ||
|
||
$query = $this->getObjectManager() | ||
->createNativeQuery( | ||
" | ||
SELECT close_revision_audit(:userId, :userName, :userEmail, :comment) | ||
", | ||
new ResultSetMapping() | ||
) | ||
->setParameter('userId', $userId) | ||
->setParameter('userName', $userName) | ||
->setParameter('userEmail', $userEmail) | ||
->setParameter('comment', $this->revisionComment->getComment()); | ||
; | ||
|
||
$query->getResult(); | ||
|
||
// Reset the revision comment | ||
$this->revisionComment->setComment(''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace ZF\Doctrine\Audit\Tools; | ||
|
||
use Exception; | ||
use Interop\Container\ContainerInterface; | ||
use Zend\ServiceManager\Factory\FactoryInterface; | ||
use ZF\Doctrine\Audit\AuditOptions; | ||
use ZF\Doctrine\Audit\RevisionComment; | ||
|
||
class RevisionAuditToolFactory implements | ||
FactoryInterface | ||
{ | ||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | ||
{ | ||
$config = $container->get('config')['zf-doctrine-audit']; | ||
|
||
$objectManager = $container->get($config['target_object_manager']); | ||
$revisionComment = $container->get(RevisionComment::class); | ||
|
||
$authentication = null; | ||
try { | ||
$authentication = $container->get('authentication'); | ||
} catch (Exception $e) { | ||
} | ||
|
||
return new $requestedName($objectManager, $revisionComment, $authentication); | ||
} | ||
} |