Skip to content

Commit

Permalink
[SYNCOPE-1830] fix membership plain attribute map (#862)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-patricelli authored Oct 15, 2024
1 parent fad9a27 commit a09c6ac
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public ElasticsearchUtils(
* @param any user, group or any object to index
* @return document specialized with content from the provided any
*/
@SuppressWarnings("unchecked")
@Transactional
public Map<String, Object> document(final Any<?> any) {
Collection<String> resources = any instanceof User
Expand Down Expand Up @@ -216,10 +217,14 @@ public Map<String, Object> document(final Any<?> any) {

Optional.ofNullable(mAttr.getUniqueValue()).ifPresent(v -> values.add(v.getValue()));

if (!builder.containsKey(mAttr.getSchema().getKey())) {
builder.put(mAttr.getSchema().getKey(), new HashSet<>());
Object attr = builder.computeIfAbsent(mAttr.getSchema().getKey(), k -> new HashSet<>());
// also support case in which there is also an existing attribute set previously
if (attr instanceof Collection) {
((Collection<Object>) attr).addAll(values);
} else {
values.add(attr);
builder.put(mAttr.getSchema().getKey(), values.size() == 1 ? values.get(0) : values);
}
builder.put(mAttr.getSchema().getKey(), values.size() == 1 ? values.get(0) : values);
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public OpenSearchUtils(
* @param any user, group or any object to index
* @return document specialized with content from the provided any
*/
@SuppressWarnings("unchecked")
@Transactional
public Map<String, Object> document(final Any<?> any) {
Collection<String> resources = any instanceof User
Expand Down Expand Up @@ -216,10 +217,14 @@ public Map<String, Object> document(final Any<?> any) {

Optional.ofNullable(mAttr.getUniqueValue()).ifPresent(v -> values.add(v.getValue()));

if (!builder.containsKey(mAttr.getSchema().getKey())) {
builder.put(mAttr.getSchema().getKey(), new HashSet<>());
Object attr = builder.computeIfAbsent(mAttr.getSchema().getKey(), k -> new HashSet<>());
// also support case in which there is also an existing attribute set previously
if (attr instanceof Collection) {
((Collection<Object>) attr).addAll(values);
} else {
values.add(attr);
builder.put(mAttr.getSchema().getKey(), values.size() == 1 ? values.get(0) : values);
}
builder.put(mAttr.getSchema().getKey(), values.size() == 1 ? values.get(0) : values);
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.syncope.common.lib.request.AnyObjectUR;
import org.apache.syncope.common.lib.request.AttrPatch;
import org.apache.syncope.common.lib.request.GroupCR;
import org.apache.syncope.common.lib.request.GroupUR;
import org.apache.syncope.common.lib.request.MembershipUR;
import org.apache.syncope.common.lib.request.UserCR;
import org.apache.syncope.common.lib.request.UserUR;
Expand All @@ -54,6 +55,7 @@
import org.apache.syncope.common.lib.to.PagedResult;
import org.apache.syncope.common.lib.to.RealmTO;
import org.apache.syncope.common.lib.to.RoleTO;
import org.apache.syncope.common.lib.to.TypeExtensionTO;
import org.apache.syncope.common.lib.to.UserTO;
import org.apache.syncope.common.lib.types.AnyTypeKind;
import org.apache.syncope.common.lib.types.ClientExceptionType;
Expand Down Expand Up @@ -1058,16 +1060,32 @@ void userByMembershipAttribute() {
// search user by membership attribute
UserTO puccini = USER_SERVICE.read("puccini");
GroupTO additional = GROUP_SERVICE.read("additional");
GroupTO employee = GROUP_SERVICE.read("employee");
TypeExtensionTO typeExtensionTO = new TypeExtensionTO();
typeExtensionTO.setAnyType(AnyTypeKind.USER.name());
typeExtensionTO.getAuxClasses().add("other");
updateGroup(new GroupUR.Builder(employee.getKey()).typeExtension(typeExtensionTO).build());
// add a membership and its plain attribute
updateUser(new UserUR.Builder(puccini.getKey()).memberships(
updateUser(new UserUR.Builder(puccini.getKey())
.plainAttr(attrAddReplacePatch("ctype", "myownctype"))
.memberships(
new MembershipUR.Builder(additional.getKey()).plainAttrs(attr("ctype", "additionalctype"))
.build()).build());
.build(), new MembershipUR.Builder(employee.getKey())
.plainAttrs(attr("ctype", "additionalemployeectype"))
.build()).build());
await().until(() -> USER_SERVICE.search(new AnyQuery.Builder().page(1).size(10)
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("ctype").equalTo("additionalctype").query())
.build()).getTotalCount() == 1);
assertTrue(USER_SERVICE.search(new AnyQuery.Builder().page(1).size(10)
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("ctype").equalTo("additionalctype").query())
.build()).getResult().stream().anyMatch(u -> "puccini".equals(u.getUsername())));
assertTrue(USER_SERVICE.search(new AnyQuery.Builder().page(1).size(10)
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("ctype").equalTo("additionalemployeectype")
.query()).build()).getResult().stream().anyMatch(u -> "puccini".equals(u.getUsername())));
// check also that search on user plain attribute (not in membership) works
assertTrue(USER_SERVICE.search(new AnyQuery.Builder().page(1).size(10)
.fiql(SyncopeClient.getUserSearchConditionBuilder().is("ctype").equalTo("myownctype").query())
.build()).getResult().stream().anyMatch(u -> "puccini".equals(u.getUsername())));
}

@Test
Expand Down

0 comments on commit a09c6ac

Please sign in to comment.