-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
including testing in precommit validate
- Loading branch information
1 parent
ad81625
commit ec44c7d
Showing
10 changed files
with
129 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function isInRole({ userRoles, requiredRoles }) { | ||
return userRoles.some((role) => requiredRoles.includes(role)); | ||
} | ||
|
||
export default isInRole; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
import isInRole from './isInRole'; | ||
|
||
describe('isInRole', () => { | ||
it('passes when user has has matched at least one role', () => { | ||
const userRoles = ['admin', 'sponsor-admin']; | ||
const requiredRoles = ['admin', 'volunteer']; | ||
|
||
expect(isInRole({ userRoles, requiredRoles })).toBe(true); | ||
}); | ||
|
||
it('passes when user matches all roles', () => { | ||
const userRoles = ['admin', 'sponsor-admin']; | ||
const requiredRoles = ['admin', 'sponsor-admin']; | ||
|
||
expect(isInRole({ userRoles, requiredRoles })).toBe(true); | ||
}); | ||
|
||
it('fails when user does not have the any matching roles', () => { | ||
const userRoles = ['admin', 'sponsor-admin']; | ||
const requiredRoles = ['volunteer', 'member']; | ||
|
||
expect(isInRole({ userRoles, requiredRoles })).toBe(false); | ||
}); | ||
|
||
it('fails when user only has one role and matches nothing', () => { | ||
const userRoles = ['admin', 'sponsor-admin']; | ||
const requiredRoles = ['member']; | ||
|
||
expect(isInRole({ userRoles, requiredRoles })).toBe(false); | ||
}); | ||
|
||
it('passes when user only has one matching role', () => { | ||
const userRoles = ['admin', 'sponsor-admin']; | ||
const requiredRoles = ['admin']; | ||
|
||
expect(isInRole({ userRoles, requiredRoles })).toBe(true); | ||
}); | ||
|
||
it('fails when user has no roles', () => { | ||
const userRoles = []; | ||
const requiredRoles = ['admin']; | ||
|
||
expect(isInRole({ userRoles, requiredRoles })).toBe(false); | ||
}); | ||
|
||
it('fails when no roles are required', () => { | ||
const userRoles = ['admin', 'volunteer']; | ||
const requiredRoles = []; | ||
|
||
expect(isInRole({ userRoles, requiredRoles })).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,15 @@ | ||
/* This empty page is intentional | ||
* having this +page.server page here forces the hooks.server handler to execute | ||
* regardless if page.svelte is fully loaded or not | ||
* https://github.com/sveltejs/kit/issues/6315 | ||
*/ | ||
import { redirect, error } from '@sveltejs/kit'; | ||
|
||
import { error, redirect } from '@sveltejs/kit'; | ||
import isInRole from '$lib/isInRole'; | ||
|
||
export async function load({ locals, url }) { | ||
const session = await locals.getSession(); | ||
|
||
if (!session) { | ||
redirect(303, `/login-redirect?returnTo=${url.pathname}`); | ||
if (!isInRole({ userRoles: session.user?.permissions, requiredRoles: ['admin', 'volunteer'] })) { | ||
throw error(401, 'Required Privileges Not Met'); | ||
} | ||
|
||
if (!session.user?.permissions.includes('admin')) { | ||
throw error(401, 'requires admin'); | ||
if (!session) { | ||
redirect(303, `/login-redirect?returnTo=${url.pathname}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { error } from '@sveltejs/kit'; | ||
|
||
import isInRole from '$lib/isInRole'; | ||
|
||
export const load = async ({ locals }) => { | ||
const session = await locals.getSession(); | ||
|
||
if (!isInRole({ userRoles: session.user?.permissions, requiredRoles: ['admin'] })) { | ||
throw error(401, 'Required Administrative Privileges'); | ||
} | ||
|
||
return {}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 10 additions & 1 deletion
11
src/routes/(admin)/admin/events/[id]/orders/+page.server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters