DweebUI/router/index.js

122 lines
4.1 KiB
JavaScript
Raw Normal View History

2024-01-08 02:29:56 +00:00
import express from "express";
2024-03-27 03:38:57 +00:00
import { Permission } from '../database/models.js';
2024-03-24 21:09:53 +00:00
export const router = express.Router();
2024-01-08 02:29:56 +00:00
// Controllers
import { Login, submitLogin, Logout } from "../controllers/login.js";
2024-01-08 02:29:56 +00:00
import { Register, submitRegister } from "../controllers/register.js";
2024-03-24 21:09:53 +00:00
import { Dashboard, Logs, Modals, Stats, Chart, SSE, Card, updateCards, Containers, Action, UpdatePermissions } from "../controllers/dashboard.js";
import { Apps, appSearch, InstallModal, ImportModal, LearnMore, Upload } from "../controllers/apps.js";
2024-01-08 02:29:56 +00:00
import { Users } from "../controllers/users.js";
import { Images, removeImage } from "../controllers/images.js";
import { Networks, removeNetwork } from "../controllers/networks.js";
import { Volumes, removeVolume } from "../controllers/volumes.js";
2024-01-08 02:29:56 +00:00
import { Account } from "../controllers/account.js";
import { Variables } from "../controllers/variables.js";
2024-01-08 02:29:56 +00:00
import { Settings } from "../controllers/settings.js";
2024-02-14 09:45:29 +00:00
import { Supporters, Thanks } from "../controllers/supporters.js";
import { Syslogs } from "../controllers/syslogs.js";
2024-04-07 22:57:17 +00:00
import { Portal, UserContainers } from "../controllers/portal.js"
2024-01-08 02:29:56 +00:00
// Auth middleware
2024-03-27 03:38:57 +00:00
const auth = async (req, res, next) => {
let user = req.session.user;
let role = req.session.role;
let path = req.path;
2024-03-27 03:38:57 +00:00
let trigger = req.header('hx-trigger-name');
2024-04-07 22:57:17 +00:00
// console.log("Auth: ", user, role, path, trigger, req.path);
if (!user) { res.redirect('/login'); return; }
else if (role == 'admin' || path == "/portal" || path == "/account" || path == "/supporters" || path == "/thank" || path == "/user_containers") { next(); return; }
// else { res.redirect('/portal'); return; }
let action = req.path.split("/")[2];
2024-03-27 03:38:57 +00:00
if (action == "start" || action == "stop" || action == "pause" || action == "restart") {
let permission = await Permission.findOne({ where: { containerName: trigger, user: user }, attributes: [`${action}`] });
2024-03-27 03:38:57 +00:00
if (permission) {
if (permission[action] == true) {
console.log(`User ${user} has permission to ${action} ${trigger}`);
next();
}
else {
console.log(`User ${user} does not have permission to ${action} ${trigger}`);
}
} else {
console.log(`No entry found for ${user} in ${trigger} permissions`);
}
}
else {
res.redirect('/portal');
}
2024-03-27 03:38:57 +00:00
}
2024-01-08 02:29:56 +00:00
// Admin routes
2024-01-08 02:29:56 +00:00
router.get("/", auth, Dashboard);
2024-03-17 19:10:16 +00:00
router.post("/action/:action", auth, Action);
2024-03-24 21:09:53 +00:00
router.post("/updatePermissions", auth, UpdatePermissions);
2024-02-14 09:45:29 +00:00
router.get("/logs", auth, Logs);
router.get("/modals", auth, Modals);
2024-03-27 06:14:39 +00:00
router.get("/stats", auth, Stats);
router.get("/chart", auth, Chart);
router.get("/sse_event", auth, SSE);
router.get("/containers", auth, Containers);
router.get("/card", auth, Card);
router.get("/new_cards", auth, updateCards);
2024-03-27 03:38:57 +00:00
router.get("/images", auth, Images);
router.post("/removeImage", auth, removeImage);
router.get("/volumes", auth, Volumes);
router.post("/removeVolume", auth, removeVolume);
router.get("/networks", auth, Networks);
router.post("/removeNetwork", auth, removeNetwork);
2024-01-08 02:29:56 +00:00
router.get("/apps", auth, Apps);
router.get("/apps/:page", auth, Apps);
router.post("/apps", auth, appSearch);
2024-03-17 08:00:11 +00:00
router.get("/install_modal", auth, InstallModal)
router.get("/import_modal", auth, ImportModal)
2024-03-17 08:00:11 +00:00
router.get("/learn_more", auth, LearnMore)
router.post("/upload", auth, Upload);
2024-01-08 02:29:56 +00:00
router.get("/users", auth, Users);
router.get("/syslogs", auth, Syslogs);
router.get("/variables", auth, Variables);
2024-01-08 02:29:56 +00:00
router.get("/settings", auth, Settings);
2024-02-14 09:45:29 +00:00
// User routes
router.get("/portal", auth, Portal);
2024-04-07 22:57:17 +00:00
router.get("/user_containers", auth, UserContainers);
router.get("/account", auth, Account);
router.get("/supporters", auth, Supporters);
router.post("/thank", auth, Thanks);
router.get("/login", Login);
router.post("/login", submitLogin);
router.get("/register", Register);
router.post("/register", submitRegister);
router.get("/logout", Logout);
// Functions
import { Install } from "../functions/install.js"
import { Uninstall } from "../functions/uninstall.js"
2024-03-27 03:38:57 +00:00
router.post("/install", Install);
router.post("/uninstall", Uninstall);
2024-03-24 21:09:53 +00:00