Add access for endpoints that can be accessed by any user

This commit is contained in:
AbegaM
2024-03-13 17:46:10 +03:00
parent c5f9660542
commit 1a9c1bf898
3 changed files with 16 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
module.exports = { module.exports = {
defaultRoutes: ['_users', '_roles', '_roles_permissions', '_users_roles'], defaultRoutes: ['_users', '_roles', '_roles_permissions', '_users_roles'],
baseTableUrl: '/api/tables', baseTableUrl: '/api/tables',
universalAccessEndpoints: ['/api/auth/change-password'],
fields: { fields: {
_users: { _users: {
SALT: 'salt', SALT: 'salt',
@@ -14,4 +15,11 @@ module.exports = {
TOO_WEAK: 'Too weak', TOO_WEAK: 'Too weak',
WEAK: 'Weak', WEAK: 'Weak',
}, },
httpVerbs: {
POST: 'CREATE',
GET: 'READ',
PUT: 'UPDATE',
DELETE: 'DELETE',
},
}; };

View File

@@ -1,6 +0,0 @@
module.exports = {
POST: 'CREATE',
GET: 'READ',
PUT: 'UPDATE',
DELETE: 'DELETE',
};

View File

@@ -1,11 +1,12 @@
const config = require('../config'); const config = require('../config');
const { decodeToken, toBoolean } = require('../utils/index'); const { decodeToken, toBoolean } = require('../utils/index');
const httpVerbs = require('../constants/httpVerbs'); const { apiConstants } = require('../constants');
const isAuthenticated = async (req, res, next) => { const isAuthenticated = async (req, res, next) => {
let payload; let payload;
const { name: tableName } = req.params; const { name: tableName } = req.params;
const verb = req.method; const verb = req.method;
const originalURL = req.originalUrl;
try { try {
if (config.auth) { if (config.auth) {
@@ -25,6 +26,11 @@ const isAuthenticated = async (req, res, next) => {
return next(); return next();
} }
// if the endpoint is set to be accessed by any user regardless of there roles, then allow access
if (apiConstants.universalAccessEndpoints.includes(originalURL)) {
return next();
}
// if table_name is not passed from the router throw unauthorized error // if table_name is not passed from the router throw unauthorized error
if (!tableName) { if (!tableName) {
return res.status(403).send({ message: 'Not authorized' }); return res.status(403).send({ message: 'Not authorized' });
@@ -45,7 +51,7 @@ const isAuthenticated = async (req, res, next) => {
let hasPermission = false; let hasPermission = false;
permissions.some((resource) => { permissions.some((resource) => {
const httpMethod = httpVerbs[verb].toLowerCase(); const httpMethod = apiConstants.httpVerbs[verb].toLowerCase();
if (toBoolean(resource[httpMethod])) { if (toBoolean(resource[httpMethod])) {
hasPermission = true; hasPermission = true;