Search in sources :

Example 1 with PrivilegeTO

use of org.apache.syncope.common.lib.to.PrivilegeTO in project syncope by apache.

the class ApplicationDataBinderImpl method getPrivilegeTO.

@Override
public PrivilegeTO getPrivilegeTO(final Privilege privilege) {
    PrivilegeTO privilegeTO = new PrivilegeTO();
    privilegeTO.setKey(privilege.getKey());
    privilegeTO.setDescription(privilege.getDescription());
    privilegeTO.setApplication(privilege.getApplication().getKey());
    privilegeTO.setSpec(privilege.getSpec());
    return privilegeTO;
}
Also used : PrivilegeTO(org.apache.syncope.common.lib.to.PrivilegeTO)

Example 2 with PrivilegeTO

use of org.apache.syncope.common.lib.to.PrivilegeTO in project syncope by apache.

the class ApplicationITCase method read.

@Test
public void read() {
    ApplicationTO mightyApp = applicationService.read("mightyApp");
    assertNotNull(mightyApp);
    assertEquals(2, mightyApp.getPrivileges().size());
    assertTrue(mightyApp.getPrivileges().stream().anyMatch(privilege -> "postMighty".equals(privilege.getKey())));
    PrivilegeTO getMighty = applicationService.readPrivilege("getMighty");
    assertNotNull(getMighty);
    assertEquals("mightyApp", getMighty.getApplication());
    RoleTO role = roleService.read("Other");
    assertFalse(role.getPrivileges().isEmpty());
    assertEquals(1, role.getPrivileges().size());
    assertTrue(role.getPrivileges().stream().anyMatch(privilege -> "postMighty".equals(privilege)));
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RoleService(org.apache.syncope.common.rest.api.service.RoleService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) PrivilegeTO(org.apache.syncope.common.lib.to.PrivilegeTO) Test(org.junit.jupiter.api.Test) EntityTO(org.apache.syncope.common.lib.to.EntityTO) RoleTO(org.apache.syncope.common.lib.to.RoleTO) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Response(javax.ws.rs.core.Response) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) ApplicationService(org.apache.syncope.common.rest.api.service.ApplicationService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ApplicationTO(org.apache.syncope.common.lib.to.ApplicationTO) AbstractITCase(org.apache.syncope.fit.AbstractITCase) ApplicationTO(org.apache.syncope.common.lib.to.ApplicationTO) PrivilegeTO(org.apache.syncope.common.lib.to.PrivilegeTO) RoleTO(org.apache.syncope.common.lib.to.RoleTO) Test(org.junit.jupiter.api.Test)

Example 3 with PrivilegeTO

use of org.apache.syncope.common.lib.to.PrivilegeTO in project syncope by apache.

the class ApplicationDataBinderImpl method update.

@Override
public Application update(final Application toBeUpdated, final ApplicationTO applicationTO) {
    toBeUpdated.setKey(applicationTO.getKey());
    Application application = applicationDAO.save(toBeUpdated);
    application.setDescription(applicationTO.getDescription());
    // 1. add or update all (valid) privileges from TO
    applicationTO.getPrivileges().forEach(privilegeTO -> {
        if (privilegeTO == null) {
            LOG.error("Null {}", PrivilegeTO.class.getSimpleName());
        } else {
            Privilege privilege = applicationDAO.findPrivilege(privilegeTO.getKey());
            if (privilege == null) {
                privilege = entityFactory.newEntity(Privilege.class);
                privilege.setKey(privilegeTO.getKey());
                privilege.setApplication(application);
                application.add(privilege);
            } else if (!application.equals(privilege.getApplication())) {
                SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidPrivilege);
                sce.getElements().add("Privilege " + privilege.getKey() + " already owned by " + privilege.getApplication());
                throw sce;
            }
            privilege.setDescription(privilegeTO.getDescription());
            privilege.setSpec(privilegeTO.getSpec());
        }
    });
    // 2. remove all privileges not contained in the TO
    for (Iterator<? extends Privilege> itor = application.getPrivileges().iterator(); itor.hasNext(); ) {
        Privilege privilege = itor.next();
        if (!applicationTO.getPrivileges().stream().anyMatch(privilegeTO -> privilege.getKey().equals(privilegeTO.getKey()))) {
            privilege.setApplication(null);
            itor.remove();
        }
    }
    return application;
}
Also used : ApplicationDAO(org.apache.syncope.core.persistence.api.dao.ApplicationDAO) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) ApplicationDataBinder(org.apache.syncope.core.provisioning.api.data.ApplicationDataBinder) PrivilegeTO(org.apache.syncope.common.lib.to.PrivilegeTO) Application(org.apache.syncope.core.persistence.api.entity.Application) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) Component(org.springframework.stereotype.Component) Privilege(org.apache.syncope.core.persistence.api.entity.Privilege) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) ApplicationTO(org.apache.syncope.common.lib.to.ApplicationTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) Privilege(org.apache.syncope.core.persistence.api.entity.Privilege) Application(org.apache.syncope.core.persistence.api.entity.Application) PrivilegeTO(org.apache.syncope.common.lib.to.PrivilegeTO)

