Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign guest name picker and fix opening it for shared folders #3606

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 63 additions & 16 deletions cypress/e2e/share-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ describe('Public sharing of office documents', function() {
cy.uploadFile(shareOwner, 'document.odt', 'application/vnd.oasis.opendocument.text', '/my-share/document.odt')
})

const matrix = [shareOwner, otherUser, null]
const userMatrix = [shareOwner, otherUser]
const guestName = randHash()

describe('Open a shared file', function() {
for (const index in matrix) {
const viewingUser = matrix[index]
for (const index in userMatrix) {
const viewingUser = userMatrix[index]

it('Loads file as user: ' + viewingUser?.userId, () => {
cy.shareLink(shareOwner, '/document.odt').then((token) => {
if (viewingUser !== null) {
cy.login(viewingUser)
} else {
cy.logout()
}
cy.login(viewingUser)

cy.visit(`/s/${token}`, {
onBeforeLoad(win) {
cy.spy(win, 'postMessage').as('postMessage')
Expand All @@ -58,38 +57,86 @@ describe('Public sharing of office documents', function() {
cy.get('@loleafletframe').within(() => {
cy.get('#closebutton').click()
})

cy.get('#viewer', { timeout: 5000 }).should('not.exist')
})
})
}

it('Loads file as guest: ' + guestName, () => {
cy.shareLink(shareOwner, '/document.odt').then((token) => {
cy.logout()

cy.visit(`/s/${token}`, {
onBeforeLoad(win) {
cy.spy(win, 'postMessage').as('postMessage')
},
})

cy.inputCollaboraGuestName(guestName)
cy.waitForCollabora()
cy.waitForPostMessage('App_LoadingStatus', { Status: 'Document_Loaded' })

cy.get('@loleafletframe').within(() => {
cy.get('#closebutton').click()
})

cy.get('#viewer', { timeout: 5000 }).should('not.exist')
})
})
})

describe('Open a file in a shared folder', function() {
for (const index in matrix) {
const viewingUser = matrix[index]
for (const index in userMatrix) {
const viewingUser = userMatrix[index]
it('Loads file in shared folder as user: ' + viewingUser?.userId, () => {
if (viewingUser !== null) {
cy.login(viewingUser)
} else {
cy.logout()
}
cy.login(viewingUser)

cy.shareLink(shareOwner, '/my-share').then((token) => {
cy.visit(`/s/${token}`, {
onBeforeLoad(win) {
cy.spy(win, 'postMessage').as('postMessage')
},
})

cy.get('tr[data-file="document.odt"] a.name').click()

cy.waitForViewer()
cy.waitForCollabora()
cy.waitForPostMessage('App_LoadingStatus', { Status: 'Document_Loaded' })

cy.get('@loleafletframe').within(() => {
cy.get('#closebutton').click()
})

cy.get('#viewer', { timeout: 5000 }).should('not.exist')
})
})

}

it('Loads file in shared folder as guest: ' + guestName, () => {
cy.shareLink(shareOwner, '/my-share').then((token) => {
cy.logout()

cy.visit(`/s/${token}`, {
onBeforeLoad(win) {
cy.spy(win, 'postMessage').as('postMessage')
},
})

cy.get('tr[data-file="document.odt"] a.name').click()

cy.inputCollaboraGuestName(guestName)
cy.waitForViewer()
cy.waitForCollabora()
cy.waitForPostMessage('App_LoadingStatus', { Status: 'Document_Loaded' })

cy.get('@loleafletframe').within(() => {
cy.get('#closebutton').click()
})

cy.get('#viewer', { timeout: 5000 }).should('not.exist')
})
})
})
})
11 changes: 10 additions & 1 deletion cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Cypress.Commands.add('waitForViewer', () => {
.and('have.class', 'modal-mask')
.and('not.have.class', 'icon-loading')
})

Cypress.Commands.add('waitForCollabora', (wrapped = false) => {
if (wrapped) {
cy.get('[data-cy="documentframe"]', { timeout: 30000 })
Expand All @@ -223,6 +224,7 @@ Cypress.Commands.add('waitForCollabora', (wrapped = false) => {
.as('loleafletframe')
cy.get('@loleafletframe').find('#main-document-content').should('be.visible')
})

Cypress.Commands.add('waitForPostMessage', (messageId, values = undefined) => {
cy.get('@postMessage', { timeout: 20000 }).should(spy => {
const calls = spy.getCalls()
Expand All @@ -238,6 +240,13 @@ Cypress.Commands.add('waitForPostMessage', (messageId, values = undefined) => {
}
})
})

Cypress.Commands.add('inputCollaboraGuestName', (guestName = 'cloud') => {
cy.get('[data-cy="guestNameModal"]').should('be.visible')
cy.get('[data-cy="guestNameInput"]').type(guestName)
cy.get('[data-cy="guestNameSubmit"]').click()
})

Cypress.Commands.add('uploadSystemTemplate', () => {
cy.login(new User('admin', 'admin'))
cy.visit('/settings/admin/richdocuments')
Expand All @@ -248,4 +257,4 @@ Cypress.Commands.add('uploadSystemTemplate', () => {
mimeType: 'application/vnd.oasis.opendocument.presentation-template',
}, { force: true })
cy.get('#richdocuments-templates li').contains('systemtemplate.otp')
})
})
8 changes: 6 additions & 2 deletions lib/Controller/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,15 @@ public function editOnlineTarget(int $fileId, ?string $target = null): RedirectR
}

#[PublicPage]
public function token(int $fileId, ?string $shareToken = null, ?string $path = null): DataResponse {
public function token(int $fileId, ?string $shareToken = null, ?string $path = null, ?string $guestName = null): DataResponse {
try {
$share = $shareToken ? $this->shareManager->getShareByToken($shareToken) : null;
$file = $shareToken ? $this->getFileForShare($share, $fileId, $path) : $this->getFileForUser($fileId, $path);

if ($this->userId === null) {
$this->userId = $guestName;
}

$wopi = $this->getToken($file, $share);

return new DataResponse(array_merge(
Expand Down Expand Up @@ -471,7 +475,7 @@ private function getToken(File $file, ?IShare $share = null, ?int $version = nul
return $this->tokenManager->generateWopiTokenForTemplate($templateFile, $share?->getShareOwner() ?? $this->userId, $file->getId());
}

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

private function getWopiFileId(int $fileId, ?int $version = null): string {
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/WopiMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct(IDBConnection $db,
* @param int $templateDestination
* @return Wopi
*/
public function generateFileToken($fileId, $owner, $editor, $version, $updatable, $serverHost, $guestDisplayname = null, $hideDownload = false, $direct = false, $templateId = 0, $share = null) {
public function generateFileToken($fileId, $owner, $editor, $version, $updatable, $serverHost, ?string $guestDisplayname = null, $hideDownload = false, $direct = false, $templateId = 0, $share = null) {
$token = $this->random->generate(32, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);

$wopi = Wopi::fromParams([
Expand Down
5 changes: 4 additions & 1 deletion lib/Listener/ShareLinkListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ public function handle(Event $event): void {
/** @var IShare $share */
$share = $event->getShare();
$owner = $share->getShareOwner();
$loggedInUser = $this->permissionManager->loggedInUser();

if ($this->permissionManager->isEnabledForUser($owner)) {
$this->initialStateService->prepareParams(['userId' => $loggedInUser]);
$this->initialStateService->provideCapabilities();

Util::addScript('richdocuments', 'richdocuments-files');
Util::addScript('richdocuments', 'richdocuments-viewer', 'viewer');
Util::addScript('richdocuments', 'richdocuments-public');
Util::addScript('richdocuments', 'richdocuments-public', 'viewer');
}
}
}
20 changes: 19 additions & 1 deletion lib/PermissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function userMatchesGroupList(?string $userId = null, ?array $groupList
$incognito = true;
}
$user = $this->userSession->getUser();
$userId = $user ? $user->getUID() : null;
$userId = $user?->getUID();
if ($incognito) {
\OC_User::setIncognitoMode(true);
}
Expand Down Expand Up @@ -98,6 +98,24 @@ private function userMatchesGroupList(?string $userId = null, ?array $groupList
return false;
}

public function loggedInUser(): ?string {
$incognito = false;

if (\OC_User::isIncognitoMode()) {
\OC_User::setIncognitoMode(false);
$incognito = true;
}

$user = $this->userSession->getUser();
$userId = $user?->getUID();

if ($incognito) {
\OC_User::setIncognitoMode(true);
}

return $userId;
}

public function isEnabledForUser(?string $userId = null): bool {
if ($this->userMatchesGroupList($userId, $this->appConfig->getUseGroups())) {
return true;
Expand Down
4 changes: 4 additions & 0 deletions lib/Service/InitialStateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,18 @@ public function prepareParams(array $params): array {
}

private function provideOptions(): void {
$this->initialState->provideInitialState('loggedInUser', $this->userId ?? false);

$this->initialState->provideInitialState('theme', $this->config->getAppValue(Application::APPNAME, 'theme', 'nextcloud'));
$this->initialState->provideInitialState('uiDefaults', [
'UIMode' => $this->config->getAppValue(Application::APPNAME, 'uiDefaults-UIMode', 'notebookbar')
]);

$logoSet = $this->config->getAppValue('theming', 'logoheaderMime', '') !== '';
if (!$logoSet) {
$logoSet = $this->config->getAppValue('theming', 'logoMime', '') !== '';
}

$this->initialState->provideInitialState('theming-customLogo', ($logoSet ?
$this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo())
: false));
Expand Down
3 changes: 2 additions & 1 deletion lib/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function generateWopiToken(string $fileId, ?string $shareToken = null, ?s
$owneruid = null;
$hideDownload = false;
$rootFolder = $this->rootFolder;

// if the user is not logged-in do use the sharers storage
if ($shareToken !== null) {
/** @var File $file */
Expand Down Expand Up @@ -198,7 +199,7 @@ public function generateWopiToken(string $fileId, ?string $shareToken = null, ?s
);

$serverHost = $this->urlGenerator->getAbsoluteURL('/');
$guestName = $this->userId === null ? $this->prepareGuestName($this->helper->getGuestNameFromCookie()) : null;
$guestName = $editoruid === null ? $this->prepareGuestName($this->helper->getGuestNameFromCookie()) : $editoruid;
return $this->wopiMapper->generateFileToken($fileId, $owneruid, $editoruid, $version, $updatable, $serverHost, $guestName, $hideDownload, $direct, 0, $shareToken);
}

Expand Down
Loading
Loading