Browse Source

refactor: clean up the data update/delete events hasPermission code a bit

Kristian Vos 8 months ago
parent
commit
2f1942df65

+ 2 - 9
backend/src/modules/DataModule/models/minifiedUsers/events/MinifiedUserDeletedEvent.ts

@@ -1,19 +1,12 @@
-import { HydratedDocument } from "mongoose";
 import ModelDeletedEvent from "@/modules/DataModule/ModelDeletedEvent";
-import { MinifiedUserSchema } from "../schema";
+import doesModelExist from "@/modules/DataModule/permissions/doesModelExist";
 
 export default abstract class MinifiedUserDeletedEvent extends ModelDeletedEvent {
 	protected static _modelName = "minifiedUsers";
 
-	// TODO make this function shared
 	/**
 	 * If a modelId was specified, any user can subscribe.
 	 * If not, only admins can subscribe.
 	 */
-	protected static _hasPermission = (
-		model: HydratedDocument<MinifiedUserSchema>
-	) => {
-		if (model) return true;
-		return false;
-	};
+	protected static _hasPermission = doesModelExist;
 }

+ 2 - 9
backend/src/modules/DataModule/models/minifiedUsers/events/MinifiedUserUpdatedEvent.ts

@@ -1,19 +1,12 @@
-import { HydratedDocument } from "mongoose";
 import ModelUpdatedEvent from "@/modules/DataModule/ModelUpdatedEvent";
-import { MinifiedUserSchema } from "../schema";
+import doesModelExist from "@/modules/DataModule/permissions/doesModelExist";
 
 export default abstract class MinifiedUserUpdatedEvent extends ModelUpdatedEvent {
 	protected static _modelName = "minifiedUsers";
 
-	// TODO make this function shared
 	/**
 	 * If a modelId was specified, any user can subscribe.
 	 * If not, only admins can subscribe.
 	 */
-	protected static _hasPermission = (
-		model: HydratedDocument<MinifiedUserSchema>
-	) => {
-		if (model) return true;
-		return false;
-	};
+	protected static _hasPermission = doesModelExist;
 }

+ 2 - 8
backend/src/modules/DataModule/models/news/events/NewsDeletedEvent.ts

@@ -1,14 +1,8 @@
-import { HydratedDocument } from "mongoose";
 import ModelDeletedEvent from "@/modules/DataModule/ModelDeletedEvent";
-import { NewsStatus } from "@/modules/DataModule/models/news/NewsStatus";
-import { NewsSchema } from "../schema";
+import isNewsPublished from "@/modules/DataModule/permissions/isNewsPublished";
 
 export default abstract class NewsDeletedEvent extends ModelDeletedEvent {
 	protected static _modelName = "news";
 
-	// TODO make this function shared
-	protected static _hasPermission = (model: HydratedDocument<NewsSchema>) => {
-		if (model?.status === NewsStatus.PUBLISHED) return true;
-		return false;
-	};
+	protected static _hasPermission = isNewsPublished;
 }

+ 2 - 8
backend/src/modules/DataModule/models/news/events/NewsUpdatedEvent.ts

@@ -1,14 +1,8 @@
-import { HydratedDocument } from "mongoose";
 import ModelUpdatedEvent from "@/modules/DataModule/ModelUpdatedEvent";
-import { NewsStatus } from "@/modules/DataModule/models/news/NewsStatus";
-import { NewsSchema } from "../schema";
+import isNewsPublished from "@/modules/DataModule/permissions/isNewsPublished";
 
 export default abstract class NewsUpdatedEvent extends ModelUpdatedEvent {
 	protected static _modelName = "news";
 
-	// TODO make this function shared
-	protected static _hasPermission = (model: HydratedDocument<NewsSchema>) => {
-		if (model?.status === NewsStatus.PUBLISHED) return true;
-		return false;
-	};
+	protected static _hasPermission = isNewsPublished;
 }

+ 8 - 26
backend/src/modules/DataModule/models/stations/events/StationDeletedEvent.ts

@@ -1,32 +1,14 @@
-import { Schema, HydratedDocument } from "mongoose";
 import ModelDeletedEvent from "@/modules/DataModule/ModelDeletedEvent";
