/** * * @param OnFlushEventArgs $eventArgs * @throws Sds\DoctrineExtensions\Exception\BadMethodCallException */ public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); $eventManager = $documentManager->getEventManager(); foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { $changeSet = $unitOfWork->getDocumentChangeSet($document); $metadata = $documentManager->getClassMetadata(get_class($document)); foreach ($changeSet as $field => $change) { $old = $change[0]; $new = $change[1]; // Check for change and readonly annotation if (!isset($metadata->fieldMappings[$field]['readonly']) || $old == null || $old == $new) { continue; } // Raise preReadonlyRollback if ($eventManager->hasListeners(ReadonlyEvents::preReadonlyRollback)) { $eventManager->dispatchEvent(ReadonlyEvents::preReadonlyRollback, new EventArgs($field, $old, $new, $document, $documentManager)); $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); $changeSet = $unitOfWork->getDocumentChangeSet($document); $new = $changeSet[$field][1]; // Continue if value has been changed back to old. if ($old == $new) { continue; } } $metadata->reflFields[$field]->setValue($document, $old); $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); // Raise postReadonlyRollback if ($eventManager->hasListeners(ReadonlyEvents::postReadonlyRollback)) { $eventManager->dispatchEvent(ReadonlyEvents::postReadonlyRollback, new EventArgs($field, $old, $new, $document, $documentManager)); } } } }
/** * Updates fiile permissions on Flush * * @param OnFlushEventArgs $eventArgs * @return boolean */ public function onFlush(OnFlushEventArgs $eventArgs) { $dm = $eventArgs->getDocumentManager(); $uow = $dm->getUnitOfWork(); $filter = function ($element) { return $element instanceof ApplicationInterface && $element->getPermissions()->hasChanged(); }; $inserts = array_filter($uow->getScheduledDocumentInsertions(), $filter); $updates = array_filter($uow->getScheduledDocumentUpdates(), $filter); foreach (array($inserts, $updates) as $isUpdate => $documents) { foreach ($documents as $document) { $permissions = $document->getPermissions(); foreach ($document->getAttachments() as $attachment) { $attachment->getPermissions()->clear()->inherit($permissions); if ($isUpdate) { $uow->computeChangeSet($dm->getClassMetadata(get_class($attachment)), $attachment); } } if ($image = $document->contact->image) { $image->getPermissions()->clear()->inherit($permissions); if ($isUpdate) { $uow->computeChangeSet($dm->getClassMetadata(get_class($image)), $image); } } } } }
/** * * @param \Doctrine\ODM\MongoDB\Event\OnFlushEventArgs $eventArgs */ public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); $documentValidator = $this->getDocumentValidator(); foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { $metadata = $documentManager->getClassMetadata(get_class($document)); $validatorResult = $documentValidator->isValid($document, $metadata); if (!$validatorResult->getResult()) { // Updates to invalid documents are not allowed. Roll them back $unitOfWork->detach($document); $eventManager = $documentManager->getEventManager(); // Raise invalidUpdate if ($eventManager->hasListeners(Events::invalidUpdate)) { $eventManager->dispatchEvent(Events::invalidUpdate, new EventArgs($document, $documentManager, $validatorResult->getMessages())); } } } foreach ($unitOfWork->getScheduledDocumentInsertions() as $document) { $metadata = $documentManager->getClassMetadata(get_class($document)); $validatorResult = $documentValidator->isValid($document, $metadata); if (!$validatorResult->getResult()) { //stop creation $unitOfWork->detach($document); $eventManager = $documentManager->getEventManager(); // Raise invalidCreate if ($eventManager->hasListeners(Events::invalidCreate)) { $eventManager->dispatchEvent(Events::invalidCreate, new EventArgs($document, $documentManager, $validatorResult->getMessages())); } } } }
/** * * @param OnFlushEventArgs $eventArgs */ public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { $changeSet = $unitOfWork->getDocumentChangeSet($document); $metadata = $documentManager->getClassMetadata(get_class($document)); foreach ($changeSet as $field => $change) { $old = $change[0]; $new = $change[1]; // Check for change if ($old == null || $old == $new) { continue; } if (!isset($new) || $new == '') { continue; } $requireRecompute = false; if (isset($metadata->crypt['hash']) && isset($metadata->crypt['hash'][$field])) { HashService::hashField($field, $document, $metadata); $requireRecompute = true; } elseif (isset($metadata->crypt['blockCipher']) && isset($metadata->crypt['blockCipher'][$field])) { BlockCipherService::encryptField($field, $document, $metadata); $requireRecompute = true; } if ($requireRecompute) { $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); } } } }
public function onFlush(OnFlushEventArgs $args) { $uow = $args->getDocumentManager()->getUnitOfWork(); foreach ($uow->getScheduledDocumentUpdates() as $document) { $uow->clearDocumentChangeSet(spl_object_hash($document)); } }
/** * * @param \Doctrine\ODM\MongoDB\Event\OnFlushEventArgs $eventArgs */ public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); $softDeleter = $this->getSoftDeleter(); foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { $metadata = $documentManager->getClassMetadata(get_class($document)); if (!isset($metadata->softDelete) || !($field = $metadata->softDelete['flag'])) { continue; } $eventManager = $documentManager->getEventManager(); $changeSet = $unitOfWork->getDocumentChangeSet($document); if (!isset($changeSet[$field])) { if ($softDeleter->isSoftDeleted($document)) { // Updates to softDeleted documents are not allowed. Roll them back $unitOfWork->clearDocumentChangeSet(spl_object_hash($document)); // Raise softDeletedUpdateDenied if ($eventManager->hasListeners(SoftDeleteEvents::softDeletedUpdateDenied)) { $eventManager->dispatchEvent(SoftDeleteEvents::softDeletedUpdateDenied, new LifecycleEventArgs($document, $documentManager)); } continue; } else { continue; } } if ($changeSet[$field][1]) { // Trigger soft delete events // Raise preSoftDelete if ($eventManager->hasListeners(SoftDeleteEvents::preSoftDelete)) { $eventManager->dispatchEvent(SoftDeleteEvents::preSoftDelete, new LifecycleEventArgs($document, $documentManager)); } if ($softDeleter->isSoftDeleted($document)) { // Raise postSoftDelete if ($eventManager->hasListeners(SoftDeleteEvents::postSoftDelete)) { $eventManager->dispatchEvent(SoftDeleteEvents::postSoftDelete, new LifecycleEventArgs($document, $documentManager)); } } else { // Soft delete has been rolled back $metadata = $documentManager->getClassMetadata(get_class($document)); $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); } } else { // Trigger restore events // Raise preRestore if ($eventManager->hasListeners(SoftDeleteEvents::preRestore)) { $eventManager->dispatchEvent(SoftDeleteEvents::preRestore, new LifecycleEventArgs($document, $documentManager)); } if (!$softDeleter->isSoftDeleted($document)) { // Raise postRestore if ($eventManager->hasListeners(SoftDeleteEvents::postRestore)) { $eventManager->dispatchEvent(SoftDeleteEvents::postRestore, new LifecycleEventArgs($document, $documentManager)); } } else { // Restore has been rolled back $metadata = $documentManager->getClassMetadata(get_class($document)); $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); } } } }
public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); foreach ($unitOfWork->getScheduledDocumentInsertions() as $document) { $unitOfWork->detach($document); } }
public function onFlush(OnFlushEventArgs $args) { $dm = $args->getDocumentManager(); foreach ($dm->getUnitOfWork()->getScheduledDocumentInsertions() as $document) { $document->setName('name #changed'); $metadata = $dm->getClassMetadata(get_class($document)); $dm->getUnitOfWork()->recomputeSingleDocumentChangeSet($metadata, $document); } }
public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { $metadata = $documentManager->getClassMetadata(get_class($document)); $document->addPhonenumber(new Phonenumber('2222')); $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); } }
/** * Updates permissions on embedded files. * * @param OnFlushEventArgs $args */ public function onFlush(OnFlushEventArgs $args) { $dm = $args->getDocumentManager(); $uow = $dm->getUnitOfWork(); $filter = function ($element) { return $element instanceof $this->targetDocument && $element instanceof PermissionsAwareInterface && $element->getPermissions()->hasChanged(); }; $inserts = array_filter($uow->getScheduledDocumentInsertions(), $filter); $updates = array_filter($uow->getScheduledDocumentUpdates(), $filter); $this->process($inserts, $dm, $uow, true); $this->process($updates, $dm, $uow); }
public function onFlush(OnFlushEventArgs $args) { $dm = $args->getDocumentManager(); $uow = $dm->getUnitOfWork(); foreach (array_merge($uow->getScheduledDocumentInsertions(), $uow->getScheduledDocumentUpserts()) as $document) { $document->setValue('value 2'); $metadata = $dm->getClassMetadata(get_class($document)); $dm->getUnitOfWork()->recomputeSingleDocumentChangeSet($metadata, $document); if ($uow->isScheduledForUpdate($document)) { throw new \Exception('Document should not be scheduled for update!'); } } }
/** * * @param \Doctrine\ODM\MongoDB\Event\OnFlushEventArgs $eventArgs */ public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); foreach ($unitOfWork->getScheduledDocumentInsertions() as $document) { $this->create($document, $documentManager); } foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { $this->update($document, $documentManager); } foreach ($unitOfWork->getScheduledDocumentDeletions() as $document) { $this->delete($document, $documentManager); } }
public function onFlush(OnFlushEventArgs $args) { $dm = $args->getDocumentManager(); $uow = $dm->getUnitOfWork(); foreach ($uow->getScheduledDocumentInsertions() as $document) { $this->inserts++; if ($document instanceof GH1138Document) { $document->name .= '-changed'; $cm = $dm->getClassMetadata(GH1138Document::CLASSNAME); $uow->recomputeSingleDocumentChangeSet($cm, $document); } } foreach ($uow->getScheduledDocumentUpdates() as $document) { $this->updates++; } }
public function onFlush(OnFlushEventArgs $eventArgs) { $dm = $eventArgs->getDocumentManager(); $unitOfWork = $dm->getUnitOfWork(); foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { if (!method_exists($document, 'addHistoryEvent')) { continue; } $historyRecord = $this->createEvent(); $historyRecord->setType(HistoryEventType::RECORD_UPDATED); $historyRecord->setTitle('Record updated'); $changeSet = $unitOfWork->getDocumentChangeSet($document); $historyRecord->setInfo(substr($this->getDetailedUpdateInfo($changeSet), 0, 100000)); $document->addHistoryEvent($historyRecord); $dm->persist($historyRecord); // Prevent a recursion of the history events change becoming another unit of work to // record as history again and again. $unitOfWork->recomputeSingleDocumentChangeSet($dm->getClassMetadata(get_class($document)), $document); } }
/** * * @param \Doctrine\ODM\MongoDB\Event\OnFlushEventArgs $eventArgs */ public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); $eventManager = $documentManager->getEventManager(); //Check update permissions foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { $metadata = $documentManager->getClassMetadata(get_class($document)); // Check for roles changes if (!isset($metadata->roles)) { continue; } $changeSet = $unitOfWork->getDocumentChangeSet($document); if (!isset($changeSet[$metadata->roles])) { continue; } $old = $changeSet[$metadata->roles][0]; $new = $changeSet[$metadata->roles][1]; $eventArgs = new EventArgs($old, $new, $document, $documentManager); // Raise preUpdateRoles if ($eventManager->hasListeners(Events::preUpdateRoles)) { $eventManager->dispatchEvent(Events::preUpdateRoles, $eventArgs); } if ($metadata->reflFields[$metadata->roles]->getValue($document) == $old) { //Roll back changes and continue $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); continue; } // Raise onUpdateRoles if ($eventManager->hasListeners(Events::onUpdateRoles)) { $eventManager->dispatchEvent(Events::onUpdateRoles, $eventArgs); } // Force change set update $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); // Raise postUpdateRoles if ($eventManager->hasListeners(Events::postUpdateRoles)) { $eventManager->dispatchEvent(Events::postUpdateRoles, $eventArgs); } } }
/** * * @param \Doctrine\ODM\MongoDB\Event\OnFlushEventArgs $eventArgs */ public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { $metadata = $documentManager->getClassMetadata(get_class($document)); if (!isset($metadata->state)) { continue; } $eventManager = $documentManager->getEventManager(); $changeSet = $unitOfWork->getDocumentChangeSet($document); $field = $metadata->state; if (!isset($changeSet[$field])) { continue; } $fromState = $changeSet[$field][0]; $toState = $changeSet[$field][1]; // Raise preTransition if ($eventManager->hasListeners(StateEvents::preTransition)) { $eventManager->dispatchEvent(StateEvents::preTransition, new TransitionEventArgs(new Transition($fromState, $toState), $document, $documentManager)); } if ($document->getState() == $fromState) { //State change has been rolled back $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); continue; } // Raise onTransition if ($eventManager->hasListeners(StateEvents::onTransition)) { $eventManager->dispatchEvent(StateEvents::onTransition, new TransitionEventArgs(new Transition($fromState, $toState), $document, $documentManager)); } // Force change set update $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); // Raise postTransition - this is when workflow vars should be updated if ($eventManager->hasListeners(StateEvents::postTransition)) { $eventManager->dispatchEvent(StateEvents::postTransition, new TransitionEventArgs(new Transition($fromState, $toState), $document, $documentManager)); } } }
/** * * @param \Doctrine\ODM\MongoDB\Event\OnFlushEventArgs $eventArgs */ public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); $eventManager = $documentManager->getEventManager(); $freezer = $this->getFreezer(); foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { $metadata = $documentManager->getClassMetadata(get_class($document)); if (!isset($metadata->freeze) || !($field = $metadata->freeze['flag'])) { continue; } $changeSet = $unitOfWork->getDocumentChangeSet($document); if (!isset($changeSet[$field])) { if ($freezer->isFrozen($document)) { // Updates to frozen documents are not allowed. Roll them back $unitOfWork->clearDocumentChangeSet(spl_object_hash($document)); // Raise frozenUpdateDenied if ($eventManager->hasListeners(Events::frozenUpdateDenied)) { $eventManager->dispatchEvent(Events::frozenUpdateDenied, new AccessControlEventArgs($document, $documentManager, 'update')); } continue; } else { continue; } } if ($changeSet[$field][1]) { // Trigger freeze events // Raise preFreeze if ($eventManager->hasListeners(Events::preFreeze)) { $eventManager->dispatchEvent(Events::preFreeze, new LifecycleEventArgs($document, $documentManager)); } if ($freezer->isFrozen($document)) { // Raise postFreeze if ($eventManager->hasListeners(Events::postFreeze)) { $eventManager->dispatchEvent(Events::postFreeze, new LifecycleEventArgs($document, $documentManager)); } } else { // Freeze has been rolled back $metadata = $documentManager->getClassMetadata(get_class($document)); $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); } } else { // Trigger thaw events // Raise preThaw if ($eventManager->hasListeners(Events::preThaw)) { $eventManager->dispatchEvent(Events::preThaw, new LifecycleEventArgs($document, $documentManager)); } if (!$freezer->isFrozen($document)) { // Raise postThaw if ($eventManager->hasListeners(Events::postThaw)) { $eventManager->dispatchEvent(Events::postThaw, new LifecycleEventArgs($document, $documentManager)); } } else { // Thaw has been rolled back $metadata = $documentManager->getClassMetadata(get_class($document)); $unitOfWork->recomputeSingleDocumentChangeSet($metadata, $document); } } } foreach ($unitOfWork->getScheduledDocumentDeletions() as $document) { $metadata = $documentManager->getClassMetadata(get_class($document)); if (!isset($metadata->freeze) || !($field = $metadata->freeze['flag']) || !$freezer->isFrozen($document)) { continue; } // Deletions of frozen documents are not allowed. Roll them back $documentManager->persist($document); // Raise frozenDeleteDenied if ($eventManager->hasListeners(Events::frozenDeleteDenied)) { $eventManager->dispatchEvent(Events::frozenDeleteDenied, new AccessControlEventArgs($document, $documentManager, 'delete')); } } }
/** * * @param \Doctrine\ODM\MongoDB\Event\OnFlushEventArgs $eventArgs */ public function onFlush(OnFlushEventArgs $eventArgs) { $documentManager = $eventArgs->getDocumentManager(); $unitOfWork = $documentManager->getUnitOfWork(); $eventManager = $documentManager->getEventManager(); $accessController = $this->getAccessController(); foreach ($unitOfWork->getScheduledDocumentInsertions() as $document) { //Check create permissions if (!$accessController->isAllowed(Actions::create, null, $document)->getIsAllowed()) { //stop creation $metadata = $documentManager->getClassMetadata(get_class($document)); if ($metadata->isEmbeddedDocument) { list($mapping, $parent) = $unitOfWork->getParentAssociation($document); $parentMetadata = $documentManager->getClassMetadata(get_class($parent)); if ($mapping['type'] == 'many') { $collection = $parentMetadata->reflFields[$mapping['fieldName']]->getValue($parent); $collection->removeElement($document); $unitOfWork->recomputeSingleDocumentChangeSet($parentMetadata, $parent); } else { $parentMetadata->reflFields[$mapping->field]->setValue($document, null); } } $unitOfWork->detach($document); if ($eventManager->hasListeners(AccessControlEvents::createDenied)) { $eventManager->dispatchEvent(AccessControlEvents::createDenied, new EventArgs($document, $documentManager, Actions::create)); } } } //Check update permissions foreach ($unitOfWork->getScheduledDocumentUpdates() as $document) { $metadata = $documentManager->getClassMetadata(get_class($document)); if (isset($metadata->accessControl['ignoreUpdate'])) { // Skip any updates on fields marked with @AccessControl\UpdateIgnore $changeSet = $unitOfWork->getDocumentChangeSet($document); $checkPermission = false; foreach ($changeSet as $field => $change) { if (!in_array($field, $metadata->accessControl['ignoreUpdate'])) { $checkPermission = true; break; } } } else { $checkPermission = true; } if ($checkPermission && !$accessController->isAllowed(Actions::update, null, $document)->getIsAllowed()) { //roll back changes if (!isset($changeSet)) { $changeSet = $unitOfWork->getDocumentChangeSet($document); } foreach ($changeSet as $field => $change) { $metadata->reflFields[$field]->setValue($document, $change[0]); } //stop updates $unitOfWork->clearDocumentChangeSet(spl_object_hash($document)); if ($eventManager->hasListeners(AccessControlEvents::updateDenied)) { $eventManager->dispatchEvent(AccessControlEvents::updateDenied, new EventArgs($document, $documentManager, Actions::update)); } continue; } } //Check delete permsisions foreach ($unitOfWork->getScheduledDocumentDeletions() as $document) { if (!$accessController->isAllowed(Actions::delete, null, $document)->getIsAllowed()) { //stop delete $documentManager->persist($document); if ($eventManager->hasListeners(AccessControlEvents::deleteDenied)) { $eventManager->dispatchEvent(AccessControlEvents::deleteDenied, new EventArgs($document, $documentManager, Actions::delete)); } } } }