BufferedReader oBufReader = null;
StringBuffer sADAllUsers = new StringBuffer();
StringBuffer sADDNGroup = new StringBuffer();
StringBuffer sADAllGroups = new StringBuffer();
HashSet <List<String>>hsAllADUsers = new HashSet <List<String>>();
HashSet <String>hsAllADGroups = new HashSet <String>();
HashSet <String>hsAllMSTRGroups = new HashSet <String>();
HashSet <String>hsAllMSTRUsers = new HashSet <String>();
HashSet <String>hsMatchADGroups = new HashSet <String>();
HashSet <String>hsMSTRGroupUsers = new HashSet <String>();
String sPropFileLine = null;
String loginInfo=" -s "+servername+" -u "+username+" -p "+password;

//Read all users in Microstrategy
ResultSet oAllMSTRUsers = executeCapture("LIST LOGIN, TRUSTEDLOGIN FOR USERS IN GROUP \"Everyone\";");
oAllMSTRUsers.moveFirst();
printOut("Checking for users with incorrect trusted auth id...");
while (!oAllMSTRUsers.isEof()) {
if(!((oAllMSTRUsers.getFieldValueString(DisplayPropertyEnum.LOGIN).toLowerCase()).equals(oAllMSTRUsers.getFieldValueString(1).toLowerCase()))&&!(oAllMSTRUsers.getFieldValueString(DisplayPropertyEnum.LOGIN).toLowerCase().equals("administrator"))) {
printOut(oAllMSTRUsers.getFieldValueString(DisplayPropertyEnum.LOGIN)+" Trusted Login is incorrect"); 
if (execute("ALTER USER '"+oAllMSTRUsers.getFieldValueString(DisplayPropertyEnum.LOGIN)+"' TRUSTEDLOGIN '" +oAllMSTRUsers.getFieldValueString(DisplayPropertyEnum.LOGIN)+"';") == ErrorHandler.EXIT_CODE_SUCCESS) {
printOut("User's Trusted Login Successfully set to: " + oAllMSTRUsers.getFieldValueString(DisplayPropertyEnum.LOGIN));
}
else {
printErr("Alter user failed");
execute("TRIGGER EVENT \"LDAPSychFail\"");
}
}
//printOut(oAllMSTRUsers.getFieldValueString(DisplayPropertyEnum.LOGIN).toLowerCase()); 
hsAllMSTRUsers.add((oAllMSTRUsers.getFieldValueString(DisplayPropertyEnum.LOGIN)).trim().toLowerCase()); 
oAllMSTRUsers.moveNext();
}

//Read AD pattern from properties file
try {
oBufReader = new BufferedReader(new FileReader(groupfilepath));
while ((sPropFileLine = oBufReader.readLine()) != null) { 
//if(sPropFileLine.trim().toLowerCase().indexOf("pricestrat")>-1) 
hsMatchADGroups.add(sPropFileLine.trim().toLowerCase());
}
}
catch (Exception e) {
printErr("Error reading properties file: " + e.getLocalizedMessage()); 
}

//Read Original AD groups by pattern
for ( Iterator ipattern = hsMatchADGroups.iterator(); ipattern.hasNext(); ) {
String sThisADGroup = ipattern.next().toString().toLowerCase();
printOut("Command:cmd /c dsquery group -name \""+sThisADGroup+"*\""+loginInfo+" -limit 2000 |dsget group -samid");
sADAllGroups.append("cmd /c dsquery group -name \""+sThisADGroup+"*\""+loginInfo+" -limit 2000 |dsget group -samid"); 

Process procReadADGroups=Runtime.getRuntime().exec(sADAllGroups.toString()); 
//int exitValue1 = procReadADGroups.waitFor();
BufferedReader brInputReadADGroups = new BufferedReader(new InputStreamReader(procReadADGroups.getInputStream()));
BufferedReader brErrorReadADGroups = new BufferedReader(new InputStreamReader(procReadADGroups.getErrorStream()));
String sReadADGroupErrors = null; 
String sReadADGroup = brInputReadADGroups.readLine(); 
while ((sReadADGroup = brInputReadADGroups.readLine())!= null) { 
sReadADGroup.trim(); 
if((! sReadADGroup.equals("dsget succeeded"))) {
hsAllADGroups.add(sReadADGroup.toLowerCase()); 
printOut("Group that match "+sThisADGroup.toLowerCase()+"*: "+sReadADGroup.toLowerCase());
}
} 
while ((sReadADGroupErrors = brErrorReadADGroups.readLine()) != null) {
printOut("Error reading groups that match "+sThisADGroup.toLowerCase()+"* :"+sReadADGroupErrors);
} 
sADAllGroups.delete(0,sADAllGroups.length());
}

