|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, onMounted, ref, watch } from 'vue'; |
| 3 | +
|
| 4 | +import { InputSwitch, useConfirm, useToast } from '@/lib/primevue'; |
| 5 | +import { usePermissionStore } from '@/store'; |
| 6 | +import { Permissions } from '@/utils/constants'; |
| 7 | +
|
| 8 | +import type { Ref } from 'vue'; |
| 9 | +
|
| 10 | +// Props |
| 11 | +type Props = { |
| 12 | + bucketId: string; |
| 13 | + bucketName: string; |
| 14 | + bucketPublic: boolean; |
| 15 | + userId: string; |
| 16 | +}; |
| 17 | +
|
| 18 | +const props = withDefaults(defineProps<Props>(), {}); |
| 19 | +
|
| 20 | +// Store |
| 21 | +const permissionStore = usePermissionStore(); |
| 22 | +
|
| 23 | +// State |
| 24 | +const isInternal: Ref<boolean> = ref(false); |
| 25 | +const fetchBucketInternal = async (): Promise<boolean> => { |
| 26 | + const bucketIdpPermissions = await permissionStore.fetchBucketIdpPermissions({ |
| 27 | + bucketId: props.bucketId, |
| 28 | + permCode: 'READ', |
| 29 | + idp: 'idir' |
| 30 | + }); |
| 31 | + const hasPerms = (bucketIdpPermissions?.length ?? 0) > 0; |
| 32 | +
|
| 33 | + return hasPerms || props.bucketPublic; |
| 34 | +}; |
| 35 | +
|
| 36 | +// check bucket for the IDP permission |
| 37 | +const isBucketInternal: Ref<boolean> = ref(false); |
| 38 | +const isParentBucketInternal: Ref<boolean> = ref(false); |
| 39 | +const fetchParentBucketInternal = async (): Promise<boolean> => { |
| 40 | + const bucketIdpPermissions = await permissionStore.fetchBucketIdpPermissions({ |
| 41 | + bucketId: props.bucketId, |
| 42 | + permCode: 'READ', |
| 43 | + idp: 'idir' |
| 44 | + }); |
| 45 | + return (bucketIdpPermissions?.length ?? 0) > 0; |
| 46 | +}; |
| 47 | +
|
| 48 | +const isToggleEnabled = computed(() => { |
| 49 | + return ( |
| 50 | + !isBucketInternal.value && |
| 51 | + !props.bucketPublic && |
| 52 | + usePermissionStore().isUserElevatedRights() && |
| 53 | + permissionStore.isBucketActionAllowed(props.bucketId, props.userId, Permissions.MANAGE) |
| 54 | + ); |
| 55 | +}); |
| 56 | +
|
| 57 | +// Actions |
| 58 | +const toast = useToast(); |
| 59 | +const confirm = useConfirm(); |
| 60 | +
|
| 61 | +const toggleIdp = async (value: boolean) => { |
| 62 | + if (value) { |
| 63 | + confirm.require({ |
| 64 | + message: "Setting this file to 'Internal only' will allow all IDIR users " + 'to view and download it.', |
| 65 | + header: 'Set file to Internal only?', |
| 66 | + acceptLabel: 'Set to Internal only', |
| 67 | + rejectLabel: 'Cancel', |
| 68 | + accept: () => { |
| 69 | + permissionStore |
| 70 | + .addBucketIdpPermission(props.bucketId, 'idir', 'READ') |
| 71 | + .then(() => { |
| 72 | + isInternal.value = true; |
| 73 | + toast.success('File set to Internal only', `"${props.bucketName}" is now Internal only`); |
| 74 | + }) |
| 75 | + .catch((e) => toast.error('Setting file to Internal only failed', e.response?.data.detail, { life: 0 })); |
| 76 | + }, |
| 77 | + reject: () => (isInternal.value = false), |
| 78 | + onHide: () => (isInternal.value = false) |
| 79 | + }); |
| 80 | + } else |
| 81 | + confirm.require({ |
| 82 | + message: |
| 83 | + "Setting this file to private will remove 'Internal only' access. " + |
| 84 | + 'Only users with permissions will be able to view or download the file.', |
| 85 | + header: 'Set file to private?', |
| 86 | + acceptLabel: 'Set to private', |
| 87 | + rejectLabel: 'Cancel', |
| 88 | + accept: () => { |
| 89 | + permissionStore |
| 90 | + .deleteBucketIdpPermission(props.bucketId, 'idir', 'READ') |
| 91 | + .then(() => { |
| 92 | + isInternal.value = false; |
| 93 | + toast.success('File set to private', `"${props.bucketName}" is no longer 'Internal only'`); |
| 94 | + }) |
| 95 | + .catch((e) => toast.error('Setting file to private failed', e.response?.data.detail, { life: 0 })); |
| 96 | + }, |
| 97 | + reject: () => (isInternal.value = true), |
| 98 | + onHide: () => (isInternal.value = true) |
| 99 | + }); |
| 100 | +}; |
| 101 | +
|
| 102 | +onMounted(async () => { |
| 103 | + isInternal.value = await fetchBucketInternal(); |
| 104 | + isParentBucketInternal.value = await fetchParentBucketInternal(); |
| 105 | +}); |
| 106 | +
|
| 107 | +watch(props, () => { |
| 108 | + if (props.bucketPublic === true) isInternal.value = true; |
| 109 | + else { |
| 110 | + const perms = permissionStore.getBucketIdpPermissions.filter( |
| 111 | + (p: any) => p.permCode === Permissions.READ && p.bucketId === props.bucketId && p.idp === 'idir' |
| 112 | + ); |
| 113 | + if (perms.length > 0) isInternal.value = true; |
| 114 | + else { |
| 115 | + isInternal.value = false; |
| 116 | + } |
| 117 | + } |
| 118 | +}); |
| 119 | +</script> |
| 120 | + |
| 121 | +<template> |
| 122 | + <span |
| 123 | + v-tooltip=" |
| 124 | + isToggleEnabled |
| 125 | + ? '' |
| 126 | + : props.bucketPublic |
| 127 | + ? 'Enabled by Public Sharing' |
| 128 | + : 'Change the folder\'s \'Internal only\' setting to update this file' |
| 129 | + " |
| 130 | + > |
| 131 | + <InputSwitch |
| 132 | + :model-value="isInternal" |
| 133 | + aria-label="Toggle to make file Internl only" |
| 134 | + :disabled="!isToggleEnabled" |
| 135 | + @update:model-value="toggleIdp($event)" |
| 136 | + /> |
| 137 | + </span> |
| 138 | +</template> |
0 commit comments