use of org.apache.syncope.common.lib.to.MembershipTO in project syncope by apache.
the class GroupITCase method uDynMembership.
@Test
public void uDynMembership() {
assertTrue(userService.read("c9b2dec2-00a7-4855-97c0-d854842b4b24").getDynMemberships().isEmpty());
GroupTO group = getBasicSampleTO("uDynMembership");
group.setUDynMembershipCond("cool==true");
group = createGroup(group).getEntity();
assertNotNull(group);
final String groupKey = group.getKey();
List<MembershipTO> memberships = userService.read("c9b2dec2-00a7-4855-97c0-d854842b4b24").getDynMemberships();
assertTrue(memberships.stream().anyMatch(m -> m.getGroupKey().equals(groupKey)));
assertEquals(1, groupService.read(group.getKey()).getDynamicUserMembershipCount());
GroupPatch patch = new GroupPatch();
patch.setKey(group.getKey());
patch.setUDynMembershipCond("cool==false");
groupService.update(patch);
assertTrue(userService.read("c9b2dec2-00a7-4855-97c0-d854842b4b24").getDynMemberships().isEmpty());
assertEquals(0, groupService.read(group.getKey()).getDynamicUserMembershipCount());
}
use of org.apache.syncope.common.lib.to.MembershipTO in project syncope by apache.
the class GroupITCase method aDynMembership.
@Test
public void aDynMembership() {
String fiql = SyncopeClient.getAnyObjectSearchConditionBuilder("PRINTER").is("location").notNullValue().query();
// 1. create group with a given aDynMembership condition
GroupTO group = getBasicSampleTO("aDynMembership");
group.getADynMembershipConds().put("PRINTER", fiql);
group = createGroup(group).getEntity();
assertEquals(fiql, group.getADynMembershipConds().get("PRINTER"));
group = groupService.read(group.getKey());
final String groupKey = group.getKey();
assertEquals(fiql, group.getADynMembershipConds().get("PRINTER"));
// verify that the condition is dynamically applied
AnyObjectTO newAny = AnyObjectITCase.getSampleTO("aDynMembership");
newAny.getResources().clear();
newAny = createAnyObject(newAny).getEntity();
assertNotNull(newAny.getPlainAttr("location"));
List<MembershipTO> memberships = anyObjectService.read("fc6dbc3a-6c07-4965-8781-921e7401a4a5").getDynMemberships();
assertTrue(memberships.stream().anyMatch(m -> m.getGroupKey().equals(groupKey)));
memberships = anyObjectService.read("8559d14d-58c2-46eb-a2d4-a7d35161e8f8").getDynMemberships();
assertTrue(memberships.stream().anyMatch(m -> m.getGroupKey().equals(groupKey)));
memberships = anyObjectService.read(newAny.getKey()).getDynMemberships();
assertTrue(memberships.stream().anyMatch(m -> m.getGroupKey().equals(groupKey)));
// 2. update group and change aDynMembership condition
fiql = SyncopeClient.getAnyObjectSearchConditionBuilder("PRINTER").is("location").nullValue().query();
GroupPatch patch = new GroupPatch();
patch.setKey(group.getKey());
patch.getADynMembershipConds().put("PRINTER", fiql);
group = updateGroup(patch).getEntity();
assertEquals(fiql, group.getADynMembershipConds().get("PRINTER"));
group = groupService.read(group.getKey());
assertEquals(fiql, group.getADynMembershipConds().get("PRINTER"));
// verify that the condition is dynamically applied
AnyObjectPatch anyPatch = new AnyObjectPatch();
anyPatch.setKey(newAny.getKey());
anyPatch.getPlainAttrs().add(new AttrPatch.Builder().operation(PatchOperation.DELETE).attrTO(new AttrTO.Builder().schema("location").build()).build());
newAny = updateAnyObject(anyPatch).getEntity();
assertFalse(newAny.getPlainAttr("location").isPresent());
memberships = anyObjectService.read("fc6dbc3a-6c07-4965-8781-921e7401a4a5").getDynMemberships();
assertFalse(memberships.stream().anyMatch(m -> m.getGroupKey().equals(groupKey)));
memberships = anyObjectService.read("8559d14d-58c2-46eb-a2d4-a7d35161e8f8").getDynMemberships();
assertFalse(memberships.stream().anyMatch(m -> m.getGroupKey().equals(groupKey)));
memberships = anyObjectService.read(newAny.getKey()).getDynMemberships();
assertTrue(memberships.stream().anyMatch(m -> m.getGroupKey().equals(groupKey)));
}
use of org.apache.syncope.common.lib.to.MembershipTO in project syncope by apache.
the class MappingManagerImpl method setIntValues.
@Transactional(readOnly = true)
@Override
public void setIntValues(final Item mapItem, final Attribute attr, final AnyTO anyTO, final AnyUtils anyUtils) {
List<Object> values = null;
if (attr != null) {
values = attr.getValue();
for (ItemTransformer transformer : MappingUtils.getItemTransformers(mapItem)) {
values = transformer.beforePull(mapItem, anyTO, values);
}
}
values = values == null ? Collections.emptyList() : values;
IntAttrName intAttrName;
try {
intAttrName = intAttrNameParser.parse(mapItem.getIntAttrName(), anyUtils.getAnyTypeKind());
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}' specified, ignoring", mapItem.getIntAttrName(), e);
return;
}
if (intAttrName.getField() != null) {
switch(intAttrName.getField()) {
case "password":
if (anyTO instanceof UserTO && !values.isEmpty()) {
((UserTO) anyTO).setPassword(ConnObjectUtils.getPassword(values.get(0)));
}
break;
case "username":
if (anyTO instanceof UserTO) {
((UserTO) anyTO).setUsername(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString());
}
break;
case "name":
if (anyTO instanceof GroupTO) {
((GroupTO) anyTO).setName(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString());
} else if (anyTO instanceof AnyObjectTO) {
((AnyObjectTO) anyTO).setName(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString());
}
break;
case "mustChangePassword":
if (anyTO instanceof UserTO && !values.isEmpty() && values.get(0) != null) {
((UserTO) anyTO).setMustChangePassword(BooleanUtils.toBoolean(values.get(0).toString()));
}
break;
case "userOwner":
case "groupOwner":
if (anyTO instanceof GroupTO && attr != null) {
// using a special attribute (with schema "", that will be ignored) for carrying the
// GroupOwnerSchema value
AttrTO attrTO = new AttrTO();
attrTO.setSchema(StringUtils.EMPTY);
if (values.isEmpty() || values.get(0) == null) {
attrTO.getValues().add(StringUtils.EMPTY);
} else {
attrTO.getValues().add(values.get(0).toString());
}
((GroupTO) anyTO).getPlainAttrs().add(attrTO);
}
break;
default:
}
} else if (intAttrName.getSchemaType() != null) {
GroupableRelatableTO groupableTO = null;
Group group = null;
if (anyTO instanceof GroupableRelatableTO && intAttrName.getMembershipOfGroup() != null) {
groupableTO = (GroupableRelatableTO) anyTO;
group = groupDAO.findByName(intAttrName.getMembershipOfGroup());
}
switch(intAttrName.getSchemaType()) {
case PLAIN:
AttrTO attrTO = new AttrTO();
attrTO.setSchema(intAttrName.getSchemaName());
PlainSchema schema = plainSchemaDAO.find(intAttrName.getSchemaName());
for (Object value : values) {
AttrSchemaType schemaType = schema == null ? AttrSchemaType.String : schema.getType();
if (value != null) {
PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
switch(schemaType) {
case String:
attrValue.setStringValue(value.toString());
break;
case Binary:
attrValue.setBinaryValue((byte[]) value);
break;
default:
try {
attrValue.parseValue(schema, value.toString());
} catch (ParsingValidationException e) {
LOG.error("While parsing provided value {}", value, e);
attrValue.setStringValue(value.toString());
schemaType = AttrSchemaType.String;
}
break;
}
attrTO.getValues().add(attrValue.getValueAsString(schemaType));
}
}
if (groupableTO == null || group == null) {
anyTO.getPlainAttrs().add(attrTO);
} else {
Optional<MembershipTO> membership = groupableTO.getMembership(group.getKey());
if (!membership.isPresent()) {
membership = Optional.of(new MembershipTO.Builder().group(group.getKey(), group.getName()).build());
groupableTO.getMemberships().add(membership.get());
}
membership.get().getPlainAttrs().add(attrTO);
}
break;
case DERIVED:
attrTO = new AttrTO();
attrTO.setSchema(intAttrName.getSchemaName());
if (groupableTO == null || group == null) {
anyTO.getDerAttrs().add(attrTO);
} else {
Optional<MembershipTO> membership = groupableTO.getMembership(group.getKey());
if (!membership.isPresent()) {
membership = Optional.of(new MembershipTO.Builder().group(group.getKey(), group.getName()).build());
groupableTO.getMemberships().add(membership.get());
}
membership.get().getDerAttrs().add(attrTO);
}
break;
case VIRTUAL:
attrTO = new AttrTO();
attrTO.setSchema(intAttrName.getSchemaName());
// virtual attributes don't get transformed, iterate over original attr.getValue()
if (attr != null && attr.getValue() != null && !attr.getValue().isEmpty()) {
attr.getValue().stream().filter(value -> value != null).forEachOrdered(value -> attrTO.getValues().add(value.toString()));
}
if (groupableTO == null || group == null) {
anyTO.getVirAttrs().add(attrTO);
} else {
Optional<MembershipTO> membership = groupableTO.getMembership(group.getKey());
if (!membership.isPresent()) {
membership = Optional.of(new MembershipTO.Builder().group(group.getKey(), group.getName()).build());
groupableTO.getMemberships().add(membership.get());
}
membership.get().getVirAttrs().add(attrTO);
}
break;
default:
}
}
}
use of org.apache.syncope.common.lib.to.MembershipTO in project syncope by apache.
the class AbstractAnyDataBinder method fill.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void fill(final Any any, final Membership membership, final MembershipTO membershipTO, final AnyUtils anyUtils, final SyncopeClientCompositeException scce) {
SyncopeClientException invalidValues = SyncopeClientException.build(ClientExceptionType.InvalidValues);
membershipTO.getPlainAttrs().stream().filter(attrTO -> !attrTO.getValues().isEmpty()).forEach(attrTO -> {
PlainSchema schema = getPlainSchema(attrTO.getSchema());
if (schema != null) {
GroupablePlainAttr attr = (GroupablePlainAttr) GroupableRelatable.class.cast(any).getPlainAttr(schema.getKey(), membership).orElse(null);
if (attr == null) {
attr = anyUtils.newPlainAttr();
attr.setOwner(any);
attr.setMembership(membership);
attr.setSchema(schema);
}
fillAttr(attrTO.getValues(), anyUtils, schema, attr, invalidValues);
if (attr.getValuesAsStrings().isEmpty()) {
attr.setOwner(null);
} else {
any.add(attr);
}
}
});
if (!invalidValues.isEmpty()) {
scce.addException(invalidValues);
}
}
use of org.apache.syncope.common.lib.to.MembershipTO in project syncope by apache.
the class UserReportlet method doExtract.
private void doExtract(final ContentHandler handler, final List<User> users) throws SAXException {
AttributesImpl atts = new AttributesImpl();
for (User user : users) {
atts.clear();
for (Feature feature : conf.getFeatures()) {
String type = null;
String value = null;
switch(feature) {
case key:
type = ReportXMLConst.XSD_STRING;
value = user.getKey();
break;
case username:
type = ReportXMLConst.XSD_STRING;
value = user.getUsername();
break;
case workflowId:
type = ReportXMLConst.XSD_STRING;
value = user.getWorkflowId();
break;
case status:
type = ReportXMLConst.XSD_STRING;
value = user.getStatus();
break;
case creationDate:
type = ReportXMLConst.XSD_DATETIME;
value = user.getCreationDate() == null ? "" : FormatUtils.format(user.getCreationDate());
break;
case lastLoginDate:
type = ReportXMLConst.XSD_DATETIME;
value = user.getLastLoginDate() == null ? "" : FormatUtils.format(user.getLastLoginDate());
break;
case changePwdDate:
type = ReportXMLConst.XSD_DATETIME;
value = user.getChangePwdDate() == null ? "" : FormatUtils.format(user.getChangePwdDate());
break;
case passwordHistorySize:
type = ReportXMLConst.XSD_INT;
value = String.valueOf(user.getPasswordHistory().size());
break;
case failedLoginCount:
type = ReportXMLConst.XSD_INT;
value = String.valueOf(user.getFailedLogins());
break;
default:
}
if (type != null && value != null) {
atts.addAttribute("", "", feature.name(), type, value);
}
}
handler.startElement("", "", "user", atts);
// Using UserTO for attribute values, since the conversion logic of
// values to String is already encapsulated there
UserTO userTO = userDataBinder.getUserTO(user, true);
doExtractAttributes(handler, userTO, conf.getPlainAttrs(), conf.getDerAttrs(), conf.getVirAttrs());
if (conf.getFeatures().contains(Feature.relationships)) {
handler.startElement("", "", "relationships", null);
for (RelationshipTO rel : userTO.getRelationships()) {
atts.clear();
atts.addAttribute("", "", "anyObjectKey", ReportXMLConst.XSD_STRING, rel.getOtherEndKey());
handler.startElement("", "", "relationship", atts);
if (conf.getFeatures().contains(Feature.resources)) {
for (URelationship actualRel : user.getRelationships(rel.getOtherEndKey())) {
doExtractResources(handler, anyObjectDataBinder.getAnyObjectTO(actualRel.getRightEnd(), true));
}
}
handler.endElement("", "", "relationship");
}
handler.endElement("", "", "relationships");
}
if (conf.getFeatures().contains(Feature.memberships)) {
handler.startElement("", "", "memberships", null);
for (MembershipTO memb : userTO.getMemberships()) {
atts.clear();
atts.addAttribute("", "", "groupKey", ReportXMLConst.XSD_STRING, memb.getGroupKey());
atts.addAttribute("", "", "groupName", ReportXMLConst.XSD_STRING, memb.getGroupName());
handler.startElement("", "", "membership", atts);
if (conf.getFeatures().contains(Feature.resources)) {
UMembership actualMemb = user.getMembership(memb.getGroupKey()).orElse(null);
if (actualMemb == null) {
LOG.warn("Unexpected: cannot find membership for group {} for user {}", memb.getGroupKey(), user);
} else {
doExtractResources(handler, groupDataBinder.getGroupTO(actualMemb.getRightEnd(), true));
}
}
handler.endElement("", "", "membership");
}
handler.endElement("", "", "memberships");
}
if (conf.getFeatures().contains(Feature.resources)) {
doExtractResources(handler, userTO);
}
handler.endElement("", "", "user");
}
}
Aggregations