Files
classroomio/apps/dashboard/src/lib/components/RoleBasedSecurity/index.svelte
T

39 lines
960 B
Svelte
Raw Normal View History

2021-09-11 21:52:46 +03:00
<script lang="ts">
import { profile } from '$lib/utils/store/user';
2021-09-11 21:52:46 +03:00
import { group } from '../Course/store';
2024-05-18 21:50:42 +01:00
import type { GroupPerson } from '$lib/utils/types';
2024-07-08 06:52:23 +01:00
import { isOrgAdmin } from '$lib/utils/store/org';
2021-09-11 21:52:46 +03:00
export let allowedRoles: number[] = [];
export let onDenied = () => {};
// Since orgAdmin can see all, we need a way to show the content only to students
2024-07-21 03:02:33 +01:00
export let onlyStudent = false;
2021-09-11 21:52:46 +03:00
let userRole: number = 0;
let show = false;
2021-09-11 21:52:46 +03:00
2024-07-21 03:02:33 +01:00
function isAllowed(userRole) {
return allowedRoles.includes(userRole);
}
2021-09-11 21:52:46 +03:00
$: {
2024-05-18 21:50:42 +01:00
const user: GroupPerson = $group.people.find((person) => person.profile_id === $profile.id)!;
2021-09-11 21:52:46 +03:00
2024-05-18 21:50:42 +01:00
userRole = user ? user.role_id : userRole;
2024-07-21 03:02:33 +01:00
if (!$isOrgAdmin && $group.people.length && !isAllowed(userRole)) {
onDenied();
}
2021-09-11 21:52:46 +03:00
}
2024-07-21 03:02:33 +01:00
$: if (onlyStudent) {
show = isAllowed(userRole);
} else {
show = isAllowed(userRole) || $isOrgAdmin;
}
2021-09-11 21:52:46 +03:00
</script>
2024-07-21 03:02:33 +01:00
{#if show}
2021-09-11 21:52:46 +03:00
<slot />
{/if}