瀏覽代碼

refactor: Hard code frontend view permissions for roles

Owen Diffey 8 月之前
父節點
當前提交
a36ebbf737
共有 1 個文件被更改,包括 35 次插入1 次删除
  1. 35 1
      backend/src/modules/DataModule/models/users/jobs/GetPermissions.ts

+ 35 - 1
backend/src/modules/DataModule/models/users/jobs/GetPermissions.ts

@@ -6,6 +6,7 @@ import { UserSchema } from "../schema";
 import ModuleManager from "@/ModuleManager";
 import Job from "@/Job";
 import Event from "@/modules/EventsModule/Event";
+import { UserRole } from "../UserRole";
 
 export type GetPermissionsResult = Record<string, boolean>;
 
@@ -44,7 +45,8 @@ export default class GetPermissions extends DataModuleJob {
 		const jobNames = Object.keys(jobs);
 		const eventNames = Object.keys(events);
 
-		const permissions: GetPermissionsResult = {};
+		const permissions: GetPermissionsResult =
+			this._getFrontendViewPermissions(user);
 
 		await forEachIn(jobNames, async jobName => {
 			const job = jobs[jobName];
@@ -65,6 +67,38 @@ export default class GetPermissions extends DataModuleJob {
 		return permissions;
 	}
 
+	protected _getFrontendViewPermissions(
+		user: HydratedDocument<UserSchema> | null
+	): Record<string, boolean> {
+		if (!user) return {};
+
+		const moderatorPermissions = {
+			"admin.view": true,
+			"admin.view.import": true,
+			"admin.view.news": true,
+			"admin.view.playlists": true,
+			"admin.view.punishments": true,
+			"admin.view.reports": true,
+			"admin.view.songs": true,
+			"admin.view.stations": true,
+			"admin.view.users": true,
+			"admin.view.youtubeVideos": true
+		};
+
+		if (user.role === UserRole.MODERATOR) return moderatorPermissions;
+
+		const adminPermissions = {
+			...moderatorPermissions,
+			"admin.view.dataRequests": true,
+			"admin.view.statistics": true,
+			"admin.view.youtube": true
+		};
+
+		if (user.role === UserRole.ADMIN) return adminPermissions;
+
+		return {};
+	}
+
 	protected _getAllJobs(): Record<string, typeof Job> {
 		const modules = Object.entries(ModuleManager.getModules() ?? {});
 		let jobs: (string | typeof Job)[][] = [];