-import { StationSchema } from "../schema";
-import { StationPrivacy } from "../StationPrivacy";
-import { UserSchema } from "../../users/schema";
-import { StationType } from "../StationType";
+import isUnlisted from "@/modules/DataModule/permissions/isUnlisted";
+import isPublic from "@/modules/DataModule/permissions/isPublic";
+import isOwner from "@/modules/DataModule/permissions/isOwner";
 
 export default abstract class StationDeletedEvent extends ModelDeletedEvent {
 	protected static _modelName = "stations";
 
-	// TODO make this function shared
-	protected static _hasPermission = (
-		model: HydratedDocument<StationSchema>,
-		user: HydratedDocument<UserSchema> | null
-	) => {
-		if (!model) return false;
-		if (
-			model.privacy === StationPrivacy.PUBLIC ||
-			model.privacy === StationPrivacy.UNLISTED
-		)
-			return true;
-		// If the station isn't public/unlisted, and the user isn't logged in, don't give permission
-		if (!user) return false;
-		if (
-			model.type === StationType.COMMUNITY &&
-			model.owner?.toString() === user._id.toString()
-		)
-			return true;
-
-		return false;
-	};
+	protected static _hasPermission = [
+		isPublic,
+		isUnlisted,
+		isOwner // TODO only check isOwner for community stations, if owner = user id
+	];
 }

+ 8 - 26
backend/src/modules/DataModule/models/stations/events/StationUpdatedEvent.ts

@@ -1,32 +1,14 @@
-import { Schema, HydratedDocument } from "mongoose";
 import ModelUpdatedEvent from "@/modules/DataModule/ModelUpdatedEvent";
-import { StationPrivacy } from "../StationPrivacy";
-import { StationType } from "../StationType";
-import { StationSchema } from "../schema";
-import { UserSchema } from "../../users/schema";
+import isPublic from "@/modules/DataModule/permissions/isPublic";
+import isUnlisted from "@/modules/DataModule/permissions/isUnlisted";
+import isOwner from "@/modules/DataModule/permissions/isOwner";
 
 export default abstract class StationUpdatedEvent extends ModelUpdatedEvent {
 	protected static _modelName = "stations";
 
-	// TODO make this function shared
-	protected static _hasPermission = (
-		model: HydratedDocument<StationSchema>,
-		user: HydratedDocument<UserSchema> | null
-	) => {
-		if (!model) return false;
-		if (
-			model.privacy === StationPrivacy.PUBLIC ||
-			model.privacy === StationPrivacy.UNLISTED
-		)
-			return true;
-		// If the station isn't public/unlisted, and the user isn't logged in, don't give permission
-		if (!user) return false;
-		if (
-			model.type === StationType.COMMUNITY &&
-			model.owner?.toString() === user._id.toString()
-		)
-			return true;
-
-		return false;
-	};
+	protected static _hasPermission = [
+		isPublic,
+		isUnlisted,
+		isOwner // TODO only check isOwner for community stations, if owner = user id
+	];
 }

+ 10 - 0
backend/src/modules/DataModule/permissions/doesModelExist.ts

@@ -0,0 +1,10 @@
+import { HydratedDocument, Schema } from "mongoose";
+
+/**
+ * Simply used to check if the provided model exists.
+ * Used for events/jobs where any user is allowed to access it,
+ * as long as a valid object id was provided.
+ */
+export default <ModelSchemaType extends Schema>(
+	model: HydratedDocument<ModelSchemaType>
+) => !!model;

+ 9 - 0
backend/src/modules/DataModule/permissions/isNewsPublished.ts

@@ -0,0 +1,9 @@
+import { HydratedDocument } from "mongoose";
+import { NewsStatus } from "../models/news/NewsStatus";
+import { NewsSchema } from "../models/news/schema";
+
+/**
+ * Simply used to check if a news model exists and is published
+ */
+export default (model: HydratedDocument<NewsSchema>) =>
+	model && model.status === NewsStatus.PUBLISHED;

+ 1 - 1
backend/src/modules/DataModule/permissions/isOwner.ts

@@ -7,7 +7,7 @@ export default (
 		| (HydratedDocument<any> & { createdBy?: any }),
 	user?: HydratedDocument<UserSchema>
 ) => {
-	if (!(user && model)) return false;
+	if (!user || !model) return false;
 
 	let ownerAttribute;