Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/client/permission-resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ import { getGroupConfigs } from '../services/group-config';
import { permissionCache } from '../helpers/cache';

export async function resolvePermission(args, admin) {
let elements;
if (args.router === RouterTypes.GROUP) {
elements = await getGroupConfigs({ domain: args.domain }, true);
} else if (args.router === RouterTypes.CONFIG) {
elements = await getConfigs({ domain: args.domain, group: args.parent }, true);
} else {
return [];
}

const cacheKey = permissionCache.permissionKey(admin._id, args.domain, args.parent, args.actions, args.router);
if (permissionCache.has(cacheKey)) {
return permissionCache.get(cacheKey);
}

let elements = await getElements(args.domain, args.parent, args.router);

let result = [];
for (const element of elements) {
result.push({
Expand All @@ -37,6 +30,21 @@ export async function resolvePermission(args, admin) {
}
}

permissionCache.set(cacheKey, result);
if (result.length) {
permissionCache.set(cacheKey, result);
}

return result;
}
}

const getElements = async (domain, parent, router) => {
if (router === RouterTypes.GROUP) {
return getGroupConfigs({ domain }, true);
}

if (router === RouterTypes.CONFIG) {
return getConfigs({ domain, group: parent }, true);
}

return Promise.resolve([]);
};
15 changes: 11 additions & 4 deletions src/helpers/cache.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ActionTypes } from '../models/permission';

class Cache {
constructor() {
this.cache = new Map();
Expand Down Expand Up @@ -25,22 +27,27 @@ class Cache {
});
}

permissionReset(domainId, action, router) {
permissionReset(domainId, action, router, parentId) {
if (!domainId || !action || !router) {
return;
}

const keys = this.cache.keys();
for (const key of keys) {
const parsedKey = JSON.parse(key);
if (parsedKey.domainId === String(domainId) &&
parsedKey.actions.includes(action) &&
parsedKey.router === router) {
if (this.matchesKey(parsedKey, domainId, action, router, parentId)) {
this.cache.delete(key);
}
}
}

matchesKey(parsedKey, domainId, action, router, parentId) {
return parsedKey.domainId === String(domainId) &&
(parentId === undefined || parsedKey.parentId === String(parentId)) &&
(action === ActionTypes.ALL || parsedKey.actions.includes(action)) &&
parsedKey.router === router;
}

isPermissionCacheActivated() {
return process.env.PERMISSION_CACHE_ACTIVATED === 'true';
}
Expand Down
10 changes: 10 additions & 0 deletions src/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BadRequestError, NotFoundError } from '../exceptions';
import { checkEnvironmentStatusChange_v2 } from '../middleware/validators';
import { getComponentById, getComponents } from './component';
import { resolveVerification } from '../client/relay';
import { permissionCache } from '../helpers/cache';

async function verifyAddComponentInput(configId, admin) {
const config = await getConfigById(configId);
Expand Down Expand Up @@ -79,6 +80,9 @@ export async function createConfig(args, admin) {
await config.save();
updateDomainVersion(config.domain);

// resets permission cache
permissionCache.permissionReset(config.domain, ActionTypes.ALL, RouterTypes.CONFIG, config.group);

return config;
}

Expand All @@ -89,6 +93,9 @@ export async function deleteConfig(id, admin) {
await config.deleteOne();
updateDomainVersion(config.domain);

// resets permission cache
permissionCache.permissionReset(config.domain, ActionTypes.ALL, RouterTypes.CONFIG, config.group);

return config;
}

Expand All @@ -106,6 +113,9 @@ export async function updateConfig(id, args, admin) {
if (duplicatedKey) {
throw new BadRequestError(`Config ${args.key} already exist`);
}

// resets permission cache
permissionCache.permissionReset(config.domain, ActionTypes.ALL, RouterTypes.CONFIG, config.group);
}

// validates existing environment
Expand Down
10 changes: 10 additions & 0 deletions src/services/group-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { checkGroup } from '../external/switcher-api-facade';
import { ActionTypes, RouterTypes } from '../models/permission';
import { getDomainById, updateDomainVersion } from './domain';
import { checkEnvironmentStatusChange_v2 } from '../middleware/validators';
import { permissionCache } from '../helpers/cache';

async function verifyGroupInput(groupId, admin) {
let groupconfig = await getGroupConfigById(groupId);
Expand Down Expand Up @@ -64,6 +65,9 @@ export async function createGroup(args, admin) {
// creates group config
updateDomainVersion(domain._id);
await groupconfig.save();

// resets permission cache
permissionCache.permissionReset(domain._id, ActionTypes.ALL, RouterTypes.GROUP);

return groupconfig;
}
Expand All @@ -75,6 +79,9 @@ export async function deleteGroup(id, admin) {
await groupconfig.deleteOne();
updateDomainVersion(groupconfig.domain);

// resets permission cache
permissionCache.permissionReset(groupconfig.domain, ActionTypes.ALL, RouterTypes.GROUP);

return groupconfig;
}

Expand All @@ -88,6 +95,9 @@ export async function updateGroup(id, args, admin) {
if (groupFound) {
throw new BadRequestError(`Group ${args.name} already exist`);
}

// resets permission cache
permissionCache.permissionReset(groupconfig.domain, ActionTypes.ALL, RouterTypes.GROUP);
}

const updates = Object.keys(args);
Expand Down