use of org.apache.syncope.core.persistence.api.entity.AnyUtils in project syncope by apache.
the class JPAAnyUtilsFactory method getInstance.
@Override
public AnyUtils getInstance(final AnyTypeKind anyTypeKind) {
AnyUtils instance;
synchronized (instances) {
instance = instances.get(anyTypeKind);
if (instance == null) {
instance = new JPAAnyUtils(anyTypeKind);
ApplicationContextProvider.getBeanFactory().autowireBean(instance);
instances.put(anyTypeKind, instance);
}
}
return instance;
}
use of org.apache.syncope.core.persistence.api.entity.AnyUtils in project syncope by apache.
the class AzurePropagationActions method after.
@Transactional
@Override
public void after(final PropagationTask task, final TaskExec execution, final ConnectorObject afterObj) {
if (task.getOperation() == ResourceOperation.DELETE || task.getOperation() == ResourceOperation.NONE) {
return;
}
if (AnyTypeKind.USER.equals(task.getAnyTypeKind())) {
User user = userDAO.find(task.getEntityKey());
if (user == null) {
LOG.error("Could not find user {}, skipping", task.getEntityKey());
} else {
boolean modified = false;
AnyUtils anyUtils = anyUtilsFactory.getInstance(user);
// Azure User ID
PlainSchema azureId = plainSchemaDAO.find(getAzureIdSchema());
if (azureId == null) {
LOG.error("Could not find schema {}, skipping", getAzureIdSchema());
} else {
// set back the __UID__ received by Azure
UPlainAttr attr = user.getPlainAttr(getAzureIdSchema()).orElse(null);
if (attr == null) {
attr = entityFactory.newEntity(UPlainAttr.class);
attr.setSchema(azureId);
attr.setOwner(user);
user.add(attr);
try {
attr.add(afterObj.getUid().getUidValue(), anyUtils);
modified = true;
} catch (InvalidPlainAttrValueException e) {
LOG.error("Invalid value for attribute {}: {}", azureId.getKey(), afterObj.getUid().getUidValue(), e);
}
} else {
LOG.debug("User {} has already {} assigned: {}", user, azureId.getKey(), attr.getValuesAsStrings());
}
}
if (modified) {
userDAO.save(user);
}
}
} else if (AnyTypeKind.GROUP.equals(task.getAnyTypeKind())) {
Group group = groupDAO.find(task.getEntityKey());
if (group == null) {
LOG.error("Could not find group {}, skipping", task.getEntityKey());
} else {
boolean modified = false;
AnyUtils anyUtils = anyUtilsFactory.getInstance(group);
// Azure Group ID
PlainSchema azureId = plainSchemaDAO.find(getAzureGroupIdSchema());
if (azureId == null) {
LOG.error("Could not find schema {}, skipping", getAzureGroupIdSchema());
} else {
// set back the __UID__ received by Azure
GPlainAttr attr = group.getPlainAttr(getAzureGroupIdSchema()).orElse(null);
if (attr == null) {
attr = entityFactory.newEntity(GPlainAttr.class);
attr.setSchema(azureId);
attr.setOwner(group);
group.add(attr);
try {
attr.add(afterObj.getUid().getUidValue(), anyUtils);
modified = true;
} catch (InvalidPlainAttrValueException e) {
LOG.error("Invalid value for attribute {}: {}", azureId.getKey(), afterObj.getUid().getUidValue(), e);
}
} else {
LOG.debug("Group {} has already {} assigned: {}", group, azureId.getKey(), attr.getValuesAsStrings());
}
}
if (modified) {
groupDAO.save(group);
}
}
}
}
use of org.apache.syncope.core.persistence.api.entity.AnyUtils in project syncope by apache.
the class AbstractPushResultHandler method doHandle.
protected void doHandle(final Any<?> any) throws JobExecutionException {
AnyUtils anyUtils = anyUtilsFactory.getInstance(any);
ProvisioningReport result = new ProvisioningReport();
profile.getResults().add(result);
result.setKey(any.getKey());
result.setAnyType(any.getType().getKey());
result.setName(getName(any));
Boolean enabled = any instanceof User && profile.getTask().isSyncStatus() ? ((User) any).isSuspended() ? Boolean.FALSE : Boolean.TRUE : null;
LOG.debug("Propagating {} with key {} towards {}", anyUtils.getAnyTypeKind(), any.getKey(), profile.getTask().getResource());
Object output = null;
Result resultStatus = null;
// Try to read remote object BEFORE any actual operation
Optional<? extends Provision> provision = profile.getTask().getResource().getProvision(any.getType());
Optional<MappingItem> connObjectKey = MappingUtils.getConnObjectKeyItem(provision.get());
Optional<String> connObjecKeyValue = mappingManager.getConnObjectKeyValue(any, provision.get());
ConnectorObject beforeObj = null;
if (connObjectKey.isPresent() && connObjecKeyValue.isPresent()) {
beforeObj = getRemoteObject(provision.get().getObjectClass(), connObjectKey.get().getExtAttrName(), connObjecKeyValue.get(), provision.get().getMapping().getItems().iterator());
} else {
LOG.debug("ConnObjectKeyItem {} or its value {} are null", connObjectKey, connObjecKeyValue);
}
Boolean status = profile.getTask().isSyncStatus() ? enabled : null;
if (profile.isDryRun()) {
if (beforeObj == null) {
result.setOperation(toResourceOperation(profile.getTask().getUnmatchingRule()));
} else {
result.setOperation(toResourceOperation(profile.getTask().getMatchingRule()));
}
result.setStatus(ProvisioningReport.Status.SUCCESS);
} else {
String operation = beforeObj == null ? UnmatchingRule.toEventName(profile.getTask().getUnmatchingRule()) : MatchingRule.toEventName(profile.getTask().getMatchingRule());
boolean notificationsAvailable = notificationManager.notificationsAvailable(AuditElements.EventCategoryType.PUSH, any.getType().getKind().name().toLowerCase(), profile.getTask().getResource().getKey(), operation);
boolean auditRequested = auditManager.auditRequested(AuditElements.EventCategoryType.PUSH, any.getType().getKind().name().toLowerCase(), profile.getTask().getResource().getKey(), operation);
try {
if (beforeObj == null) {
result.setOperation(toResourceOperation(profile.getTask().getUnmatchingRule()));
switch(profile.getTask().getUnmatchingRule()) {
case ASSIGN:
for (PushActions action : profile.getActions()) {
action.beforeAssign(profile, any);
}
if (!profile.getTask().isPerformCreate()) {
LOG.debug("PushTask not configured for create");
result.setStatus(ProvisioningReport.Status.IGNORE);
} else {
assign(any, status, result);
}
break;
case PROVISION:
for (PushActions action : profile.getActions()) {
action.beforeProvision(profile, any);
}
if (!profile.getTask().isPerformCreate()) {
LOG.debug("PushTask not configured for create");
result.setStatus(ProvisioningReport.Status.IGNORE);
} else {
provision(any, status, result);
}
break;
case UNLINK:
for (PushActions action : profile.getActions()) {
action.beforeUnlink(profile, any);
}
if (!profile.getTask().isPerformUpdate()) {
LOG.debug("PushTask not configured for update");
result.setStatus(ProvisioningReport.Status.IGNORE);
} else {
link(any, true, result);
}
break;
case IGNORE:
LOG.debug("Ignored any: {}", any);
result.setStatus(ProvisioningReport.Status.IGNORE);
break;
default:
}
} else {
result.setOperation(toResourceOperation(profile.getTask().getMatchingRule()));
switch(profile.getTask().getMatchingRule()) {
case UPDATE:
for (PushActions action : profile.getActions()) {
action.beforeUpdate(profile, any);
}
if (!profile.getTask().isPerformUpdate()) {
LOG.debug("PushTask not configured for update");
result.setStatus(ProvisioningReport.Status.IGNORE);
} else {
update(any, result);
}
break;
case DEPROVISION:
for (PushActions action : profile.getActions()) {
action.beforeDeprovision(profile, any);
}
if (!profile.getTask().isPerformDelete()) {
LOG.debug("PushTask not configured for delete");
result.setStatus(ProvisioningReport.Status.IGNORE);
} else {
deprovision(any, result);
}
break;
case UNASSIGN:
for (PushActions action : profile.getActions()) {
action.beforeUnassign(profile, any);
}
if (!profile.getTask().isPerformDelete()) {
LOG.debug("PushTask not configured for delete");
result.setStatus(ProvisioningReport.Status.IGNORE);
} else {
unassign(any, result);
}
break;
case LINK:
for (PushActions action : profile.getActions()) {
action.beforeLink(profile, any);
}
if (!profile.getTask().isPerformUpdate()) {
LOG.debug("PushTask not configured for update");
result.setStatus(ProvisioningReport.Status.IGNORE);
} else {
link(any, false, result);
}
break;
case UNLINK:
for (PushActions action : profile.getActions()) {
action.beforeUnlink(profile, any);
}
if (!profile.getTask().isPerformUpdate()) {
LOG.debug("PushTask not configured for update");
result.setStatus(ProvisioningReport.Status.IGNORE);
} else {
link(any, true, result);
}
break;
case IGNORE:
LOG.debug("Ignored any: {}", any);
result.setStatus(ProvisioningReport.Status.IGNORE);
break;
default:
}
}
for (PushActions action : profile.getActions()) {
action.after(profile, any, result);
}
if (result.getStatus() == null) {
result.setStatus(ProvisioningReport.Status.SUCCESS);
}
resultStatus = AuditElements.Result.SUCCESS;
if (connObjectKey.isPresent() && connObjecKeyValue.isPresent()) {
output = getRemoteObject(provision.get().getObjectClass(), connObjectKey.get().getExtAttrName(), connObjecKeyValue.get(), provision.get().getMapping().getItems().iterator());
}
} catch (IgnoreProvisionException e) {
throw e;
} catch (Exception e) {
result.setStatus(ProvisioningReport.Status.FAILURE);
result.setMessage(ExceptionUtils.getRootCauseMessage(e));
resultStatus = AuditElements.Result.FAILURE;
output = e;
LOG.warn("Error pushing {} towards {}", any, profile.getTask().getResource(), e);
for (PushActions action : profile.getActions()) {
action.onError(profile, any, result, e);
}
throw new JobExecutionException(e);
} finally {
if (notificationsAvailable || auditRequested) {
Map<String, Object> jobMap = new HashMap<>();
jobMap.put(AfterHandlingEvent.JOBMAP_KEY, new AfterHandlingEvent(AuditElements.EventCategoryType.PUSH, any.getType().getKind().name().toLowerCase(), profile.getTask().getResource().getKey(), operation, resultStatus, beforeObj, output, any));
AfterHandlingJob.schedule(scheduler, jobMap);
}
}
}
}
use of org.apache.syncope.core.persistence.api.entity.AnyUtils in project syncope by apache.
the class PullUtils method match.
public Optional<String> match(final AnyType anyType, final String name, final ExternalResource resource, final Connector connector) {
Optional<? extends Provision> provision = resource.getProvision(anyType);
if (!provision.isPresent()) {
return Optional.empty();
}
Optional<String> result = Optional.empty();
AnyUtils anyUtils = anyUtilsFactory.getInstance(anyType.getKind());
final List<ConnectorObject> found = new ArrayList<>();
connector.search(provision.get().getObjectClass(), new EqualsFilter(new Name(name)), obj -> found.add(obj), MappingUtils.buildOperationOptions(MappingUtils.getPullItems(provision.get().getMapping().getItems()).iterator()));
if (found.isEmpty()) {
LOG.debug("No {} found on {} with __NAME__ {}", provision.get().getObjectClass(), resource, name);
} else {
if (found.size() > 1) {
LOG.warn("More than one {} found on {} with __NAME__ {} - taking first only", provision.get().getObjectClass(), resource, name);
}
ConnectorObject connObj = found.iterator().next();
try {
List<String> anyKeys = match(connObj, provision.get(), anyUtils);
if (anyKeys.isEmpty()) {
LOG.debug("No matching {} found for {}, aborting", anyUtils.getAnyTypeKind(), connObj);
} else {
if (anyKeys.size() > 1) {
LOG.warn("More than one {} found {} - taking first only", anyUtils.getAnyTypeKind(), anyKeys);
}
result = Optional.ofNullable(anyKeys.iterator().next());
}
} catch (IllegalArgumentException e) {
LOG.warn(e.getMessage());
}
}
return result;
}
use of org.apache.syncope.core.persistence.api.entity.AnyUtils in project syncope by apache.
the class ReconciliationReportlet method doExtract.
private void doExtract(final ContentHandler handler, final List<? extends Any<?>> anys) throws SAXException, ReportException {
final Set<Missing> missing = new HashSet<>();
final Set<Misaligned> misaligned = new HashSet<>();
for (Any<?> any : anys) {
missing.clear();
misaligned.clear();
AnyUtils anyUtils = anyUtilsFactory.getInstance(any);
anyUtils.getAllResources(any).forEach(resource -> {
Provision provision = resource.getProvision(any.getType()).orElse(null);
Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(provision);
final String connObjectKeyValue = connObjectKeyItem.isPresent() ? mappingManager.getConnObjectKeyValue(any, provision).get() : StringUtils.EMPTY;
if (provision != null && connObjectKeyItem.isPresent() && StringUtils.isNotBlank(connObjectKeyValue)) {
// 1. read from the underlying connector
Connector connector = connFactory.getConnector(resource);
ConnectorObject connectorObject = connector.getObject(provision.getObjectClass(), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue), MappingUtils.buildOperationOptions(provision.getMapping().getItems().iterator()));
if (connectorObject == null) {
// 2. not found on resource?
LOG.error("Object {} with class {} not found on resource {}", connObjectKeyValue, provision.getObjectClass(), resource);
missing.add(new Missing(resource.getKey(), connObjectKeyValue));
} else {
// 3. found but misaligned?
Pair<String, Set<Attribute>> preparedAttrs = mappingManager.prepareAttrs(any, null, false, null, provision);
preparedAttrs.getRight().add(AttributeBuilder.build(Uid.NAME, preparedAttrs.getLeft()));
preparedAttrs.getRight().add(AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), preparedAttrs.getLeft()));
final Map<String, Set<Object>> syncopeAttrs = new HashMap<>();
preparedAttrs.getRight().forEach(attr -> {
syncopeAttrs.put(attr.getName(), getValues(attr));
});
final Map<String, Set<Object>> resourceAttrs = new HashMap<>();
connectorObject.getAttributes().stream().filter(attr -> (!OperationalAttributes.PASSWORD_NAME.equals(attr.getName()) && !OperationalAttributes.ENABLE_NAME.equals(attr.getName()))).forEachOrdered(attr -> {
resourceAttrs.put(attr.getName(), getValues(attr));
});
syncopeAttrs.keySet().stream().filter(syncopeAttr -> !resourceAttrs.containsKey(syncopeAttr)).forEach(name -> {
misaligned.add(new Misaligned(resource.getKey(), connObjectKeyValue, name, syncopeAttrs.get(name), Collections.emptySet()));
});
resourceAttrs.forEach((key, values) -> {
if (syncopeAttrs.containsKey(key)) {
if (!Objects.equals(syncopeAttrs.get(key), values)) {
misaligned.add(new Misaligned(resource.getKey(), connObjectKeyValue, key, syncopeAttrs.get(key), values));
}
} else {
misaligned.add(new Misaligned(resource.getKey(), connObjectKeyValue, key, Collections.emptySet(), values));
}
});
}
}
});
if (!missing.isEmpty() || !misaligned.isEmpty()) {
doExtract(handler, any, missing, misaligned);
}
}
}
Aggregations