Skip to content

Commit

Permalink
Merge pull request #3910 from nextcloud/backport/3907/stable30
Browse files Browse the repository at this point in the history
[stable30] fix: guest username not picked up the second time loading a share link
  • Loading branch information
elzody authored Aug 14, 2024
2 parents 868112b + 421ba4a commit b6b2c0a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 42 deletions.
20 changes: 11 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"test:coverage": "NODE_ENV=test jest --coverage"
},
"dependencies": {
"@nextcloud/auth": "^2.3.0",
"@nextcloud/auth": "^2.4.0",
"@nextcloud/axios": "^2.5.0",
"@nextcloud/capabilities": "^1.2.0",
"@nextcloud/dialogs": "^5.3.5",
Expand Down
6 changes: 3 additions & 3 deletions src/components/GuestNamePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
</template>

<script>
import { setGuestNickname } from '@nextcloud/auth'
import { NcButton, NcIconSvgWrapper, NcModal, NcTextField } from '@nextcloud/vue'
import { translate as t } from '@nextcloud/l10n'
import axios from '@nextcloud/axios'
import { setGuestNameCookie } from '../helpers/guestName.js'
export default {
name: 'GuestNamePicker',
Expand Down Expand Up @@ -107,10 +107,10 @@ export default {
this.guestName = guestName
},
async submit() {
setGuestNameCookie(this.guestName)
setGuestNickname(this.guestName)
this.show = false
await this.onSubmit(this.guestName)
await this.onSubmit()
},
},
}
Expand Down
36 changes: 13 additions & 23 deletions src/helpers/guestName.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,23 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { getCurrentUser } from '@nextcloud/auth'
import {
getCurrentUser,
getGuestNickname,
} from '@nextcloud/auth'
import getLoggedInUser from '../helpers/getLoggedInUser.js'

const cookieAlreadySet = (cookieName) => {
return document.cookie
.split(';')
.some(cookie => {
return cookie.trim().startsWith(`${cookieName}=`)
})
}

const setGuestNameCookie = (username) => {
if (username !== '') {
document.cookie = 'guestUser=' + encodeURIComponent(username) + '; path=/'
}
}

const shouldAskForGuestName = (mimetype, canWrite) => {
/**
* Determines if the user should be asked to enter a guest name
*
* @param {string} mimetype - Mimetype of the file
* @param {boolean} canWrite - If write access is granted
*/
export function shouldAskForGuestName(mimetype, canWrite) {
const noLoggedInUser = !getLoggedInUser()
const noGuestCookie = !cookieAlreadySet('guestUser')
const noGuest = !getGuestNickname()
const noCurrentUser = !getCurrentUser() || getCurrentUser()?.uid === ''
const isReadOnlyPDF = mimetype === 'application/pdf' && !canWrite

return noLoggedInUser && noGuestCookie && noCurrentUser && !isReadOnlyPDF
}

export {
setGuestNameCookie,
shouldAskForGuestName,
return noLoggedInUser && noGuest && noCurrentUser && !isReadOnlyPDF
}
9 changes: 3 additions & 6 deletions src/view/Office.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ import pickLink from '../mixins/pickLink.js'
import saveAs from '../mixins/saveAs.js'
import uiMention from '../mixins/uiMention.js'
import version from '../mixins/version.js'
import { getCurrentUser } from '@nextcloud/auth'
import { getCurrentUser, getGuestNickname } from '@nextcloud/auth'
import { shouldAskForGuestName } from '../helpers/guestName.js'
const FRAME_DOCUMENT = 'FRAME_DOCUMENT'
Expand Down Expand Up @@ -183,8 +183,6 @@ export default {
errorType: null,
loadingMsg: null,
guestName: null,
showLinkPicker: false,
showZotero: false,
modified: false,
Expand Down Expand Up @@ -286,8 +284,7 @@ export default {
spawnDialog(GuestNamePicker, {
fileName: basename(this.filename),
onSubmit: async (guestName) => {
this.guestName = guestName
onSubmit: async () => {
await this.load()
},
})
Expand All @@ -307,7 +304,7 @@ export default {
// Generate WOPI token
const { data } = await axios.post(generateUrl('/apps/richdocuments/token'), {
fileId: fileid, shareToken: this.shareToken, version, guestName: this.guestName,
fileId: fileid, shareToken: this.shareToken, version, guestName: getGuestNickname(),
})
Config.update('urlsrc', data.urlSrc)
Config.update('wopi_callback_url', loadState('richdocuments', 'wopi_callback_url', ''))
Expand Down

0 comments on commit b6b2c0a

Please sign in to comment.