Move UI configs to DB

This commit is contained in:
Shweelan Samson authored and Zaid Abdulla committed 2019-01-16 05:22:14 -08:00
1 parent 192ed6b034
commit 1cceba3f8b
22 files changed
+337 -448

No files matched your search

+32 -17
View File
@@ -8,21 +8,24 @@ const koaBodyParser = require("koa-bodyparser");
const koaJwt = require("koa-jwt");
const jwt = require("jsonwebtoken");
const config = require("./ui/config");
const config = require("./config");
const api = require("./ui/api");
const Authorizer = require("./common/authorizer");
const expression = require("./common/expression");
const logger = require("./logger");
const localCache = require("./local-cache");
const koa = new Koa();
const router = new Router();
const JWT_SECRET = config.server.jwtSecret;
const JWT_SECRET = config.get("UI_JWT_SECRET");
const JWT_COOKIE = "genieacs-ui-jwt";
function getPermissionSets(roles) {
const allPermissions = config.permissions;
const permissionSets = roles.map(role => {
function getPermissionSets(ctx) {
const allPermissions = localCache.getPermissionsConfig(
ctx.state.configSnapshot
);
const permissionSets = ctx.state.user.roles.map(role => {
return Object.values(allPermissions[role] || {}).map(p => {
p = Object.assign({}, p);
for (let [k, v] of Object.entries(p)) {
@@ -47,8 +50,9 @@ function getPermissionSets(roles) {
return permissionSets;
}
function authSimple(username, password) {
const user = config.auth.simple.users[username];
function authSimple(ctx, username, password) {
const authConfig = localCache.getAuthConfig(ctx.state.configSnapshot);
const user = authConfig.simple.users[username];
if (user && user.password === password) {
const roles = user.roles.split(",").map(s => s.trim());
return Promise.resolve(roles);
@@ -60,6 +64,12 @@ koa.on("error", async err => {
throw err;
});
koa.use(async (ctx, next) => {
const configSnapshot = await localCache.getCurrentSnapshot();
ctx.state.configSnapshot = configSnapshot;
return next();
});
koa.use(
koaJwt({
secret: JWT_SECRET,
@@ -69,18 +79,21 @@ koa.use(
);
koa.use(async (ctx, next) => {
if (ctx.state.user && ctx.state.user.roles) {
ctx.state.authorizer = new Authorizer(
getPermissionSets(ctx.state.user.roles)
);
} else {
ctx.state.authorizer = new Authorizer([]);
}
if (ctx.state.user && ctx.state.user.roles)
ctx.state.authorizer = new Authorizer(getPermissionSets(ctx));
else ctx.state.authorizer = new Authorizer([]);
return next();
});
router.post("/login", async ctx => {
if (!JWT_SECRET) {
ctx.status = 500;
ctx.body = "UI_JWT_SECRET is not set";
logger.error({ message: "UI_JWT_SECRET is not set" });
return;
}
const username = ctx.request.body.username;
const password = ctx.request.body.password;
@@ -105,7 +118,7 @@ router.post("/login", async ctx => {
logger.accessWarn(log);
}
const roles = await authSimple(username, password);
const roles = await authSimple(ctx, username, password);
if (roles) return void success(roles, "simple");
@@ -135,7 +148,7 @@ router.use("/api", api.routes(), api.allowedMethods());
router.get("/", async ctx => {
let permissionSets = [];
if (ctx.state.user && ctx.state.user.roles)
permissionSets = getPermissionSets(ctx.state.user.roles);
permissionSets = getPermissionSets(ctx);
ctx.body = `
<html>
@@ -146,7 +159,9 @@ router.get("/", async ctx => {
</head>
<body>
<script>
window.clientConfig = ${JSON.stringify(config.getClientConfig())};
window.clientConfig = ${JSON.stringify({
ui: localCache.getUiConfig(ctx.state.configSnapshot)
})};
window.username = ${JSON.stringify(
ctx.state.user ? ctx.state.user.username : ""
)};