No description
- TypeScript 50.8%
- Svelte 48.3%
- JavaScript 0.6%
- HTML 0.3%
| assets | ||
| e2e | ||
| src | ||
| static | ||
| .env.example | ||
| .gitignore | ||
| .npmrc | ||
| .prettierignore | ||
| .prettierrc | ||
| drizzle.config.ts | ||
| local.db.bak | ||
| package-lock.json | ||
| package.json | ||
| playwright.config.ts | ||
| README.md | ||
| sample_data.sql | ||
| svelte.config.js | ||
| tsconfig.json | ||
| vite.config.ts | ||
| vitest-setup-client.ts | ||
Lab 8: Implement Access Control
Enda Lee 2025
Getting Started
- Clone or download this repository.
- Open the application folder in VS Code.
- Create a
.envfile in the application root using .env.example as a template. - Copy
local.db.baktolocal.db - In the terminal, run
npm installto install dependencies. - Start the development server with npm run dev.
Overview
The applicaion is the completed Locations example from Week 5, with some changes:
-
Updated database, see the Drizzle schema and Zod validation-
src/lib/server/db/. -
The
usertable includes the following user accounts:username password role admin Password admin alice Password user bob Password user test Password user -
Each user has their own set of locations, some of which are shared. The admin user can access everything. The following access control matrix defines access rules for each role.
Action User Manager Admin Authentication Login/Register ✅ ✅ ✅ View own profile ✅ ✅ ✅ Locations View own locations ✅ ✅ ✅ View shared locations ✅ ✅ ✅ View all locations ❌ ✅ ✅ Create own locations ✅ ✅ ✅ Edit own locations ✅ ✅ ✅ Edit any location ❌ ✅ ✅ Delete own locations ✅ ✅ ✅ Delete any location ❌ ✅ ✅ Categories View categories ✅ ✅ ✅ Manage categories ❌ ✅ ✅ User Management View user list ❌ ❌ ✅ Change user roles ❌ ❌ ✅ Delete users ❌ ❌ ✅
For full details and examples see the Lab 6 videos.
Exercise
- Implement access control control, i.e. authentication and authorisation for this application. The rules should be based on the matrix above. This should be based on the examples from lab 6
Do it in stages:
- Use
hooks.server.tsto check for a session cookie. - Use the shared Layout to check for
event.locals.userand pass it to the page (or redirect to login if required). - Group pages according to required access, e.g. public or private
- Authorisation: Check that the current user has permission before retrieving/ displaying data. This should be checked in the view (pages) but also at the service layer - so check user before getting data.
The result does not need to look identical to the screencast video example (e.g. you may implement grouping differently), but the same methods should be used to implement security at each level.
Note: Some of the required code and functionality is already included in this example, some will need to be copied from the solution. It is up to you to figure this out and 'wire it up' correctly.
- Verify that the access rules implement the rules defined in the matrix.
Enda Lee 2025