Search in sources :

Example 1 with Privilege

use of org.apache.syncope.core.persistence.api.entity.Privilege in project syncope by apache.

the class RoleDataBinderImpl method update.

@Override
public Role update(final Role toBeUpdated, final RoleTO roleTO) {
    toBeUpdated.setKey(roleTO.getKey());
    Role role = roleDAO.save(toBeUpdated);
    role.getEntitlements().clear();
    role.getEntitlements().addAll(roleTO.getEntitlements());
    role.getRealms().clear();
    for (String realmFullPath : roleTO.getRealms()) {
        Realm realm = realmDAO.findByFullPath(realmFullPath);
        if (realm == null) {
            LOG.debug("Invalid realm full path {}, ignoring", realmFullPath);
        } else {
            role.add(realm);
        }
    }
    role.getDynRealms().clear();
    for (String key : roleTO.getDynRealms()) {
        DynRealm dynRealm = dynRealmDAO.find(key);
        if (dynRealm == null) {
            LOG.debug("Invalid dynamic ream {}, ignoring", key);
        } else {
            role.add(dynRealm);
        }
    }
    role = roleDAO.save(role);
    // dynamic membership
    roleDAO.clearDynMembers(role);
    if (role.getKey() == null && roleTO.getDynMembershipCond() != null) {
        setDynMembership(role, roleTO.getDynMembershipCond());
    } else if (role.getDynMembership() != null && roleTO.getDynMembershipCond() == null) {
        role.setDynMembership(null);
    } else if (role.getDynMembership() == null && roleTO.getDynMembershipCond() != null) {
        setDynMembership(role, roleTO.getDynMembershipCond());
    } else if (role.getDynMembership() != null && roleTO.getDynMembershipCond() != null && !role.getDynMembership().getFIQLCond().equals(roleTO.getDynMembershipCond())) {
        setDynMembership(role, roleTO.getDynMembershipCond());
    }
    role.getPrivileges().clear();
    for (String key : roleTO.getPrivileges()) {
        Privilege privilege = applicationDAO.findPrivilege(key);
        if (privilege == null) {
            LOG.debug("Invalid privilege {}, ignoring", key);
        } else {
            role.add(privilege);
        }
    }
    return role;
}
Also used : Role(org.apache.syncope.core.persistence.api.entity.Role) Privilege(org.apache.syncope.core.persistence.api.entity.Privilege) Realm(org.apache.syncope.core.persistence.api.entity.Realm) DynRealm(org.apache.syncope.core.persistence.api.entity.DynRealm) DynRealm(org.apache.syncope.core.persistence.api.entity.DynRealm)

Example 2 with Privilege

use of org.apache.syncope.core.persistence.api.entity.Privilege 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 3 with Privilege

use of org.apache.syncope.core.persistence.api.entity.Privilege in project syncope by apache.

the class ApplicationTest method crud.

@Test
public void crud() {
    // 1. create application
    Application application = entityFactory.newEntity(Application.class);
    application.setKey(UUID.randomUUID().toString());
    String privilege1Key = UUID.randomUUID().toString();
    Privilege privilege = entityFactory.newEntity(Privilege.class);
    privilege.setKey(privilege1Key);
    privilege.setSpec("{ \"one\": true }");
    application.add(privilege);
    String privilege2Key = UUID.randomUUID().toString();
    privilege = entityFactory.newEntity(Privilege.class);
    privilege.setKey(privilege2Key);
    privilege.setSpec("{ \"two\": true }");
    application.add(privilege);
    String privilege3Key = UUID.randomUUID().toString();
    privilege = entityFactory.newEntity(Privilege.class);
    privilege.setKey(privilege3Key);
    privilege.setSpec("{ \"three\": true }");
    application.add(privilege);
    application = applicationDAO.save(application);
    assertNotNull(application);
    assertNull(application.getDescription());
    assertEquals(3, application.getPrivileges().size());
    // 2. update application
    application.setDescription("A description");
    Privilege priv3 = applicationDAO.findPrivilege(privilege3Key);
    priv3.setApplication(null);
    application.getPrivileges().remove(priv3);
    assertEquals(2, application.getPrivileges().size());
    applicationDAO.save(application);
    applicationDAO.flush();
    application = applicationDAO.find(application.getKey());
    assertNotNull(application);
    assertNotNull(application.getDescription());
    assertEquals(2, application.getPrivileges().size());
    // 3. delete application
    applicationDAO.delete(application);
    applicationDAO.flush();
    assertNull(applicationDAO.find(application.getKey()));
    assertNull(applicationDAO.findPrivilege(privilege1Key));
    assertNull(applicationDAO.findPrivilege(privilege2Key));
}
Also used : Privilege(org.apache.syncope.core.persistence.api.entity.Privilege) Application(org.apache.syncope.core.persistence.api.entity.Application) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 4 with Privilege

use of org.apache.syncope.core.persistence.api.entity.Privilege in project syncope by apache.

the class ApplicationTest method find.

@Test
public void find() {
    Application mightyApp = applicationDAO.find("mightyApp");
    assertNotNull(mightyApp);
    assertEquals(2, mightyApp.getPrivileges().size());
    Privilege getMighty = applicationDAO.findPrivilege("getMighty");
    assertNotNull(getMighty);
    assertEquals(getMighty, mightyApp.getPrivilege("getMighty").get());
}
Also used : Privilege(org.apache.syncope.core.persistence.api.entity.Privilege) Application(org.apache.syncope.core.persistence.api.entity.Application) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Aggregations

Privilege (org.apache.syncope.core.persistence.api.entity.Privilege)4 Application (org.apache.syncope.core.persistence.api.entity.Application)3 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)2 Test (org.junit.jupiter.api.Test)2 Iterator (java.util.Iterator)1 Collectors (java.util.stream.Collectors)1 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)1 ApplicationTO (org.apache.syncope.common.lib.to.ApplicationTO)1 PrivilegeTO (org.apache.syncope.common.lib.to.PrivilegeTO)1 ClientExceptionType (org.apache.syncope.common.lib.types.ClientExceptionType)1 ApplicationDAO (org.apache.syncope.core.persistence.api.dao.ApplicationDAO)1 DynRealm (org.apache.syncope.core.persistence.api.entity.DynRealm)1 EntityFactory (org.apache.syncope.core.persistence.api.entity.EntityFactory)1 Realm (org.apache.syncope.core.persistence.api.entity.Realm)1 Role (org.apache.syncope.core.persistence.api.entity.Role)1 ApplicationDataBinder (org.apache.syncope.core.provisioning.api.data.ApplicationDataBinder)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Component (org.springframework.stereotype.Component)1