-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |