Sfoglia il codice sorgente

refactor: Allow users to subscribe to their own events

Owen Diffey 4 settimane fa
parent
commit
5dc7774382

+ 2 - 1
backend/src/modules/DataModule/models/User/events/UserCreatedEvent.ts

@@ -1,9 +1,10 @@
 import ModelCreatedEvent from "@/modules/DataModule/ModelCreatedEvent";
 import isAdmin from "@/modules/DataModule/permissions/isAdmin";
+import isSelf from "@/modules/DataModule/permissions/modelPermissions/isSelf";
 import User from "../../User";
 
 export default abstract class UserCreatedEvent extends ModelCreatedEvent {
 	protected static _model = User;
 
-	protected static _hasPermission = isAdmin;
+	protected static _hasModelPermission = [isAdmin, isSelf];
 }

+ 2 - 1
backend/src/modules/DataModule/models/User/events/UserDeletedEvent.ts

@@ -1,9 +1,10 @@
 import ModelDeletedEvent from "@/modules/DataModule/ModelDeletedEvent";
 import isAdmin from "@/modules/DataModule/permissions/isAdmin";
+import isSelf from "@/modules/DataModule/permissions/modelPermissions/isSelf";
 import User from "../../User";
 
 export default abstract class UserDeletedEvent extends ModelDeletedEvent {
 	protected static _model = User;
 
-	protected static _hasPermission = isAdmin;
+	protected static _hasModelPermission = [isAdmin, isSelf];
 }

+ 2 - 2
backend/src/modules/DataModule/models/User/events/UserUpdatedEvent.ts

@@ -1,10 +1,10 @@
 import ModelUpdatedEvent from "@/modules/DataModule/ModelUpdatedEvent";
 import isAdmin from "@/modules/DataModule/permissions/isAdmin";
+import isSelf from "@/modules/DataModule/permissions/modelPermissions/isSelf";
 import User from "../../User";
 
 export default abstract class UserUpdatedEvent extends ModelUpdatedEvent {
 	protected static _model = User;
 
-	protected static _hasPermission = isAdmin;
-	// TODO maybe allow this for the current logged in user?
+	protected static _hasModelPermission = [isAdmin, isSelf];
 }

+ 4 - 0
backend/src/modules/DataModule/permissions/modelPermissions/isSelf.ts

@@ -0,0 +1,4 @@
+import User from "@models/User";
+
+export default (model?: User, user?: User) =>
+	model && user && model._id === user._id;