Skip to content

Commit

Permalink
add annelis export
Browse files Browse the repository at this point in the history
  • Loading branch information
folland87 committed Sep 26, 2024
1 parent 697cade commit 0854257
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions src/api/commons/middlewares/rbac.middlewares.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
import { ForbiddenError, UnauthorizedError } from '../http-errors';
import { ForbiddenError, UnauthorizedError } from "../http-errors";

export function requireAuth(req, res, next) {
if (['development', 'testing'].includes(process.env.NODE_ENV)) return next();
if (['/signup', '/signin', '/token', '/recovery/password'].includes(req.path)) return next();
if (req.path.startsWith('/opendata')) return next();
if (req.path.startsWith('/curiexplore')) return next();
if (req.path.startsWith('/assets/avatars')) return next();
if (req.path.startsWith('/assets/logos')) return next();
if (["development", "testing"].includes(process.env.NODE_ENV)) return next();
if (["/signup", "/signin", "/token", "/recovery/password"].includes(req.path))
return next();
if (req.path.startsWith("/opendata")) return next();
if (req.path.startsWith("/exports/annelis")) return next();
if (req.path.startsWith("/curiexplore")) return next();
if (req.path.startsWith("/assets/avatars")) return next();
if (req.path.startsWith("/assets/logos")) return next();
if (!req?.currentUser?.id) {
throw new UnauthorizedError('You must be connected');
throw new UnauthorizedError("You must be connected");
}
if (req.currentUser.isDeleted) {
throw new ForbiddenError('Inactive user');
throw new ForbiddenError("Inactive user");
}
if ((req.method !== 'GET') && (req.currentUser.role === 'viewer')) {
throw new ForbiddenError('Insufficient user rights');
if (req.method !== "GET" && req.currentUser.role === "viewer") {
throw new ForbiddenError("Insufficient user rights");
}
return next();
}

export function requireRoles(roles) {
return (req, res, next) => {
if (['development', 'testing'].includes(process.env.NODE_ENV)) return next();
if (["development", "testing"].includes(process.env.NODE_ENV))
return next();
if (!req.currentUser.id) {
throw new UnauthorizedError('You must be connected');
throw new UnauthorizedError("You must be connected");
}
if (!roles.includes(req.currentUser.role)) {
throw new ForbiddenError('Insufficient user rights');
throw new ForbiddenError("Insufficient user rights");
}
return next();
};
}

export function forbidReadersToWrite(req, res, next) {
if (['development', 'testing'].includes(process.env.NODE_ENV)) return next();
if ([
'/signup',
'/signin',
'/token',
'/recovery/password',
'/me',
'/me/password',
'/me/avatar',
].includes(req.path)) return next();
if (req.currentUser.role === 'reader' && req.method !== 'GET') {
throw new ForbiddenError('Insufficient user rights');
if (["development", "testing"].includes(process.env.NODE_ENV)) return next();
if (
[
"/signup",
"/signin",
"/token",
"/recovery/password",
"/me",
"/me/password",
"/me/avatar",
].includes(req.path)
)
return next();
if (req.currentUser.role === "reader" && req.method !== "GET") {
throw new ForbiddenError("Insufficient user rights");
}
return next();
}

0 comments on commit 0854257

Please sign in to comment.