Browse Source

refactor: Move event model permission override to base class

Owen Diffey 9 months ago
parent
commit
cb261c6773

+ 20 - 0
backend/src/modules/DataModule/DataModuleEvent.ts

@@ -1,5 +1,9 @@
 import DataModule from "../DataModule";
 import ModuleEvent from "../EventsModule/ModuleEvent";
+import { HydratedDocument } from "mongoose";
+import { UserSchema } from "./models/users/schema";
+import { GetPermissionsResult } from "./models/users/jobs/GetPermissions";
+import GetModelPermissions from "./models/users/jobs/GetModelPermissions";
 
 export default abstract class DataModuleEvent extends ModuleEvent {
 	protected static _module = DataModule;
@@ -13,4 +17,20 @@ export default abstract class DataModuleEvent extends ModuleEvent {
 	public static getModelName() {
 		return this._modelName;
 	}
+
+	public static async hasPermission(
+		user: HydratedDocument<UserSchema> | null,
+		scope?: string
+	) {
+		const permissions = (await new GetModelPermissions({
+			_id: user?._id,
+			modelName: this.getModelName(),
+			modelId: scope
+		}).execute()) as GetPermissionsResult;
+
+		return !!(
+			permissions[`event:${this.getKey(scope)}`] ||
+			permissions[`event:${this.getPath()}:*`]
+		);
+	}
 }

+ 0 - 20
backend/src/modules/DataModule/ModelUpdatedEvent.ts

@@ -1,25 +1,5 @@
-import { HydratedDocument } from "mongoose";
 import DataModuleEvent from "./DataModuleEvent";
-import { UserSchema } from "./models/users/schema";
-import { GetPermissionsResult } from "./models/users/jobs/GetPermissions";
-import GetModelPermissions from "./models/users/jobs/GetModelPermissions";
 
 export default abstract class ModelUpdatedEvent extends DataModuleEvent {
 	protected static _name = "updated";
-
-	public static async hasPermission(
-		user: HydratedDocument<UserSchema> | null,
-		scope?: string
-	) {
-		const permissions = (await new GetModelPermissions({
-			_id: user?._id,
-			modelName: this.getModelName(),
-			modelId: scope
-		}).execute()) as GetPermissionsResult;
-
-		return !!(
-			permissions[`event:${this.getKey(scope)}`] ||
-			permissions[`event:${this.getPath()}:*`]
-		);
-	}
 }