Update setLock to reuse existing lock without generating new lock ID

- Modified `StoredObjectLockManager` logic to prevent unnecessary lock ID regeneration when updating existing locks.
- Added `testSetLockExistingUpdatesLockWithoutNewLockId` to verify behavior.
- Ensured `persist` is not called again for existing locks.
This commit is contained in:
2026-04-08 21:32:45 +02:00
parent 4a224054e2
commit 25962e0e39
2 changed files with 29 additions and 5 deletions

View File

@@ -72,10 +72,6 @@ class StoredObjectLockManager implements EventSubscriberInterface
?\DateTimeImmutable $expiresAt = null,
array $users = [],
): StoredObjectLock {
if (null === $lockId) {
$lockId = 'opaquelocktoken:'.Uuid::uuid4();
}
if (null === $expiresAt) {
$expiresAt = $this->clock->now()->add(new \DateInterval('PT60M'));
}
@@ -83,7 +79,9 @@ class StoredObjectLockManager implements EventSubscriberInterface
if ($document->isLockedAt($this->clock->now())) {
foreach ($document->getLocks() as $lock) {
if ($lock->isActiveAt($this->clock->now())) {
$lock->setToken($lockId);
if (null !== $lockId) {
$lock->setToken($lockId);
}
$lock->setExpireAt($expiresAt);
foreach ($users as $user) {
$lock->addUser($user);
@@ -96,6 +94,10 @@ class StoredObjectLockManager implements EventSubscriberInterface
}
}
if (null === $lockId) {
$lockId = 'opaquelocktoken:'.Uuid::uuid4();
}
// there is no lock yet, we must create one
$lock = new StoredObjectLock(
$document,

View File

@@ -144,6 +144,28 @@ class StoredObjectLockManagerTest extends TestCase
$this->assertContains($user, $lock->getUsers());
}
public function testSetLockExistingUpdatesLockWithoutNewLockId(): void
{
$document = new StoredObject();
$initialExpire = $this->clock->now()->add(new \DateInterval('PT10M'));
$lock = new StoredObjectLock($document, StoredObjectLockMethodEnum::WEBDAV, $this->clock->now(), 'initial-token', $initialExpire);
$newExpire = $this->clock->now()->add(new \DateInterval('PT20M'));
$user = new User();
// Should NOT call persist again
$this->entityManager->persist(Argument::any())->shouldNotBeCalled();
$result = $this->manager->setLock($document, StoredObjectLockMethodEnum::WOPI, null, $newExpire, [$user]);
$this->assertInstanceOf(StoredObjectLock::class, $result);
$this->assertCount(1, $document->getLocks());
$this->assertSame($lock, $document->getLocks()->first());
$this->assertSame('initial-token', $lock->getToken());
$this->assertSame($newExpire, $lock->getExpireAt());
$this->assertContains($user, $lock->getUsers());
}
public function testDeleteLock(): void
{
$document = new StoredObject();