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

Allow to delete passwords on public links when password is enforced but permission is set #9857

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
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)) {
AlexAndBear marked this conversation as resolved.
Show resolved Hide resolved
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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh I don't get this check in the first place? If password is not enforced we don't allow the user to add a password ?

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
90 changes: 65 additions & 25 deletions packages/web-pkg/src/helpers/share/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ 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
passwordPolicyService: PasswordPolicyService
language: Language
store: Store<any>
storageId?: any
Expand All @@ -22,37 +27,36 @@ export interface CreateQuicklink {
ability: Ability
}

export const copyQuicklink = async (args: CreateQuicklink) => {
const { store, language } = args
// 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

// 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 () => {
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 +68,40 @@ export const copyQuicklink = async (args: CreateQuicklink) => {
})
}
}
export const copyQuicklink = async (args: CreateQuicklink) => {
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 +125,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
})
}
)
}

Comment on lines -77 to -96
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

centralized

await copyQuicklink({
ability,
passwordPolicyService,
clientService,
language,
resource: item,
Expand Down
3 changes: 3 additions & 0 deletions packages/web-runtime/src/services/auth/abilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export const getAbilities = (
],
'Logo.Write.all': [{ action: 'update-all', subject: 'Logo' }],
'PublicLink.Write.all': [{ action: 'create-all', subject: 'PublicLink' }],
'ReadOnlyPublicLinkPassword.Delete.all': [
{ action: 'delete-all', subject: 'ReadOnlyPublicLinkPassword' }
],
'Roles.ReadWrite.all': [
{ action: 'create-all', subject: 'Role' },
{ action: 'delete-all', subject: 'Role' },
Expand Down