Reputation: 31
we are using spring ldap to do user operations(get,update,delete) in active directory.
We have requirement like if we have usergroup(testusergroup) in active directory then we need to fetch all users from that group. Otherwise we need to fetch all users from domain. Having usergroup in active directory is not mandatory.
I am using below code to fetch users.
If group exists :
LdapQuery userQuery = LdapQueryBuilder.query().where("objectClass").is("user").and(LdapQueryBuilder.query().where("objectCategory").is("person"))
.and(LdapQueryBuilder.query().where("memberOf").is(userGroupDN));
If no group :
LdapQuery userQuery = LdapQueryBuilder.query().where("objectClass").is("user").and(LdapQueryBuilder.query().where("objectCategory").is("person"));
This code is working exactly for my requirement. But i would like to reduce the code duplication because i have this kind of queries in multiple places.
I tried like below.
LdapQuery query = LdapQueryBuilder.query().where("objectClass").is("user").and(LdapQueryBuilder.query().where("objectCategory").is("person"))
If group exists :
query = query.and(LdapQueryBuilder.query().where("memberOf").is(userGroupDN));
It is showing me error like this(The method and(ContainerCriteria) is undefined for the type LdapQuery)
Can anyone help me on this.
Upvotes: 1
Views: 3366
Reputation: 31
After had analysis found the solution for this issue. We are posting here may be it will help someone
LdapQuery query = LdapQueryBuilder.query().where("objectClass").is("user").and(LdapQueryBuilder.query().where("objectCategory").is("person"));
query = query.and(LdapQueryBuilder.query().where("memberOf").is(userGroupDN));
If we do like above it will not allow, because LdapQuery does not have any method related to adding Condition Criteria or Container Criteria.
So we changed it to like below.
ContainerCriteria criteria = LdapQueryBuilder.query().where("objectClass").is("user").and(LdapQueryBuilder.query().where("objectCategory").is("person"));
criteria = criteria..and(LdapQueryBuilder.query().where("memberOf").is(userGroupDN));
Upvotes: 2