Skip to content

Commit

Permalink
Object perms
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas ONeil <[email protected]>
  • Loading branch information
loneil committed Jan 19, 2023
1 parent ac7f6e7 commit 16ecd0d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 35 deletions.
10 changes: 7 additions & 3 deletions frontend/src/components/object/ObjectList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Button from 'primevue/button';
import { useToast } from 'primevue/usetoast';
// State
import { storeToRefs } from 'pinia';
import { useBucketStore } from '@/store';
import { useObjectStore } from '@/store';
// Components
import DeleteObjectButton from './DeleteObjectButton.vue';
Expand All @@ -17,6 +18,7 @@ import ObjectSidebar from './ObjectSidebar.vue';
import ObjectTable from './ObjectTable.vue';
import ObjectUpload from './ObjectUpload.vue';
const bucketStore = useBucketStore();
const objectStore = useObjectStore();
const { multiSelectedObjects } = storeToRefs(objectStore);
const route = useRoute();
Expand All @@ -43,6 +45,10 @@ const closeUpload = () => {
const listObjects = async () => {
try {
await Promise.all([
bucketStore.getBucketPermissionsForUser(route.query.bucketId?.toString() || ''),
objectStore.listObjects({ bucketId: route.query.bucketId })
]);
await objectStore.listObjects({ bucketId: route.query.bucketId });
} catch (error: any) {
toast.add({ severity: 'error', summary: 'Unable to load Objects.', detail: error, life: 5000 });
Expand Down Expand Up @@ -108,6 +114,4 @@ onMounted(() => {
</div>
</template>

<style scoped>
</style>
<style scoped></style>
22 changes: 1 addition & 21 deletions frontend/src/components/object/ObjectPermission.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ const removeBucketUser = (userId: string) => {

<template>
<div>
<Button class="mt-1 mb-4">
<font-awesome-icon
icon="fa-solid fa-user-plus"
class="mr-1"
/> Add user
</Button>

<DataTable
:loading="loading"
:value="selectedObjectPermissions"
Expand All @@ -62,26 +55,13 @@ const removeBucketUser = (userId: string) => {
>
<template #empty>
<div class="flex justify-content-center">
<h3>There are no users associated with this bucket.</h3>
<h3>There are no users associated with this object.</h3>
</div>
</template>
<Column
field="fullName"
header="Name"
/>
<Column
header="Upload"
body-class="content-center"
>
<template #body="{ data }">
<Checkbox
v-model="data.create"
input-id="create"
:binary="true"
@input="(value) => updateObjectPermission(value, data.userId, Permissions.CREATE)"
/>
</template>
</Column>
<Column
header="Read"
body-class="content-center"
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/components/object/ObjectTable.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
// Types
import { ButtonMode } from '@/interfaces/common/enums';
import type { Permission } from '@/interfaces';
// Vue
import { storeToRefs } from 'pinia';
Expand All @@ -11,8 +12,10 @@ import Column from 'primevue/column';
import DataTable from 'primevue/datatable';
import Dialog from 'primevue/dialog';
// State
import { useBucketStore } from '@/store';
import { useObjectStore } from '@/store';
// Other
import { Permissions } from '@/utils/constants';
import { formatDateLong } from '@/utils/formatters';
import DeleteObjectButton from './DeleteObjectButton.vue';
import DownloadObjectButton from './DownloadObjectButton.vue';
Expand All @@ -25,6 +28,7 @@ defineProps({
},
});
const { selectedBucketPermissionsForUser } = storeToRefs(useBucketStore());
const { loading, multiSelectedObjects, objectList } = storeToRefs(
useObjectStore()
);
Expand All @@ -44,6 +48,17 @@ const showPermissions = async (objectId: string, objectName: string) => {
permissionsObjectId.value = objectId;
permissionsObjectName.value = objectName;
};
// Permission gaurds for the buttons
const isActionAllowed = (objectPermissions: Permission[], perm: string) => {
// If you have bucket manage you can do it all
// Or if you have the permission on the object
return (
selectedBucketPermissionsForUser.value.some(
(p) => p.permCode === Permissions.MANAGE
) || objectPermissions.some((op) => op.permCode === perm)
);
};
</script>

<template>
Expand Down Expand Up @@ -128,10 +143,12 @@ const showPermissions = async (objectId: string, objectName: string) => {
>
<template #body="{ data }">
<DownloadObjectButton
v-if="isActionAllowed(data.permissions, Permissions.READ)"
:mode="ButtonMode.ICON"
:ids="[data.id]"
/>
<Button
v-if="isActionAllowed(data.permissions, Permissions.MANAGE)"
class="p-button-lg p-button-rounded p-button-text"
@click="showPermissions(data.id, data.name)"
>
Expand All @@ -144,6 +161,7 @@ const showPermissions = async (objectId: string, objectName: string) => {
<font-awesome-icon icon="fa-solid fa-circle-info" />
</Button>
<DeleteObjectButton
v-if="isActionAllowed(data.permissions, Permissions.DELETE)"
:mode="ButtonMode.ICON"
:ids="[data.id]"
/>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/interfaces/UserPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface UserPermissions {
userId: string;
elevatedRights?: boolean;
fullName: string;
create: boolean;
create?: boolean;
read: boolean;
update: boolean;
delete: boolean;
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/services/permissionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@ export default {
objectSearchPermissions(params?: Object) {
return comsAxios().get(`${PATH}/${OBJECT}`, { params: params });
},

/**
* @function objectGetPermissions
* Returns a list of permissions for the object
* @param {string} objectId ID of the object
* @param {Object} params Optional object containing the data to filter against
* @returns {Promise} An axios response
*/
objectGetPermissions(objectId: string, params?: Object) {
return comsAxios().get(`${PATH}/${OBJECT}/${objectId}`, { params: params });
},
};
23 changes: 22 additions & 1 deletion frontend/src/store/bucketStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const useBucketStore = defineStore('bucket', () => {
const loading = ref(false);
const buckets = ref([] as Bucket[]);
const permissions = ref([] as UserPermissions[]);
const selectedBucketPermissionsForUser = ref([] as Permission[]);

// actions
async function load() {
Expand Down Expand Up @@ -48,6 +49,7 @@ export const useBucketStore = defineStore('bucket', () => {
return bucket;
}

// Get the set of permissions for a bucket
async function getBucketPermissions(bucketId: string) {
try {
loading.value = true;
Expand Down Expand Up @@ -96,6 +98,23 @@ export const useBucketStore = defineStore('bucket', () => {
}
}

// Get a user's bucket permissions
async function getBucketPermissionsForUser(bucketId: string) {
try {
loading.value = true;

if (currentUser.value) {
const bucketPerms =
(await permissionService.bucketSearchPermissions({ bucketId, userId: currentUser.value.userId })).data;
if(bucketPerms && bucketPerms[0] && bucketPerms[0].permissions) {
selectedBucketPermissionsForUser.value =bucketPerms[0].permissions;
}
}
} finally {
loading.value = false;
}
}

async function addBucketPermission(bucketId: string, userId: string, permCode: string) {
try {
loading.value = true;
Expand Down Expand Up @@ -135,10 +154,12 @@ export const useBucketStore = defineStore('bucket', () => {
load,
getBucketInfo,
getBucketPermissions,
getBucketPermissionsForUser,
addBucketPermission,
deleteBucketPermission,
removeBucketUser,
buckets,
permissions
permissions,
selectedBucketPermissionsForUser
};
});
19 changes: 10 additions & 9 deletions frontend/src/store/objectStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ export const useObjectStore = defineStore('objectStore', () => {
obj.metadata = metadata;
obj.name = metadata.metadata.find((x: { key: string }) => x.key === 'name')?.value;
}
});

// Add the permissions to each object list item
obj.permissions = permResponse
.find((p: { objectId: string, permission: UserPermissions}) => p.objectId === obj.id).permissions;
});

objectList.value = objects;
}
} catch (error) {
Expand All @@ -110,27 +114,24 @@ export const useObjectStore = defineStore('objectStore', () => {
try {
loading.value = true;

const searchPerms = (
await objectService.searchForPermissions(objectId)
const objPerms = (
await permissionService.objectGetPermissions(objectId)
).data;

if (searchPerms[0]) {
const perms = searchPerms[0].permissions;

if (objPerms.length) {
// Get the user records for the unique user IDs in the perms
const uniqueIds = [...new Set(perms.map((x: any) => x.userId))].join(',');
const uniqueIds = [...new Set(objPerms.map((x: any) => x.userId))].join(',');
const uniqueUsers = (await userService.searchForUsers({ userId: uniqueIds })).data;

const hasPermission = (userId: string, permission: string) => {
return perms.some((perm: any) => perm.userId === userId && perm.permCode === permission);
return objPerms.some((perm: any) => perm.userId === userId && perm.permCode === permission);
};

const userPermissions: UserPermissions[] = [];
uniqueUsers.forEach((user: User) => {
userPermissions.push({
userId: user.userId,
fullName: user.fullName,
create: hasPermission(user.userId, Permissions.CREATE),
read: hasPermission(user.userId, Permissions.READ),
update: hasPermission(user.userId, Permissions.UPDATE),
delete: hasPermission(user.userId, Permissions.DELETE),
Expand Down

0 comments on commit 16ecd0d

Please sign in to comment.