code: 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);
      }
docs: Updates permissions on embedded files.
  
  @param OnFlushEventArgs $args