Skip to content

Commit

Permalink
Pushing current progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Zelzahn committed Jul 23, 2024
1 parent 53ac7e3 commit bf0a742
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 22 deletions.
21 changes: 19 additions & 2 deletions controller/src/index-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import {
saveFileInContainer,
} from "@inrupt/solid-client";
import { Session } from "@inrupt/solid-client-authn-browser";
import { Index, IndexItem, Permission, url, UserTypeObject } from "./types";
import {
Index,
IndexItem,
Permission,
Type,
url,
UserTypeObject,
} from "./types";
import {
setAgentAccess,
setPublicAccess,
Expand Down Expand Up @@ -102,7 +109,7 @@ export async function editPermissions(
const itemIndex = index.items.findIndex(({ id }) => id === itemId);

if (itemIndex === -1) {
throw new Error("Element not found");
throw new Error("Item is not found, is it present in the index file?");
}

const item = index.items[itemIndex];
Expand Down Expand Up @@ -130,6 +137,16 @@ export async function editPermissions(
return index;
}

export function getItemId(index: Index, resource: url, user: url) {
return index.items.find(
(indexItem) =>
indexItem.resources.includes(resource) &&
indexItem.userType &&
indexItem.userType.type === Type.WebID &&
indexItem.userType.url === user
)?.id;
}

async function updateACL(
session: Session,
resources: url[],
Expand Down
20 changes: 12 additions & 8 deletions controller/src/pod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ async function listThings(
): Promise<FormattedThing[]> {
const dataset = await getSolidDataset(url, { fetch: session.fetch });
return Promise.all(
getThingAll(dataset).map(async (t) => ({
url: t.url,
properties: getPropertyAll(t),
accessModes: await getAccessModes(session, t.url),
}))
getThingAll(dataset)
// For some reason the appointments container is utterly borked permissions-wise
.filter((t) => !t.url.includes("appointments"))
.map(async (t) => ({
url: t.url,
properties: getPropertyAll(t),
accessModes: await getAccessModes(session, t.url),
}))
);
}

Expand Down Expand Up @@ -80,8 +83,8 @@ async function getAccessModes(
function accessModesToPermissions(
record: Record<url, AccessModes>
): Record<url, Permission[]> {
const result: {
[agent: string]: Permission[]
const result: {
[agent: string]: Permission[];
} = {};
// List -> Set -> List is done to filter out possible duplicate Permission.Control's
const mapToPermission = (accessModes: [string, boolean][]) => [
Expand Down Expand Up @@ -210,5 +213,6 @@ export async function getAppointments(
};
};

return fetchResources(session, `${url}/appointments/`, mapAppointment);
// As the appointments folder is borked, cooltoinments is used as a back-up.
return fetchResources(session, `${url}/cooltoinments/`, mapAppointment);
}
2 changes: 1 addition & 1 deletion loama/src/components/explorer/ResourceExplorer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</div>
</div>
<SelectedEntry v-else :name="selectedEntry.name" :isContainer="selectedEntry.isContainer"
:agents="selectedEntry.accessModes" @close="selectedEntry = null" />
:url="selectedEntry.url" :agents="selectedEntry.accessModes" @close="selectedEntry = null" />
</div>
</div>
</template>
Expand Down
39 changes: 28 additions & 11 deletions loama/src/components/explorer/SelectedEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
@update:checked="updatePermissions(option.name, $event)">
{{ option.label }}
</LoSwitch>
<output v-if="updateStatus" name="result" :for="permissionOptions.map((o) => o.name).join(' ')">
{{ updateStatus }}
</output>
</form>
</article>
</div>
Expand All @@ -27,24 +30,28 @@
<script setup lang="ts">
import ExplorerEntity from './ExplorerEntity.vue';
import { PhXCircle } from '@phosphor-icons/vue';
import { ref } from 'vue';
import { ref, watch } from 'vue';
import LoSwitch from '../LoSwitch.vue';
import { type url, Permission } from 'loama-controller/dist/types';
import { editPermissions, getOrCreateIndex } from 'loama-controller';
import { Permission, Type } from 'loama-controller/dist/types';
import { editPermissions, getOrCreateIndex, addPermissions, getItemId } from 'loama-controller';
import { store } from '@/store';
import type { Session } from '@inrupt/solid-client-authn-browser';
import type { Result } from '@/utils/types';
const props = defineProps<{ name: string; isContainer: boolean; agents: Record<url, Permission[]> }>();
const props = defineProps<{ name: string; url: string; isContainer: boolean; agents: Record<string, Permission[]> }>();
const selectedAgent = ref(Object.keys(props.agents)[0]);
const updateStatus = ref<Result | null>(null)
const permissionOptions = ref([
watch(props, () => updateStatus.value = null);
const permissionOptions = [
{ name: 'Read', label: "Able to read data" },
{ name: 'Write', label: "Able to add new data" },
{ name: 'Append', label: 'Able to modify existing data' },
{ name: 'Control', label: 'Able to manage access & permissions' }
])
];
// @ts-ignore If you cast the permission to a Permissions the comparison no longer works.
const isByDefaultSelected = (permission: string) => props.agents[selectedAgent.value].includes(permission)
Expand All @@ -60,13 +67,23 @@ const updatePermissions = async (type: string, newValue: boolean) => {
permissions = permissions.filter((p) => p !== type)
}
// TODO: Maybe map selectedAgent to the one from the index file
await editPermissions(store.session as Session, indexFile, selectedAgent.value, permissions);
const itemId = getItemId(indexFile, props.url, selectedAgent.value);
try {
if (itemId) {
await editPermissions(store.session as Session, indexFile, itemId, permissions)
} else {
// NOTE: This should be more fleshed out, e.g. username support
const userType = { type: Type.WebID, url: selectedAgent.value };
await addPermissions(store.session as Session, indexFile, [props.url], userType, permissions);
}
// TODO: Make sure the changed index file is shown here
// props.agents[selectedAgent.value] = permissions;
// TODO: invalidate the data and refresh it
// TODO: Some kind of notification?
updateStatus.value = { ok: true, value: 'The change is successfully performed!' }
} catch (e) {
updateStatus.value = { ok: false, error: e }
}
}
</script>

Expand Down
2 changes: 2 additions & 0 deletions loama/src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { FormattedThing } from 'loama-controller/dist/types'

export type Entry = FormattedThing & { name: string; isContainer: boolean }

export type Result = { ok: true; value: string } | { ok: false; error: unknown }

0 comments on commit bf0a742

Please sign in to comment.