Refactor AccessTokenService to utilize UserPermissionUtil for permission retrieval; introduce PermissionNamespace enum

This commit is contained in:
Simon Larsen 2024-09-26 13:59:28 +01:00
parent 54e8a4c224
commit 98fe5deba7
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
4 changed files with 65 additions and 25 deletions

View File

@ -4,7 +4,6 @@ import BaseService from "./BaseService";
import TeamMemberService from "./TeamMemberService";
import TeamPermissionService from "./TeamPermissionService";
import LIMIT_MAX from "../../Types/Database/LimitMax";
import { JSONObject } from "../../Types/JSON";
import ObjectID from "../../Types/ObjectID";
import Permission, {
UserGlobalAccessPermission,
@ -15,11 +14,9 @@ import Label from "Common/Models/DatabaseModels/Label";
import TeamMember from "Common/Models/DatabaseModels/TeamMember";
import TeamPermission from "Common/Models/DatabaseModels/TeamPermission";
import UserPermissionUtil from "../Utils/UserPermission/UserPermission";
import PermissionNamespace from "../Types/Permission/PermissionNamespace";
enum PermissionNamespace {
GlobalPermission = "global-permissions",
ProjectPermission = "project-permissions",
}
export class AccessTokenService extends BaseService {
public constructor() {
@ -113,21 +110,16 @@ export class AccessTokenService extends BaseService {
public async getUserGlobalAccessPermission(
userId: ObjectID,
): Promise<UserGlobalAccessPermission | null> {
const json: JSONObject | null = await GlobalCache.getJSONObject(
"user",
userId.toString(),
);
const json: UserGlobalAccessPermission | null = await UserPermissionUtil.getUserGlobalAccessPermissionFromCache(
userId,
)
if (!json) {
return await this.refreshUserGlobalAccessPermission(userId);
}
const accessPermission: UserGlobalAccessPermission =
json as UserGlobalAccessPermission;
return json;
accessPermission._type = "UserGlobalAccessPermission";
return accessPermission;
}
public async refreshUserTenantAccessPermission(
@ -217,14 +209,10 @@ export class AccessTokenService extends BaseService {
projectId: ObjectID,
): Promise<UserTenantAccessPermission | null> {
const json: UserTenantAccessPermission | null =
(await GlobalCache.getJSONObject(
PermissionNamespace.ProjectPermission,
userId.toString() + projectId.toString(),
)) as UserTenantAccessPermission;
if (json) {
json._type = "UserTenantAccessPermission";
}
await UserPermissionUtil.getUserTenantAccessPermissionFromCache(
userId,
projectId,
);
if (!json) {
return await this.refreshUserTenantAccessPermission(userId, projectId);

View File

@ -0,0 +1,6 @@
enum PermissionNamespace {
GlobalPermission = "global-permissions",
ProjectPermission = "project-permissions",
}
export default PermissionNamespace;

View File

@ -13,7 +13,6 @@ import Permission, {
UserGlobalAccessPermission,
UserTenantAccessPermission,
} from "../../Types/Permission";
import AccessTokenService from "../Services/AccessTokenService";
import { getModelTypeByName } from "../../Models/DatabaseModels/Index";
import { getModelTypeByName as getAnalyticsModelTypeByname } from "../../Models/AnalyticsModels/Index";
import DatabaseRequestType from "../Types/BaseDatabase/DatabaseRequestType";
@ -23,6 +22,7 @@ import ListenToModelEventJSON from "../../Types/Realtime/ListenToModelEventJSON"
import EventName from "../../Types/Realtime/EventName";
import CookieUtil from "./Cookie";
import Dictionary from "../../Types/Dictionary";
import UserPermissionUtil from "./UserPermission/UserPermission";
export default abstract class Realtime {
private static socketServer: SocketServer | null = null;
@ -128,7 +128,7 @@ export default abstract class Realtime {
logger.debug("Fetching user global access permissions");
const userGlobalAccessPermission: UserGlobalAccessPermission | null =
await AccessTokenService.getUserGlobalAccessPermission(
await UserPermissionUtil.getUserGlobalAccessPermissionFromCache(
userAuthorizationData.userId,
);
@ -155,7 +155,7 @@ export default abstract class Realtime {
// if it has the access to the tenant, check if it has access to the model
const userTenantAccessPermission: UserTenantAccessPermission | null =
await AccessTokenService.getUserTenantAccessPermission(
await UserPermissionUtil.getUserTenantAccessPermissionFromCache(
userId,
projectId,
);

View File

@ -1,10 +1,56 @@
import { JSONObject } from "../../../Types/JSON";
import ObjectID from "../../../Types/ObjectID";
import Permission, {
UserGlobalAccessPermission,
UserPermission,
UserTenantAccessPermission,
} from "../../../Types/Permission";
import GlobalCache from "../../Infrastructure/GlobalCache";
import PermissionNamespace from "../../Types/Permission/PermissionNamespace";
export default class UserPermissionUtil {
public static async getUserTenantAccessPermissionFromCache(
userId: ObjectID,
projectId: ObjectID,
): Promise<UserTenantAccessPermission | null> {
const json: UserTenantAccessPermission | null =
(await GlobalCache.getJSONObject(
PermissionNamespace.ProjectPermission,
userId.toString() + projectId.toString(),
)) as UserTenantAccessPermission;
if (json) {
json._type = "UserTenantAccessPermission";
}
if (!json) {
return null;
}
return json;
}
public static async getUserGlobalAccessPermissionFromCache(
userId: ObjectID,
): Promise<UserGlobalAccessPermission | null> {
const json: JSONObject | null = await GlobalCache.getJSONObject(
"user",
userId.toString(),
);
if (!json) {
return null;
}
const accessPermission: UserGlobalAccessPermission =
json as UserGlobalAccessPermission;
accessPermission._type = "UserGlobalAccessPermission";
return accessPermission;
}
public static getDefaultUserTenantAccessPermission(
projectId: ObjectID,
): UserTenantAccessPermission {