Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of group research for a user through group members #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public class LDAPConnector {
final public static String GROUP_ATTRIBUTE = "GROUP_ATTRIBUTE";
final public static String GROUP_MEMBERS_ATTRIBUTE_NAME = "GROUP_MEMBERS_ATTRIBUTE_NAME";
final public static String ACCESS_GROUP_NAME = "ACCESS_GROUP_NAME";


final public static String GROUPS_FETCH_BY_MEMBER_OF = "GROUPS_FETCH_BY_MEMBER_OF";


private String host;
private int port;
Expand All @@ -70,7 +72,9 @@ public class LDAPConnector {
private String[] groupAttributeNames;
private String groupMembersAttributeName;
private String accessGroupName;


private boolean groupsFetchByMemberOf;

public LDAPConnector(Map<String, Object> configuration) {
this.host = (String) configuration.get(HOST);
this.port = Integer.parseInt((String) configuration.get(PORT));
Expand All @@ -94,6 +98,7 @@ public LDAPConnector(Map<String, Object> configuration) {
this.groupAttributeNames = (String[]) configuration.get(GROUP_ATTRIBUTE);
this.groupMembersAttributeName = (String) configuration.get(GROUP_MEMBERS_ATTRIBUTE_NAME);
this.accessGroupName = (String) configuration.get(ACCESS_GROUP_NAME);
this.groupsFetchByMemberOf = Boolean.valueOf((String) configuration.get(GROUPS_FETCH_BY_MEMBER_OF));
}

protected LDAPConnection connectToLDAP() {
Expand Down Expand Up @@ -168,7 +173,7 @@ public boolean authenticateUser(String username, String password) throws LDAPExc
}
}

private boolean isAccessGroupDefined() {
public boolean isAccessGroupDefined() {
return !StringUtilities.isEmpty(accessGroupName);
}

Expand Down Expand Up @@ -376,15 +381,25 @@ private String getGroupName(String groupDistingushedName) {
}

}

public List<String> getUserGroup(String userName) throws LDAPException {
if(groupsFetchByMemberOf){
return getUserGroupByUserMemberOf(userName);
}
else{
return getUserGroupByGroupMember(userName);
}
}

public List<String> getUserGroup(String username) throws LDAPException {
public List<String> getUserGroupByUserMemberOf(String username) throws LDAPException {

logger.debug("IN");

List<String> userGroups = new ArrayList<String>();

try {
LDAPEntry entry = getUserById(username);


LDAPAttribute groupAttribute = entry.getAttribute(userMemberOfAttributeName);
if(groupAttribute == null) {
Expand All @@ -409,6 +424,59 @@ public List<String> getUserGroup(String username) throws LDAPException {
return userGroups;
}

public List<String> getUserGroupByGroupMember(String username) throws LDAPException {

logger.debug("IN");

LDAPConnection connection = null;
List<String> userGroups = new ArrayList<String>();
LDAPEntry user = getUserById(username);
String userDN = user.getDN();
try {
connection = this.connectToLDAPAsAdmin();

String searchPath = groupSearchPath;

if (StringUtilities.isEmpty(searchPath)) {
searchPath = baseDN;
} else {
if(StringUtilities.isNotEmpty(baseDN)) {
searchPath += "," + baseDN;
}
}


String searchQuery = "(&(objectclass=" + groupObjectClass + ")(" + groupMembersAttributeName + "=" + userDN + "))";
LDAPSearchResults searchResults = connection.search(searchPath, LDAPConnection.SCOPE_SUB,
searchQuery, groupAttributeNames, false);

while (searchResults.hasMore()) {
LDAPEntry entry = searchResults.next();
System.out.println(entry.getDN());
if (entry != null) {

String groupDistingushedName = entry.getDN();

if (isValidGroup(groupDistingushedName)) {
String groupName = getGroupName(groupDistingushedName);
if (!isAccessGroupDefined() || !isAccessGroup(groupName)) {
userGroups.add(groupName);
}
}
}
}

} catch(Throwable t) {
throw new RuntimeException("An unexpected error occured while serching users of Access group", t);
} finally {
closeConnection(connection);
logger.debug("OUT");
}

return userGroups;
}


private boolean isAccessGroup(String groupName) {
return accessGroupName.equals(groupName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public SpagoBIUserProfile createUserProfile(String username) {
logger.debug("IN");

logger.debug("Creating user profile for user [" + username + "] ...");



SpagoBIUserProfile userProfile = new SpagoBIUserProfile();
userProfile.setUniqueIdentifier(username);
Expand All @@ -74,6 +76,15 @@ public SpagoBIUserProfile createUserProfile(String username) {
userProfile.setIsSuperadmin(false);

LDAPConnector ldapConnector = LdapConnectorFactory.createLDAPConnector();

// we have to check that user belongs to access group
if (ldapConnector.isAccessGroupDefined()) {
logger.debug("Access group is defined: checking if user belongs to access group");
if(!ldapConnector.checkUserIsInAccessGroup(username)){
return null;
}
}

List ldapRoles = null;
Map attributes = null;
try {
Expand Down