Skip to content

Commit

Permalink
feat: use meta operation access
Browse files Browse the repository at this point in the history
  • Loading branch information
7sete7 committed Nov 29, 2024
1 parent efc0101 commit 5a9fd10
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/imports/model/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export const UserModel = z.object({
readAccess: z.boolean().or(z.string()).optional(),
updateAccess: z.boolean().or(z.string()).optional(),
createAccess: z.boolean().optional(),

updateDocument: z.boolean().or(z.string()).optional(),
deleteDocument: z.boolean().or(z.string()).optional(),
})
.optional(),
})
Expand Down
21 changes: 15 additions & 6 deletions src/server/routes/api/document/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import fp from 'fastify-plugin';

import { getUserFromRequest } from '@imports/auth/getUser';
import { getDocument } from '@imports/document';
import { logger } from '@imports/utils/logger';
import { WithoutId } from 'mongodb';
import { loadMetaObjects } from '@imports/meta/loadMetaObjects';
import { MetaObject } from '@imports/model/MetaObject';
import { MetaObjectSchema, MetaObjectType } from '@imports/types/metadata';
import { checkMetaOperation } from '@imports/utils/accessUtils';
import { logger } from '@imports/utils/logger';
import { WithoutId } from 'mongodb';

const documentAPi: FastifyPluginCallback = async fastify => {
fastify.get<{ Params: { name: string } }>('/api/document/:name', async (req, reply) => {
Expand Down Expand Up @@ -58,11 +59,15 @@ const documentAPi: FastifyPluginCallback = async fastify => {

try {
const user = await getUserFromRequest(req);

if (user == null || user.admin !== true) {
if (user == null) {
return reply.status(401).send('Unauthorized');
}

const metaOperationAccess = checkMetaOperation({ user, operation: 'updateDocument', document: id });
if (metaOperationAccess === false) {
return reply.status(403).send('Forbidden');
}

const document = req.body as MetaObjectType;

if (document == null) {
Expand Down Expand Up @@ -112,11 +117,15 @@ const documentAPi: FastifyPluginCallback = async fastify => {

try {
const user = await getUserFromRequest(req);

if (user == null || user.admin !== true) {
if (user == null) {
return reply.status(401).send('Unauthorized');
}

const metaOperationAccess = checkMetaOperation({ user, operation: 'deleteDocument', document: id });
if (metaOperationAccess === false) {
return reply.status(403).send('Forbidden');
}

const result = await MetaObject.MetaObject.deleteOne({ _id: id });

if (result.deletedCount === 0) {
Expand Down

0 comments on commit 5a9fd10

Please sign in to comment.