Skip to content

Commit

Permalink
Merge pull request #3 from scmenthusiast/master
Browse files Browse the repository at this point in the history
Major release with improvements and new features
  • Loading branch information
prasadveautodesk authored Jun 28, 2018
2 parents d93be4a + d4793a1 commit 310fcf4
Show file tree
Hide file tree
Showing 18 changed files with 208 additions and 246 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.adsk.jira.ldapgroupsync.plugin</groupId>
<artifactId>jira-ldap-group-sync-plugin</artifactId>
<version>6.5</version>
<version>6.6</version>
<developers>
<developer>
<name>Venkat Prasad</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Properties;
import java.util.Set;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapContext;

/**
Expand All @@ -18,11 +19,11 @@
*/
public interface LDAPGroupSyncUtil {

public List<String> getUsersInGroup(LdapContext ctx, String groupName);
public List<String> getUsersInLdapGroup(LdapContext ctx, String groupName);

public List<String> getNestedGroups(LdapContext ctx, String groupName);
public List<String> getNestedLdapGroups(LdapContext ctx, String groupName);

public Set<String> getGroupMembers(LdapContext ctx, String groupName);
public Set<String> getLdapGroupMembers(LdapContext ctx, SearchResult sr, Set<String> users);

public SearchControls getGroupSearchControls();

Expand All @@ -41,8 +42,10 @@ public interface LDAPGroupSyncUtil {
public void addUserToJiraGroup(String userName, String groupName);

public void removeUserFromJiraGroup(String userName, String groupName);

public MessageBean sync(LdapContext ctx, String ldap_group, String jira_group);
public Set<String> getLdapGroupStrings(String ldapGroup);
public SearchResult getGroupSearchResult(LdapContext ctx, String groupName);
public long process(LdapContext ctx, String ldap_group, String jira_group);
public MessageBean sync(LdapContext ctx, String ldap_group, String jira_group);

public void setLdapUrl(String ldapUrl);
public void setSecurityPrincipal(String securityPrincipal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public LDAPGroupSyncUtilImpl(ApplicationProperties props, GroupManager groupMana
}
}

public List<String> getUsersInGroup(LdapContext ctx, String groupName) {
public List<String> getUsersInLdapGroup(LdapContext ctx, String groupNameSpace) {
List<String> users = new ArrayList<String>();
try {
String searchFilter = MessageFormat.format(UserMemberSearchFilter, groupName);
String searchFilter = MessageFormat.format(UserMemberSearchFilter, groupNameSpace);

NamingEnumeration answer = ctx.search(baseDn, searchFilter, getMemberSearchControls());
while (answer.hasMoreElements()) {
Expand All @@ -125,7 +125,7 @@ public List<String> getUsersInGroup(LdapContext ctx, String groupName) {
return users;
}

public List<String> getNestedGroups(LdapContext ctx, String groupName) {
public List<String> getNestedLdapGroups(LdapContext ctx, String groupName) {
List<String> groups = new ArrayList<String>();
try {
if( ctx != null ) {
Expand All @@ -144,8 +144,7 @@ public List<String> getNestedGroups(LdapContext ctx, String groupName) {
return groups;
}

public Set<String> getGroupMembers(LdapContext ctx, String groupName) {
Set<String> users = null;
public SearchResult getGroupSearchResult(LdapContext ctx, String groupName) {
try {
if( ctx != null ) {
String searchFilter = MessageFormat.format(GroupSearchFilter, groupName);
Expand All @@ -154,25 +153,37 @@ public Set<String> getGroupMembers(LdapContext ctx, String groupName) {

if (answer.hasMoreElements()) {
SearchResult sr = (SearchResult)answer.next();
logger.debug(">>>" + sr.getNameInNamespace());
return sr;
}
} else {
logger.error("LDAP Connection Null or Failed.");
}
} catch (NamingException e) {
logger.error(e);
}

return null;
}

public Set<String> getLdapGroupMembers(LdapContext ctx, SearchResult sr, Set<String> users)
{
try {
if( ctx != null ) {
if (sr != null) {
logger.debug(" >>>" + sr.getNameInNamespace());

users = new HashSet<String>(); // create set object

List<String> new_users_list = getUsersInGroup(ctx, sr.getNameInNamespace());
if(new_users_list.size() > 0) {
users.addAll(new_users_list);
}
users.addAll(getUsersInLdapGroup(ctx, sr.getNameInNamespace()));

// support nested groups
if("TRUE".equalsIgnoreCase(IsNested)) {
List<String> groups = getNestedGroups(ctx, sr.getNameInNamespace());
if(groups.size() > 0) {
for(String group : groups) {
List<String> nested_user_list = getUsersInGroup(ctx, group);
if(nested_user_list.size() > 0) {
users.addAll(nested_user_list);
}
}
String searchGroupFilter = MessageFormat.format(GroupMemberSearchFilter, sr.getNameInNamespace());

NamingEnumeration groupAnswer = ctx.search(baseDn, searchGroupFilter, getGroupSearchControls());

while (groupAnswer.hasMoreElements()) {
SearchResult gsr = (SearchResult) groupAnswer.next();
logger.debug("*** processing nested group *** "+ gsr.getName());
getLdapGroupMembers(ctx, gsr, users);
}
}
}
Expand Down Expand Up @@ -302,8 +313,9 @@ public void createJiraGroup(String groupName) {
}
}



public MessageBean sync(LdapContext ctx, String ldap_group, String jira_group) {

logger.debug(" >>> "+ ldap_group +" : "+ jira_group +" <<< ");
MessageBean message = new MessageBean();

Expand All @@ -313,41 +325,67 @@ public MessageBean sync(LdapContext ctx, String ldap_group, String jira_group) {
return message;
}

Set<String> ldap_group_users = getGroupMembers(ctx, ldap_group);
if( ldap_group_users == null ) {
logger.debug("LDAP Group ("+ldap_group+") does not exists.");
message.setMessage("LDAP Group ("+ldap_group+") does not exists.");
message.setStatus(1);

} else {

logger.debug(" >>> Size: "+ ldap_group_users.size());

List<String> jira_group_users = getJiraGroupMembers(jira_group);
if( jira_group_users != null ) {
for(String j : jira_group_users) {
if(!ldap_group_users.contains(j)) {
removeUserFromJiraGroup(j, jira_group);
long index = process(ctx, ldap_group, jira_group);
message.setMessage("Successful. Fetch Size("+index+")");
message.setStatus(0);

return message;
}

public Set<String> getLdapGroupStrings(String ldapGroup) {
Set<String> groups = new HashSet<String>();
String[] fields = ldapGroup.trim().split(",");
for(String f : fields){
groups.add(f.trim());
}
return groups;
}

public long process(LdapContext ctx, String ldap_group, String jira_group) {

long index = 0;

Set<String> ldapGroupStrings = getLdapGroupStrings(ldap_group);

Set<String> ldap_group_users = new HashSet<String>();

for(String lGroup : ldapGroupStrings) {
logger.debug("*** Processing Ldap Group ("+lGroup+").");
SearchResult sr = getGroupSearchResult(ctx, lGroup);

ldap_group_users.addAll(getLdapGroupMembers(ctx, sr, ldap_group_users));

if( ldap_group_users.isEmpty() ) {
logger.debug("LDAP Group ("+lGroup+") does not exists.");

} else {

logger.debug(" >>> Size: "+ ldap_group_users.size());
index = ldap_group_users.size();

List<String> jira_group_users = getJiraGroupMembers(jira_group);
if( jira_group_users != null ) {
for(String j : jira_group_users) {
if(!ldap_group_users.contains(j)) {
removeUserFromJiraGroup(j, jira_group);
}
}
}
for(String i : ldap_group_users) {
if(!jira_group_users.contains(i)) {
for(String i : ldap_group_users) {
if(!jira_group_users.contains(i)) {
addUserToJiraGroup(i, jira_group);
}
}
} else {
logger.debug("JIRA Group members return NULL. So adding LDAP users.");
for(String i : ldap_group_users) {
addUserToJiraGroup(i, jira_group);
}
}
} else {
logger.debug("JIRA Group members return NULL. So adding LDAP users.");
for(String i : ldap_group_users) {
addUserToJiraGroup(i, jira_group);
}
}

message.setMessage("Successful. Fetch Size("+ldap_group_users.size()+")");
message.setStatus(0);

}

return message;
return index;
}

public void setLdapUrl(String ldapUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Response getLdapGroupSyncConfigs() {
return Response.status(Response.Status.FORBIDDEN).entity(authError).build();
}

List<LdapGroupSyncMapBean> results = ldapGroupSyncAoMgr.getAllGroupsMapProperties();
List<LdapGroupSyncMapBean> results = ldapGroupSyncAoMgr.getGroupsMapProperties();

long totalTime = System.currentTimeMillis() - startTime;
logger.debug("["+loggedInAppUser.getUsername()+"] Get Config. Took "+ totalTime/ 1000d +" Seconds");
Expand Down Expand Up @@ -113,11 +113,6 @@ public Response adminLdapGroupSync(LdapGroupSyncBean syncBean) {
messageBean.setMessage("This plugin does not support JIRA default group ("+syncBean.getJiraGroup()+"). Skipping!");
return Response.ok(messageBean).build();
}

if(ldapGroupSyncAoMgr.isJiraGroupNotInSupport(syncBean.getJiraGroup()) == true) { //skip not supported
messageBean.setMessage("This JIRA group ("+syncBean.getJiraGroup()+") is mapped not to support. Skipping!");
return Response.ok(messageBean).build();
}

MessageBean result = null;
LdapContext ctx = null;
Expand Down Expand Up @@ -172,11 +167,6 @@ public Response selfLdapGroupSync(@PathParam("ldapJiraGroup") String ldapJiraGro
return Response.ok(messageBean).build();
}

if(ldapGroupSyncAoMgr.isJiraGroupNotInSupport(ldapJiraGroup) == true) { //skip not supported
messageBean.setMessage("This JIRA group ("+ldapJiraGroup+") is mapped not to support. Skipping!");
return Response.ok(messageBean).build();
}

MessageBean result = null;
LdapContext ctx = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
*/
public interface LdapGroupSyncAOMgr {
public ActiveObjects getActiveObjects();
public List<LdapGroupSyncMapBean> getAllGroupsMapProperties();
public List<LdapGroupSyncMapBean> getSupportedGroupsMapProperties();
public List<LdapGroupSyncMapBean> getGroupsMapProperties();
public LdapGroupSyncMapBean getGroupsMapProperty(long configId);
public void addGroupsMapProperty(LdapGroupSyncMapBean configBean);
public void setGroupsMapProperty(LdapGroupSyncMapBean configBean);
public boolean findGroupsMapProperty(LdapGroupSyncMapBean configBean);
public boolean findGroupsMapProperty2(LdapGroupSyncMapBean configBean);
public boolean isJiraGroupNotInSupport(String jiraGroup);
public void removeGroupsMapProperty(long configId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,9 @@ private LdapGroupSyncAOMgrImpl(ActiveObjects ao) {

public ActiveObjects getActiveObjects() {
return this.ao;
}

public List<LdapGroupSyncMapBean> getSupportedGroupsMapProperties() {
final LdapGroupSyncMap[] maps = getActiveObjects()
.find(LdapGroupSyncMap.class, Query.select().where("SUPPORT = ?", true));
List<LdapGroupSyncMapBean> configList = new ArrayList<LdapGroupSyncMapBean>();
for(LdapGroupSyncMap map : maps){
LdapGroupSyncMapBean bean = new LdapGroupSyncMapBean();
bean.setConfigId(map.getID());
bean.setLdapGroup(map.getLdapGroup());
bean.setJiraGroup(map.getJiraGroup());
bean.setSupport(map.getSupport());
configList.add(bean);
}
return configList;
}
}

public List<LdapGroupSyncMapBean> getAllGroupsMapProperties() {
public List<LdapGroupSyncMapBean> getGroupsMapProperties() {
final LdapGroupSyncMap[] maps = getActiveObjects()
.find(LdapGroupSyncMap.class, Query.select());
List<LdapGroupSyncMapBean> configList = new ArrayList<LdapGroupSyncMapBean>();
Expand All @@ -52,7 +37,6 @@ public List<LdapGroupSyncMapBean> getAllGroupsMapProperties() {
bean.setConfigId(map.getID());
bean.setLdapGroup(map.getLdapGroup());
bean.setJiraGroup(map.getJiraGroup());
bean.setSupport(map.getSupport());
configList.add(bean);
}
return configList;
Expand All @@ -62,7 +46,6 @@ public void addGroupsMapProperty(LdapGroupSyncMapBean configBean) {
final LdapGroupSyncMap map = ao.create(LdapGroupSyncMap.class);
map.setLdapGroup(configBean.getLdapGroup());
map.setJiraGroup(configBean.getJiraGroup());
map.setSupport(configBean.isSupport());
map.save();
}

Expand All @@ -73,7 +56,6 @@ public void setGroupsMapProperty(LdapGroupSyncMapBean configBean) {
final LdapGroupSyncMap map = maps[0];
map.setLdapGroup(configBean.getLdapGroup());
map.setJiraGroup(configBean.getJiraGroup());
map.setSupport(configBean.isSupport());
map.save();
}
}
Expand All @@ -100,12 +82,6 @@ public void removeGroupsMapProperty(long configId) {
ao.delete(map);
}
}

public boolean isJiraGroupNotInSupport(String jiraGroup) {
final LdapGroupSyncMap[] maps = ao.find(LdapGroupSyncMap.class, Query.select()
.where("JIRA_GROUP = ? AND SUPPORT = ?", jiraGroup, false));
return maps.length > 0;
}

public LdapGroupSyncMapBean getGroupsMapProperty(long configId) {
final LdapGroupSyncMap[] maps = ao.find(LdapGroupSyncMap.class, Query.select().where("ID = ?", configId));
Expand All @@ -115,7 +91,6 @@ public LdapGroupSyncMapBean getGroupsMapProperty(long configId) {
bean.setConfigId(map.getID());
bean.setLdapGroup(map.getLdapGroup());
bean.setJiraGroup(map.getJiraGroup());
bean.setSupport(map.getSupport());
return bean;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,4 @@ public interface LdapGroupSyncMap extends Entity {

public void setJiraGroup(String jiraGroup);

public boolean getSupport();

public void setSupport(boolean support);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class LdapGroupSyncMapBean {
private String ldapGroup;
@JsonProperty
private String jiraGroup;
@JsonProperty
private boolean support;

public long getConfigId() {
return configId;
Expand All @@ -37,14 +35,6 @@ public String getJiraGroup() {
public void setJiraGroup(String jiraGroup) {
this.jiraGroup = jiraGroup;
}

public boolean isSupport() {
return support;
}

public void setSupport(boolean support) {
this.support = support;
}

public void clear() {
this.ldapGroup = null;
Expand Down
Loading

0 comments on commit 310fcf4

Please sign in to comment.