Skip to content

Commit 30a23dc

Browse files
committed
[server] WorkspaceService.getWorkspaces
1 parent d9dbf82 commit 30a23dc

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

components/server/src/workspace/gitpod-server-impl.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,22 +1078,13 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
10781078

10791079
const user = await this.checkUser("getWorkspaces");
10801080

1081-
const res = await this.workspaceDb.trace(ctx).find({
1082-
limit: 20,
1083-
...options,
1084-
userId: user.id,
1085-
includeHeadless: false,
1081+
return this.workspaceService.getWorkspaces(user.id, options, async (workspace, instance) => {
1082+
if (instance) {
1083+
return this.guardAccess({ kind: "workspaceInstance", subject: instance, workspace: workspace }, "get");
1084+
} else {
1085+
return this.guardAccess({ kind: "workspace", subject: workspace }, "get");
1086+
}
10861087
});
1087-
await Promise.all(res.map((ws) => this.guardAccess({ kind: "workspace", subject: ws.workspace }, "get")));
1088-
await Promise.all(
1089-
res.map((ws) =>
1090-
this.guardAccess(
1091-
{ kind: "workspaceInstance", subject: ws.latestInstance, workspace: ws.workspace },
1092-
"get",
1093-
),
1094-
),
1095-
);
1096-
return res;
10971088
}
10981089

10991090
public async isWorkspaceOwner(ctx: TraceContext, workspaceId: string): Promise<boolean> {

components/server/src/workspace/workspace-service.spec.db.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,36 @@ describe("WorkspaceService", async () => {
233233
expect(strangerWs?.id, "stranger has access to shared workspace").to.equal(ws.id);
234234
});
235235

236+
it("should getWorkspaces", async () => {
237+
const svc = container.get(WorkspaceService);
238+
await createTestWorkspace(svc, org, owner, project);
239+
240+
const ownerResult = await svc.getWorkspaces(owner.id, {});
241+
expect(ownerResult).to.have.lengthOf(1);
242+
243+
const memberResult = await svc.getWorkspaces(member.id, {});
244+
expect(memberResult).to.have.lengthOf(0);
245+
246+
const strangerResult = await svc.getWorkspaces(stranger.id, {});
247+
expect(strangerResult).to.have.lengthOf(0);
248+
});
249+
250+
it("should getWorkspaces - shared", async () => {
251+
const svc = container.get(WorkspaceService);
252+
const ws = await createTestWorkspace(svc, org, owner, project);
253+
254+
await svc.controlAdmission(owner.id, ws.id, "everyone");
255+
256+
const ownerResult = await svc.getWorkspaces(owner.id, {});
257+
expect(ownerResult).to.have.lengthOf(1);
258+
259+
const memberResult = await svc.getWorkspaces(member.id, {});
260+
expect(memberResult).to.have.lengthOf(1);
261+
262+
const strangerResult = await svc.getWorkspaces(stranger.id, {});
263+
expect(strangerResult).to.have.lengthOf(1);
264+
});
265+
236266
it("should getOwnerToken", async () => {
237267
const svc = container.get(WorkspaceService);
238268
const ws = await createTestWorkspace(svc, org, owner, project);

components/server/src/workspace/workspace-service.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ export class WorkspaceService {
140140
};
141141
}
142142

143+
async getWorkspaces(
144+
userId: string,
145+
options: GitpodServer.GetWorkspacesOptions,
146+
check: (workspace: Workspace, instance?: WorkspaceInstance) => Promise<void> = async () => {},
147+
): Promise<WorkspaceInfo[]> {
148+
const res = await this.db.find({
149+
limit: 20,
150+
...options,
151+
userId,
152+
includeHeadless: false,
153+
});
154+
await Promise.all(
155+
res.map(async (info) => {
156+
await this.auth.checkPermissionOnWorkspace(userId, "access", info.workspace.id);
157+
await check(info.workspace, info.latestInstance);
158+
}),
159+
);
160+
return res;
161+
}
162+
143163
async getCurrentInstance(userId: string, workspaceId: string): Promise<WorkspaceInstance> {
144164
await this.auth.checkPermissionOnWorkspace(userId, "access", workspaceId);
145165
const result = await this.db.findCurrentInstance(workspaceId);

0 commit comments

Comments
 (0)