Skip to content

Commit 28a15ab

Browse files
committed
feat: rbac
1 parent a567edb commit 28a15ab

File tree

15 files changed

+1125
-646
lines changed

15 files changed

+1125
-646
lines changed

apps/api/src/controllers/data.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
2+
import { requirePermission } from "../lib/roles";
23
import { prisma } from "../prisma";
34

45
export function dataRoutes(fastify: FastifyInstance) {
56
// Get total count of all tickets
67
fastify.get(
78
"/api/v1/data/tickets/all",
8-
9+
{
10+
preHandler: requirePermission(["issue::read"]),
11+
},
912
async (request: FastifyRequest, reply: FastifyReply) => {
1013
const result = await prisma.ticket.count({
1114
where: { hidden: false },
@@ -18,7 +21,9 @@ export function dataRoutes(fastify: FastifyInstance) {
1821
// Get total count of all completed tickets
1922
fastify.get(
2023
"/api/v1/data/tickets/completed",
21-
24+
{
25+
preHandler: requirePermission(["issue::read"]),
26+
},
2227
async (request: FastifyRequest, reply: FastifyReply) => {
2328
const result = await prisma.ticket.count({
2429
where: { isComplete: true, hidden: false },
@@ -31,7 +36,9 @@ export function dataRoutes(fastify: FastifyInstance) {
3136
// Get total count of all open tickets
3237
fastify.get(
3338
"/api/v1/data/tickets/open",
34-
39+
{
40+
preHandler: requirePermission(["issue::read"]),
41+
},
3542
async (request: FastifyRequest, reply: FastifyReply) => {
3643
const result = await prisma.ticket.count({
3744
where: { isComplete: false, hidden: false },
@@ -44,7 +51,9 @@ export function dataRoutes(fastify: FastifyInstance) {
4451
// Get total of all unsassigned tickets
4552
fastify.get(
4653
"/api/v1/data/tickets/unassigned",
47-
54+
{
55+
preHandler: requirePermission(["issue::read"]),
56+
},
4857
async (request: FastifyRequest, reply: FastifyReply) => {
4958
const result = await prisma.ticket.count({
5059
where: { userId: null, hidden: false, isComplete: false },

apps/api/src/controllers/notebook.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
22
import { track } from "../lib/hog";
3+
import { requirePermission } from "../lib/roles";
34
import { checkSession } from "../lib/session";
45
import { prisma } from "../prisma";
56

@@ -19,7 +20,9 @@ export function notebookRoutes(fastify: FastifyInstance) {
1920
// Create a new entry
2021
fastify.post(
2122
"/api/v1/notebook/note/create",
22-
23+
{
24+
preHandler: requirePermission(["document::create"]),
25+
},
2326
async (request: FastifyRequest, reply: FastifyReply) => {
2427
const { content, title }: any = request.body;
2528
const user = await checkSession(request);
@@ -43,7 +46,9 @@ export function notebookRoutes(fastify: FastifyInstance) {
4346
// Get all entries
4447
fastify.get(
4548
"/api/v1/notebooks/all",
46-
49+
{
50+
preHandler: requirePermission(["document::read"]),
51+
},
4752
async (request: FastifyRequest, reply: FastifyReply) => {
4853
const user = await checkSession(request);
4954

@@ -58,7 +63,9 @@ export function notebookRoutes(fastify: FastifyInstance) {
5863
// Get a single entry
5964
fastify.get(
6065
"/api/v1/notebooks/note/:id",
61-
66+
{
67+
preHandler: requirePermission(["document::read"]),
68+
},
6269
async (request: FastifyRequest, reply: FastifyReply) => {
6370
const user = await checkSession(request);
6471

@@ -75,14 +82,17 @@ export function notebookRoutes(fastify: FastifyInstance) {
7582
// Delete an entry
7683
fastify.delete(
7784
"/api/v1/notebooks/note/:id",
85+
{
86+
preHandler: requirePermission(["document::delete"]),
87+
},
7888
async (request: FastifyRequest, reply: FastifyReply) => {
7989
const user = await checkSession(request);
8090
const { id }: any = request.params;
8191

8292
await prisma.notes.delete({
83-
where: {
93+
where: {
8494
id: id,
85-
userId: user!.id
95+
userId: user!.id,
8696
},
8797
});
8898

@@ -95,15 +105,18 @@ export function notebookRoutes(fastify: FastifyInstance) {
95105
// Update an entry
96106
fastify.put(
97107
"/api/v1/notebooks/note/:id/update",
108+
{
109+
preHandler: requirePermission(["document::update"]),
110+
},
98111
async (request: FastifyRequest, reply: FastifyReply) => {
99112
const user = await checkSession(request);
100113
const { id }: any = request.params;
101114
const { content, title }: any = request.body;
102115

103116
await prisma.notes.update({
104-
where: {
117+
where: {
105118
id: id,
106-
userId: user!.id
119+
userId: user!.id,
107120
},
108121
data: {
109122
title: title,

apps/api/src/controllers/roles.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function roleRoutes(fastify: FastifyInstance) {
9898
},
9999
async (request: FastifyRequest, reply: FastifyReply) => {
100100
const { id }: any = request.params;
101-
const { name, description, permissions, isDefault }: any = request.body;
101+
const { name, description, permissions, isDefault, users }: any = request.body;
102102

103103
try {
104104
const updatedRole = await prisma.role.update({
@@ -109,6 +109,9 @@ export function roleRoutes(fastify: FastifyInstance) {
109109
permissions,
110110
isDefault,
111111
updatedAt: new Date(),
112+
users: {
113+
set: Array.isArray(users) ? users.map(userId => ({ id: userId })) : [{ id: users }], // Ensure users is an array of objects with unique IDs when updating
114+
},
112115
},
113116
});
114117

@@ -156,7 +159,7 @@ export function roleRoutes(fastify: FastifyInstance) {
156159
fastify.post(
157160
"/api/v1/role/assign",
158161
{
159-
// preHandler: requirePermission(['role::assign']),
162+
preHandler: requirePermission(['role::update']),
160163
},
161164
async (request: FastifyRequest, reply: FastifyReply) => {
162165
const { userId, roleId }: any = request.body;

0 commit comments

Comments
 (0)