Search in sources :

Example 21 with Organization

use of org.eclipse.che.multiuser.organization.shared.model.Organization in project che-server by eclipse-che.

the class OrganizationResourceDistributionServicePermissionsFilter method filter.

@Override
protected void filter(GenericResourceMethod genericMethodResource, Object[] arguments) throws ApiException {
    final String methodName = genericMethodResource.getMethod().getName();
    final Subject currentSubject = EnvironmentContext.getCurrent().getSubject();
    String organizationId;
    switch(methodName) {
        case GET_RESOURCES_CAP_METHOD:
            if (superPrivilegesChecker.hasSuperPrivileges()) {
                // user is able to see information about all organizations
                return;
            }
        // fall through
        case CAP_RESOURCES_METHOD:
            // we should check permissions on parent organization level
            Organization organization = organizationManager.getById((String) arguments[0]);
            organizationId = organization.getParent();
            if (organizationId == null) {
                // requested organization is root so manager should throw exception
                return;
            }
            break;
        case GET_DISTRIBUTED_RESOURCES:
            organizationId = (String) arguments[0];
            // get organization to ensure that organization exists
            organizationManager.getById(organizationId);
            if (superPrivilegesChecker.hasSuperPrivileges()) {
                // user is able to see information about all organizations
                return;
            }
            break;
        default:
            throw new ForbiddenException("The user does not have permission to perform this operation");
    }
    if (!currentSubject.hasPermission(OrganizationDomain.DOMAIN_ID, organizationId, OrganizationDomain.MANAGE_RESOURCES)) {
        throw new ForbiddenException("The user does not have permission to manage resources of organization with id '" + organizationId + "'");
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) Organization(org.eclipse.che.multiuser.organization.shared.model.Organization) Subject(org.eclipse.che.commons.subject.Subject)

Example 22 with Organization

use of org.eclipse.che.multiuser.organization.shared.model.Organization in project devspaces-images by redhat-developer.

the class OrganizationManagerTest method shouldThrowConflictExceptionOnCreationIfOrganizationNameIsReserved.

@Test(expectedExceptions = ConflictException.class)
public void shouldThrowConflictExceptionOnCreationIfOrganizationNameIsReserved() throws Exception {
    final Organization organization = DtoFactory.newDto(OrganizationDto.class).withName("reserved").withParent(null);
    manager.create(organization);
}
Also used : Organization(org.eclipse.che.multiuser.organization.shared.model.Organization) Test(org.testng.annotations.Test)

Example 23 with Organization

use of org.eclipse.che.multiuser.organization.shared.model.Organization in project devspaces-images by redhat-developer.

the class OrganizationManagerTest method shouldGenerateIdentifierWhenCreatingOrganization.

@Test
public void shouldGenerateIdentifierWhenCreatingOrganization() throws Exception {
    final Organization organization = DtoFactory.newDto(OrganizationDto.class).withName("newOrg").withId("identifier");
    manager.create(organization);
    verify(organizationDao).create(organizationCaptor.capture());
    final String id = organizationCaptor.getValue().getId();
    assertNotNull(id);
    assertNotEquals(id, "identifier");
}
Also used : Organization(org.eclipse.che.multiuser.organization.shared.model.Organization) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.testng.annotations.Test)

Example 24 with Organization

use of org.eclipse.che.multiuser.organization.shared.model.Organization in project devspaces-images by redhat-developer.

the class OrganizationManagerTest method shouldGetOrganizationByName.

@Test
public void shouldGetOrganizationByName() throws Exception {
    final OrganizationImpl toFetch = new OrganizationImpl("org123", "toFetchOrg", "org321");
    when(organizationDao.getByName(eq("org123"))).thenReturn(toFetch);
    final Organization fetched = manager.getByName("org123");
    assertEquals(fetched, toFetch);
    verify(organizationDao).getByName("org123");
}
Also used : Organization(org.eclipse.che.multiuser.organization.shared.model.Organization) OrganizationImpl(org.eclipse.che.multiuser.organization.spi.impl.OrganizationImpl) Test(org.testng.annotations.Test)

