Skip to content

Commit

Permalink
fix: Cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Sep 8, 2023
1 parent 360ea4e commit eb70dc1
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 192 deletions.
8 changes: 7 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ module.exports = {
'plugin:cypress/recommended',
],
env: {
'cypress/globals': true
'cypress/globals': true,
},
rules: {
'import/no-named-as-default-member': 'off',
'jsdoc/check-values': 'off',
'jsdoc/valid-types': 'off',
'jsdoc/no-undefined-types': 'off',
}
}
10 changes: 7 additions & 3 deletions lib/Controller/DirectViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public function show($token) {
}

try {
list($urlSrc, $wopi) = $this->tokenManager->getTokenForTemplate($item, $direct->getUid(), $direct->getTemplateDestination(), true);
$urlSrc = $this->tokenManager->getUrlSrc($item);

$wopi = $this->tokenManager->generateWopiTokenForTemplate($item, $direct->getUid(), $direct->getTemplateDestination(), true);

$targetFile = $folder->getById($direct->getTemplateDestination())[0];
$relativePath = $folder->getRelativePath($targetFile->getPath());
Expand All @@ -120,7 +122,8 @@ public function show($token) {
return $response;
}

list($urlSrc, $wopi) = $this->tokenManager->getToken($item->getId(), null, $direct->getUid(), true);
$urlSrc = $this->tokenManager->getUrlSrc($item);
$wopi = $this->tokenManager->generateWopiToken($item->getId(), null, $direct->getUid(), true);
} catch (\Exception $e) {
$this->logger->error('Failed to generate token for existing file on direct editing', ['exception' => $e]);
return $this->renderErrorPage('Failed to open the requested file.');
Expand Down Expand Up @@ -181,7 +184,8 @@ public function showPublicShare(Direct $direct) {
'directGuest' => empty($direct->getUid()),
];

list($urlSrc, $wopi) = $this->tokenManager->getToken($node->getId(), $direct->getShare(), $direct->getUid(), true);
$urlSrc = $this->tokenManager->getUrlSrc($node);
$wopi = $this->tokenManager->generateWopiToken($node->getId(), $direct->getShare(), $direct->getUid(), true);
if (!empty($direct->getInitiatorHost())) {
$this->tokenManager->upgradeFromDirectInitiator($direct, $wopi);
}
Expand Down
58 changes: 26 additions & 32 deletions lib/Controller/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ public function __construct(
}

/**
* @PublicPage
* @NoCSRFRequired
*
* Returns the access_token and urlsrc for WOPI access for given $fileId
* Requests is accepted only when a secret_token is provided set by admin in
* settings page
*
* @return array access_token, urlsrc
*/
#[PublicPage]

Check failure on line 79 in lib/Controller/DocumentController.php

View workflow job for this annotation

GitHub Actions / Nextcloud

UndefinedAttributeClass

lib/Controller/DocumentController.php:79:4: UndefinedAttributeClass: Attribute class OCP\AppFramework\Http\Attribute\PublicPage does not exist (see https://psalm.dev/241)
#[NoCSRFRequired]

Check failure on line 80 in lib/Controller/DocumentController.php

View workflow job for this annotation

GitHub Actions / Nextcloud

UndefinedAttributeClass

lib/Controller/DocumentController.php:80:4: UndefinedAttributeClass: Attribute class OCP\AppFramework\Http\Attribute\NoCSRFRequired does not exist (see https://psalm.dev/241)
public function extAppGetData(int $fileId): array {
$secretToken = $this->request->getParam('secret_token');
$apps = array_filter(explode(',', $this->appConfig->getAppValue('external_apps')));
Expand All @@ -92,7 +91,8 @@ public function extAppGetData(int $fileId): array {
]);
try {
$file = $this->getFileForUser($fileId);
list($urlSrc, $wopi) = $this->tokenManager->getToken($this->getWopiFileId($file->getId()));
$urlSrc = $this->tokenManager->getUrlSrc($file);
$wopi = $this->tokenManager->generateWopiToken($this->getWopiFileId($file->getId()));
return [
'status' => 'success',
'urlsrc' => $urlSrc,
Expand Down Expand Up @@ -186,7 +186,8 @@ public function createFromTemplate(int $templateId, string $fileName, string $di
$file = $folder->newFile($fileName);

$template = $this->templateManager->get($templateId);
list($urlSrc, $wopi) = $this->tokenManager->getTokenForTemplate($template, $this->userId, $file->getId());
$urlSrc = $this->tokenManager->getUrlSrc($file);
$wopi = $this->tokenManager->generateWopiTokenForTemplate($template, $this->userId, $file->getId());

$params = [
'permissions' => $template->getPermissions(),
Expand Down Expand Up @@ -237,17 +238,10 @@ public function publicPage(string $shareToken, string $fileName = null, int $fil

/**
* Open file on Source instance with token from Initiator instance
*
* @PublicPage
* @NoCSRFRequired
*
* @param string $shareToken
* @param $remoteServer
* @param $remoteServerToken
* @param null $filePath
* @return TemplateResponse
*/
public function remote($shareToken, $remoteServer, $remoteServerToken, $filePath = null) {
#[PublicPage]

Check failure on line 242 in lib/Controller/DocumentController.php

View workflow job for this annotation

GitHub Actions / Nextcloud

UndefinedAttributeClass

lib/Controller/DocumentController.php:242:4: UndefinedAttributeClass: Attribute class OCP\AppFramework\Http\Attribute\PublicPage does not exist (see https://psalm.dev/241)
#[NoCSRFRequired]

Check failure on line 243 in lib/Controller/DocumentController.php

View workflow job for this annotation

GitHub Actions / Nextcloud

UndefinedAttributeClass

lib/Controller/DocumentController.php:243:4: UndefinedAttributeClass: Attribute class OCP\AppFramework\Http\Attribute\NoCSRFRequired does not exist (see https://psalm.dev/241)
public function remote(string $shareToken, string $remoteServer, string $remoteServerToken, string $filePath = null): TemplateResponse {
try {
$share = $this->shareManager->getShareByToken($shareToken);
// not authenticated ?
Expand All @@ -269,7 +263,8 @@ public function remote($shareToken, $remoteServer, $remoteServerToken, $filePath
}

if ($node instanceof Node) {
list($urlSrc, $wopi) = $this->tokenManager->getToken($node->getId(), $shareToken, $this->userId);
$urlSrc = $this->tokenManager->getUrlSrc($node);
$wopi = $this->tokenManager->generateWopiToken($node->getId(), $shareToken, $this->userId);

$remoteWopi = $this->federationService->getRemoteFileDetails($remoteServer, $remoteServerToken);
if ($remoteWopi === null) {
Expand Down Expand Up @@ -305,7 +300,7 @@ public function remote($shareToken, $remoteServer, $remoteServerToken, $filePath
return new TemplateResponse('core', '403', [], 'guest');
}

private function renderErrorPage(string $message, $status = Http::STATUS_INTERNAL_SERVER_ERROR) {
private function renderErrorPage(string $message, int $status = Http::STATUS_INTERNAL_SERVER_ERROR): TemplateResponse {
$params = [
'errors' => [['error' => $message]]
];
Expand All @@ -314,12 +309,10 @@ private function renderErrorPage(string $message, $status = Http::STATUS_INTERNA
return $response;
}

/**
* @NoCSRFRequired
* @NoAdminRequired
* @UseSession
*/
public function editOnline(string $path = null, ?string $userId = null, ?string $target = null) {
#[NoCSRFRequired]
#[NoAdminRequired]
#[UseSession]
public function editOnline(string $path = null, ?string $userId = null, ?string $target = null): RedirectResponse|TemplateResponse {
if ($path === null) {
return $this->renderErrorPage('No path provided');
}
Expand Down Expand Up @@ -351,9 +344,7 @@ public function editOnline(string $path = null, ?string $userId = null, ?string
}
$redirectUrl = $this->urlGenerator->getAbsoluteURL('/index.php/f/' . $file->getId());
return new RedirectResponse($redirectUrl);
} catch (NotFoundException $e) {
} catch (NotPermittedException $e) {
} catch (NoUserException $e) {
} catch (NotFoundException|NotPermittedException|NoUserException) {
}

return $this->renderErrorPage('File not found', Http::STATUS_NOT_FOUND);
Expand All @@ -362,7 +353,7 @@ public function editOnline(string $path = null, ?string $userId = null, ?string
#[NoCSRFRequired]
#[NoAdminRequired]
#[UseSession]
public function editOnlineTarget(int $fileId, ?string $target = null) {
public function editOnlineTarget(int $fileId, ?string $target = null): RedirectResponse|TemplateResponse {
if (!$this->userId) {
return $this->renderErrorPage('File not found', Http::STATUS_NOT_FOUND);
}
Expand Down Expand Up @@ -428,7 +419,7 @@ private function getFileForUser(int $fileId, string $path = null): File {
* @throws NotFoundException
* @throws NotPermittedException
*/
private function getFileForShare(IShare $share, int $fileId, ?string $path = null): File {
private function getFileForShare(IShare $share, ?int $fileId, ?string $path = null): File {
// not authenticated ?
if ($share->getPassword()) {
if (!$this->session->exists('public_link_authenticated')
Expand All @@ -447,6 +438,10 @@ private function getFileForShare(IShare $share, int $fileId, ?string $path = nul
return $node;
}

if ($fileId === null && $path === null) {
throw new NotFoundException();
}

if ($path !== null) {
$node = $node->get($path);
} else {
Expand All @@ -465,11 +460,10 @@ private function getToken(File $file, ?IShare $share = null, int $version = null
// Pass through $version
$templateFile = $this->templateManager->getTemplateSource($file->getId());
if ($templateFile) {
[, $wopi] = $this->tokenManager->getTokenForTemplate($templateFile, $share?->getShareOwner(), $file->getId());
} else {
[, $wopi] = $this->tokenManager->getToken($this->getWopiFileId($file->getId(), $version), $share?->getToken(), $this->userId);
return $this->tokenManager->generateWopiTokenForTemplate($templateFile, $share?->getShareOwner(), $file->getId());
}
return $wopi;

return $this->tokenManager->generateWopiToken($this->getWopiFileId($file->getId(), $version), $share?->getToken(), $this->userId);
}

private function getWopiFileId(int $fileId, int $version = null): string {
Expand Down
16 changes: 8 additions & 8 deletions lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,12 @@ public function putFile($fileId,

if ($isPutRelative) {
// generate a token for the new file (the user still has to be logged in)
list(, $wopi) = $this->tokenManager->getToken((string)$file->getId(), null, $wopi->getEditorUid(), $wopi->getDirect());
$wopi = $this->tokenManager->generateWopiToken((string)$file->getId(), null, $wopi->getEditorUid(), $wopi->getDirect());

$wopi = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopi->getToken();
$url = $this->urlGenerator->getAbsoluteURL($wopi);
$wopiUrl = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopi->getToken();
$wopiUrl = $this->urlGenerator->getAbsoluteURL($wopiUrl);

return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $url ], Http::STATUS_OK);
return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $wopiUrl ], Http::STATUS_OK);
}
if ($wopi->hasTemplateId()) {
$wopi->setTemplateId(null);
Expand Down Expand Up @@ -684,12 +684,12 @@ public function postFile(string $fileId, string $access_token): JSONResponse {

// generate a token for the new file (the user still has to be
// logged in)
list(, $wopi) = $this->tokenManager->getToken((string)$file->getId(), null, $wopi->getEditorUid(), $wopi->getDirect());
$wopi = $this->tokenManager->generateWopiToken((string)$file->getId(), null, $wopi->getEditorUid(), $wopi->getDirect());

$wopi = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopi->getToken();
$url = $this->urlGenerator->getAbsoluteURL($wopi);
$wopiUrl = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopi->getToken();
$wopiUrl = $this->urlGenerator->getAbsoluteURL($wopiUrl);

return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $url ], Http::STATUS_OK);
return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $wopiUrl ], Http::STATUS_OK);
} catch (NotFoundException $e) {
$this->logger->warning($e->getMessage(), ['exception' => $e]);
return new JSONResponse([], Http::STATUS_NOT_FOUND);
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/Wopi.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @method void setOwnerUid(string $uid)
* @method string getOwnerUid()
* @method void setEditorUid(string $uid)
* @method void setEditorUid(?string $uid)
* @method string getEditorUid()
* @method void setFileid(int $fileid)
* @method int getFileid()
Expand Down
34 changes: 11 additions & 23 deletions lib/Service/InitialStateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,21 @@
use OCA\Richdocuments\AppInfo\Application;
use OCA\Richdocuments\Db\Wopi;
use OCP\AppFramework\Services\IInitialState;
use OCP\Defaults;
use OCP\IConfig;
use OCP\IURLGenerator;

class InitialStateService {
/** @var IInitialState */
private $initialState;

/** @var CapabilitiesService */
private $capabilitiesService;

/** @var IConfig */
private $config;

/** @var string|null */
private $userId;

/** @var bool */
private $hasProvidedCapabilities = false;
private bool $hasProvidedCapabilities = false;

public function __construct(
IInitialState $initialState,
CapabilitiesService $capabilitiesService,
IConfig $config,
$userId
private IInitialState $initialState,
private CapabilitiesService $capabilitiesService,
private IURLGenerator $urlGenerator,
private Defaults $themingDefaults,
private IConfig $config,
private ?string $userId,
) {
$this->initialState = $initialState;
$this->capabilitiesService = $capabilitiesService;
$this->config = $config;
$this->userId = $userId;
}

public function provideCapabilities(): void {
Expand All @@ -66,6 +53,7 @@ public function provideCapabilities(): void {
$this->initialState->provideInitialState('productName', $this->capabilitiesService->getProductName());
$this->initialState->provideInitialState('hasDrawSupport', $this->capabilitiesService->hasDrawSupport());
$this->initialState->provideInitialState('hasNextcloudBranding', $this->capabilitiesService->hasNextcloudBranding());
$this->initialState->provideInitialState('instanceid', $this->config->getSystemValue('instanceid'));

$this->provideOptions();

Expand Down Expand Up @@ -112,7 +100,7 @@ private function provideOptions(): void {
$logoSet = $this->config->getAppValue('theming', 'logoMime', '') !== '';
}
$this->initialState->provideInitialState('theming-customLogo', ($logoSet ?
\OC::$server->getURLGenerator()->getAbsoluteURL(\OC::$server->getThemingDefaults()->getLogo())
$this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo())
: false));
}
}
22 changes: 12 additions & 10 deletions lib/Service/UserScopeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,32 @@

namespace OCA\Richdocuments\Service;

use InvalidArgumentException;
use OC_Util;
use OCP\IUserManager;
use OCP\IUserSession;

class UserScopeService {
public function __construct(IUserSession $userSession, IUserManager $userManager) {
$this->userSession = $userSession;
$this->userManager = $userManager;
public function __construct(
private IUserSession $userSession,
private IUserManager $userManager
) {
}

/**
* Set a valid user in IUserSession since lots of server logic is relying on obtaining
* the current acting user from that
*
* @param $uid
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setUserScope(string $uid = null) {
public function setUserScope(string $uid = null): void {
if ($uid === null) {
return;
}

$user = $this->userManager->get($uid);
if ($user === null) {
throw new \InvalidArgumentException('No user found for the uid ' . $uid);
throw new InvalidArgumentException('No user found for the uid ' . $uid);
}
$this->userSession->setUser($user);
}
Expand All @@ -59,8 +61,8 @@ public function setUserScope(string $uid = null) {
*
* @param string $owner
*/
public function setFilesystemScope(string $owner) {
\OC_Util::tearDownFS();
\OC_Util::setupFS($owner);
public function setFilesystemScope(string $owner): void {
OC_Util::tearDownFS();
OC_Util::setupFS($owner);
}
}
Loading

0 comments on commit eb70dc1

Please sign in to comment.