2021-09-11 21:52:46 +03:00
|
|
|
<script lang="ts">
|
2023-08-02 19:35:27 +01:00
|
|
|
import { profile } from '$lib/utils/store/user';
|
2025-12-02 09:54:38 +01:00
|
|
|
import { group } from '$lib/components/Course/store';
|
2024-07-08 06:52:23 +01:00
|
|
|
import { isOrgAdmin } from '$lib/utils/store/org';
|
2021-09-11 21:52:46 +03:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
interface Props {
|
|
|
|
|
allowedRoles?: number[];
|
|
|
|
|
onDenied?: any;
|
|
|
|
|
// Since orgAdmin can see all, we need a way to show the content only to students
|
|
|
|
|
onlyStudent?: boolean;
|
|
|
|
|
children?: import('svelte').Snippet;
|
|
|
|
|
}
|
2021-09-11 21:52:46 +03:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
let { allowedRoles = [], onDenied = () => {}, onlyStudent = false, children }: Props = $props();
|
|
|
|
|
|
|
|
|
|
const userRole: number = $derived.by(() => {
|
|
|
|
|
const user = $group.people.find((person) => person.profile_id === $profile.id);
|
|
|
|
|
return user ? user.role_id : 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const show = $derived.by(() => {
|
|
|
|
|
if (onlyStudent) {
|
|
|
|
|
return isAllowed(userRole);
|
|
|
|
|
} else {
|
|
|
|
|
return isAllowed(userRole) || !!$isOrgAdmin;
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-09-11 21:52:46 +03:00
|
|
|
|
2024-07-21 03:02:33 +01:00
|
|
|
function isAllowed(userRole) {
|
|
|
|
|
return allowedRoles.includes(userRole);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
$effect(() => {
|
2024-07-21 03:02:33 +01:00
|
|
|
if (!$isOrgAdmin && $group.people.length && !isAllowed(userRole)) {
|
2024-05-19 18:55:28 +01:00
|
|
|
onDenied();
|
|
|
|
|
}
|
2025-11-01 15:49:29 +01:00
|
|
|
});
|
2021-09-11 21:52:46 +03:00
|
|
|
</script>
|
|
|
|
|
|
2024-07-21 03:02:33 +01:00
|
|
|
{#if show}
|
2025-11-01 15:49:29 +01:00
|
|
|
{@render children?.()}
|
2021-09-11 21:52:46 +03:00
|
|
|
{/if}
|