Skip to content

Commit

Permalink
feat: add permission options to requestable files
Browse files Browse the repository at this point in the history
  • Loading branch information
NuttyShrimp committed Dec 2, 2024
1 parent 7a53613 commit 352538a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
9 changes: 3 additions & 6 deletions controller/src/classes/accessRequests/AccessRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getDatetime, getLinkedResourceUrlAll, getResourceInfo, getStringNoLocal
import { AccessRequestMessage, Permission, RequestResponseMessage, ResourceAccessRequestNode, Resources } from "../../types";
import { IAccessRequest, IController, IInbox, IInboxConstructor, IStore } from "../../types/modules";
import { cacheBustedFetch } from "../../util";
import { PermissionToACL } from "../utils/Permissions";

const REQUEST_RESPONSE_TYPES = ["as:Accept", "as:Reject"]

Expand Down Expand Up @@ -94,7 +95,7 @@ export abstract class AccessRequest implements IAccessRequest {
await this.resources.saveToRemote();
}

async sendRequestNotification(originWebId: string, resources: string[]) {
async sendRequestNotification(originWebId: string, resources: string[], permissions: Permission[]) {
const requestableResources = await this.resources.getCurrent();
const filteredResources = resources.filter(r => requestableResources.items.includes(r));

Expand Down Expand Up @@ -128,11 +129,7 @@ export abstract class AccessRequest implements IAccessRequest {
"@type": "acl:Authorization",
"acl:agent": originWebId,
"acl:accessTo": r,
"acl:mode": [
{
"@id": "acl:Read"
}
]
"acl:mode": permissions.map(p => ({ "@id": PermissionToACL(p) }))
}
});
};
Expand Down
16 changes: 16 additions & 0 deletions controller/src/classes/utils/Permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Permission } from "../../types";

export const PermissionToACL = (permission: Permission): string => {
switch (permission) {
case Permission.Read:
return "acl:Read"
case Permission.Write:
return "acl:Write"
case Permission.Append:
return "acl:Append"
case Permission.Control:
return "acl:Control"
default:
throw new Error("Permission not found");
}
}
2 changes: 1 addition & 1 deletion controller/src/types/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface IAccessRequest {
disallowAccessRequest(resourceUrl: string): Promise<void>

// Notifications
sendRequestNotification(originWebId: string, resources: string[]): Promise<void>;
sendRequestNotification(originWebId: string, resources: string[], permissions: Permission[]): Promise<void>;
sendResponseNotification(type: "accept" | "reject", message: AccessRequestMessage): Promise<void>;

loadAccessRequests(): Promise<AccessRequestMessage[]>;
Expand Down
9 changes: 9 additions & 0 deletions loama/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Permission } from "loama-controller";

export const uriToName = (uri: string, isContainer: boolean) => {
const splitted = uri.split('/');

Expand All @@ -17,3 +19,10 @@ export const debounce = (fn: Function, wait: number) => {
}, wait);
}
}

export const allPermissions = [
Permission.Read,
Permission.Write,
Permission.Append,
Permission.Control,
]
34 changes: 29 additions & 5 deletions loama/src/views/RequestView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
<div>
<label class="input-label" for="webid">
<span>Web ID </span>
<RequestWebIdPopup title="Target Web Id" message="The web ID url where you want to request access to files." />
<RequestWebIdPopup title="Target Web Id"
message="The web ID url where you want to request access to files." />
</label>
<LoInput v-model="webId" name="webid" />
</div>
<div>
<label class="input-label" for="webid">
<span>Pod</span>
<RequestWebIdPopup title="Target pod" message="Select one of the pods (retrieved from the given web Id). The pods should have access requestable resources" />
<RequestWebIdPopup title="Target pod"
message="Select one of the pods (retrieved from the given web Id). The pods should have access requestable resources" />
</label>
<Select name="pod-url" v-model="selectedPodUrl" :options="podUrls" placeholder="Select a pod URL" />
</div>
Expand All @@ -24,23 +26,30 @@
<div v-else>
<p>No pod selected!</p>
</div>
<div class="permission-box" v-if="Object.keys(selectedEntries).length > 0">
<LoSwitch v-for="perm in allPermissions" :key="perm" :id="perm" :default-value="false"
@update:checked="checked => requestedPermissions[perm] = checked">
{{ perm }}
</LoSwitch>
</div>
<LoButton @click="sendRequestNotification" :disabled="Object.keys(selectedEntries).length < 1">Request Access
</LoButton>
</div>
</template>
<script setup lang="ts">
import LoInput from '@/components/LoInput.vue';
import { getDefaultSession } from '@inrupt/solid-client-authn-browser';
import { createBasicController, type ResourceAccessRequestNode } from 'loama-controller';
import { Permission, createBasicController, type ResourceAccessRequestNode } from 'loama-controller';
import { ref, watch } from 'vue';
import Select from 'primevue/select';
import { listWebIdPodUrls } from 'loama-common';
import Tree, { type TreeSelectionKeys } from 'primevue/tree';
import type { TreeNode } from 'primevue/treenode';
import { debounce } from '@/lib/utils';
import { allPermissions, debounce } from '@/lib/utils';
import LoButton from '@/components/LoButton.vue';
import { useToast } from 'primevue/usetoast';
import RequestWebIdPopup from '@/components/popups/RequestWebIdPopup.vue';
import LoSwitch from '@/components/LoSwitch.vue';
const toast = useToast();
Expand All @@ -55,6 +64,12 @@ const debouncedPodFetcher = debounce(async (newVal: string) => {
let pods = await listWebIdPodUrls(newVal, session.fetch);
podUrls.value = pods;
}, 1000)
const requestedPermissions = ref({
[Permission.Read]: false,
[Permission.Write]: false,
[Permission.Append]: false,
[Permission.Control]: false,
})
const accessRequestNodeToTreeNode = (key: string, node: ResourceAccessRequestNode): TreeNode => {
return {
Expand All @@ -68,8 +83,10 @@ const accessRequestNodeToTreeNode = (key: string, node: ResourceAccessRequestNod
const sendRequestNotification = async () => {
const session = getDefaultSession();
const checkedEntries = Object.keys(selectedEntries.value).filter(k => selectedEntries.value[k].checked)
const permissions = Object.entries(requestedPermissions.value).filter(([_, checked]) => checked).map(([perm, _]) => perm as Permission)
try {
await controller.value.AccessRequest().sendRequestNotification(session.info.webId!, checkedEntries)
await controller.value.AccessRequest().sendRequestNotification(session.info.webId!, checkedEntries, permissions)
toast.add({
severity: "success",
summary: "Access request(s) sent to user",
Expand Down Expand Up @@ -126,4 +143,11 @@ watch(selectedPodUrl, async (newPodUrl) => {
display: flex;
}
}
.permission-box {
display: flex;
padding: .5em;
border-radius: 1em;
background: white;
}
</style>

0 comments on commit 352538a

Please sign in to comment.