Files
classroomio/apps/dashboard/src/lib/features/ui/role-based-security.svelte
T

43 lines
1.1 KiB
Svelte
Raw Normal View History

2021-09-11 21:52:46 +03:00
<script lang="ts">
import { profile } from '$lib/utils/store/user';
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)) {
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}