Skip to content

Commit

Permalink
Merge pull request #391 from nextcloud/fix/unlock-app-lock
Browse files Browse the repository at this point in the history
Allow force unlock of automated client locks
  • Loading branch information
juliusknorr authored Nov 27, 2024
2 parents 5bef7ab + 16d856f commit b56da4a
Show file tree
Hide file tree
Showing 11 changed files with 2,211 additions and 229 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
\.idea/

/build/
/css/
/js/
/vendor/
/node_modules/
Expand Down
3 changes: 2 additions & 1 deletion lib/Db/LocksRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function save(FileLock $lock) {
->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('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 @@ -24,5 +24,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 @@ -24,6 +24,7 @@
use OCP\Files\Lock\OwnerLockedException;
use OCP\Files\NotFoundException;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;

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


private array $locks = [];
Expand All @@ -61,6 +63,7 @@ public function __construct(
IAppManager $appManager,
IEventDispatcher $eventDispatcher,
IUserSession $userSession,
IRequest $request,
) {
$this->l10n = $l10n;
$this->userManager = $userManager;
Expand All @@ -70,6 +73,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 @@ -162,9 +166,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 @@ -317,7 +321,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 @@ -326,6 +334,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 b56da4a

Please sign in to comment.