|
@@ -6,9 +6,7 @@ import { Log } from "./LogBook";
|
|
|
import { SessionSchema } from "./schemas/session";
|
|
|
import { JobOptions } from "./types/JobOptions";
|
|
|
import { Jobs, Modules } from "./types/Modules";
|
|
|
-import { StationType } from "./schemas/station";
|
|
|
-import { UserRole, UserSchema } from "./schemas/user";
|
|
|
-import permissions from "./permissions";
|
|
|
+import { UserSchema } from "./schemas/user";
|
|
|
import { Models } from "./types/Models";
|
|
|
|
|
|
export default class JobContext {
|
|
@@ -20,6 +18,8 @@ export default class JobContext {
|
|
|
|
|
|
private user?: UserSchema;
|
|
|
|
|
|
+ private permissions?: Record<string, boolean>;
|
|
|
+
|
|
|
public constructor(job: Job, session?: SessionSchema) {
|
|
|
this.job = job;
|
|
|
this.jobQueue = JobQueue.getPrimaryInstance();
|
|
@@ -96,36 +96,28 @@ export default class JobContext {
|
|
|
if (!this.session?.userId) throw new Error("No user found for session");
|
|
|
}
|
|
|
|
|
|
- public async assertPermission(
|
|
|
- permission: string,
|
|
|
- scope?: { stationId?: Types.ObjectId }
|
|
|
+ public async getUserPermissions(
|
|
|
+ scope?: { stationId?: Types.ObjectId },
|
|
|
+ refresh = false
|
|
|
) {
|
|
|
- if (!this.session?.userId) throw new Error("Insufficient permissions");
|
|
|
-
|
|
|
- const user = await this.getUser();
|
|
|
-
|
|
|
- const roles: (UserRole | "owner" | "dj")[] = [user.role];
|
|
|
+ if (this.permissions && !refresh) return this.permissions;
|
|
|
|
|
|
- if (scope?.stationId) {
|
|
|
- const Station = await this.getModel("station");
|
|
|
+ this.permissions = await this.executeJob(
|
|
|
+ "api",
|
|
|
+ "getUserPermissions",
|
|
|
+ scope ?? {}
|
|
|
+ );
|
|
|
|
|
|
- const station = await Station.findById(scope.stationId);
|
|
|
-
|
|
|
- if (
|
|
|
- station.type === StationType.COMMUNITY &&
|
|
|
- station.owner === this.session.userId
|
|
|
- )
|
|
|
- roles.push("owner");
|
|
|
- else if (station.djs.find(dj => dj === this.session?.userId))
|
|
|
- roles.push("dj");
|
|
|
- }
|
|
|
+ return this.permissions;
|
|
|
+ }
|
|
|
|
|
|
- let hasPermission;
|
|
|
- roles.forEach(role => {
|
|
|
- if (permissions[role] && permissions[role][permission])
|
|
|
- hasPermission = true;
|
|
|
- });
|
|
|
+ public async assertPermission(
|
|
|
+ permission: string,
|
|
|
+ scope?: { stationId?: Types.ObjectId }
|
|
|
+ ) {
|
|
|
+ const permissions = await this.getUserPermissions(scope);
|
|
|
|
|
|
- if (!hasPermission) throw new Error("Insufficient permissions");
|
|
|
+ if (!permissions[permission])
|
|
|
+ throw new Error("Insufficient permissions");
|
|
|
}
|
|
|
}
|