// Read ALL MSTR Groups
ResultSet oAllMSTRGroups = executeCapture("LIST ALL GROUPS;");
oAllMSTRGroups.moveFirst();
while (!oAllMSTRGroups.isEof()) {
//printOut(oAllMSTRGroups.getFieldValueString(DisplayPropertyEnum.NAME).toLowerCase()); 
hsAllMSTRGroups.add(oAllMSTRGroups.getFieldValueString(DisplayPropertyEnum.NAME).toLowerCase()); 
oAllMSTRGroups.moveNext();
}

//Compare to find New AD groups
HashSet <String>hsADOnlyGroups = new HashSet <String>();
for (Iterator ingrp= hsAllADGroups.iterator(); ingrp.hasNext(); ) {
String sGroup = ingrp.next().toString().trim().toLowerCase(); 
if (!hsAllMSTRGroups.contains(sGroup)) {
printOut("Microstrategy group missing for AD group "+sGroup);
hsAllMSTRGroups.remove(sGroup); 
} 
}

for (Iterator i3 = hsAllADGroups.iterator(); i3.hasNext();) {
String sADGroup = i3.next().toString().trim().toLowerCase();
printOut("Current AD Group: "+sADGroup.toLowerCase());

printOut("Command: cmd /c dsquery group domainroot -name \""+sADGroup+"\""+loginInfo);
sADDNGroup.append("cmd /c dsquery group domainroot -name \""+sADGroup+"\""+loginInfo);
Process procReadDNGroup=Runtime.getRuntime().exec(sADDNGroup.toString());
BufferedReader brInputReadDNGroup = new BufferedReader(new InputStreamReader( procReadDNGroup.getInputStream()));
BufferedReader brErrorReadDNGroup = new BufferedReader(new InputStreamReader( procReadDNGroup.getErrorStream()));
String sReadADDNGroupErrors = null; 
String sReadADDNGroup = null;
String DNGroup=null;
while ((sReadADDNGroup = brInputReadDNGroup.readLine())!= null) { 
//printOut("Line read: "+sReadADDNGroup);
DNGroup =sReadADDNGroup.trim().toLowerCase(); 
}
while ((sReadADDNGroupErrors = brErrorReadDNGroup.readLine()) != null) {
printOut("Error reading DN of group "+sADGroup+": "+sReadADDNGroupErrors); 
}

//printOut("Current AD Group DN: "+ DNGroup);
procReadDNGroup.waitFor();
sADDNGroup.delete(0,sADDNGroup.length());
//Read AD users by group name
sADAllUsers.append("cmd /c dsget group "+DNGroup+" -members -expand "+loginInfo+" | dsget user -samid -upn -email -c 2>null");
//printOut("cmd /c dsget group "+DNGroup+" -members -expand "+loginInfo+" | dsget user -samid -upn -email -c 2>null"); 
Process procReadADUsers=Runtime.getRuntime().exec(sADAllUsers.toString());
BufferedReader brInputReadADUsers = new BufferedReader(new InputStreamReader(procReadADUsers.getInputStream()));
BufferedReader brErrorReadADUsers = new BufferedReader(new InputStreamReader(procReadADUsers.getErrorStream()));
String sReadADUserErrors = null; 
String sReadADUser = brInputReadADUsers.readLine(); 
while ((sReadADUser = brInputReadADUsers.readLine())!= null) { 
sReadADUser=sReadADUser.trim().toLowerCase(); 
//printOut("User Read: "+sReadADUser );
//List<String> ADUserFieldList=new List<String>(); 
if((! sReadADUser.equals("dsget succeeded")) ) { 
String[] ADUserFieldList = sReadADUser.split("\\s+");
/*for (int i=0;i< ADUserFieldList.length;i++) {
printOut(ADUserFieldList[i]);
}*/ 
hsAllADUsers.add(Arrays.asList(ADUserFieldList)); 
} 
} 
while ((sReadADUserErrors = brErrorReadADUsers.readLine()) != null) {
printOut("Error reading members of AD group "+sADGroup+": "+sReadADUserErrors); 
}

int exitValue2 = procReadADUsers.waitFor();
printOut(" "+exitValue2);
sADAllUsers.delete(0,sADAllUsers.length()); 

//Read MSTR users in groups
ResultSet oAllMSTRGroupUsers = executeCapture("LIST LOGIN FOR USERS IN GROUP \""+sADGroup+"\";");

oAllMSTRGroupUsers.moveFirst();
while (!oAllMSTRGroupUsers.isEof()) {
hsMSTRGroupUsers.add(oAllMSTRGroupUsers.getFieldValueString(DisplayPropertyEnum.LOGIN).toLowerCase()); 
oAllMSTRGroupUsers.moveNext();
}

//Find Users missing in MSTR group
//HashSet <String>hsADOnlyUsers = new HashSet <String>();
for (Iterator imstr= hsAllADUsers.iterator(); imstr.hasNext(); ) {
List<String> sUserInfo =((List<String>) imstr.next());
printOut("Processing User: "+sUserInfo.get(0)+", list length: "+sUserInfo.size());
if (sUserInfo.size()<2) {
//user in invalid state
continue;
}
String sMSTRUser=sUserInfo.get(0).trim().toLowerCase();
String sMSTRUpn=sUserInfo.get(1).trim().toLowerCase();
String sMSTRMail=null;
try { 
sMSTRMail=sUserInfo.get(2).trim().toLowerCase();
}catch (Exception e) {
printOut("no e-mail address set up on AD for user "+sMSTRUser);
}
if (!hsMSTRGroupUsers.contains(sMSTRUser)) {
//Check if user is present in every one mstr group
if(!hsAllMSTRUsers.contains(sMSTRUser)) {
printOut("Creating User..."+sMSTRUser);
if (execute("CREATE USER '"+sMSTRUser+"' TRUSTEDLOGIN '"+sMSTRUser+"' FULLNAME '"+sMSTRUpn.replaceAll("'","^'")+"' ALLOWSTDAUTH FALSE ;") == ErrorHandler.EXIT_CODE_SUCCESS) {
//printOut("New User created: "+sMSTRUser);
try {
//BufferedReader oBufReaderemail = null;
if (!sMSTRMail.isEmpty()) {
execute("ADD ADDRESS \"MyEmail\" PHYSICALADDRESS \""+ sMSTRMail+"\" DELIVERYTYPE EMAIL DEVICE \"mstrblog mail\" TO USER \""+sMSTRUser+"\";");
}
}
catch (Exception e) {
printErr("exception " + e.getLocalizedMessage()); 
}
}
else { 
printErr("Create user failed: "+sMSTRUser);
} 
hsAllMSTRUsers.add(sMSTRUser);
}
printOut(sMSTRUser+" is missing in mstr group" );
printOut("Adding user to group "+sADGroup);
if (execute("ADD USER '"+sMSTRUser+"' TO GROUP '"+sADGroup+"';") == ErrorHandler.EXIT_CODE_SUCCESS){
printOut(sMSTRUser+" sucessfully added to group "+sADGroup);
}
else { 
printErr("Adding user "+sMSTRUser+" to group "+sADGroup+" failed.");
} 
} 
} 
hsAllADUsers.clear();
hsMSTRGroupUsers.clear();
}

Leave a Reply

Your email address will not be published. Required fields are marked *