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';
|
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
|
|
|
|
2024-11-21 15:25:42 +01:00
|
|
|
export let allowedRoles: number[] = [];
|
2024-05-19 18:55:28 +01:00
|
|
|
export let onDenied = () => {};
|
2024-11-21 15:25:42 +01:00
|
|
|
// 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;
|
2024-11-21 15:25:42 +01:00
|
|
|
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-05-19 18:55:28 +01:00
|
|
|
|
2024-07-21 03:02:33 +01:00
|
|
|
if (!$isOrgAdmin && $group.people.length && !isAllowed(userRole)) {
|
2024-05-19 18:55:28 +01:00
|
|
|
onDenied();
|
|
|
|
|
}
|
2021-09-11 21:52:46 +03:00
|
|
|
}
|
2024-07-21 03:02:33 +01:00
|
|
|
|
2024-11-21 15:25:42 +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}
|