use of org.apache.syncope.core.persistence.api.entity.Realm in project syncope by apache.
the class JPAAnyObjectDAO method countByRealm.
@Override
public Map<String, Integer> countByRealm(final AnyType anyType) {
Query query = entityManager().createQuery("SELECT e.realm, COUNT(e) FROM " + JPAAnyObject.class.getSimpleName() + " e " + "WHERE e.type=:type GROUP BY e.realm");
query.setParameter("type", anyType);
@SuppressWarnings("unchecked") List<Object[]> results = query.getResultList();
return results.stream().collect(Collectors.toMap(result -> ((Realm) result[0]).getFullPath(), result -> ((Number) result[1]).intValue()));
}
use of org.apache.syncope.core.persistence.api.entity.Realm in project syncope by apache.
the class RealmTest method save.
@Test
public void save() {
Realm realm = entityFactory.newEntity(Realm.class);
realm.setName("last");
realm.setParent(realmDAO.findByFullPath("/even/two"));
assertNull(realm.getKey());
Realm actual = realmDAO.save(realm);
assertNotNull(actual.getKey());
assertEquals("last", actual.getName());
assertEquals("/even/two/last", actual.getFullPath());
assertEquals(realmDAO.findByFullPath("/even/two"), actual.getParent());
assertEquals("20ab5a8c-4b0c-432c-b957-f7fb9784d9f7", realm.getAccountPolicy().getKey());
assertEquals("ce93fcda-dc3a-4369-a7b0-a6108c261c85", realm.getPasswordPolicy().getKey());
realm = actual;
realm.setAccountPolicy((AccountPolicy) policyDAO.find("06e2ed52-6966-44aa-a177-a0ca7434201f"));
realm.setPasswordPolicy((PasswordPolicy) policyDAO.find("986d1236-3ac5-4a19-810c-5ab21d79cba1"));
actual = realmDAO.save(realm);
assertEquals("06e2ed52-6966-44aa-a177-a0ca7434201f", actual.getAccountPolicy().getKey());
assertEquals("986d1236-3ac5-4a19-810c-5ab21d79cba1", actual.getPasswordPolicy().getKey());
}
use of org.apache.syncope.core.persistence.api.entity.Realm in project syncope by apache.
the class DefaultRealmPullResultHandler method create.
private void create(final RealmTO realmTO, final SyncDelta delta, final String operation, final ProvisioningReport result) throws JobExecutionException {
Object output;
Result resultStatus;
try {
Realm realm = realmDAO.save(binder.create(profile.getTask().getDestinatioRealm(), realmTO));
PropagationByResource propByRes = new PropagationByResource();
for (String resource : realm.getResourceKeys()) {
propByRes.add(ResourceOperation.CREATE, resource);
}
List<PropagationTaskTO> tasks = propagationManager.createTasks(realm, propByRes, null);
taskExecutor.execute(tasks, false);
RealmTO actual = binder.getRealmTO(realm, true);
result.setKey(actual.getKey());
result.setName(profile.getTask().getDestinatioRealm().getFullPath() + "/" + actual.getName());
output = actual;
resultStatus = Result.SUCCESS;
for (PullActions action : profile.getActions()) {
action.after(profile, delta, actual, result);
}
LOG.debug("Realm {} successfully created", actual.getKey());
} catch (PropagationException e) {
// A propagation failure doesn't imply a pull failure.
// The propagation exception status will be reported into the propagation task execution.
LOG.error("Could not propagate Realm {}", delta.getUid().getUidValue(), e);
output = e;
resultStatus = Result.FAILURE;
} catch (Exception e) {
throwIgnoreProvisionException(delta, e);
result.setStatus(ProvisioningReport.Status.FAILURE);
result.setMessage(ExceptionUtils.getRootCauseMessage(e));
LOG.error("Could not create Realm {} ", delta.getUid().getUidValue(), e);
output = e;
resultStatus = Result.FAILURE;
}
finalize(operation, resultStatus, null, output, delta);
}
use of org.apache.syncope.core.persistence.api.entity.Realm in project syncope by apache.
the class DefaultRealmPullResultHandler method deprovision.
private List<ProvisioningReport> deprovision(final SyncDelta delta, final List<String> keys, final boolean unlink) throws JobExecutionException {
if (!profile.getTask().isPerformUpdate()) {
LOG.debug("PullTask not configured for update");
finalize(unlink ? MatchingRule.toEventName(MatchingRule.UNASSIGN) : MatchingRule.toEventName(MatchingRule.DEPROVISION), Result.SUCCESS, null, null, delta);
return Collections.<ProvisioningReport>emptyList();
}
LOG.debug("About to deprovision {}", keys);
final List<ProvisioningReport> results = new ArrayList<>();
for (String key : keys) {
LOG.debug("About to unassign resource {}", key);
ProvisioningReport result = new ProvisioningReport();
result.setOperation(ResourceOperation.DELETE);
result.setAnyType(REALM_TYPE);
result.setStatus(ProvisioningReport.Status.SUCCESS);
result.setKey(key);
Realm realm = realmDAO.find(key);
RealmTO before = binder.getRealmTO(realm, true);
if (before == null) {
result.setStatus(ProvisioningReport.Status.FAILURE);
result.setMessage(String.format("Realm '%s' not found", key));
} else {
result.setName(before.getFullPath());
}
if (!profile.isDryRun()) {
Object output;
Result resultStatus;
if (before == null) {
resultStatus = Result.FAILURE;
output = null;
} else {
try {
if (unlink) {
for (PullActions action : profile.getActions()) {
action.beforeUnassign(profile, delta, before);
}
} else {
for (PullActions action : profile.getActions()) {
action.beforeDeprovision(profile, delta, before);
}
}
PropagationByResource propByRes = new PropagationByResource();
propByRes.add(ResourceOperation.DELETE, profile.getTask().getResource().getKey());
taskExecutor.execute(propagationManager.createTasks(realm, propByRes, null), false);
if (unlink) {
realm.getResources().remove(profile.getTask().getResource());
output = binder.getRealmTO(realmDAO.save(realm), true);
} else {
output = binder.getRealmTO(realm, true);
}
for (PullActions action : profile.getActions()) {
action.after(profile, delta, RealmTO.class.cast(output), result);
}
resultStatus = Result.SUCCESS;
LOG.debug("{} successfully updated", realm);
} catch (PropagationException e) {
// A propagation failure doesn't imply a pull failure.
// The propagation exception status will be reported into the propagation task execution.
LOG.error("Could not propagate Realm {}", delta.getUid().getUidValue(), e);
output = e;
resultStatus = Result.FAILURE;
} catch (Exception e) {
throwIgnoreProvisionException(delta, e);
result.setStatus(ProvisioningReport.Status.FAILURE);
result.setMessage(ExceptionUtils.getRootCauseMessage(e));
LOG.error("Could not update Realm {}", delta.getUid().getUidValue(), e);
output = e;
resultStatus = Result.FAILURE;
}
}
finalize(unlink ? MatchingRule.toEventName(MatchingRule.UNASSIGN) : MatchingRule.toEventName(MatchingRule.DEPROVISION), resultStatus, before, output, delta);
}
results.add(result);
}
return results;
}
use of org.apache.syncope.core.persistence.api.entity.Realm in project syncope by apache.
the class DefaultRealmPullResultHandler method delete.
private List<ProvisioningReport> delete(final SyncDelta delta, final List<String> keys) throws JobExecutionException {
if (!profile.getTask().isPerformDelete()) {
LOG.debug("PullTask not configured for delete");
finalize(ResourceOperation.DELETE.name().toLowerCase(), Result.SUCCESS, null, null, delta);
return Collections.<ProvisioningReport>emptyList();
}
LOG.debug("About to delete {}", keys);
List<ProvisioningReport> results = new ArrayList<>();
for (String key : keys) {
Object output;
Result resultStatus = Result.FAILURE;
ProvisioningReport result = new ProvisioningReport();
try {
result.setKey(key);
result.setOperation(ResourceOperation.DELETE);
result.setAnyType(REALM_TYPE);
result.setStatus(ProvisioningReport.Status.SUCCESS);
Realm realm = realmDAO.find(key);
RealmTO before = binder.getRealmTO(realm, true);
if (before == null) {
result.setStatus(ProvisioningReport.Status.FAILURE);
result.setMessage(String.format("Realm '%s' not found", key));
} else {
result.setName(before.getFullPath());
}
if (!profile.isDryRun()) {
for (PullActions action : profile.getActions()) {
action.beforeDelete(profile, delta, before);
}
try {
if (!realmDAO.findChildren(realm).isEmpty()) {
throw SyncopeClientException.build(ClientExceptionType.HasChildren);
}
Set<String> adminRealms = Collections.singleton(realm.getFullPath());
AnyCond keyCond = new AnyCond(AttributeCond.Type.ISNOTNULL);
keyCond.setSchema("key");
SearchCond allMatchingCond = SearchCond.getLeafCond(keyCond);
int users = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.USER);
int groups = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.GROUP);
int anyObjects = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.ANY_OBJECT);
if (users + groups + anyObjects > 0) {
SyncopeClientException containedAnys = SyncopeClientException.build(ClientExceptionType.AssociatedAnys);
containedAnys.getElements().add(users + " user(s)");
containedAnys.getElements().add(groups + " group(s)");
containedAnys.getElements().add(anyObjects + " anyObject(s)");
throw containedAnys;
}
PropagationByResource propByRes = new PropagationByResource();
for (String resource : realm.getResourceKeys()) {
propByRes.add(ResourceOperation.DELETE, resource);
}
List<PropagationTaskTO> tasks = propagationManager.createTasks(realm, propByRes, null);
taskExecutor.execute(tasks, false);
realmDAO.delete(realm);
output = null;
resultStatus = Result.SUCCESS;
for (PullActions action : profile.getActions()) {
action.after(profile, delta, before, result);
}
} catch (Exception e) {
throwIgnoreProvisionException(delta, e);
result.setStatus(ProvisioningReport.Status.FAILURE);
result.setMessage(ExceptionUtils.getRootCauseMessage(e));
LOG.error("Could not delete {}", realm, e);
output = e;
}
finalize(ResourceOperation.DELETE.name().toLowerCase(), resultStatus, before, output, delta);
}
results.add(result);
} catch (DelegatedAdministrationException e) {
LOG.error("Not allowed to read Realm {}", key, e);
} catch (Exception e) {
LOG.error("Could not delete Realm {}", key, e);
}
}
return results;
}
Aggregations