Files
SnapOtter/apps/api/src/permissions.ts
T
Siddharth Kumar Sah 85b1cfc10a chore: rename Stirling-Image to ashim across entire codebase
Complete rebrand from Stirling-Image to ashim following the project
move to https://github.com/ashim-hq/ashim.

Changes across 117 files:
- Package scope: @stirling-image/* → @ashim/*
- GitHub URLs: stirling-image/stirling-image → ashim-hq/ashim
- Docker Hub: stirlingimage/stirling-image → ashimhq/ashim
- GitHub Pages: stirling-image.github.io → ashim-hq.github.io
- All branding text: "Stirling Image" → "ashim"
- Docker service/volumes/user: stirling → ashim
- Database: stirling.db → ashim.db
- localStorage keys: stirling-token → ashim-token
- Environment variables: STIRLING_GPU → ASHIM_GPU
- Python cache dirs: .cache/stirling-image → .cache/ashim
- SVG filter IDs, test prefixes, and all other references
2026-04-14 20:55:42 +08:00

45 lines
1.3 KiB
TypeScript

import type { Permission, Role } from "@ashim/shared";
import type { FastifyReply, FastifyRequest } from "fastify";
import { getAuthUser } from "./plugins/auth.js";
const ROLE_PERMISSIONS: Record<Role, Permission[]> = {
admin: [
"tools:use",
"files:own",
"files:all",
"apikeys:own",
"apikeys:all",
"pipelines:own",
"pipelines:all",
"settings:read",
"settings:write",
"users:manage",
"teams:manage",
"branding:manage",
],
user: ["tools:use", "files:own", "apikeys:own", "pipelines:own", "settings:read"],
};
export function getPermissions(role: Role): Permission[] {
return ROLE_PERMISSIONS[role] ?? [];
}
export function hasPermission(role: Role, permission: Permission): boolean {
return getPermissions(role).includes(permission);
}
export function requirePermission(permission: Permission) {
return (request: FastifyRequest, reply: FastifyReply) => {
const user = getAuthUser(request);
if (!user) {
reply.status(401).send({ error: "Authentication required", code: "AUTH_REQUIRED" });
return null;
}
if (!hasPermission(user.role as Role, permission)) {
reply.status(403).send({ error: "Insufficient permissions", code: "FORBIDDEN" });
return null;
}
return user;
};
}