use of org.apache.syncope.common.lib.to.AnyObjectTO in project syncope by apache.
the class AnyObjectDataBinderImpl method getAnyObjectTO.
@Override
public AnyObjectTO getAnyObjectTO(final AnyObject anyObject, final boolean details) {
AnyObjectTO anyObjectTO = new AnyObjectTO();
anyObjectTO.setType(anyObject.getType().getKey());
BeanUtils.copyProperties(anyObject, anyObjectTO, IGNORE_PROPERTIES);
Map<VirSchema, List<String>> virAttrValues = details ? virAttrHandler.getValues(anyObject) : Collections.<VirSchema, List<String>>emptyMap();
fillTO(anyObjectTO, anyObject.getRealm().getFullPath(), anyObject.getAuxClasses(), anyObject.getPlainAttrs(), derAttrHandler.getValues(anyObject), virAttrValues, anyObjectDAO.findAllResources(anyObject), details);
if (details) {
// dynamic realms
anyObjectTO.getDynRealms().addAll(userDAO.findDynRealms(anyObject.getKey()));
// relationships
anyObjectTO.getRelationships().addAll(anyObjectDAO.findAllRelationships(anyObject).stream().map(relationship -> getRelationshipTO(relationship.getType().getKey(), relationship.getLeftEnd().getKey().equals(anyObject.getKey()) ? relationship.getRightEnd() : relationship.getLeftEnd())).collect(Collectors.toList()));
// memberships
anyObjectTO.getMemberships().addAll(anyObject.getMemberships().stream().map(membership -> {
return getMembershipTO(anyObject.getPlainAttrs(membership), derAttrHandler.getValues(anyObject, membership), virAttrHandler.getValues(anyObject, membership), membership);
}).collect(Collectors.toList()));
// dynamic memberships
anyObjectTO.getDynMemberships().addAll(anyObjectDAO.findDynGroups(anyObject.getKey()).stream().map(group -> {
return new MembershipTO.Builder().group(group.getKey(), group.getName()).build();
}).collect(Collectors.toList()));
}
return anyObjectTO;
}
use of org.apache.syncope.common.lib.to.AnyObjectTO in project syncope by apache.
the class ConnObjectUtils method getAnyPatch.
/**
* Build {@link AnyPatch} out of connector object attributes and schema mapping.
*
* @param key any object to be updated
* @param obj connector object
* @param original any object to get diff from
* @param pullTask pull task
* @param provision provision information
* @param anyUtils utils
* @param <T> any object
* @return modifications for the any object to be updated
*/
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public <T extends AnyPatch> T getAnyPatch(final String key, final ConnectorObject obj, final AnyTO original, final PullTask pullTask, final Provision provision, final AnyUtils anyUtils) {
AnyTO updated = getAnyTOFromConnObject(obj, pullTask, provision, anyUtils);
updated.setKey(key);
T anyPatch = null;
if (null != anyUtils.getAnyTypeKind()) {
switch(anyUtils.getAnyTypeKind()) {
case USER:
UserTO originalUser = (UserTO) original;
UserTO updatedUser = (UserTO) updated;
if (StringUtils.isBlank(updatedUser.getUsername())) {
updatedUser.setUsername(originalUser.getUsername());
}
// update password if and only if password is really changed
User user = userDAO.authFind(key);
if (StringUtils.isBlank(updatedUser.getPassword()) || ENCRYPTOR.verify(updatedUser.getPassword(), user.getCipherAlgorithm(), user.getPassword())) {
updatedUser.setPassword(null);
}
updatedUser.setSecurityQuestion(updatedUser.getSecurityQuestion());
updatedUser.setMustChangePassword(originalUser.isMustChangePassword());
anyPatch = (T) AnyOperations.diff(updatedUser, originalUser, true);
break;
case GROUP:
GroupTO originalGroup = (GroupTO) original;
GroupTO updatedGroup = (GroupTO) updated;
if (StringUtils.isBlank(updatedGroup.getName())) {
updatedGroup.setName(originalGroup.getName());
}
updatedGroup.setUserOwner(originalGroup.getUserOwner());
updatedGroup.setGroupOwner(originalGroup.getGroupOwner());
updatedGroup.setUDynMembershipCond(originalGroup.getUDynMembershipCond());
updatedGroup.getADynMembershipConds().putAll(originalGroup.getADynMembershipConds());
updatedGroup.getTypeExtensions().addAll(originalGroup.getTypeExtensions());
anyPatch = (T) AnyOperations.diff(updatedGroup, originalGroup, true);
break;
case ANY_OBJECT:
AnyObjectTO originalAnyObject = (AnyObjectTO) original;
AnyObjectTO updatedAnyObject = (AnyObjectTO) updated;
if (StringUtils.isBlank(updatedAnyObject.getName())) {
updatedAnyObject.setName(originalAnyObject.getName());
}
anyPatch = (T) AnyOperations.diff(updatedAnyObject, originalAnyObject, true);
break;
default:
}
}
return anyPatch;
}
use of org.apache.syncope.common.lib.to.AnyObjectTO in project syncope by apache.
the class ConnObjectUtils method getAnyTO.
/**
* Build a UserTO / GroupTO / AnyObjectTO out of connector object attributes and schema mapping.
*
* @param obj connector object
* @param pullTask pull task
* @param provision provision information
* @param anyUtils utils
* @param <T> any object
* @return UserTO for the user to be created
*/
@Transactional(readOnly = true)
public <T extends AnyTO> T getAnyTO(final ConnectorObject obj, final PullTask pullTask, final Provision provision, final AnyUtils anyUtils) {
T anyTO = getAnyTOFromConnObject(obj, pullTask, provision, anyUtils);
// (for users) if password was not set above, generate if resource is configured for that
if (anyTO instanceof UserTO && StringUtils.isBlank(((UserTO) anyTO).getPassword()) && provision.getResource().isRandomPwdIfNotProvided()) {
UserTO userTO = (UserTO) anyTO;
List<PasswordPolicy> passwordPolicies = new ArrayList<>();
Realm realm = realmDAO.findByFullPath(userTO.getRealm());
if (realm != null) {
realmDAO.findAncestors(realm).stream().filter(ancestor -> ancestor.getPasswordPolicy() != null).forEach(ancestor -> {
passwordPolicies.add(ancestor.getPasswordPolicy());
});
}
userTO.getResources().stream().map(resource -> resourceDAO.find(resource)).filter(resource -> resource != null && resource.getPasswordPolicy() != null).forEach(resource -> {
passwordPolicies.add(resource.getPasswordPolicy());
});
String password;
try {
password = passwordGenerator.generate(passwordPolicies);
} catch (InvalidPasswordRuleConf e) {
LOG.error("Could not generate policy-compliant random password for {}", userTO, e);
password = SecureRandomUtils.generateRandomPassword(16);
}
userTO.setPassword(password);
}
return anyTO;
}
use of org.apache.syncope.common.lib.to.AnyObjectTO in project syncope by apache.
the class TemplateUtils method apply.
@Transactional(readOnly = true)
public <T extends AnyTO> void apply(final T anyTO, final AnyTO template) {
fill(anyTO, template);
MapContext jexlContext = new MapContext();
JexlUtils.addFieldsToContext(anyTO, jexlContext);
JexlUtils.addAttrTOsToContext(anyTO.getPlainAttrs(), jexlContext);
JexlUtils.addAttrTOsToContext(anyTO.getDerAttrs(), jexlContext);
JexlUtils.addAttrTOsToContext(anyTO.getVirAttrs(), jexlContext);
if (template instanceof AnyObjectTO) {
fillRelationships((GroupableRelatableTO) anyTO, ((GroupableRelatableTO) template));
fillMemberships((GroupableRelatableTO) anyTO, ((GroupableRelatableTO) template));
} else if (template instanceof UserTO) {
if (StringUtils.isNotBlank(((UserTO) template).getUsername())) {
String evaluated = JexlUtils.evaluate(((UserTO) template).getUsername(), jexlContext);
if (StringUtils.isNotBlank(evaluated)) {
((UserTO) anyTO).setUsername(evaluated);
}
}
if (StringUtils.isNotBlank(((UserTO) template).getPassword())) {
String evaluated = JexlUtils.evaluate(((UserTO) template).getPassword(), jexlContext);
if (StringUtils.isNotBlank(evaluated)) {
((UserTO) anyTO).setPassword(evaluated);
}
}
fillRelationships((GroupableRelatableTO) anyTO, ((GroupableRelatableTO) template));
fillMemberships((GroupableRelatableTO) anyTO, ((GroupableRelatableTO) template));
} else if (template instanceof GroupTO) {
if (StringUtils.isNotBlank(((GroupTO) template).getName())) {
String evaluated = JexlUtils.evaluate(((GroupTO) template).getName(), jexlContext);
if (StringUtils.isNotBlank(evaluated)) {
((GroupTO) anyTO).setName(evaluated);
}
}
if (((GroupTO) template).getUserOwner() != null) {
final User userOwner = userDAO.find(((GroupTO) template).getUserOwner());
if (userOwner != null) {
((GroupTO) anyTO).setUserOwner(userOwner.getKey());
}
}
if (((GroupTO) template).getGroupOwner() != null) {
final Group groupOwner = groupDAO.find(((GroupTO) template).getGroupOwner());
if (groupOwner != null) {
((GroupTO) anyTO).setGroupOwner(groupOwner.getKey());
}
}
}
}
use of org.apache.syncope.common.lib.to.AnyObjectTO in project syncope by apache.
the class AnyObjectITCase method update.
@Test
public void update() {
AnyObjectTO anyObjectTO = getSampleTO("update");
anyObjectTO = createAnyObject(anyObjectTO).getEntity();
assertEquals(1, anyObjectTO.getPlainAttrs().size());
AnyObjectPatch anyObjectPatch = new AnyObjectPatch();
anyObjectPatch.setKey(anyObjectTO.getKey());
String newLocation = "new" + getUUIDString();
anyObjectPatch.getPlainAttrs().add(attrAddReplacePatch("location", newLocation));
anyObjectTO = updateAnyObject(anyObjectPatch).getEntity();
assertEquals(newLocation, anyObjectTO.getPlainAttr("location").get().getValues().get(0));
}
Aggregations