mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-04-04 12:03:44 +00:00
Compare commits
4 Commits
master
...
508-lock-s
| Author | SHA1 | Date | |
|---|---|---|---|
|
76d3612d33
|
|||
|
277e4fa490
|
|||
|
fe11780ad5
|
|||
|
c1e5346ef9
|
@@ -97,6 +97,12 @@ class StoredObject implements Document, TrackCreationInterface
|
|||||||
#[ORM\OneToMany(mappedBy: 'storedObject', targetEntity: StoredObjectVersion::class, cascade: ['persist'], orphanRemoval: true)]
|
#[ORM\OneToMany(mappedBy: 'storedObject', targetEntity: StoredObjectVersion::class, cascade: ['persist'], orphanRemoval: true)]
|
||||||
private Collection&Selectable $versions;
|
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
|
* @param StoredObject::STATUS_* $status
|
||||||
*/
|
*/
|
||||||
@@ -107,6 +113,41 @@ class StoredObject implements Document, TrackCreationInterface
|
|||||||
$this->uuid = Uuid::uuid4();
|
$this->uuid = Uuid::uuid4();
|
||||||
$this->versions = new ArrayCollection();
|
$this->versions = new ArrayCollection();
|
||||||
$this->prefix = self::generatePrefix();
|
$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 isLockedAt(\DateTimeImmutable $at): bool
|
||||||
|
{
|
||||||
|
foreach ($this->locks as $lock) {
|
||||||
|
if ($lock->isActiveAt($at)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Collection<int, StoredObjectLock>
|
||||||
|
*/
|
||||||
|
public function getLocks(): Collection
|
||||||
|
{
|
||||||
|
return $this->locks;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addGenerationTrial(): self
|
public function addGenerationTrial(): self
|
||||||
|
|||||||
132
src/Bundle/ChillDocStoreBundle/Entity/StoredObjectLock.php
Normal file
132
src/Bundle/ChillDocStoreBundle/Entity/StoredObjectLock.php
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the lock must be considered as active.
|
||||||
|
*
|
||||||
|
* A StoredObjectLock is active if there isn't any expiration date, or
|
||||||
|
* if the expiration date and time is before the given time.
|
||||||
|
*/
|
||||||
|
public function isActiveAt(\DateTimeImmutable $at): bool
|
||||||
|
{
|
||||||
|
return null === $this->getExpireAt() || $at < $this->getExpireAt();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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';
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
<?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\Tests\Entity;
|
||||||
|
|
||||||
|
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||||
|
use Chill\DocStoreBundle\Entity\StoredObjectLock;
|
||||||
|
use Chill\DocStoreBundle\Entity\StoredObjectLockMethodEnum;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* @covers \Chill\DocStoreBundle\Entity\StoredObjectLock
|
||||||
|
*/
|
||||||
|
class StoredObjectLockTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testIsActiveAtWithNoExpiration(): void
|
||||||
|
{
|
||||||
|
$storedObject = new StoredObject();
|
||||||
|
$now = new \DateTimeImmutable();
|
||||||
|
$lock = new StoredObjectLock(
|
||||||
|
$storedObject,
|
||||||
|
StoredObjectLockMethodEnum::WOPI,
|
||||||
|
$now,
|
||||||
|
'token',
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertTrue($lock->isActiveAt($now));
|
||||||
|
self::assertTrue($lock->isActiveAt($now->modify('+1 year')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsActiveAtWithFutureExpiration(): void
|
||||||
|
{
|
||||||
|
$storedObject = new StoredObject();
|
||||||
|
$now = new \DateTimeImmutable();
|
||||||
|
$expireAt = $now->modify('+1 hour');
|
||||||
|
$lock = new StoredObjectLock(
|
||||||
|
$storedObject,
|
||||||
|
StoredObjectLockMethodEnum::WOPI,
|
||||||
|
$now,
|
||||||
|
'token',
|
||||||
|
$expireAt
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertTrue($lock->isActiveAt($now));
|
||||||
|
self::assertTrue($lock->isActiveAt($now->modify('+30 minutes')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsActiveAtWithPastExpiration(): void
|
||||||
|
{
|
||||||
|
$storedObject = new StoredObject();
|
||||||
|
$now = new \DateTimeImmutable();
|
||||||
|
$expireAt = $now->modify('-1 hour');
|
||||||
|
$lock = new StoredObjectLock(
|
||||||
|
$storedObject,
|
||||||
|
StoredObjectLockMethodEnum::WOPI,
|
||||||
|
$now->modify('-2 hours'),
|
||||||
|
'token',
|
||||||
|
$expireAt
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertFalse($lock->isActiveAt($now));
|
||||||
|
self::assertFalse($lock->isActiveAt($expireAt));
|
||||||
|
self::assertFalse($lock->isActiveAt($expireAt->modify('+1 second')));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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');
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/Bundle/ChillMainBundle/Test/RandomUserTrait.php
Normal file
30
src/Bundle/ChillMainBundle/Test/RandomUserTrait.php
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<?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\MainBundle\Test;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Entity\User;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
trait RandomUserTrait
|
||||||
|
{
|
||||||
|
public function getRandomUser(EntityManagerInterface $em): User
|
||||||
|
{
|
||||||
|
$userRepository = $em->getRepository(User::class);
|
||||||
|
$count = $userRepository->count([]);
|
||||||
|
$random = mt_rand(0, $count - 1);
|
||||||
|
|
||||||
|
return $em->createQuery('SELECT u FROM '.User::class.' u ')
|
||||||
|
->setMaxResults(1)
|
||||||
|
->setFirstResult($random)
|
||||||
|
->getSingleResult();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,10 +13,19 @@ namespace Chill\WopiBundle\Service\Wopi;
|
|||||||
|
|
||||||
use ChampsLibres\WopiLib\Contract\Entity\Document;
|
use ChampsLibres\WopiLib\Contract\Entity\Document;
|
||||||
use ChampsLibres\WopiLib\Contract\Service\DocumentLockManagerInterface;
|
use ChampsLibres\WopiLib\Contract\Service\DocumentLockManagerInterface;
|
||||||
use Chill\MainBundle\Redis\ChillRedis;
|
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||||
|
use Chill\DocStoreBundle\Entity\StoredObjectLock;
|
||||||
|
use Chill\DocStoreBundle\Entity\StoredObjectLockMethodEnum;
|
||||||
|
use Chill\MainBundle\Entity\User;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Psr\Http\Message\RequestInterface;
|
use Psr\Http\Message\RequestInterface;
|
||||||
|
use Symfony\Component\Clock\ClockInterface;
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
use Symfony\Component\HttpKernel\Event\TerminateEvent;
|
||||||
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
use Webmozart\Assert\Assert;
|
||||||
|
|
||||||
class ChillDocumentLockManager implements DocumentLockManagerInterface
|
final class ChillDocumentLockManager implements DocumentLockManagerInterface, EventSubscriberInterface
|
||||||
{
|
{
|
||||||
private const LOCK_DURATION = 60 * 30;
|
private const LOCK_DURATION = 60 * 30;
|
||||||
|
|
||||||
@@ -25,26 +34,43 @@ class ChillDocumentLockManager implements DocumentLockManagerInterface
|
|||||||
*/
|
*/
|
||||||
private const LOCK_GRACEFUL_DURATION_TIME = 3;
|
private const LOCK_GRACEFUL_DURATION_TIME = 3;
|
||||||
|
|
||||||
|
private bool $mustFlush = false;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly ChillRedis $redis,
|
private readonly Security $security,
|
||||||
|
private readonly EntityManagerInterface $entityManager,
|
||||||
|
private readonly ClockInterface $clock,
|
||||||
private readonly int $ttlAfterDeleteSeconds = self::LOCK_GRACEFUL_DURATION_TIME,
|
private readonly int $ttlAfterDeleteSeconds = self::LOCK_GRACEFUL_DURATION_TIME,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
public static function getSubscribedEvents(): array
|
||||||
|
{
|
||||||
|
return [TerminateEvent::class => 'onKernelTerminate'];
|
||||||
|
}
|
||||||
|
|
||||||
public function deleteLock(Document $document, RequestInterface $request): bool
|
public function deleteLock(Document $document, RequestInterface $request): bool
|
||||||
{
|
{
|
||||||
if (0 === $this->redis->exists($this->getCacheId($document))) {
|
Assert::isInstanceOf($document, StoredObject::class);
|
||||||
return true;
|
|
||||||
|
foreach ($document->getLocks() as $lock) {
|
||||||
|
if ($lock->isActiveAt($this->clock->now())) {
|
||||||
|
$lock->setExpireAt($this->clock->now()->add(new \DateInterval('PT'.$this->ttlAfterDeleteSeconds.'S')));
|
||||||
|
$this->mustFlush = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// some queries (ex.: putFile) may be executed on the same time than the unlock, so
|
return true;
|
||||||
// we add a delay before unlocking the file, instead of deleting it immediatly
|
|
||||||
return $this->redis->expire($this->getCacheId($document), $this->ttlAfterDeleteSeconds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLock(Document $document, RequestInterface $request): string
|
public function getLock(Document $document, RequestInterface $request): string
|
||||||
{
|
{
|
||||||
if (false !== $value = $this->redis->get($this->getCacheId($document))) {
|
Assert::isInstanceOf($document, StoredObject::class);
|
||||||
return $value;
|
|
||||||
|
foreach ($document->getLocks() as $lock) {
|
||||||
|
if ($lock->isActiveAt($this->clock->now())) {
|
||||||
|
return $lock->getToken();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new \RuntimeException('wopi key does not exists');
|
throw new \RuntimeException('wopi key does not exists');
|
||||||
@@ -52,28 +78,61 @@ class ChillDocumentLockManager implements DocumentLockManagerInterface
|
|||||||
|
|
||||||
public function hasLock(Document $document, RequestInterface $request): bool
|
public function hasLock(Document $document, RequestInterface $request): bool
|
||||||
{
|
{
|
||||||
$r = $this->redis->exists($this->getCacheId($document));
|
Assert::isInstanceOf($document, StoredObject::class);
|
||||||
|
|
||||||
if (is_bool($r)) {
|
foreach ($document->getLocks() as $lock) {
|
||||||
return $r;
|
if ($lock->isActiveAt($this->clock->now())) {
|
||||||
}
|
return true;
|
||||||
if (is_int($r)) {
|
}
|
||||||
return $r > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new \RuntimeException('data type not supported');
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onKernelTerminate(TerminateEvent $event): void
|
||||||
|
{
|
||||||
|
if ($this->mustFlush) {
|
||||||
|
$this->entityManager->flush();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setLock(Document $document, string $lockId, RequestInterface $request): bool
|
public function setLock(Document $document, string $lockId, RequestInterface $request): bool
|
||||||
{
|
{
|
||||||
$key = $this->getCacheId($document);
|
Assert::isInstanceOf($document, StoredObject::class);
|
||||||
$this->redis->setex($key, self::LOCK_DURATION, $lockId);
|
$user = $this->security->getUser();
|
||||||
|
|
||||||
|
if ($document->isLockedAt($this->clock->now())) {
|
||||||
|
foreach ($document->getLocks() as $lock) {
|
||||||
|
if ($lock->isActiveAt($this->clock->now())) {
|
||||||
|
$lock->setToken($lockId);
|
||||||
|
$lock->setExpireAt($this->clock->now()->add(new \DateInterval('PT'.self::LOCK_DURATION.'S')));
|
||||||
|
if ($user instanceof User) {
|
||||||
|
$lock->addUser($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->mustFlush = true;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// there is no lock yet, we must create one
|
||||||
|
$lock = new StoredObjectLock(
|
||||||
|
$document,
|
||||||
|
method: StoredObjectLockMethodEnum::WOPI,
|
||||||
|
createdAt: $this->clock->now(),
|
||||||
|
token: $lockId,
|
||||||
|
expireAt: $this->clock->now()->add(new \DateInterval('PT'.self::LOCK_DURATION.'S')),
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($user instanceof User) {
|
||||||
|
$lock->addUser($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->entityManager->persist($lock);
|
||||||
|
$this->mustFlush = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getCacheId(Document $document): string
|
|
||||||
{
|
|
||||||
return sprintf('wopi_lib_lock_%s', $document->getWopiDocId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,15 @@ declare(strict_types=1);
|
|||||||
namespace Chill\WopiBundle\Tests\Service\Wopi;
|
namespace Chill\WopiBundle\Tests\Service\Wopi;
|
||||||
|
|
||||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||||
use Chill\MainBundle\Redis\ChillRedis;
|
use Chill\MainBundle\Test\RandomUserTrait;
|
||||||
use Chill\WopiBundle\Service\Wopi\ChillDocumentLockManager;
|
use Chill\WopiBundle\Service\Wopi\ChillDocumentLockManager;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
use Prophecy\PhpUnit\ProphecyTrait;
|
||||||
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Psr\Http\Message\RequestInterface;
|
use Psr\Http\Message\RequestInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
use Symfony\Component\Clock\MockClock;
|
||||||
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@@ -27,14 +31,31 @@ final class ChillDocumentLockManagerTest extends KernelTestCase
|
|||||||
{
|
{
|
||||||
use ProphecyTrait;
|
use ProphecyTrait;
|
||||||
|
|
||||||
|
use RandomUserTrait;
|
||||||
|
|
||||||
|
private MockClock $clock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ObjectProphecy<Security>
|
||||||
|
*/
|
||||||
|
private ObjectProphecy $security;
|
||||||
|
|
||||||
|
private EntityManagerInterface $em;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
self::bootKernel();
|
self::bootKernel();
|
||||||
|
$this->em = self::getContainer()->get('doctrine.orm.entity_manager');
|
||||||
|
$this->security = $this->prophesize(Security::class);
|
||||||
|
$this->clock = new MockClock();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRelock()
|
public function testRelock()
|
||||||
{
|
{
|
||||||
|
$user = $this->getRandomUser($this->em);
|
||||||
|
$this->security->getUser()->willReturn($user);
|
||||||
$manager = $this->makeManager(1);
|
$manager = $this->makeManager(1);
|
||||||
|
|
||||||
$document = new StoredObject();
|
$document = new StoredObject();
|
||||||
$request = $this->prophesize(RequestInterface::class);
|
$request = $this->prophesize(RequestInterface::class);
|
||||||
|
|
||||||
@@ -50,15 +71,22 @@ final class ChillDocumentLockManagerTest extends KernelTestCase
|
|||||||
|
|
||||||
$this->assertTrue($manager->deleteLock($document, $request->reveal()));
|
$this->assertTrue($manager->deleteLock($document, $request->reveal()));
|
||||||
|
|
||||||
sleep(3); // wait for redis to remove the key
|
$this->clock->sleep(10);
|
||||||
|
|
||||||
$this->assertFalse($manager->hasLock($document, $request->reveal()));
|
$this->assertFalse($manager->hasLock($document, $request->reveal()));
|
||||||
|
$this->em->remove($document);
|
||||||
|
$this->em->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSingleLock()
|
public function testSingleLock()
|
||||||
{
|
{
|
||||||
|
$user = $this->getRandomUser($this->em);
|
||||||
|
$this->security->getUser()->willReturn($user);
|
||||||
$manager = $this->makeManager(1);
|
$manager = $this->makeManager(1);
|
||||||
$document = new StoredObject();
|
$document = new StoredObject();
|
||||||
|
$this->em->persist($document);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
$request = $this->prophesize(RequestInterface::class);
|
$request = $this->prophesize(RequestInterface::class);
|
||||||
|
|
||||||
$this->assertFalse($manager->hasLock($document, $request->reveal()));
|
$this->assertFalse($manager->hasLock($document, $request->reveal()));
|
||||||
@@ -69,15 +97,16 @@ final class ChillDocumentLockManagerTest extends KernelTestCase
|
|||||||
|
|
||||||
$this->assertTrue($manager->deleteLock($document, $request->reveal()));
|
$this->assertTrue($manager->deleteLock($document, $request->reveal()));
|
||||||
|
|
||||||
sleep(3); // wait for redis to remove the key
|
$this->clock->sleep(10);
|
||||||
|
|
||||||
$this->assertFalse($manager->hasLock($document, $request->reveal()));
|
$this->assertFalse($manager->hasLock($document, $request->reveal()));
|
||||||
|
|
||||||
|
$this->em->remove($document);
|
||||||
|
$this->em->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function makeManager(int $ttlAfterDeleteSeconds = -1): ChillDocumentLockManager
|
private function makeManager(int $ttlAfterDeleteSeconds = -1): ChillDocumentLockManager
|
||||||
{
|
{
|
||||||
$redis = self::getContainer()->get(ChillRedis::class);
|
return new ChillDocumentLockManager($this->security->reveal(), $this->em, $this->clock, $ttlAfterDeleteSeconds);
|
||||||
|
|
||||||
return new ChillDocumentLockManager($redis, $ttlAfterDeleteSeconds);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user