Skip to content

Commit

Permalink
Merge pull request #439 from nextcloud/backport/391/stable28
Browse files Browse the repository at this point in the history
[stable28] Allow force unlock of automated client locks
  • Loading branch information
juliusknorr authored Dec 4, 2024
2 parents 30c6b83 + faf8859 commit 389e16a
Show file tree
Hide file tree
Showing 13 changed files with 2,317 additions and 249 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
\.idea/

/build/
/css/
/js/
/vendor/
/node_modules/
Expand Down
4 changes: 1 addition & 3 deletions lib/DAV/LockBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
use OCP\Files\NotFoundException;
use Sabre\DAV\Locks\Backend\BackendInterface;
use Sabre\DAV\Locks\LockInfo;
use Sabre\DAV\Server;

class LockBackend implements BackendInterface {
/** @var FileService */
Expand All @@ -52,9 +51,8 @@ class LockBackend implements BackendInterface {
private $absolute = false;

public function __construct(
Server $server, FileService $fileService, LockService $lockService, bool $absolute
FileService $fileService, LockService $lockService, bool $absolute
) {
$this->server = $server;
$this->fileService = $fileService;
$this->lockService = $lockService;
$this->absolute = $absolute;
Expand Down
2 changes: 1 addition & 1 deletion lib/DAV/LockPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function initialize(Server $server) {
$absolute = true;
break;
}
$this->locksBackend = new LockBackend($server, $this->fileService, $this->lockService, $absolute);
$this->locksBackend = new LockBackend($this->fileService, $this->lockService, $absolute);
$server->on('propFind', [$this, 'customProperties']);
parent::initialize($server);
}
Expand Down
11 changes: 6 additions & 5 deletions lib/Db/LocksRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ class LocksRequest extends LocksRequestBuilder {
public function save(FileLock $lock) {
$qb = $this->getLocksInsertSql();
$qb->setValue('user_id', $qb->createNamedParameter($lock->getOwner()))
->setValue('file_id', $qb->createNamedParameter($lock->getFileId()))
->setValue('token', $qb->createNamedParameter($lock->getToken()))
->setValue('creation', $qb->createNamedParameter($lock->getCreatedAt()))
->setValue('type', $qb->createNamedParameter($lock->getType()))
->setValue('ttl', $qb->createNamedParameter($lock->getTimeout()));
->setValue('file_id', $qb->createNamedParameter($lock->getFileId()))
->setValue('token', $qb->createNamedParameter($lock->getToken()))
->setValue('creation', $qb->createNamedParameter($lock->getCreatedAt()))
->setValue('type', $qb->createNamedParameter($lock->getType()))
->setValue('ttl', $qb->createNamedParameter($lock->getTimeout()))
->setValue('owner', $qb->createNamedParameter($lock->getDisplayName()));

try {
$qb->execute();
Expand Down
1 change: 1 addition & 0 deletions lib/Listeners/LoadAdditionalScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ public function handle(Event $event): void {

Util::addInitScript(Application::APP_ID, 'files_lock-init');
Util::addScript(Application::APP_ID, 'files_lock-main');
Util::addStyle(Application::APP_ID, 'files_lock-main');
}
}
28 changes: 26 additions & 2 deletions lib/Service/LockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use OCP\Files\Lock\OwnerLockedException;
use OCP\Files\NotFoundException;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;

Expand All @@ -65,6 +66,7 @@ class LockService {
private IAppManager $appManager;
private IEventDispatcher $eventDispatcher;
private IUserSession $userSession;
private IRequest $request;


private array $locks = [];
Expand All @@ -83,6 +85,7 @@ public function __construct(
IAppManager $appManager,
IEventDispatcher $eventDispatcher,
IUserSession $userSession,
IRequest $request,
) {
$this->l10n = $l10n;
$this->userManager = $userManager;
Expand All @@ -92,6 +95,7 @@ public function __construct(
$this->appManager = $appManager;
$this->eventDispatcher = $eventDispatcher;
$this->userSession = $userSession;
$this->request = $request;

$this->setup('app', 'files_lock');
}
Expand Down Expand Up @@ -184,9 +188,9 @@ public function lock(LockContext $lockScope): FileLock {
$this->generateToken($lock);
$lock->setCreation(time());
$this->notice('locking file', false, ['fileLock' => $lock]);
$this->injectMetadata($lock);
$this->locksRequest->save($lock);
$this->propagateEtag($lockScope);
$this->injectMetadata($lock);
return $lock;
}
}
Expand Down Expand Up @@ -339,7 +343,11 @@ public function injectMetadata(FileLock $lock): FileLock {
$displayName = $this->getAppName($lock->getOwner()) ?? null;
}
if ($lock->getType() === ILock::TYPE_TOKEN) {
$displayName = $this->userManager->getDisplayName($lock->getOwner()) ?? $lock->getDisplayName();
$clientHint = $this->getClientHint();
$displayName = $lock->getDisplayName() ?: (
$this->userManager->getDisplayName($lock->getOwner()) . ' ' .
($clientHint ? ('(' . $clientHint . ')') : '')
);
}

if ($displayName) {
Expand All @@ -348,6 +356,22 @@ public function injectMetadata(FileLock $lock): FileLock {
return $lock;
}

private function getClientHint(): ?string {
if ($this->request->isUserAgent([IRequest::USER_AGENT_CLIENT_DESKTOP])) {
return $this->l10n->t('Desktop client');
}

if ($this->request->isUserAgent([IRequest::USER_AGENT_CLIENT_IOS])) {
return $this->l10n->t('iOS client');
}

if ($this->request->isUserAgent([IRequest::USER_AGENT_CLIENT_ANDROID])) {
return $this->l10n->t('Android client');
}

return null;
}

/**
* @param FileLock $lock
*/
Expand Down
Loading

0 comments on commit 389e16a

Please sign in to comment.