Tom Butcher bce5afad7e Implement caching and permission checks for user permissions
This commit introduces a caching mechanism for user permissions using Redis, enhancing performance by reducing database queries. The `permissions.js` file has been updated to include functions for saving and retrieving user permissions from Redis, as well as middleware for checking permissions in various routes. Additionally, tests have been added to ensure the correctness of the permission logic and caching behavior, improving the overall security and efficiency of the application.
2026-08-20 19:35:15 +01:00

94 lines
2.9 KiB
JavaScript

import express from 'express';
import { isAuthenticated } from '../../keycloak.js';
import { checkPermissions } from '../../database/permissions.js';
import { getFilter, convertPropertiesString, getSort } from '../../utils.js';
const router = express.Router();
const listAllowedFilters = [
'name',
'_id',
'state',
'tags',
'connectedAt',
'createdAt',
'updatedAt',
'_reference',
];
const listAllowedSorters = ['name', 'state', 'connectedAt', 'createdAt', 'updatedAt'];
const propertiesAllowedFilters = ['tags'];
import {
listHostsRouteHandler,
getHostRouteHandler,
editHostRouteHandler,
newHostRouteHandler,
deleteHostRouteHandler,
listHostsByPropertiesRouteHandler,
getHostStatsRouteHandler,
getHostHistoryRouteHandler,
searchHostsRouteHandler,
getHostPropertyValuesRouteHandler,
getHostNeighborsRouteHandler,
} from '../../services/management/hosts.js';
// list of hosts
router.get('/', isAuthenticated, async (req, res) => {
const { page, limit, property, search, sort, order } = req.query;
const filter = await getFilter(req.query, listAllowedFilters);
listHostsRouteHandler(req, res, page, limit, property, filter, search, getSort(sort, listAllowedSorters), order);
});
router.get('/properties', isAuthenticated, async (req, res) => {
let properties = convertPropertiesString(req.query.properties);
const filter = await getFilter(req.query, propertiesAllowedFilters, false);
var masterFilter = {};
if (req.query.masterFilter) {
masterFilter = JSON.parse(req.query.masterFilter);
}
listHostsByPropertiesRouteHandler(req, res, properties, filter, masterFilter);
});
router.get('/values', isAuthenticated, async (req, res) => {
const { property } = req.query;
getHostPropertyValuesRouteHandler(req, res, property);
});
router.get('/search', isAuthenticated, async (req, res) => {
const { search } = req.query;
searchHostsRouteHandler(req, res, search);
});
router.post('/', isAuthenticated, checkPermissions('host', 'new'), async (req, res) => {
newHostRouteHandler(req, res);
});
// get host stats
router.get('/stats', isAuthenticated, async (req, res) => {
getHostStatsRouteHandler(req, res);
});
// get hosts history
router.get('/history', isAuthenticated, async (req, res) => {
getHostHistoryRouteHandler(req, res);
});
router.get('/neighbors', isAuthenticated, async (req, res) => {
const { property, search, sort, order, id } = req.query;
const filter = await getFilter(req.query, listAllowedFilters);
getHostNeighborsRouteHandler(req, res, property, filter, search, getSort(sort, listAllowedSorters), order, id);
});
router.get('/:id', isAuthenticated, async (req, res) => {
getHostRouteHandler(req, res);
});
router.put('/:id', isAuthenticated, checkPermissions('host', 'edit'), async (req, res) => {
editHostRouteHandler(req, res);
});
router.delete('/:id', isAuthenticated, async (req, res) => {
deleteHostRouteHandler(req, res);
});
export default router;