use of org.apache.syncope.core.persistence.api.entity.user.User in project syncope by apache.
the class PullUtils method findByConnObjectKey.
private List<String> findByConnObjectKey(final ConnectorObject connObj, final Provision provision, final AnyUtils anyUtils) {
String connObjectKey = null;
Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(provision);
if (connObjectKeyItem.isPresent()) {
Attribute connObjectKeyAttr = connObj.getAttributeByName(connObjectKeyItem.get().getExtAttrName());
if (connObjectKeyAttr != null) {
connObjectKey = AttributeUtil.getStringValue(connObjectKeyAttr);
}
}
if (connObjectKey == null) {
return Collections.emptyList();
}
for (ItemTransformer transformer : MappingUtils.getItemTransformers(connObjectKeyItem.get())) {
List<Object> output = transformer.beforePull(connObjectKeyItem.get(), null, Collections.<Object>singletonList(connObjectKey));
if (output != null && !output.isEmpty()) {
connObjectKey = output.get(0).toString();
}
}
List<String> result = new ArrayList<>();
IntAttrName intAttrName;
try {
intAttrName = intAttrNameParser.parse(connObjectKeyItem.get().getIntAttrName(), provision.getAnyType().getKind());
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}' specified, ignoring", connObjectKeyItem.get().getIntAttrName(), e);
return result;
}
if (intAttrName.getField() != null) {
switch(intAttrName.getField()) {
case "key":
Any<?> any = getAnyDAO(provision.getAnyType().getKind()).find(connObjectKey);
if (any != null) {
result.add(any.getKey());
}
break;
case "username":
User user = userDAO.findByUsername(connObjectKey);
if (user != null) {
result.add(user.getKey());
}
break;
case "name":
Group group = groupDAO.findByName(connObjectKey);
if (group != null) {
result.add(group.getKey());
}
AnyObject anyObject = anyObjectDAO.findByName(connObjectKey);
if (anyObject != null) {
result.add(anyObject.getKey());
}
break;
default:
}
} else if (intAttrName.getSchemaType() != null) {
switch(intAttrName.getSchemaType()) {
case PLAIN:
PlainAttrValue value = anyUtils.newPlainAttrValue();
PlainSchema schema = plainSchemaDAO.find(intAttrName.getSchemaName());
if (schema == null) {
value.setStringValue(connObjectKey);
} else {
try {
value.parseValue(schema, connObjectKey);
} catch (ParsingValidationException e) {
LOG.error("While parsing provided __UID__ {}", value, e);
value.setStringValue(connObjectKey);
}
}
result.addAll(getAnyDAO(provision.getAnyType().getKind()).findByPlainAttrValue(intAttrName.getSchemaName(), value).stream().map(Entity::getKey).collect(Collectors.toList()));
break;
case DERIVED:
result.addAll(getAnyDAO(provision.getAnyType().getKind()).findByDerAttrValue(intAttrName.getSchemaName(), connObjectKey).stream().map(Entity::getKey).collect(Collectors.toList()));
break;
default:
}
}
return result;
}
use of org.apache.syncope.core.persistence.api.entity.user.User in project syncope by apache.
the class DerAttrTest method derAttrFromSpecialAttrs.
@Test
public void derAttrFromSpecialAttrs() {
DerSchema info = derSchemaDAO.find("info");
assertEquals("username + ' - ' + creationDate + '[' + failedLogins + ']'", info.getExpression());
User user = userDAO.findByUsername("vivaldi");
assertNotNull(user);
String value = derAttrHandler.getValue(user, info);
assertNotNull(value);
assertFalse(value.isEmpty());
assertTrue(value.startsWith("vivaldi - 2010-10-20"));
assertTrue(value.endsWith("[0]"));
}
use of org.apache.syncope.core.persistence.api.entity.user.User in project syncope by apache.
the class DefaultUserProvisioningManager method update.
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public Pair<UserPatch, List<PropagationStatus>> update(final UserPatch userPatch, final ProvisioningReport result, final Boolean enabled, final Set<String> excludedResources, final boolean nullPriorityAsync) {
WorkflowResult<Pair<UserPatch, Boolean>> updated;
try {
updated = uwfAdapter.update(userPatch);
} catch (Exception e) {
LOG.error("Update of user {} failed, trying to pull its status anyway (if configured)", userPatch.getKey(), e);
result.setStatus(ProvisioningReport.Status.FAILURE);
result.setMessage("Update failed, trying to pull status anyway (if configured)\n" + e.getMessage());
updated = new WorkflowResult<>(Pair.of(userPatch, false), new PropagationByResource(), new HashSet<>());
}
if (enabled != null) {
User user = userDAO.find(userPatch.getKey());
WorkflowResult<String> enableUpdate = null;
if (user.isSuspended() == null) {
enableUpdate = uwfAdapter.activate(userPatch.getKey(), null);
} else if (enabled && user.isSuspended()) {
enableUpdate = uwfAdapter.reactivate(userPatch.getKey());
} else if (!enabled && !user.isSuspended()) {
enableUpdate = uwfAdapter.suspend(userPatch.getKey());
}
if (enableUpdate != null) {
if (enableUpdate.getPropByRes() != null) {
updated.getPropByRes().merge(enableUpdate.getPropByRes());
updated.getPropByRes().purge();
}
updated.getPerformedTasks().addAll(enableUpdate.getPerformedTasks());
}
}
List<PropagationTaskTO> tasks = propagationManager.getUserUpdateTasks(updated, updated.getResult().getLeft().getPassword() != null, excludedResources);
PropagationReporter propagationReporter = taskExecutor.execute(tasks, nullPriorityAsync);
return Pair.of(updated.getResult().getLeft(), propagationReporter.getStatuses());
}
use of org.apache.syncope.core.persistence.api.entity.user.User in project syncope by apache.
the class MappingManagerImpl method getIntValues.
@Transactional(readOnly = true)
@Override
public List<PlainAttrValue> getIntValues(final Provision provision, final Item mapItem, final IntAttrName intAttrName, final Any<?> any) {
LOG.debug("Get internal values for {} as '{}' on {}", any, mapItem.getIntAttrName(), provision.getResource());
Any<?> reference = null;
Membership<?> membership = null;
if (intAttrName.getEnclosingGroup() == null && intAttrName.getRelatedAnyObject() == null) {
reference = any;
}
if (any instanceof GroupableRelatable) {
GroupableRelatable<?, ?, ?, ?, ?> groupableRelatable = (GroupableRelatable<?, ?, ?, ?, ?>) any;
if (intAttrName.getEnclosingGroup() != null) {
Group group = groupDAO.findByName(intAttrName.getEnclosingGroup());
if (group == null || !groupableRelatable.getMembership(group.getKey()).isPresent()) {
LOG.warn("No membership for {} in {}, ignoring", intAttrName.getEnclosingGroup(), groupableRelatable);
} else {
reference = group;
}
} else if (intAttrName.getRelatedAnyObject() != null) {
AnyObject anyObject = anyObjectDAO.findByName(intAttrName.getRelatedAnyObject());
if (anyObject == null || groupableRelatable.getRelationships(anyObject.getKey()).isEmpty()) {
LOG.warn("No relationship for {} in {}, ignoring", intAttrName.getRelatedAnyObject(), groupableRelatable);
} else {
reference = anyObject;
}
} else if (intAttrName.getMembershipOfGroup() != null) {
Group group = groupDAO.findByName(intAttrName.getMembershipOfGroup());
membership = groupableRelatable.getMembership(group.getKey()).orElse(null);
}
}
if (reference == null) {
LOG.warn("Could not determine the reference instance for {}", mapItem.getIntAttrName());
return Collections.emptyList();
}
List<PlainAttrValue> values = new ArrayList<>();
boolean transform = true;
AnyUtils anyUtils = anyUtilsFactory.getInstance(reference);
if (intAttrName.getField() != null) {
PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
switch(intAttrName.getField()) {
case "key":
attrValue.setStringValue(reference.getKey());
values.add(attrValue);
break;
case "realm":
attrValue.setStringValue(reference.getRealm().getFullPath());
values.add(attrValue);
break;
case "password":
// ignore
break;
case "userOwner":
case "groupOwner":
Mapping uMapping = provision.getAnyType().equals(anyTypeDAO.findUser()) ? provision.getMapping() : null;
Mapping gMapping = provision.getAnyType().equals(anyTypeDAO.findGroup()) ? provision.getMapping() : null;
if (reference instanceof Group) {
Group group = (Group) reference;
String groupOwnerValue = null;
if (group.getUserOwner() != null && uMapping != null) {
groupOwnerValue = getGroupOwnerValue(provision, group.getUserOwner());
}
if (group.getGroupOwner() != null && gMapping != null) {
groupOwnerValue = getGroupOwnerValue(provision, group.getGroupOwner());
}
if (StringUtils.isNotBlank(groupOwnerValue)) {
attrValue.setStringValue(groupOwnerValue);
values.add(attrValue);
}
}
break;
case "suspended":
if (reference instanceof User) {
attrValue.setBooleanValue(((User) reference).isSuspended());
values.add(attrValue);
}
break;
case "mustChangePassword":
if (reference instanceof User) {
attrValue.setBooleanValue(((User) reference).isMustChangePassword());
values.add(attrValue);
}
break;
default:
try {
Object fieldValue = FieldUtils.readField(reference, intAttrName.getField(), true);
if (fieldValue instanceof Date) {
// needed because ConnId does not natively supports the Date type
attrValue.setStringValue(DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format((Date) fieldValue));
} else if (Boolean.TYPE.isInstance(fieldValue)) {
attrValue.setBooleanValue((Boolean) fieldValue);
} else if (Double.TYPE.isInstance(fieldValue) || Float.TYPE.isInstance(fieldValue)) {
attrValue.setDoubleValue((Double) fieldValue);
} else if (Long.TYPE.isInstance(fieldValue) || Integer.TYPE.isInstance(fieldValue)) {
attrValue.setLongValue((Long) fieldValue);
} else {
attrValue.setStringValue(fieldValue.toString());
}
values.add(attrValue);
} catch (Exception e) {
LOG.error("Could not read value of '{}' from {}", intAttrName.getField(), reference, e);
}
}
} else if (intAttrName.getSchemaType() != null) {
switch(intAttrName.getSchemaType()) {
case PLAIN:
PlainAttr<?> attr;
if (membership == null) {
attr = reference.getPlainAttr(intAttrName.getSchemaName()).orElse(null);
} else {
attr = ((GroupableRelatable<?, ?, ?, ?, ?>) reference).getPlainAttr(intAttrName.getSchemaName(), membership).orElse(null);
}
if (attr == null) {
LOG.warn("Invalid PlainSchema {} or PlainAttr not found for {}", intAttrName.getSchemaName(), reference);
} else {
if (attr.getUniqueValue() != null) {
values.add(anyUtils.clonePlainAttrValue(attr.getUniqueValue()));
} else if (attr.getValues() != null) {
attr.getValues().forEach(value -> values.add(anyUtils.clonePlainAttrValue(value)));
}
}
break;
case DERIVED:
DerSchema derSchema = derSchemaDAO.find(intAttrName.getSchemaName());
if (derSchema == null) {
LOG.warn("Invalid DerSchema: {}", intAttrName.getSchemaName());
} else {
String derValue = membership == null ? derAttrHandler.getValue(reference, derSchema) : derAttrHandler.getValue(reference, membership, derSchema);
if (derValue != null) {
PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
attrValue.setStringValue(derValue);
values.add(attrValue);
}
}
break;
case VIRTUAL:
// virtual attributes don't get transformed
transform = false;
VirSchema virSchema = virSchemaDAO.find(intAttrName.getSchemaName());
if (virSchema == null) {
LOG.warn("Invalid VirSchema: {}", intAttrName.getSchemaName());
} else {
LOG.debug("Expire entry cache {}-{}", reference, intAttrName.getSchemaName());
virAttrCache.expire(reference.getType().getKey(), reference.getKey(), intAttrName.getSchemaName());
List<String> virValues = membership == null ? virAttrHandler.getValues(reference, virSchema) : virAttrHandler.getValues(reference, membership, virSchema);
virValues.forEach(virValue -> {
PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
attrValue.setStringValue(virValue);
values.add(attrValue);
});
}
break;
default:
}
} else if (intAttrName.getPrivilegesOfApplication() != null && reference instanceof User) {
Application application = applicationDAO.find(intAttrName.getPrivilegesOfApplication());
if (application == null) {
LOG.warn("Invalid application: {}", intAttrName.getPrivilegesOfApplication());
} else {
userDAO.findAllRoles((User) reference).stream().flatMap(role -> role.getPrivileges(application).stream()).forEach(privilege -> {
PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
attrValue.setStringValue(privilege.getKey());
values.add(attrValue);
});
}
}
LOG.debug("Internal values: {}", values);
List<PlainAttrValue> transformed = values;
if (transform) {
for (ItemTransformer transformer : MappingUtils.getItemTransformers(mapItem)) {
transformed = transformer.beforePropagation(mapItem, any, transformed);
}
LOG.debug("Transformed values: {}", values);
} else {
LOG.debug("No transformation occurred");
}
return transformed;
}
use of org.apache.syncope.core.persistence.api.entity.user.User in project syncope by apache.
the class MappingManagerImpl method prepareAttr.
/**
* Prepare an attribute to be sent to a connector instance.
*
* @param provision external resource
* @param mapItem mapping item for the given attribute
* @param any given any object
* @param password clear-text password
* @return connObjectKey + prepared attribute
*/
private Pair<String, Attribute> prepareAttr(final Provision provision, final Item mapItem, final Any<?> any, final String password) {
IntAttrName intAttrName;
try {
intAttrName = intAttrNameParser.parse(mapItem.getIntAttrName(), provision.getAnyType().getKind());
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}' specified, ignoring", mapItem.getIntAttrName(), e);
return null;
}
boolean readOnlyVirSchema = false;
Schema schema = null;
AttrSchemaType schemaType = AttrSchemaType.String;
if (intAttrName.getSchemaType() != null) {
switch(intAttrName.getSchemaType()) {
case PLAIN:
schema = plainSchemaDAO.find(intAttrName.getSchemaName());
if (schema != null) {
schemaType = schema.getType();
}
break;
case VIRTUAL:
schema = virSchemaDAO.find(intAttrName.getSchemaName());
readOnlyVirSchema = (schema != null && schema.isReadonly());
break;
default:
}
}
List<PlainAttrValue> values = getIntValues(provision, mapItem, intAttrName, any);
LOG.debug("Define mapping for: " + "\n* ExtAttrName " + mapItem.getExtAttrName() + "\n* is connObjectKey " + mapItem.isConnObjectKey() + "\n* is password " + mapItem.isPassword() + "\n* mandatory condition " + mapItem.getMandatoryCondition() + "\n* Schema " + intAttrName.getSchemaName() + "\n* ClassType " + schemaType.getType().getName() + "\n* Values " + values);
Pair<String, Attribute> result;
if (readOnlyVirSchema) {
result = null;
} else {
List<Object> objValues = new ArrayList<>();
for (PlainAttrValue value : values) {
if (FrameworkUtil.isSupportedAttributeType(schemaType.getType())) {
objValues.add(value.getValue());
} else {
objValues.add(value.getValueAsString(schemaType));
}
}
if (mapItem.isConnObjectKey()) {
result = Pair.of(objValues.isEmpty() ? null : objValues.iterator().next().toString(), null);
} else if (mapItem.isPassword() && any instanceof User) {
String passwordAttrValue = password;
if (StringUtils.isBlank(passwordAttrValue)) {
User user = (User) any;
if (user.canDecodePassword()) {
try {
passwordAttrValue = ENCRYPTOR.decode(user.getPassword(), user.getCipherAlgorithm());
} catch (Exception e) {
LOG.error("Could not decode password for {}", user, e);
}
} else if (provision.getResource().isRandomPwdIfNotProvided()) {
try {
passwordAttrValue = passwordGenerator.generate(provision.getResource());
} catch (InvalidPasswordRuleConf e) {
LOG.error("Could not generate policy-compliant random password for {}", user, e);
}
}
}
if (passwordAttrValue == null) {
result = null;
} else {
result = Pair.of(null, AttributeBuilder.buildPassword(passwordAttrValue.toCharArray()));
}
} else if (schema != null && schema.isMultivalue()) {
result = Pair.of(null, AttributeBuilder.build(mapItem.getExtAttrName(), objValues));
} else {
result = Pair.of(null, objValues.isEmpty() ? AttributeBuilder.build(mapItem.getExtAttrName()) : AttributeBuilder.build(mapItem.getExtAttrName(), objValues.iterator().next()));
}
}
return result;
}
Aggregations