use of org.apache.syncope.ext.scimv2.api.data.SCIMUser in project syncope by apache.
the class SCIMDataBinder method toSCIMUser.
public SCIMUser toSCIMUser(final UserTO userTO, final String location, final List<String> attributes, final List<String> excludedAttributes) {
SCIMConf conf = confManager.get();
List<String> schemas = new ArrayList<>();
schemas.add(Resource.User.schema());
if (conf.getEnterpriseUserConf() != null) {
schemas.add(Resource.EnterpriseUser.schema());
}
SCIMUser user = new SCIMUser(userTO.getKey(), schemas, new Meta(Resource.User, userTO.getCreationDate(), userTO.getLastChangeDate() == null ? userTO.getCreationDate() : userTO.getLastChangeDate(), userTO.getETagValue(), location), output(attributes, excludedAttributes, "userName", userTO.getUsername()), !userTO.isSuspended());
Map<String, AttrTO> attrs = new HashMap<>();
attrs.putAll(EntityTOUtils.buildAttrMap(userTO.getPlainAttrs()));
attrs.putAll(EntityTOUtils.buildAttrMap(userTO.getDerAttrs()));
attrs.putAll(EntityTOUtils.buildAttrMap(userTO.getVirAttrs()));
if (conf.getUserConf() != null) {
if (output(attributes, excludedAttributes, "name") && conf.getUserConf().getName() != null) {
SCIMUserName name = new SCIMUserName();
if (conf.getUserConf().getName().getFamilyName() != null && attrs.containsKey(conf.getUserConf().getName().getFamilyName())) {
name.setFamilyName(attrs.get(conf.getUserConf().getName().getFamilyName()).getValues().get(0));
}
if (conf.getUserConf().getName().getFormatted() != null && attrs.containsKey(conf.getUserConf().getName().getFormatted())) {
name.setFormatted(attrs.get(conf.getUserConf().getName().getFormatted()).getValues().get(0));
}
if (conf.getUserConf().getName().getGivenName() != null && attrs.containsKey(conf.getUserConf().getName().getGivenName())) {
name.setGivenName(attrs.get(conf.getUserConf().getName().getGivenName()).getValues().get(0));
}
if (conf.getUserConf().getName().getHonorificPrefix() != null && attrs.containsKey(conf.getUserConf().getName().getHonorificPrefix())) {
name.setHonorificPrefix(attrs.get(conf.getUserConf().getName().getHonorificPrefix()).getValues().get(0));
}
if (conf.getUserConf().getName().getHonorificSuffix() != null && attrs.containsKey(conf.getUserConf().getName().getHonorificSuffix())) {
name.setHonorificSuffix(attrs.get(conf.getUserConf().getName().getHonorificSuffix()).getValues().get(0));
}
if (conf.getUserConf().getName().getMiddleName() != null && attrs.containsKey(conf.getUserConf().getName().getMiddleName())) {
name.setMiddleName(attrs.get(conf.getUserConf().getName().getMiddleName()).getValues().get(0));
}
if (!name.isEmpty()) {
user.setName(name);
}
}
if (output(attributes, excludedAttributes, "displayName") && conf.getUserConf().getDisplayName() != null && attrs.containsKey(conf.getUserConf().getDisplayName())) {
user.setDisplayName(attrs.get(conf.getUserConf().getDisplayName()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "nickName") && conf.getUserConf().getNickName() != null && attrs.containsKey(conf.getUserConf().getNickName())) {
user.setNickName(attrs.get(conf.getUserConf().getNickName()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "profileUrl") && conf.getUserConf().getProfileUrl() != null && attrs.containsKey(conf.getUserConf().getProfileUrl())) {
user.setProfileUrl(attrs.get(conf.getUserConf().getProfileUrl()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "title") && conf.getUserConf().getTitle() != null && attrs.containsKey(conf.getUserConf().getTitle())) {
user.setTitle(attrs.get(conf.getUserConf().getTitle()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "userType") && conf.getUserConf().getUserType() != null && attrs.containsKey(conf.getUserConf().getUserType())) {
user.setUserType(attrs.get(conf.getUserConf().getUserType()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "preferredLanguage") && conf.getUserConf().getPreferredLanguage() != null && attrs.containsKey(conf.getUserConf().getPreferredLanguage())) {
user.setPreferredLanguage(attrs.get(conf.getUserConf().getPreferredLanguage()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "locale") && conf.getUserConf().getLocale() != null && attrs.containsKey(conf.getUserConf().getLocale())) {
user.setLocale(attrs.get(conf.getUserConf().getLocale()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "timezone") && conf.getUserConf().getTimezone() != null && attrs.containsKey(conf.getUserConf().getTimezone())) {
user.setTimezone(attrs.get(conf.getUserConf().getTimezone()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "emails")) {
fill(attrs, conf.getUserConf().getEmails(), user.getEmails());
}
if (output(attributes, excludedAttributes, "phoneNumbers")) {
fill(attrs, conf.getUserConf().getPhoneNumbers(), user.getPhoneNumbers());
}
if (output(attributes, excludedAttributes, "ims")) {
fill(attrs, conf.getUserConf().getIms(), user.getIms());
}
if (output(attributes, excludedAttributes, "photos")) {
fill(attrs, conf.getUserConf().getPhotos(), user.getPhotos());
}
if (output(attributes, excludedAttributes, "addresses")) {
conf.getUserConf().getAddresses().forEach(addressConf -> {
SCIMUserAddress address = new SCIMUserAddress();
if (addressConf.getFormatted() != null && attrs.containsKey(addressConf.getFormatted())) {
address.setFormatted(attrs.get(addressConf.getFormatted()).getValues().get(0));
}
if (addressConf.getStreetAddress() != null && attrs.containsKey(addressConf.getStreetAddress())) {
address.setStreetAddress(attrs.get(addressConf.getStreetAddress()).getValues().get(0));
}
if (addressConf.getLocality() != null && attrs.containsKey(addressConf.getLocality())) {
address.setLocality(attrs.get(addressConf.getLocality()).getValues().get(0));
}
if (addressConf.getRegion() != null && attrs.containsKey(addressConf.getRegion())) {
address.setRegion(attrs.get(addressConf.getRegion()).getValues().get(0));
}
if (addressConf.getCountry() != null && attrs.containsKey(addressConf.getCountry())) {
address.setCountry(attrs.get(addressConf.getCountry()).getValues().get(0));
}
if (addressConf.getType() != null) {
address.setType(addressConf.getType().name());
}
if (addressConf.isPrimary()) {
address.setPrimary(true);
}
if (!address.isEmpty()) {
user.getAddresses().add(address);
}
});
}
if (output(attributes, excludedAttributes, "x509Certificates")) {
conf.getUserConf().getX509Certificates().stream().filter(certificate -> attrs.containsKey(certificate)).forEachOrdered(certificate -> {
user.getX509Certificates().add(new Value(attrs.get(certificate).getValues().get(0)));
});
}
if (conf.getEnterpriseUserConf() != null) {
SCIMEnterpriseInfo enterpriseInfo = new SCIMEnterpriseInfo();
if (output(attributes, excludedAttributes, "employeeNumber") && conf.getEnterpriseUserConf().getEmployeeNumber() != null && attrs.containsKey(conf.getEnterpriseUserConf().getEmployeeNumber())) {
enterpriseInfo.setEmployeeNumber(attrs.get(conf.getEnterpriseUserConf().getEmployeeNumber()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "costCenter") && conf.getEnterpriseUserConf().getCostCenter() != null && attrs.containsKey(conf.getEnterpriseUserConf().getCostCenter())) {
enterpriseInfo.setCostCenter(attrs.get(conf.getEnterpriseUserConf().getCostCenter()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "organization") && conf.getEnterpriseUserConf().getOrganization() != null && attrs.containsKey(conf.getEnterpriseUserConf().getOrganization())) {
enterpriseInfo.setOrganization(attrs.get(conf.getEnterpriseUserConf().getOrganization()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "division") && conf.getEnterpriseUserConf().getDivision() != null && attrs.containsKey(conf.getEnterpriseUserConf().getDivision())) {
enterpriseInfo.setDivision(attrs.get(conf.getEnterpriseUserConf().getDivision()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "department") && conf.getEnterpriseUserConf().getDepartment() != null && attrs.containsKey(conf.getEnterpriseUserConf().getDepartment())) {
enterpriseInfo.setDepartment(attrs.get(conf.getEnterpriseUserConf().getDepartment()).getValues().get(0));
}
if (output(attributes, excludedAttributes, "manager") && conf.getEnterpriseUserConf().getManager() != null) {
SCIMUserManager manager = new SCIMUserManager();
if (conf.getEnterpriseUserConf().getManager().getKey() != null && attrs.containsKey(conf.getEnterpriseUserConf().getManager().getKey())) {
try {
UserTO userManager = userLogic.read(attrs.get(conf.getEnterpriseUserConf().getManager().getKey()).getValues().get(0));
manager.setValue(userManager.getKey());
manager.setRef(StringUtils.substringBefore(location, "/Users") + "/Users/" + userManager.getKey());
if (conf.getEnterpriseUserConf().getManager().getDisplayName() != null) {
AttrTO displayName = userManager.getPlainAttr(conf.getEnterpriseUserConf().getManager().getDisplayName()).orElse(null);
if (displayName == null) {
displayName = userManager.getDerAttr(conf.getEnterpriseUserConf().getManager().getDisplayName()).orElse(null);
}
if (displayName == null) {
displayName = userManager.getVirAttr(conf.getEnterpriseUserConf().getManager().getDisplayName()).orElse(null);
}
if (displayName != null) {
manager.setDisplayName(displayName.getValues().get(0));
}
}
} catch (Exception e) {
LOG.error("Could not read user {}", conf.getEnterpriseUserConf().getManager().getKey(), e);
}
}
if (!manager.isEmpty()) {
enterpriseInfo.setManager(manager);
}
}
if (!enterpriseInfo.isEmpty()) {
user.setEnterpriseInfo(enterpriseInfo);
}
}
if (output(attributes, excludedAttributes, "groups")) {
userTO.getMemberships().forEach(membership -> {
user.getGroups().add(new Group(membership.getGroupKey(), StringUtils.substringBefore(location, "/Users") + "/Groups/" + membership.getGroupKey(), membership.getGroupName(), Function.direct));
});
userTO.getDynMemberships().forEach(membership -> {
user.getGroups().add(new Group(membership.getGroupKey(), StringUtils.substringBefore(location, "/Users") + "/Groups/" + membership.getGroupKey(), membership.getGroupName(), Function.indirect));
});
}
if (output(attributes, excludedAttributes, "entitlements")) {
authDataAccessor.getAuthorities(userTO.getUsername()).forEach(authority -> {
user.getEntitlements().add(new Value(authority.getAuthority() + " on Realm(s) " + authority.getRealms()));
});
}
if (output(attributes, excludedAttributes, "roles")) {
userTO.getRoles().forEach(role -> {
user.getRoles().add(new Value(role));
});
}
}
return user;
}
Aggregations