Skip to content

Commit

Permalink
fix: Trigger lifecycle hooks of FileMetaData manually
Browse files Browse the repository at this point in the history
This is a workaround for the fact, that Doctrine seems to not
trigger the lifecycle hooks when "uploading" files to a repository.
  • Loading branch information
TiSiE committed Aug 5, 2021
1 parent e5b7abb commit fac56bc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
3 changes: 2 additions & 1 deletion module/Core/src/Entity/FileMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Class FileMetadata
*
* @ODM\EmbeddedDocument
* @ODM\HasLifecycleCallbacks
*
* @author Anthonius Munthi <me@itstoni.com>
* @since 0.36
Expand All @@ -18,4 +19,4 @@
class FileMetadata implements FileMetadataInterface
{
use FileMetadataTrait;
}
}
4 changes: 4 additions & 0 deletions module/Core/src/Entity/FileMetadataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public function getPermissions(): ?PermissionsInterface;
public function setContentType(string $contentType);

public function getContentType(): ?string;

public function preventPersistingAnonymousUser(): void;

public function restoreAnonymousUser(): void;
}
51 changes: 48 additions & 3 deletions module/Core/src/Entity/FileMetadataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Core\Entity;


use Auth\Entity\AnonymousUser;
use Auth\Entity\UserInterface;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

Expand All @@ -22,6 +22,13 @@ trait FileMetadataTrait
*/
protected ?UserInterface $user = null;

/**
* @ODM\Field(type="string", nullable=true)
*/
protected ?string $userToken;

protected ?AnonymousUser $anonymousUser;

/**
* @ODM\EmbedOne(targetDocument="Core\Entity\Permissions")
*/
Expand Down Expand Up @@ -63,6 +70,10 @@ public function setContentType(?string $contentType)
*/
public function getUser(): ?UserInterface
{
if (!$this->user && $this->userToken) {
$this->user = new AnonymousUser($this->userToken);
}

return $this->user;
}

Expand All @@ -74,18 +85,52 @@ public function setUser(?UserInterface $user): self
$this->user = $user;
$this->getPermissions()->grant($user, Permissions::PERMISSION_ALL);

if ($user instanceof AnonymousUser) {
$this->userToken = $user->getToken();
} elseif ($this->userToken) {
$this->userToken = null;
}

return $this;
}

/**
*
* @ODM\PrePersist
* @ODM\PreUpdate
* @ODM\PreFlush
*/
public function preventPersistingAnonymousUser(): void
{
if ($this->user instanceof AnonymousUser) {
$this->anonymousUser = $this->user;
$this->user = null;
}
}

/**
*
* @ODM\PostPersist
* @ODM\PostUpdate
*/
public function restoreAnonymousUser(): void
{
if ($this->anonymousUser) {
$this->user = $this->anonymousUser;
$this->anonymousUser = null;
}
}

/**
* @return PermissionsInterface|null
*/
public function getPermissions(): ?PermissionsInterface
{
if (!$this->permissions) {
$perms = new Permissions();
if ($this->user instanceof UserInterface) {
$perms->grant($this->user, PermissionsInterface::PERMISSION_ALL);
$user = $this->getUser();
if ($user instanceof UserInterface) {
$perms->grant($user, PermissionsInterface::PERMISSION_ALL);
}
$this->setPermissions($perms);
}
Expand Down
7 changes: 6 additions & 1 deletion module/Core/src/Service/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ public function uploadFromFile(string $entityClass, FileMetadataInterface $metad

$options = new UploadOptions();
$options->metadata = $metadata;
$metadata->preventPersistingAnonymousUser();

return $repo->uploadFromFile($source, $fileName, $options);
$result = $repo->uploadFromFile($source, $fileName, $options);

$metadata->restoreAnonymousUser();

return $result;
}

/**
Expand Down

0 comments on commit fac56bc

Please sign in to comment.