Skip to content

Commit

Permalink
permissions for submit page
Browse files Browse the repository at this point in the history
  • Loading branch information
reyniersbram committed May 19, 2024
1 parent de7589b commit 9f997b7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
42 changes: 26 additions & 16 deletions frontend/src/queries/Group.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { computed, toValue } from "vue";
import type { MaybeRefOrGetter } from "vue";
import { useMutation, useQuery, useQueryClient } from "@tanstack/vue-query";
import type { UseMutationReturnType, UseQueryReturnType } from "@tanstack/vue-query";
import type { QueryClient, UseMutationReturnType, UseQueryReturnType } from "@tanstack/vue-query";
import type Group from "@/models/Group";
import type { GroupForm } from "@/models/Group";
import {
Expand Down Expand Up @@ -51,13 +51,17 @@ export function useGroupQuery(
* Query composable for fetching all groups of a project
*/
export function useProjectGroupsQuery(
projectId: MaybeRefOrGetter<number | undefined>
projectId: MaybeRefOrGetter<number | undefined>,
queryClient?: QueryClient
): UseQueryReturnType<Group[], Error> {
return useQuery<Group[], Error>({
queryKey: computed(() => PROJECT_GROUPS_QUERY_KEY(toValue(projectId)!)),
queryFn: () => getProjectGroups(toValue(projectId)!),
enabled: !!toValue(projectId),
});
return useQuery<Group[], Error>(
{
queryKey: computed(() => PROJECT_GROUPS_QUERY_KEY(toValue(projectId)!)),
queryFn: () => getProjectGroups(toValue(projectId)!),
enabled: !!toValue(projectId),
},
queryClient
);
}

export function useUserGroupsQuery(): UseQueryReturnType<Group[], Error> {
Expand All @@ -73,20 +77,26 @@ export function useUserGroupsQuery(): UseQueryReturnType<Group[], Error> {
* @returns The group the user is in for the project, undefined if the user is not in a group
*/
export function useProjectGroupQuery(
projectId: MaybeRefOrGetter<number | undefined>
projectId: MaybeRefOrGetter<number | undefined>,
queryClient?: QueryClient
): UseQueryReturnType<Group | null, Error> {
const { data: projectGroups } = useProjectGroupsQuery(projectId);
const { data: user } = useCurrentUserQuery();
const userGroup = computed(
() =>
const { data: projectGroups } = useProjectGroupsQuery(projectId, queryClient);
const { data: user } = useCurrentUserQuery(queryClient);
const userGroup = computed(() => {
if (projectGroups.value === undefined || user.value === undefined) return undefined;
return (
projectGroups.value?.find((group) =>
group.members.some((member) => member.uid === user.value?.uid)
) || null
);
return useQuery({
queryKey: computed(() => PROJECT_USER_GROUP_QUERY_KEY(toValue(projectId)!)),
queryFn: () => userGroup,
);
});
return useQuery(
{
queryKey: computed(() => PROJECT_USER_GROUP_QUERY_KEY(toValue(projectId)!)),
queryFn: () => userGroup,
},
queryClient
);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useCanVisit, {
useIsPartOfSubjectCondition,

Check warning on line 12 in frontend/src/router/index.ts

View workflow job for this annotation

GitHub Actions / Run linters

'useIsPartOfSubjectCondition' is defined but never used
useAndCondition,
useOrCondition,
useIsInGroupOfProjectCondition,
} from "./middleware/canVisit";
import { ref } from "vue";

Expand Down Expand Up @@ -78,10 +79,7 @@ const router = createRouter({
component: () => import("../views/SubmitView.vue"),
props: (route) => ({ projectId: Number(route.params.projectId) }),
meta: {
middleware: useCanVisit((queryClient) => {
// TODO: implement -> check if user is enlroled in subject and in a group for project
return { condition: ref(true), isLoading: ref(false) };
}),
middleware: useCanVisit(useIsInGroupOfProjectCondition),
},
},
{
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/router/middleware/canVisit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import useIsAdmin from "@/composables/useIsAdmin";
import useIsTeacher from "@/composables/useIsTeacher";
import { useSubjectsQuery } from "@/queries/Subject";
import { useProjectsQuery } from "@/queries/Project";
import { useProjectGroupQuery } from "@/queries/Group";

export interface CanVisitCondition {
(
Expand Down Expand Up @@ -120,6 +121,15 @@ export const useIsInstructorOfProjectCondition: CanVisitCondition = (qc, ctx) =>
return { condition, isLoading };
};

export const useIsInGroupOfProjectCondition: CanVisitCondition = (qc, ctx) => {
const projectId = Number(ctx.to.params.projectId);
const { data: group, isLoading } = useProjectGroupQuery(projectId, qc);
const condition = computed(() => {
return group.value !== null;
});
return { condition, isLoading };
};

export const useIsPartOfSubjectCondition: CanVisitCondition = (qc, ctx) => {
const subjectId = Number(ctx.to.params.subjectId);
const { data: subjects, isLoading } = useSubjectsQuery(qc);
Expand Down

0 comments on commit 9f997b7

Please sign in to comment.