Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions Subscribers/DoctrineEncryptSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ public function prePersist(LifecycleEventArgs $args) {
$this->processFields($entity);
}

/**
* Listen a postPresist lifecycle event.
*
* @param LifecycleEventArgs $args LifecycleEventArgs
*
* @return void
*/
public function postPersist(LifecycleEventArgs $args) {
$this->checkAndReloadEntities($args);
}

/**
* Listen a preUpdate lifecycle event. Checking and encrypt entities fields
* which have @Encrypted annotation. Using changesets to avoid preUpdate event
Expand All @@ -83,17 +94,42 @@ public function preUpdate(PreUpdateEventArgs $args) {
foreach ($properties as $refProperty) {
if ($this->annReader->getPropertyAnnotation($refProperty, self::ENCRYPTED_ANN_NAME)) {
$propName = $refProperty->getName();
$args->setNewValue($propName, $this->encryptor->encrypt($args->getNewValue($propName)));
if ($args->hasChangedField($propName)) {
$args->setNewValue($propName, $this->encryptor->encrypt($args->getNewValue($propName)));
}
}
}
}
/**
* Listen a postUpdate lifecycle event.
*
* @param LifecycleEventArgs $args LifecycleEventArgs
*
* @return void
*/
public function postUpdate(LifecycleEventArgs $args) {
$this->checkAndReloadEntities($args);
}

/**
* Listen a postLoad lifecycle event. Checking and decrypt entities
* which have @Encrypted annotations
* @param LifecycleEventArgs $args
* Listen a postLoad lifecycle event.
*
* @param LifecycleEventArgs $args LifecycleEventArgs
*
* @return void
*/
public function postLoad(LifecycleEventArgs $args) {
$this->checkAndReloadEntities($args);
}

/**
* Checking and decrypt entities which have @Encrypted annotations
*
* @param LifecycleEventArgs $args LifecycleEventArgs
*
* @return void
*/
private function checkAndReloadEntities(LifecycleEventArgs $args) {
$entity = $args->getEntity();
if (!$this->hasInDecodedRegistry($entity, $args->getEntityManager())) {
if ($this->processFields($entity, false)) {
Expand All @@ -109,7 +145,9 @@ public function postLoad(LifecycleEventArgs $args) {
public function getSubscribedEvents() {
return array(
Events::prePersist,
Events::postPersist,
Events::preUpdate,
Events::postUpdate,
Events::postLoad,
);
}
Expand Down