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
|
|
|
|
2024-01-12 07:43:58 +00:00
|
|
|
export const router = express.Router();
|
2024-01-08 02:29:56 +00:00
|
|
|
|
2024-01-11 05:31:13 +00:00
|
|
|
// Controllers
|
2024-01-12 01:51:26 +00:00
|
|
|
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";
|
2024-03-19 22:20:26 +00:00
|
|
|
import { Apps, appSearch, InstallModal, ImportModal, LearnMore } from "../controllers/apps.js";
|
2024-01-08 02:29:56 +00:00
|
|
|
import { Users } from "../controllers/users.js";
|
2024-02-07 09:42:20 +00:00
|
|
|
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";
|
2024-02-08 02:13:44 +00:00
|
|
|
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";
|
2024-01-11 05:31:13 +00:00
|
|
|
import { Syslogs } from "../controllers/syslogs.js";
|
|
|
|
import { Portal } from "../controllers/portal.js"
|
2024-01-08 02:29:56 +00:00
|
|
|
|
2024-01-11 05:31:13 +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;
|
2024-04-01 22:08:02 +00:00
|
|
|
let path = req.path;
|
2024-03-27 03:38:57 +00:00
|
|
|
let trigger = req.header('hx-trigger-name');
|
2024-04-01 22:08:02 +00:00
|
|
|
|
|
|
|
console.log("Auth: ", user, role, path, trigger);
|
|
|
|
console.log(req.path);
|
|
|
|
|
|
|
|
if (!user) { res.redirect('/login'); return; }
|
|
|
|
else if (role == "admin") { next(); return;}
|
|
|
|
else if (path == "/portal" || path == "/account" || path == "/supporters" || path == "/thank") { next(); return; }
|
|
|
|
else { res.redirect('/portal'); return; }
|
2024-03-31 21:37:00 +00:00
|
|
|
|
2024-04-01 22:08:02 +00:00
|
|
|
|
|
|
|
// let action = req.path.split("/")[2];
|
|
|
|
|
|
|
|
// else if (trigger == "portal" || "supporters" || "account" || "thank") { res.redirect() return; }
|
2024-03-27 03:38:57 +00:00
|
|
|
|
2024-03-31 21:37:00 +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
|
|
|
|
2024-03-31 21:37:00 +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
|
|
|
|
2024-03-31 21:37:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-02-19 02:23:20 +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
|
|
|
|
2024-03-31 21:37:00 +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);
|
2024-03-31 21:37:00 +00:00
|
|
|
router.get("/containers", auth, Containers);
|
|
|
|
router.get("/card", auth, Card);
|
|
|
|
router.get("/new_cards", auth, updateCards);
|
2024-03-27 03:38:57 +00:00
|
|
|
|
|
|
|
|
2024-01-12 01:51:26 +00:00
|
|
|
router.get("/images", auth, Images);
|
2024-02-19 02:23:20 +00:00
|
|
|
router.post("/removeImage", auth, removeImage);
|
2024-02-06 08:35:16 +00:00
|
|
|
|
2024-01-12 01:51:26 +00:00
|
|
|
router.get("/volumes", auth, Volumes);
|
2024-02-17 08:37:07 +00:00
|
|
|
router.post("/removeVolume", auth, removeVolume);
|
2024-02-06 08:35:16 +00:00
|
|
|
|
2024-01-12 01:51:26 +00:00
|
|
|
router.get("/networks", auth, Networks);
|
2024-02-19 02:23:20 +00:00
|
|
|
router.post("/removeNetwork", auth, removeNetwork);
|
2024-01-08 02:29:56 +00:00
|
|
|
|
|
|
|
router.get("/apps", auth, Apps);
|
|
|
|
router.get("/apps/:page", auth, Apps);
|
2024-01-12 01:51:26 +00:00
|
|
|
router.post("/apps", auth, appSearch);
|
2024-03-17 08:00:11 +00:00
|
|
|
router.get("/install_modal", auth, InstallModal)
|
2024-03-19 22:20:26 +00:00
|
|
|
router.get("/import_modal", auth, ImportModal)
|
2024-03-17 08:00:11 +00:00
|
|
|
router.get("/learn_more", auth, LearnMore)
|
2024-01-08 02:29:56 +00:00
|
|
|
|
|
|
|
router.get("/users", auth, Users);
|
|
|
|
router.get("/syslogs", auth, Syslogs);
|
|
|
|
|
2024-02-08 02:13:44 +00:00
|
|
|
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
|
|
|
|
2024-02-19 02:23:20 +00:00
|
|
|
// User routes
|
2024-04-01 22:08:02 +00:00
|
|
|
router.get("/portal", auth, Portal);
|
|
|
|
router.get("/account", auth, Account);
|
|
|
|
router.get("/supporters", auth, Supporters);
|
|
|
|
router.post("/thank", auth, Thanks);
|
2024-01-11 05:31:13 +00:00
|
|
|
|
2024-02-19 02:23:20 +00:00
|
|
|
router.get("/login", Login);
|
|
|
|
router.post("/login", submitLogin);
|
|
|
|
router.get("/register", Register);
|
|
|
|
router.post("/register", submitRegister);
|
|
|
|
router.get("/logout", Logout);
|
|
|
|
|
2024-02-07 09:42:20 +00:00
|
|
|
|
2024-01-12 01:51:26 +00:00
|
|
|
// Functions
|
2024-03-17 09:20:32 +00:00
|
|
|
import { Install } from "../functions/install.js"
|
2024-02-07 09:42:20 +00:00
|
|
|
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
|
|
|
|