Skip to content

Commit

Permalink
Allow to delete passwords on public links when password is enforced b…
Browse files Browse the repository at this point in the history
…ut permission is set (#9857)

* Allow to remove password on public link if permission allows

* Fix bug where password prompt did instant pop up

* Refactor

* Remove unused capability

* Fix unit tests
  • Loading branch information
AlexAndBear committed Dec 13, 2023
1 parent 3baa649 commit 467a8a6
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Enhancement: Add permission to delete link passwords when password is enforced

We've enabled to ability to allow delete passwords on public links, even if the password is enforced.
Therefore, the user needs respective permission, granted by the server.
This feature is only possible on public links that have the viewer role.

https://github.com/owncloud/web/pull/9857
https://github.com/owncloud/ocis/issues/7538
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
:is-folder-share="resource.isFolder"
:is-modifiable="canEditLink(quicklink)"
:is-password-enforced="isPasswordEnforcedFor(quicklink)"
:is-password-removable="canDeletePublicLinkPassword(quicklink)"
:link="quicklink"
@update-link="checkLinkToUpdate"
@remove-public-link="deleteLinkConfirmation"
Expand Down Expand Up @@ -57,6 +58,7 @@
:is-folder-share="resource.isFolder"
:is-modifiable="canEditLink(link)"
:is-password-enforced="isPasswordEnforcedFor(link)"
:is-password-removable="canDeletePublicLinkPassword(link)"
:link="link"
@update-link="checkLinkToUpdate"
@remove-public-link="deleteLinkConfirmation"
Expand Down Expand Up @@ -185,6 +187,11 @@ export default defineComponent({
})
)
const canCreatePublicLinks = computed(() => can('create-all', 'PublicLink'))
const canDeleteReadOnlyPublicLinkPassword = computed(() =>
can('delete-all', 'ReadOnlyPublicLinkPassword')
)
const canCreateLinks = computed(() => {
if (unref(resource).isReceivedShare() && !unref(hasResharing)) {
return false
Expand Down Expand Up @@ -225,6 +232,7 @@ export default defineComponent({
directLinks,
indirectLinks,
canCreatePublicLinks,
canDeleteReadOnlyPublicLinkPassword,
configurationManager,
passwordPolicyService,
canCreateLinks,
Expand Down Expand Up @@ -389,6 +397,21 @@ export default defineComponent({
)
},
canDeletePublicLinkPassword(link) {
const isFolder = link.indirect || this.resource.isFolder
const isPasswordEnforced = this.isPasswordEnforcedFor(link)
if (!isPasswordEnforced) {
return true
}
const currentRole = LinkShareRoles.getByBitmask(parseInt(link.permissions), isFolder)
return (
currentRole.name === linkRoleViewerFolder.name && this.canDeleteReadOnlyPublicLinkPassword
)
},
addNewLink() {
this.checkLinkToCreate({
link: {
Expand Down Expand Up @@ -422,7 +445,7 @@ export default defineComponent({
checkLinkToUpdate({ link }) {
const params = this.getParamsForLink(link)
if (!link.password && this.isPasswordEnforcedFor(link)) {
if (!link.password && !this.canDeletePublicLinkPassword(link)) {
showQuickLinkPasswordModal(
{
...this.$language,
Expand Down Expand Up @@ -626,6 +649,7 @@ export default defineComponent({
#oc-files-sharing-sidebar {
border-radius: 5px;
}
.link-name-container {
background-color: var(--oc-color-input-bg);
border: 1px solid var(--oc-color-input-border);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ export default defineComponent({
type: Boolean,
default: false
},
isPasswordRemovable: {
type: Boolean,
default: false
},
link: {
type: Object,
required: true
Expand Down Expand Up @@ -323,7 +327,7 @@ export default defineComponent({
method: this.showPasswordModal
})
if (!this.isPasswordEnforced) {
if (this.isPasswordRemovable) {
result.push({
id: 'remove-password',
title: this.$gettext('Remove password'),
Expand All @@ -338,7 +342,7 @@ export default defineComponent({
})
}
}
if (!this.isPasswordEnforced && !this.link.password && !this.isAliasLink) {
if (!this.link.password && !this.isAliasLink) {
result.push({
id: 'add-password',
title: this.$gettext('Add password'),
Expand Down Expand Up @@ -526,6 +530,7 @@ export default defineComponent({
width: 100%;
}
}
@media (min-width: $oc-breakpoint-medium-default) {
.edit-public-link-role-dropdown {
width: 400px;
Expand All @@ -542,6 +547,7 @@ export default defineComponent({
&:first-child {
margin-top: 0;
}
&:last-child {
margin-bottom: 0;
}
Expand Down
1 change: 1 addition & 0 deletions packages/web-client/src/helpers/resource/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type AbilitySubjects =
| 'Language'
| 'Logo'
| 'PublicLink'
| 'ReadOnlyPublicLinkPassword'
| 'Role'
| 'Setting'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@ import { useStore } from '../../store'
import { useGettext } from 'vue3-gettext'
import { Store } from 'vuex'
import { FileAction, FileActionOptions } from '../types'
import { usePasswordPolicyService } from '../../passwordPolicyService'

export const useFileActionsCreateQuickLink = ({ store }: { store?: Store<any> } = {}) => {
export const useFileActionsCreateQuickLink = ({
store
}: {
store?: Store<any>
} = {}) => {
store = store || useStore()
const router = useRouter()
const language = useGettext()
const { $gettext } = language
const ability = useAbility()
const clientService = useClientService()
const passwordPolicyService = usePasswordPolicyService()

const handler = async ({ space, resources }: FileActionOptions) => {
const [resource] = resources

await copyQuicklink({
clientService,
passwordPolicyService,
resource,
storageId: space?.id || resource?.fileId || resource?.id,
store,
Expand Down
93 changes: 68 additions & 25 deletions packages/web-pkg/src/helpers/share/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import {
LinkShareRoles,
Share,
linkRoleInternalFolder,
linkRoleViewerFolder
linkRoleViewerFolder,
ShareTypes,
buildShare
} from '@ownclouders/web-client/src/helpers/share'
import { Store } from 'vuex'
import { ClientService } from '../../services'
import { ClientService, PasswordPolicyService } from '../../services'
import { useClipboard } from '@vueuse/core'
import { Ability } from '@ownclouders/web-client/src/helpers/resource/types'
import { Resource } from '@ownclouders/web-client'
import { Language } from 'vue3-gettext'
import { unref } from 'vue'
import { showQuickLinkPasswordModal } from '../../quickActions'

export interface CreateQuicklink {
clientService: ClientService
Expand All @@ -22,37 +26,40 @@ export interface CreateQuicklink {
ability: Ability
}

export const copyQuicklink = async (args: CreateQuicklink) => {
const { store, language } = args
const { $gettext } = language
export interface CopyQuickLink extends CreateQuicklink {
passwordPolicyService: PasswordPolicyService
}

// doCopy creates the requested link and copies the url to the clipboard,
// the copy action uses the clipboard // clipboardItem api to work around the webkit limitations.
//
// https://developer.apple.com/forums/thread/691873
//
// if those apis not available (or like in firefox behind dom.events.asyncClipboard.clipboardItem)
// it has a fallback to the vue-use implementation.
//
// https://webkit.org/blog/10855/
const doCopy = async () => {
// doCopy creates the requested link and copies the url to the clipboard,
// the copy action uses the clipboard // clipboardItem api to work around the webkit limitations.
//
// https://developer.apple.com/forums/thread/691873
//
// if those apis not available (or like in firefox behind dom.events.asyncClipboard.clipboardItem)
// it has a fallback to the vue-use implementation.
//
// https://webkit.org/blog/10855/
const doCopy = async ({
store,
language,
quickLinkUrl
}: {
store: Store<unknown>
language: Language
quickLinkUrl: string
}) => {
const { $gettext } = language
try {
if (typeof ClipboardItem && navigator?.clipboard?.write) {
await navigator.clipboard.write([
new ClipboardItem({
'text/plain': createQuicklink(args).then(
(link) => new Blob([link.url], { type: 'text/plain' })
)
'text/plain': new Blob([quickLinkUrl], { type: 'text/plain' })
})
])
} else {
const link = await createQuicklink(args)
const { copy } = useClipboard({ legacy: true })
await copy(link.url)
await copy(quickLinkUrl)
}
}

try {
await doCopy()
await store.dispatch('showMessage', {
title: $gettext('The link has been copied to your clipboard.')
})
Expand All @@ -64,6 +71,40 @@ export const copyQuicklink = async (args: CreateQuicklink) => {
})
}
}
export const copyQuicklink = async (args: CopyQuickLink) => {
const { store, language, resource, clientService, passwordPolicyService } = args
const { $gettext } = language

const linkSharesForResource = await clientService.owncloudSdk.shares.getShares(resource.path, {
share_types: ShareTypes?.link?.value?.toString(),
include_tags: false
})

const existingQuickLink = linkSharesForResource
.map((share: any) => buildShare(share.shareInfo, null, null))
.find((share: Share) => share.quicklink === true)

if (existingQuickLink) {
return doCopy({ store, language, quickLinkUrl: existingQuickLink.url })
}

const isPasswordEnforced =
store.getters.capabilities?.files_sharing?.public?.password?.enforced_for?.read_only === true

if (unref(isPasswordEnforced)) {
return showQuickLinkPasswordModal(
{ $gettext, store, passwordPolicyService },
async (password: string) => {
await store.dispatch('hideModal')
const quickLink = await createQuicklink({ ...args, password })
return doCopy({ store, language, quickLinkUrl: quickLink.url })
}
)
}

const quickLink = await createQuicklink(args)
return doCopy({ store, language, quickLinkUrl: quickLink.url })
}

export const createQuicklink = async (args: CreateQuicklink): Promise<Share> => {
const { clientService, resource, store, password, language, ability } = args
Expand All @@ -87,7 +128,9 @@ export const createQuicklink = async (args: CreateQuicklink): Promise<Share> =>
canContribute,
alias
).bitmask(allowResharing)
const params: { [key: string]: unknown } = {
const params: {
[key: string]: unknown
} = {
name: $gettext('Link'),
permissions: permissions.toString(),
quicklink: true
Expand Down
21 changes: 1 addition & 20 deletions packages/web-pkg/src/quickActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,9 @@ export default {
store,
passwordPolicyService
}: QuickLinkContext) => {
const passwordEnforced =
store.getters.capabilities?.files_sharing?.public?.password?.enforced_for?.read_only ===
true

if (passwordEnforced) {
return showQuickLinkPasswordModal(
{ store, $gettext: language.$gettext, passwordPolicyService },
async (password) => {
await copyQuicklink({
ability,
clientService,
language,
password,
resource: item,
store
})
}
)
}

await copyQuicklink({
ability,
passwordPolicyService,
clientService,
language,
resource: item,
Expand Down
Loading

0 comments on commit 467a8a6

Please sign in to comment.