Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to environment for permission cache #453

Merged
merged 1 commit into from
Oct 18, 2023
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
21 changes: 11 additions & 10 deletions src/helpers/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Cache {
}

set(key, value) {
if (!this.isPermissionCacheActivated()) {
if (!this._isPermissionCacheActivated()) {
return;
}

Expand All @@ -17,13 +17,18 @@ class Cache {
return this.cache.get(key);
}

permissionKey(adminId, domainId, parentId, actions, router) {
has(key) {
return this.cache.has(key);
}

permissionKey(adminId, domainId, parentId, actions, router, environment) {
return JSON.stringify({
adminId: String(adminId),
domainId: String(domainId),
parentId: String(parentId),
actions,
router
router,
environment
});
}

Expand All @@ -35,26 +40,22 @@ class Cache {
const keys = this.cache.keys();
for (const key of keys) {
const parsedKey = JSON.parse(key);
if (this.matchesKey(parsedKey, domainId, action, router, parentId)) {
if (this._matchesKey(parsedKey, domainId, action, router, parentId)) {
this.cache.delete(key);
}
}
}

matchesKey(parsedKey, domainId, action, router, parentId) {
_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() {
_isPermissionCacheActivated() {
return process.env.PERMISSION_CACHE_ACTIVATED === 'true';
}

has(key) {
return this.cache.has(key);
}
}

const permissionCache = new Cache();
Expand Down
4 changes: 3 additions & 1 deletion src/routers/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ router.post('/admin/collaboration/permission', auth, [
};

const parentId = element._id || element.key || element.name || element.strategy;
const cacheKey = permissionCache.permissionKey(req.admin._id, req.body.domain, parentId, req.body.action, req.body.router);
const cacheKey = permissionCache.permissionKey(req.admin._id, req.body.domain, parentId,
req.body.action, req.body.router, req.body.environment);

if (permissionCache.has(cacheKey)) {
return res.send(permissionCache.get(cacheKey));
}
Expand Down
35 changes: 33 additions & 2 deletions tests/unit-test/cache.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { permissionCache } from "../../src/helpers/cache";
import { EnvType } from "../../src/models/environment";
import { ActionTypes, RouterTypes } from "../../src/models/permission";

describe("Test permissionCache", () => {

beforeEach(() => {
permissionCache.cache.clear();
});

it("UNIT_CACHE - Should set and get cache", () => {
const cacheKey = permissionCache.permissionKey(
'adminId',
Expand Down Expand Up @@ -33,7 +38,7 @@ describe("Test permissionCache", () => {
expect(result).toBeUndefined();
});

it("UNIT_CACHE - Should not reload cache", () => {
it("UNIT_CACHE - Should NOT reload cache", () => {
const cacheKey = permissionCache.permissionKey(
'adminId',
'domainId',
Expand All @@ -48,7 +53,7 @@ describe("Test permissionCache", () => {
expect(result).toEqual('value');
});

it("UNIT_CACHE - Should not reload cache - empty router/action", () => {
it("UNIT_CACHE - Should NOT reload cache - empty router/action", () => {
const cacheKey = permissionCache.permissionKey(
'adminId',
'domainId',
Expand All @@ -63,4 +68,30 @@ describe("Test permissionCache", () => {
expect(result).toEqual('value');
});

it("UNIT_CACHE - Should NOT get from cache - different environment", () => {
const cacheKey = permissionCache.permissionKey(
'adminId',
'domainId',
'parentId',
[ActionTypes.UPDATE, ActionTypes.READ],
RouterTypes.GROUP,
EnvType.DEFAULT
);

permissionCache.set(cacheKey, 'value1');
const result = permissionCache.get(cacheKey);
expect(result).toEqual('value1');

const cacheKey2 = permissionCache.permissionKey(
'adminId',
'domainId',
'parentId',
[ActionTypes.UPDATE, ActionTypes.READ],
RouterTypes.GROUP
);

const result2 = permissionCache.get(cacheKey2);
expect(result2).toBeUndefined();
});

});