Skip to content

Commit

Permalink
feat: Add API parameters to specify the lock type
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Dec 19, 2023
1 parent a16f273 commit 5b86077
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
6 changes: 3 additions & 3 deletions lib/Controller/LockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public function __construct(
*
* @return DataResponse
*/
public function locking(string $fileId): DataResponse {
public function locking(string $fileId, int $lockType = ILock::TYPE_USER): DataResponse {
try {
$user = $this->userSession->getUser();
$file = $this->fileService->getFileFromId($user->getUID(), (int)$fileId);

$lock = $this->lockService->lock(new LockContext(
$file, ILock::TYPE_USER, $user->getUID()
$file, $lockType, $user->getUID()
));

return new DataResponse($lock, Http::STATUS_OK);
Expand All @@ -115,7 +115,7 @@ public function locking(string $fileId): DataResponse {
*
* @return DataResponse
*/
public function unlocking(string $fileId): DataResponse {
public function unlocking(string $fileId, int $lockType = ILock::TYPE_USER): DataResponse {
try {
$user = $this->userSession->getUser();
$this->lockService->enableUserOverride();
Expand Down
6 changes: 4 additions & 2 deletions lib/DAV/LockPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,14 @@ public function customProperties(PropFind $propFind, INode $node) {

public function httpLock(RequestInterface $request, ResponseInterface $response) {
if ($request->getHeader('X-User-Lock')) {
$lockType = (int)($request->getHeader('X-User-Lock-Type') ?? ILock::TYPE_USER);
$response->setHeader('Content-Type', 'application/xml; charset=utf-8');

$file = $this->fileService->getFileFromAbsoluteUri($this->server->getRequestUri());

try {
$lockInfo = $this->lockService->lock(new LockContext(
$file, ILock::TYPE_USER, $this->userSession->getUser()->getUID()
$file, $lockType, $this->userSession->getUser()->getUID()
));
$response->setStatus(200);
$response->setBody(
Expand All @@ -216,14 +217,15 @@ public function httpLock(RequestInterface $request, ResponseInterface $response)

public function httpUnlock(RequestInterface $request, ResponseInterface $response) {
if ($request->getHeader('X-User-Lock')) {
$lockType = (int)($request->getHeader('X-User-Lock-Type') ?? ILock::TYPE_USER);
$response->setHeader('Content-Type', 'application/xml; charset=utf-8');

$file = $this->fileService->getFileFromAbsoluteUri($this->server->getRequestUri());

try {
$this->lockService->enableUserOverride();
$this->lockService->unlock(new LockContext(
$file, ILock::TYPE_USER, $this->userSession->getUser()->getUID()
$file, $lockType, $this->userSession->getUser()->getUID()
));
$response->setStatus(200);
$response->setBody(
Expand Down
25 changes: 10 additions & 15 deletions lib/Service/LockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@
use OCP\Files\NotFoundException;
use OCP\IL10N;
use OCP\IUserManager;
use OCP\IUserSession;

/**
* Class LockService
*
* @package OCA\FilesLock\Service
*/
class LockService {
public const PREFIX = 'files_lock';

Expand All @@ -61,14 +57,14 @@ class LockService {
use TLogger;


private ?string $userId;
private IUserManager $userManager;
private IL10N $l10n;
private LocksRequest $locksRequest;
private FileService $fileService;
private ConfigService $configService;
private IAppManager $appManager;
private IEventDispatcher $eventDispatcher;
private IUserSession $userSession;


private array $locks = [];
Expand All @@ -79,23 +75,23 @@ class LockService {


public function __construct(
$userId,
IL10N $l10n,
IUserManager $userManager,
LocksRequest $locksRequest,
FileService $fileService,
ConfigService $configService,
IAppManager $appManager,
IEventDispatcher $eventDispatcher
IEventDispatcher $eventDispatcher,
IUserSession $userSession,
) {
$this->userId = $userId;
$this->l10n = $l10n;
$this->userManager = $userManager;
$this->locksRequest = $locksRequest;
$this->fileService = $fileService;
$this->configService = $configService;
$this->appManager = $appManager;
$this->eventDispatcher = $eventDispatcher;
$this->userSession = $userSession;

$this->setup('app', 'files_lock');
}
Expand Down Expand Up @@ -224,7 +220,7 @@ public function enableUserOverride(): void {
}

public function canUnlock(LockContext $request, FileLock $current): void {
$isSameUser = $current->getOwner() === $this->userId;
$isSameUser = $current->getOwner() === $this->userSession->getUser()?->getUID();
$isSameToken = $request->getOwner() === $current->getToken();
$isSameOwner = $request->getOwner() === $current->getOwner();
$isSameType = $request->getType() === $current->getType();
Expand All @@ -251,7 +247,7 @@ public function canUnlock(LockContext $request, FileLock $current): void {
'OCA\Files_External\Config\ConfigAdapter'
];
if ($request->getType() === ILock::TYPE_USER
&& $request->getNode()->getOwner()->getUID() === $this->userId
&& $request->getNode()->getOwner()->getUID() === $this->userSession->getUser()?->getUID()
&& !in_array($request->getNode()->getMountPoint()->getMountProvider(), $ignoreFileOwnership)
) {
return;
Expand All @@ -269,22 +265,21 @@ public function canUnlock(LockContext $request, FileLock $current): void {
* @throws NotFoundException
* @throws UnauthorizedUnlockException
*/
public function unlockFile(int $fileId, string $userId, bool $force = false): FileLock {
public function unlockFile(int $fileId, string $userId, bool $force = false, int $lockType = ILock::TYPE_USER): FileLock {
$lock = $this->getLockForNodeId($fileId);
if (!$lock) {
throw new LockNotFoundException();
}

$type = ILock::TYPE_USER;
if ($force) {
$userId = $lock->getOwner();
$type = $lock->getType();
$lockType = $lock->getType();
}

$node = $this->fileService->getFileFromId($userId, $fileId);
$lock = new LockContext(
$node,
$type,
$lockType,
$userId,
);
$this->propagateEtag($lock);
Expand Down

0 comments on commit 5b86077

Please sign in to comment.