Skip to content

Commit 73abfd8

Browse files
committed
fix: access vun
1 parent 4fd3d8a commit 73abfd8

File tree

4 files changed

+86
-7
lines changed

4 files changed

+86
-7
lines changed

apps/api/src/controllers/auth.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,32 @@ export function authRoutes(fastify: FastifyInstance) {
704704
async (request: FastifyRequest, reply: FastifyReply) => {
705705
const { id } = request.params as { id: string };
706706

707+
// Check if user exists
708+
const userToDelete = await prisma.user.findUnique({
709+
where: { id },
710+
});
711+
712+
if (!userToDelete) {
713+
return reply.code(404).send({
714+
message: "User not found",
715+
success: false,
716+
});
717+
}
718+
719+
// Prevent deletion of admin accounts if they're the last admin
720+
if (userToDelete.isAdmin) {
721+
const adminCount = await prisma.user.count({
722+
where: { isAdmin: true },
723+
});
724+
725+
if (adminCount <= 1) {
726+
return reply.code(400).send({
727+
message: "Cannot delete the last admin account",
728+
success: false,
729+
});
730+
}
731+
}
732+
707733
await prisma.notes.deleteMany({ where: { userId: id } });
708734
await prisma.session.deleteMany({ where: { userId: id } });
709735
await prisma.notifications.deleteMany({ where: { userId: id } });
@@ -726,6 +752,8 @@ export function authRoutes(fastify: FastifyInstance) {
726752
},
727753
});
728754

755+
await checkSession(request);
756+
729757
let user = await prisma.user.findUnique({
730758
where: { id: session!.userId },
731759
});

apps/api/src/controllers/clients.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
22
import { track } from "../lib/hog";
3+
import { requirePermission } from "../lib/roles";
34
import { prisma } from "../prisma";
45

56
export function clientRoutes(fastify: FastifyInstance) {
67
// Register a new client
78
fastify.post(
89
"/api/v1/client/create",
9-
10+
{
11+
preHandler: requirePermission(["client::create"]),
12+
},
1013
async (request: FastifyRequest, reply: FastifyReply) => {
1114
const { name, email, number, contactName }: any = request.body;
1215

@@ -35,7 +38,9 @@ export function clientRoutes(fastify: FastifyInstance) {
3538
// Update client
3639
fastify.post(
3740
"/api/v1/client/update",
38-
41+
{
42+
preHandler: requirePermission(["client::update"]),
43+
},
3944
async (request: FastifyRequest, reply: FastifyReply) => {
4045
const { name, email, number, contactName, id }: any = request.body;
4146

@@ -58,7 +63,9 @@ export function clientRoutes(fastify: FastifyInstance) {
5863
// Get all clients
5964
fastify.get(
6065
"/api/v1/clients/all",
61-
66+
{
67+
preHandler: requirePermission(["client::read"]),
68+
},
6269
async (request: FastifyRequest, reply: FastifyReply) => {
6370
const clients = await prisma.client.findMany({});
6471

@@ -72,7 +79,9 @@ export function clientRoutes(fastify: FastifyInstance) {
7279
// Delete client
7380
fastify.delete(
7481
"/api/v1/clients/:id/delete-client",
75-
82+
{
83+
preHandler: requirePermission(["client::delete"]),
84+
},
7685
async (request: FastifyRequest, reply: FastifyReply) => {
7786
const { id }: any = request.params;
7887

apps/api/src/controllers/config.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const nodemailer = require("nodemailer");
1010

1111
import { track } from "../lib/hog";
1212
import { createTransportProvider } from "../lib/nodemailer/transport";
13+
import { requirePermission } from "../lib/roles";
14+
import { checkSession } from "../lib/session";
1315
import { prisma } from "../prisma";
1416

1517
async function tracking(event: string, properties: any) {
@@ -388,9 +390,20 @@ export function configRoutes(fastify: FastifyInstance) {
388390
// Toggle all roles
389391
fastify.patch(
390392
"/api/v1/config/toggle-roles",
391-
393+
{
394+
preHandler: requirePermission(["settings::manage"]),
395+
},
392396
async (request: FastifyRequest, reply: FastifyReply) => {
393397
const { isActive }: any = request.body;
398+
const session = await checkSession(request);
399+
400+
// Double-check that user is admin
401+
if (!session?.isAdmin) {
402+
return reply.code(403).send({
403+
message: "Unauthorized. Admin access required.",
404+
success: false,
405+
});
406+
}
394407

395408
const config = await prisma.config.findFirst();
396409

apps/api/src/controllers/users.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import bcrypt from "bcrypt";
22
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
33

44
import { track } from "../lib/hog";
5+
import { requirePermission } from "../lib/roles";
56
import { checkSession } from "../lib/session";
67
import { prisma } from "../prisma";
78

89
export function userRoutes(fastify: FastifyInstance) {
910
// All users
1011
fastify.get(
1112
"/api/v1/users/all",
12-
13+
{
14+
preHandler: requirePermission(["user::read"]),
15+
},
1316
async (request: FastifyRequest, reply: FastifyReply) => {
1417
const users = await prisma.user.findMany({
1518
where: {
@@ -102,9 +105,35 @@ export function userRoutes(fastify: FastifyInstance) {
102105
// Mark Notification as read
103106
fastify.get(
104107
"/api/v1/user/notifcation/:id",
105-
106108
async (request: FastifyRequest, reply: FastifyReply) => {
107109
const { id }: any = request.params;
110+
const session = await checkSession(request);
111+
112+
if (!session) {
113+
return reply.code(401).send({
114+
message: "Unauthorized",
115+
success: false,
116+
});
117+
}
118+
119+
// Get the notification and verify it belongs to the user
120+
const notification = await prisma.notifications.findUnique({
121+
where: { id: id }
122+
});
123+
124+
if (!notification) {
125+
return reply.code(404).send({
126+
message: "Notification not found",
127+
success: false,
128+
});
129+
}
130+
131+
if (notification.userId !== session.id) {
132+
return reply.code(403).send({
133+
message: "Access denied. You can only manage your own notifications.",
134+
success: false,
135+
});
136+
}
108137

109138
await prisma.notifications.update({
110139
where: { id: id },

0 commit comments

Comments
 (0)