Example 25 with Organization

use of org.eclipse.che.multiuser.organization.shared.model.Organization in project devspaces-images by redhat-developer.

the class OrganizationPermissionsFilter method filter.

@Override
protected void filter(GenericResourceMethod genericMethodResource, Object[] arguments) throws ApiException {
    final String methodName = genericMethodResource.getMethod().getName();
    final Subject currentSubject = EnvironmentContext.getCurrent().getSubject();
    String action;
    String organizationId;
    switch(methodName) {
        case CREATE_METHOD:
            final OrganizationDto organization = (OrganizationDto) arguments[0];
            if (organization.getParent() != null) {
                organizationId = organization.getParent();
                action = OrganizationDomain.MANAGE_SUBORGANIZATIONS;
                break;
            }
            // anybody can create root organization
            return;
        case UPDATE_METHOD:
            organizationId = ((String) arguments[0]);
            action = OrganizationDomain.UPDATE;
            break;
        case REMOVE_METHOD:
            organizationId = ((String) arguments[0]);
            action = OrganizationDomain.DELETE;
            break;
        case GET_BY_PARENT_METHOD:
            organizationId = ((String) arguments[0]);
            action = OrganizationDomain.MANAGE_SUBORGANIZATIONS;
            if (superPrivilegesChecker.hasSuperPrivileges()) {
                return;
            }
            break;
        case GET_ORGANIZATIONS_METHOD:
            final String userId = (String) arguments[0];
            if (userId != null && !userId.equals(currentSubject.getUserId()) && !superPrivilegesChecker.hasSuperPrivileges()) {
                throw new ForbiddenException("The user is able to specify only his own id");
            }
            // user specified his user id or has super privileges
            return;
        // methods accessible to every user
        case GET_BY_ID_METHOD:
        case FIND_METHOD:
            return;
        default:
            throw new ForbiddenException("The user does not have permission to perform this operation");
    }
    // user is not admin and it is need to check permissions on organization instance level
    final Organization organization = manager.getById(organizationId);
    final String parentOrganizationId = organization.getParent();
    // check permissions on parent organization level when updating or removing child organization
    if (parentOrganizationId != null && (OrganizationDomain.UPDATE.equals(action) || OrganizationDomain.DELETE.equals(action))) {
        if (currentSubject.hasPermission(OrganizationDomain.DOMAIN_ID, parentOrganizationId, MANAGE_SUBORGANIZATIONS)) {
            // user has permissions to manage organization on parent organization level
            return;
        }
    }
    if (!currentSubject.hasPermission(DOMAIN_ID, organizationId, action)) {
        throw new ForbiddenException("The user does not have permission to " + action + " organization with id '" + organizationId + "'");
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) Organization(org.eclipse.che.multiuser.organization.shared.model.Organization) Subject(org.eclipse.che.commons.subject.Subject) OrganizationDto(org.eclipse.che.multiuser.organization.shared.dto.OrganizationDto)

Aggregations

Organization (org.eclipse.che.multiuser.organization.shared.model.Organization)28 Test (org.testng.annotations.Test)18 OrganizationImpl (org.eclipse.che.multiuser.organization.spi.impl.OrganizationImpl)16 OrganizationDto (org.eclipse.che.multiuser.organization.shared.dto.OrganizationDto)8 Response (io.restassured.response.Response)4 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)4 Subject (org.eclipse.che.commons.subject.Subject)4 MemberImpl (org.eclipse.che.multiuser.organization.spi.impl.MemberImpl)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Transactional (com.google.inject.persist.Transactional)2 ArrayList (java.util.ArrayList)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 ServerException (org.eclipse.che.api.core.ServerException)2 OrganizationPersistedEvent (org.eclipse.che.multiuser.organization.api.event.OrganizationPersistedEvent)2 ResourceManager (org.eclipse.che.multiuser.resource.api.usage.ResourceManager)2 Resource (org.eclipse.che.multiuser.resource.model.Resource)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2