Add locking mechanism for stored objects

- Created `StoredObjectLock` entity to manage locks on stored objects.
- Introduced `StoredObjectLockMethodEnum` to define locking methods.
- Added relationships between `StoredObject` and `StoredObjectLock` with appropriate methods for management.
- Added database migrations to create necessary tables and constraints for lock handling.
This commit is contained in:
2026-03-31 14:50:35 +02:00
parent 297628d06f
commit c1e5346ef9
4 changed files with 213 additions and 0 deletions

View File

@@ -97,6 +97,12 @@ class StoredObject implements Document, TrackCreationInterface
#[ORM\OneToMany(mappedBy: 'storedObject', targetEntity: StoredObjectVersion::class, cascade: ['persist'], orphanRemoval: true)]
private Collection&Selectable $versions;
/**
* @var Collection<int, StoredObjectLock>
*/
#[ORM\OneToMany(mappedBy: 'storedObject', targetEntity: StoredObjectLock::class, cascade: ['persist', 'remove', 'refresh', 'merge'])]
private Collection $locks;
/**
* @param StoredObject::STATUS_* $status
*/
@@ -107,6 +113,22 @@ class StoredObject implements Document, TrackCreationInterface
$this->uuid = Uuid::uuid4();
$this->versions = new ArrayCollection();
$this->prefix = self::generatePrefix();
$this->locks = new ArrayCollection();
}
/**
* @internal use @see{StoredObjectLock::__construct}
*/
public function addLock(StoredObjectLock $lock): void
{
if (!$this->locks->contains($lock)) {
$this->locks->add($lock);
}
}
public function removeLock(StoredObjectLock $lock): void
{
$this->locks->removeElement($lock);
}
public function addGenerationTrial(): self

View File

@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\Entity;
use Chill\MainBundle\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
#[ORM\Entity]
#[ORM\Table(name: 'stored_object_lock', schema: 'chill_doc')]
class StoredObjectLock
{
#[ORM\Id]
#[ORM\Column(type: 'uuid', unique: true)]
private UuidInterface $uuid;
/**
* @var Collection<int, User>
*/
#[ORM\ManyToMany(targetEntity: User::class)]
#[ORM\JoinTable(name: 'stored_object_lock_user', schema: 'chill_doc')]
#[ORM\JoinColumn(referencedColumnName: 'uuid', nullable: false)]
private Collection $users;
/**
* @param list<User> $users
*/
public function __construct(
#[ORM\ManyToOne(targetEntity: StoredObject::class, inversedBy: 'locks')]
private StoredObject $storedObject,
#[ORM\Column(type: 'string', length: 10, nullable: false, enumType: StoredObjectLockMethodEnum::class)]
private StoredObjectLockMethodEnum $method,
#[ORM\Column(type: 'datetimetz_immutable', nullable: false)]
private \DateTimeImmutable $createdAt,
#[ORM\Column(type: 'text', nullable: false, options: ['default' => ''])]
private string $token = '',
#[ORM\Column(type: 'datetimetz_immutable', nullable: true)]
private ?\DateTimeImmutable $expireAt = null,
array $users = [],
) {
$this->uuid = Uuid::uuid7();
$this->users = new ArrayCollection();
$this->storedObject->addLock($this);
foreach ($users as $user) {
$this->addUser($user);
}
}
public function addUser(User $user): void
{
if (!$this->users->contains($user)) {
$this->users->add($user);
}
}
public function removeUser(User $user): void
{
$this->users->removeElement($user);
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function setToken(string $token): void
{
$this->token = $token;
}
public function setExpireAt(?\DateTimeImmutable $expireAt): void
{
$this->expireAt = $expireAt;
}
public function getUuid(): UuidInterface
{
return $this->uuid;
}
public function getStoredObject(): StoredObject
{
return $this->storedObject;
}
public function getMethod(): StoredObjectLockMethodEnum
{
return $this->method;
}
public function getToken(): string
{
return $this->token;
}
public function getCreatedAt(): \DateTimeImmutable
{
return $this->createdAt;
}
public function getExpireAt(): ?\DateTimeImmutable
{
return $this->expireAt;
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\Entity;
enum StoredObjectLockMethodEnum: string
{
case WEBDAV = 'webdav';
case WOPI = 'wopi';
}

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Migrations\DocStore;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20260331122339 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add locks for stored objects';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE chill_doc.stored_object_lock (
uuid UUID NOT NULL, method VARCHAR(10) NOT NULL,
token TEXT DEFAULT \'\' NOT NULL, createdAt TIMESTAMP(0) WITH TIME ZONE NOT NULL,
expireAt TIMESTAMP(0) WITH TIME ZONE DEFAULT NULL, storedObject_id INT DEFAULT NULL,
PRIMARY KEY(uuid))');
$this->addSql('CREATE INDEX IDX_E66CB6516C99C13A ON chill_doc.stored_object_lock (storedObject_id)');
$this->addSql('COMMENT ON COLUMN chill_doc.stored_object_lock.uuid IS \'(DC2Type:uuid)\'');
$this->addSql('COMMENT ON COLUMN chill_doc.stored_object_lock.createdAt IS \'(DC2Type:datetimetz_immutable)\'');
$this->addSql('COMMENT ON COLUMN chill_doc.stored_object_lock.expireAt IS \'(DC2Type:datetimetz_immutable)\'');
$this->addSql('ALTER TABLE chill_doc.stored_object_lock ADD CONSTRAINT FK_E66CB6516C99C13A FOREIGN KEY (storedObject_id) REFERENCES chill_doc.stored_object (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE TABLE chill_doc.stored_object_lock_user (storedobjectlock_uuid UUID NOT NULL, user_id INT NOT NULL, PRIMARY KEY(storedobjectlock_uuid, user_id))');
$this->addSql('CREATE INDEX IDX_A4353741F52905D0 ON chill_doc.stored_object_lock_user (storedobjectlock_uuid)');
$this->addSql('CREATE INDEX IDX_A4353741A76ED395 ON chill_doc.stored_object_lock_user (user_id)');
$this->addSql('COMMENT ON COLUMN chill_doc.stored_object_lock_user.storedobjectlock_uuid IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE chill_doc.stored_object_lock_user ADD CONSTRAINT FK_A4353741F52905D0 FOREIGN KEY (storedobjectlock_uuid) REFERENCES chill_doc.stored_object_lock (uuid) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_doc.stored_object_lock_user ADD CONSTRAINT FK_A4353741A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_doc.stored_object_lock_user DROP CONSTRAINT FK_A4353741F52905D0');
$this->addSql('ALTER TABLE chill_doc.stored_object_lock_user DROP CONSTRAINT FK_A4353741A76ED395');
$this->addSql('DROP TABLE chill_doc.stored_object_lock_user');
$this->addSql('ALTER TABLE chill_doc.stored_object_lock DROP CONSTRAINT FK_E66CB6516C99C13A');
$this->addSql('DROP TABLE chill_doc.stored_object_lock');
}
}