Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[_] feat: workspace role guard #291

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/modules/workspaces/domains/workspace-team-user.domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { User } from '../../user/user.domain';
import { WorkspaceTeam } from './workspace-team.domain';
import { WorkspaceTeamUserAttributes } from '../attributes/workspace-team-users.attributes';

export class WorkspaceTeamUser implements WorkspaceTeamUserAttributes {
id: string;
teamId: string;
memberId: string;
team?: WorkspaceTeam;
createdAt: Date;
updatedAt: Date;

constructor({
id,
teamId,
memberId,
team,
createdAt,
updatedAt,
}: WorkspaceTeamUserAttributes & { team?: WorkspaceTeam }) {
this.id = id;
this.teamId = teamId;
this.memberId = memberId;
this.team = team;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

static build(
workspaceTeamUser: WorkspaceTeamUserAttributes & {
team?: WorkspaceTeam;
member?: User;
},
): WorkspaceTeamUser {
return new WorkspaceTeamUser(workspaceTeamUser);
}

toJSON() {
return {
id: this.id,
teamId: this.teamId,
memberId: this.memberId,
team: this.team ? this.team.toJSON() : undefined,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
};
}
}
5 changes: 5 additions & 0 deletions src/modules/workspaces/domains/workspace-team.domain.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { User } from '../../user/user.domain';
import { WorkspaceTeamAttributes } from '../attributes/workspace-team.attributes';

export class WorkspaceTeam implements WorkspaceTeamAttributes {
Expand All @@ -24,6 +25,10 @@ export class WorkspaceTeam implements WorkspaceTeamAttributes {
this.updatedAt = updatedAt;
}

isUserManager(user: User) {
return user.uuid === this.managerId;
}

static build(teamAttributes: WorkspaceTeamAttributes): WorkspaceTeam {
return new WorkspaceTeam(teamAttributes);
}
Expand Down
57 changes: 57 additions & 0 deletions src/modules/workspaces/domains/workspace-user.domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { WorkspaceUserAttributes } from '../attributes/workspace-users.attributes';

export class WorkspaceUser implements WorkspaceUserAttributes {
id: string;
memberId: string;
key: string;
workspaceId: string;
spaceLimit: bigint;
driveUsage: bigint;
backupsUsage: bigint;
deactivated: boolean;
createdAt: Date;
updatedAt: Date;

constructor({
id,
memberId,
key,
workspaceId,
spaceLimit,
driveUsage,
backupsUsage,
deactivated,
createdAt,
updatedAt,
}: WorkspaceUserAttributes) {
this.id = id;
this.memberId = memberId;
this.key = key;
this.workspaceId = workspaceId;
this.spaceLimit = spaceLimit;
this.driveUsage = driveUsage;
this.backupsUsage = backupsUsage;
this.deactivated = deactivated;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

static build(workspaceUser: WorkspaceUserAttributes): WorkspaceUser {
return new WorkspaceUser(workspaceUser);
}

toJSON() {
return {
id: this.id,
memberId: this.memberId,
key: this.key,
workspaceId: this.workspaceId,
spaceLimit: this.spaceLimit.toString(),
driveUsage: this.driveUsage.toString(),
backupsUsage: this.backupsUsage.toString(),
deactivated: this.deactivated,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
};
}
}
5 changes: 5 additions & 0 deletions src/modules/workspaces/domains/workspaces.domain.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { User } from '../../user/user.domain';
import { WorkspaceAttributes } from '../attributes/workspace.attributes';

export class Workspace implements WorkspaceAttributes {
Expand Down Expand Up @@ -40,6 +41,10 @@ export class Workspace implements WorkspaceAttributes {
return new Workspace(user);
}

isUserOwner(user: User) {
return user.uuid === this.ownerId;
}

toJSON() {
return {
id: this.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { SetMetadata } from '@nestjs/common';

export enum WorkspaceRole {
OWNER = 'owner',
MANAGER = 'manager',
MEMBER = 'member',
}

export enum AccessContext {
WORKSPACE = 'workspace',
TEAM = 'team',
}

export const WorkspaceContextIdFieldName = {
[AccessContext.WORKSPACE]: 'workspaceId',
[AccessContext.TEAM]: 'teamId',
};

export interface AccessOptions {
accessContext: AccessContext;
requiredRole: WorkspaceRole;
idSource: 'params' | 'body' | 'query';
}

export const WorkspaceRequiredAccess = (
accessContext: AccessContext,
requiredRole: WorkspaceRole,
idSource: 'params' | 'body' | 'query' = 'params',
) => SetMetadata('accessControl', { accessContext, requiredRole, idSource });
Loading
Loading