Skip to content

Commit b34a9fd

Browse files
authored
[server] Fix permission issue for collaborators in listenForPrebuildUpdates (#20782)
1 parent 83a98be commit b34a9fd

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

components/server/src/orgs/organization-service.spec.db.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe("OrganizationService", async () => {
3333
let stranger: User;
3434
const adminId = BUILTIN_INSTLLATION_ADMIN_USER_ID;
3535
let org: Organization;
36+
let org2: Organization;
3637
let validateDefaultWorkspaceImage: DefaultWorkspaceImageValidator | undefined;
3738

3839
beforeEach(async () => {
@@ -109,6 +110,8 @@ describe("OrganizationService", async () => {
109110

110111
await withTestCtx(SYSTEM_USER, () => os.joinOrganization(collaborator.id, invite.id));
111112

113+
org2 = await os.createOrganization(owner.id, "org2");
114+
112115
stranger = await userService.createUser({
113116
identity: {
114117
authId: "github|1234",
@@ -275,7 +278,7 @@ describe("OrganizationService", async () => {
275278
await os.createOrganization(owner.id, "org1");
276279
await os.createOrganization(owner.id, "org2");
277280
let orgs = await os.listOrganizationsByMember(owner.id, owner.id);
278-
expect(orgs.length).to.eq(3);
281+
expect(orgs.length).to.eq(4);
279282
orgs = await os.listOrganizationsByMember(member.id, member.id);
280283
expect(orgs.length).to.eq(1);
281284
orgs = await os.listOrganizationsByMember(collaborator.id, collaborator.id);
@@ -347,11 +350,12 @@ describe("OrganizationService", async () => {
347350
expect(members.some((m) => m.userId === owner.id && m.role === "owner")).to.be.true;
348351
});
349352

350-
it("should listOrganizations", async () => {
353+
it("should listOrganizations (for installation)", async () => {
351354
const strangerOrg = await os.createOrganization(stranger.id, "stranger-org");
352355
let orgs = await os.listOrganizations(owner.id, {}, "installation");
353-
expect(orgs.rows[0].id).to.eq(org.id);
354-
expect(orgs.total).to.eq(1);
356+
expect(orgs.rows.map((o) => o.id)).to.contain(org.id);
357+
expect(orgs.rows.map((o) => o.id)).to.contain(org2.id);
358+
expect(orgs.total).to.eq(2);
355359

356360
orgs = await os.listOrganizations(stranger.id, {}, "installation");
357361
expect(orgs.rows[0].id).to.eq(strangerOrg.id);
@@ -360,7 +364,28 @@ describe("OrganizationService", async () => {
360364
orgs = await os.listOrganizations(adminId, {}, "installation");
361365
expect(orgs.rows.some((org) => org.id === org.id)).to.be.true;
362366
expect(orgs.rows.some((org) => org.id === strangerOrg.id)).to.be.true;
363-
expect(orgs.total).to.eq(2);
367+
expect(orgs.total).to.eq(3);
368+
});
369+
370+
it("should listOrganizations (for member)", async () => {
371+
// Owner is member of both orgs
372+
const ownerResult = await os.listOrganizations(owner.id, {}, "member");
373+
expect(ownerResult.rows.map((o) => o.id)).to.include(org.id);
374+
expect(ownerResult.rows.map((o) => o.id)).to.include(org2.id);
375+
376+
// Member is only in org1
377+
const memberResult = await os.listOrganizations(member.id, {}, "member");
378+
expect(memberResult.rows.map((o) => o.id)).to.include(org.id);
379+
expect(memberResult.rows.map((o) => o.id)).to.not.include(org2.id);
380+
381+
// Collaborator is only in org1
382+
const collaboratorResults = await os.listOrganizations(collaborator.id, {}, "member");
383+
expect(collaboratorResults.rows.map((o) => o.id)).to.include(org.id);
384+
expect(collaboratorResults.rows.map((o) => o.id)).to.not.include(org2.id);
385+
386+
// Stranger is in no orgs
387+
const strangerResult = await os.listOrganizations(stranger.id, {}, "member");
388+
expect(strangerResult.total).to.equal(0);
364389
});
365390

366391
it("should ad as collaborator with dataops + flexibleRole", async () => {

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
268268
subjectId: SubjectId.fromUserId(userId),
269269
},
270270
async () => {
271-
const organizations = await this.getTeams(ctx ?? {});
271+
const { rows: organizations } = await this.organizationService.listOrganizations(
272+
userId,
273+
{ limit: 10 },
274+
"member",
275+
);
272276
for (const organization of organizations) {
273277
const hasPermission = await this.auth.hasPermissionOnOrganization(
274278
userId,

0 commit comments

Comments
 (0)