Fix users' roles bug

Changed users roles did not take effect as long as the user is logged
in.
This commit is contained in:
Shweelan Samson authored and Zaid Abdulla committed 2019-07-05 14:38:27 -07:00
1 parent 89d3d333d1
commit fada96bf47
1 file changed
+8 -6
+8 -6
View File
@@ -47,7 +47,9 @@ const JWT_COOKIE = "genieacs-ui-jwt";
function getPermissionSets(ctx): PermissionSet[] { function getPermissionSets(ctx): PermissionSet[] {
const allPermissions = localCache.getPermissions(ctx.state.configSnapshot); const allPermissions = localCache.getPermissions(ctx.state.configSnapshot);
const permissionSets = ctx.state.user.roles.map(role => const users = localCache.getUsers(ctx.state.configSnapshot);
const user = users[ctx.state.user.username];
const permissionSets = user.roles.map(role =>
Object.values(allPermissions[role] || {}) Object.values(allPermissions[role] || {})
); );
return permissionSets; return permissionSets;
@@ -73,7 +75,7 @@ koa.use(
); );
koa.use(async (ctx, next) => { koa.use(async (ctx, next) => {
if (ctx.state.user && ctx.state.user.roles) if (ctx.state.user && ctx.state.user.username)
ctx.state.authorizer = new Authorizer(getPermissionSets(ctx)); ctx.state.authorizer = new Authorizer(getPermissionSets(ctx));
else ctx.state.authorizer = new Authorizer([]); else ctx.state.authorizer = new Authorizer([]);
@@ -98,9 +100,9 @@ router.post("/login", async ctx => {
method: null method: null
}; };
function success(roles, method): void { function success(method): void {
log.method = method; log.method = method;
const token = jwt.sign({ username, roles }, JWT_SECRET); const token = jwt.sign({ username }, JWT_SECRET);
ctx.cookies.set(JWT_COOKIE, token, { sameSite: "lax" }); ctx.cookies.set(JWT_COOKIE, token, { sameSite: "lax" });
ctx.body = JSON.stringify(token); ctx.body = JSON.stringify(token);
logger.accessInfo(log); logger.accessInfo(log);
@@ -115,7 +117,7 @@ router.post("/login", async ctx => {
const roles = await authSimple(ctx.state.configSnapshot, username, password); const roles = await authSimple(ctx.state.configSnapshot, username, password);
if (roles) return void success(roles, "simple"); if (roles) return void success("simple");
failure(); failure();
}); });
@@ -142,7 +144,7 @@ router.use("/api", api.routes(), api.allowedMethods());
router.get("/", async ctx => { router.get("/", async ctx => {
let permissionSets = []; let permissionSets = [];
if (ctx.state.user && ctx.state.user.roles) if (ctx.state.user && ctx.state.user.username)
permissionSets = getPermissionSets(ctx); permissionSets = getPermissionSets(ctx);
ctx.body = ` ctx.body = `