|
24 | 24 | import java.util.stream.Stream; |
25 | 25 |
|
26 | 26 | import jakarta.ws.rs.BadRequestException; |
| 27 | +import jakarta.ws.rs.ForbiddenException; |
27 | 28 | import jakarta.ws.rs.NotFoundException; |
28 | 29 | import jakarta.ws.rs.core.Response; |
29 | 30 | import jakarta.ws.rs.core.Response.Status; |
30 | 31 |
|
| 32 | +import org.keycloak.admin.client.Keycloak; |
31 | 33 | import org.keycloak.admin.client.resource.OrganizationMemberResource; |
32 | 34 | import org.keycloak.admin.client.resource.OrganizationResource; |
33 | 35 | import org.keycloak.admin.client.resource.UserResource; |
| 36 | +import org.keycloak.models.Constants; |
34 | 37 | import org.keycloak.models.OrganizationModel; |
35 | 38 | import org.keycloak.models.RealmModel; |
36 | 39 | import org.keycloak.models.UserModel; |
|
49 | 52 | import org.keycloak.testsuite.organization.admin.AbstractOrganizationTest; |
50 | 53 | import org.keycloak.testsuite.pages.AppPage; |
51 | 54 | import org.keycloak.testsuite.updaters.RealmAttributeUpdater; |
| 55 | +import org.keycloak.testsuite.util.AdminClientUtil; |
52 | 56 | import org.keycloak.testsuite.util.UserBuilder; |
53 | 57 |
|
54 | 58 | import org.hamcrest.Matchers; |
@@ -705,6 +709,38 @@ public void testGetMemberOrganizationsBriefVsFullRepresentation() { |
705 | 709 | fullOrgsGlobal.get(0).getAttributes().containsKey("testAttribute")); |
706 | 710 | } |
707 | 711 |
|
| 712 | + @Test |
| 713 | + public void testGetMemberOrganizationsForbiddenForNonAdminUser() throws Exception { |
| 714 | + // create 2 orgs |
| 715 | + OrganizationRepresentation orgA = createOrganization("orga"); |
| 716 | + OrganizationRepresentation orgB = createOrganization("orgb"); |
| 717 | + |
| 718 | + // create userA and add as member of both orgs |
| 719 | + OrganizationResource orgAResource = testRealm().organizations().get(orgA.getId()); |
| 720 | + OrganizationResource orgBResource = testRealm().organizations().get(orgB.getId()); |
| 721 | + UserRepresentation userA = addMember(orgAResource, "usera@orga.org"); |
| 722 | + orgBResource.members().addMember(userA.getId()).close(); |
| 723 | + |
| 724 | + // create userB (non-admin user) |
| 725 | + UserRepresentation userB = UserBuilder.create() |
| 726 | + .username("userb") |
| 727 | + .password("password") |
| 728 | + .enabled(true) |
| 729 | + .build(); |
| 730 | + try (Response response = testRealm().users().create(userB)) { |
| 731 | + userB.setId(ApiUtil.getCreatedId(response)); |
| 732 | + } |
| 733 | + getCleanup().addCleanup(() -> testRealm().users().get(userB.getId()).remove()); |
| 734 | + |
| 735 | + // send request as userB to OrganizationsResource.getOrganizations with member-id = userA |
| 736 | + try (Keycloak userBClient = AdminClientUtil.createAdminClient(suiteContext.isAdapterCompatTesting(), |
| 737 | + TEST_REALM_NAME, "userb", "password", Constants.ADMIN_CLI_CLIENT_ID, null)) { |
| 738 | + userBClient.realm(TEST_REALM_NAME).organizations().members().getOrganizations(userA.getId(), true); |
| 739 | + fail("Expected ForbiddenException"); |
| 740 | + } catch (ForbiddenException expected) { |
| 741 | + } |
| 742 | + } |
| 743 | + |
708 | 744 | private void loginViaNonOrgIdP(String idpAlias) { |
709 | 745 | oauth.clientId("broker-app"); |
710 | 746 | loginPage.open(bc.consumerRealmName()); |
|
0 commit comments