Skip to content

Commit

Permalink
Fix type signature of auth check functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danieladugyan committed Oct 18, 2024
1 parent 7582cb4 commit 5dfe86a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/lib/utils/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import * as m from "$paraglide/messages";
* Check if the user is authorized to perform an action.
* @returns Whether the user is authorized.
*/
export const isAuthorized = (apiName: string, user?: AuthUser): boolean => {
export const isAuthorized = (
apiName: string,
user: AuthUser | undefined,
): boolean => {
if (dev && !!user?.studentId) return true;
if (user?.policies.includes(apiName)) return true;
return false;
Expand All @@ -17,7 +20,10 @@ export const isAuthorized = (apiName: string, user?: AuthUser): boolean => {
* Authorize a user to perform an action or a list of actions.
* @throws {HttpError} If the user is not authorized.
*/
export const authorize = (apiName: string | string[], user?: AuthUser) => {
export const authorize = (
apiName: string | string[],
user: AuthUser | undefined,
) => {
const apiNames = Array.isArray(apiName) ? apiName : [apiName];
for (const name of apiNames) {
if (!isAuthorized(name, user)) {
Expand Down
3 changes: 2 additions & 1 deletion src/routes/(app)/admin/debug/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<script lang="ts">
import { version } from "$app/environment";
import { page } from "$app/stores";
import { isAuthorized } from "$lib/utils/authorization";
export let data;
$: user = data.user;
$: policies = user.policies.toSorted();
</script>

{#if isAuthorized("core:admin")}
{#if isAuthorized("core:admin", $page.data.user)}
<section class="mb-4">
<h1 class="mb-2 text-lg font-semibold">Actions</h1>
<form action="?/keycloakSync" method="POST" class="flex items-center gap-4">
Expand Down

0 comments on commit 5dfe86a

Please sign in to comment.