Example 4 with PrivilegeTO

use of org.apache.syncope.common.lib.to.PrivilegeTO in project syncope by apache.

the class ApplicationITCase method crud.

@Test
public void crud() {
    // 1. create application
    ApplicationTO application = new ApplicationTO();
    application.setKey(UUID.randomUUID().toString());
    PrivilegeTO privilegeTO = new PrivilegeTO();
    privilegeTO.setKey(UUID.randomUUID().toString());
    privilegeTO.setSpec("{ \"one\": true }");
    application.getPrivileges().add(privilegeTO);
    privilegeTO = new PrivilegeTO();
    privilegeTO.setKey(UUID.randomUUID().toString());
    privilegeTO.setSpec("{ \"two\": true }");
    application.getPrivileges().add(privilegeTO);
    privilegeTO = new PrivilegeTO();
    privilegeTO.setKey(UUID.randomUUID().toString());
    privilegeTO.setSpec("{ \"three\": true }");
    application.getPrivileges().add(privilegeTO);
    Response response = applicationService.create(application);
    assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
    application = getObject(response.getLocation(), ApplicationService.class, ApplicationTO.class);
    assertNotNull(application);
    assertNull(application.getDescription());
    assertEquals(3, application.getPrivileges().size());
    // 2. update application
    application.setDescription("A description");
    application.getPrivileges().remove(1);
    applicationService.update(application);
    application = applicationService.read(application.getKey());
    assertNotNull(application);
    assertNotNull(application.getDescription());
    assertEquals(2, application.getPrivileges().size());
    // 3. assign application's privileges to a new role
    RoleTO role = new RoleTO();
    role.setKey("privileged");
    role.getPrivileges().addAll(application.getPrivileges().stream().map(EntityTO::getKey).collect(Collectors.toList()));
    response = roleService.create(role);
    assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
    role = getObject(response.getLocation(), RoleService.class, RoleTO.class);
    assertNotNull(role);
    assertEquals(2, role.getPrivileges().size());
    // 4. delete application => delete privileges
    applicationService.delete(application.getKey());
    try {
        applicationService.read(application.getKey());
        fail("This should not happen");
    } catch (SyncopeClientException e) {
        assertEquals(ClientExceptionType.NotFound, e.getType());
    }
    role = roleService.read(role.getKey());
    assertNotNull(role);
    assertTrue(role.getPrivileges().isEmpty());
}
Also used : Response(javax.ws.rs.core.Response) EntityTO(org.apache.syncope.common.lib.to.EntityTO) RoleService(org.apache.syncope.common.rest.api.service.RoleService) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) ApplicationTO(org.apache.syncope.common.lib.to.ApplicationTO) PrivilegeTO(org.apache.syncope.common.lib.to.PrivilegeTO) RoleTO(org.apache.syncope.common.lib.to.RoleTO) ApplicationService(org.apache.syncope.common.rest.api.service.ApplicationService) Test(org.junit.jupiter.api.Test)

Aggregations

PrivilegeTO (org.apache.syncope.common.lib.to.PrivilegeTO)4 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)3 ApplicationTO (org.apache.syncope.common.lib.to.ApplicationTO)3 Collectors (java.util.stream.Collectors)2 Response (javax.ws.rs.core.Response)2 EntityTO (org.apache.syncope.common.lib.to.EntityTO)2 RoleTO (org.apache.syncope.common.lib.to.RoleTO)2 ClientExceptionType (org.apache.syncope.common.lib.types.ClientExceptionType)2 ApplicationService (org.apache.syncope.common.rest.api.service.ApplicationService)2 RoleService (org.apache.syncope.common.rest.api.service.RoleService)2 Test (org.junit.jupiter.api.Test)2 Iterator (java.util.Iterator)1 UUID (java.util.UUID)1 ApplicationDAO (org.apache.syncope.core.persistence.api.dao.ApplicationDAO)1 Application (org.apache.syncope.core.persistence.api.entity.Application)1 EntityFactory (org.apache.syncope.core.persistence.api.entity.EntityFactory)1 Privilege (org.apache.syncope.core.persistence.api.entity.Privilege)1 ApplicationDataBinder (org.apache.syncope.core.provisioning.api.data.ApplicationDataBinder)1 AbstractITCase (org.apache.syncope.fit.AbstractITCase)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1