Created the framework class called ADReader which reads the data from ActiveDirectory of current domains in the nework that connected.
static void krishh_ADExportToCSV(Args _args)
{
AxaptaUserManager mgr;
container domainNames;
int i;
int domainCount;
str name;
System.Collections.ArrayList domains = new System.Collections.ArrayList();
TextIo ioData;
FileIOPermission permission;
str filename = "c:\\temp\\tmotest_"+FileUtil::createFilenameTimeStamp()+".csv";
ADReader adReader = new ADReader();
map costcenter = new Map(types::String, types::String);
str key;
#File
permission = new FileIOPermission(filename, #io_write);
permission.assert();
// Open file for writing
ioData = new TextIo(filename, #io_write);
// Check file open status
if (!ioData)
{
// File '%1' could not be opened.
throw error(strfmt("@SYS19312", filename));
}
// Set file delimiters
ioData.outRecordDelimiter('\r\n');
ioData.outFieldDelimiter(';');
ioData.write(
[
"Domain"
, "Company"
, "Country"
, "DepartmentNumber"
, "EmployeeType"
, "Mail"
, "Manager"
, "Manager_Converted"
, "GivenName"
, "Sn"
, "TelephoneNumber"
, "UserId"
, "samAccountName"
, "EmployeeNumber"
, "EmployeeNumber_Converted"
, "Disabled"
, "MemberOf"
, "ADGroups"
]);
mgr = new AxaptaUserManager();
// Get the names of the domains from the kernel class
domainNames = mgr.enumerateDomains('');
if(domainNames)
{
domainCount = conLen(domainNames);
// Add all the domain names to the domains combo box
// dual loop to sort the domains alphabetically using the default comparator
if(domainCount > 0)
{
domains.Clear();
for(i = 0; i < domainCount; i++)
{
if (conPeek(domainNames, i+1) == "Krishhdax.com")
{
adReader.reset();
adReader.parmSelectedDomainName(conPeek(domainNames, i+1));
while(adReader.next())
{
if ( adReader.getEmployeeType() == "Employee Master Account"
|| adReader.getEmployeeType() == "Consultant Master Account")
{
ioData.write(
[
adReader.parmSelectedDomainName()
, adReader.getCompany()
, adReader.getCountry()
, adReader.getDepartmentNumber()
, adReader.getEmployeeType()
, adReader.getMail()
, adReader.getManager()
, subStr(adReader.getManager()
, strfind(adReader.getManager(), "CN=", 1, strLen(adReader.getManager()))+3
, strfind(adReader.getManager(), ",", strfind(adReader.getManager(), "CN=", 1, strLen(adReader.getManager())), strLen(adReader.getManager())) - strfind(adReader.getManager(), "CN=",1 , strLen(adReader.getManager())) - 3
)
, adReader.getGivenName()
, adReader.getSn()
, adReader.getTelephoneNumber()
, adReader.getUserId()
, adReader.getsamAccountName()
, adReader.getEmployeeNumber()
, subStr(adReader.getEmployeeNumber(), 4, 255)
, adReader.getDisabled()
, con2Str(adReader.getMemberOf())
, con2Str(adReader.getADGroups())
]);
}
}
}
}
}
}
CodeAccessPermission::revertAssert();
}
class ADReader
{
System.DirectoryServices.DirectorySearcher directorySearcher;
System.DirectoryServices.SearchResultCollection searchResultCollection;
System.DirectoryServices.SearchResult searchResult;
System.DirectoryServices.DirectoryEntry entry;
System.DirectoryServices.PropertyValueCollection propertyValueCollection;
System.DirectoryServices.SearchScope searchScope;
str selectedDomainName;
str prefix;
str criteria;
Filename filename;
System.Collections.Specialized.StringCollection propertyList;
System.DirectoryServices.PropertyCollection propertyCollection;
int numberOfUsers, currentUserNumber;
NetworkAlias networkAlias;
Description company;
Description department;
Description title;
Email mail;
Name givenName;
str manager, departmentNumber, telephoneNumber, userId, sn, employeeType, employeeNumber, country, samAccountName;
container memberOf, adGroups;
Integer userAccountControl;
}
public container getADGroups()
{
str memberOfStr, searchStr;
Counter pos, posCount;
Counter posFrom, posTo, searchStrLen;
if (!ADGroups)
{
searchStr = 'CN=';
searchStrLen = strLen(searchStr);
posCount = conlen(memberOf);
for (Pos=1; pos<=posCount;pos++)
{
memberOfStr = conpeek(memberOf, pos);
posTo = 0;
posFrom = strScan(memberOfStr, searchStr, posTo, strLen(memberOfStr));
while (posFrom)
{
posTo = strScan(memberOfStr, ',', posFrom, strLen(memberOfStr));
ADGroups += [subStr(memberOfStr, posFrom+searchStrLen, posTo-posFrom-searchStrLen)];
posFrom = strScan(memberOfStr, searchStr, posTo, strLen(memberOfStr));
}
}
}
return ADGroups;
}
private void setSearchPropertyList(System.DirectoryServices.DirectorySearcher _directorySearcher)
{
propertyList = _directorySearcher.get_PropertiesToLoad();
propertyList.Add('samaccountname');
propertyList.Add('company');
propertyList.Add('department');
propertyList.Add('title');
propertyList.Add('displayName');
propertyList.Add('distinguishedName');
propertyList.Add('objectClass');
propertyList.Add('member');
propertyList.Add('manager');
propertyList.Add('mail');
propertyList.Add('telephoneNumber');
propertyList.Add('Userid');
propertyList.Add('sn');
propertyList.Add('EmployeeType');
propertyList.Add('employeeNumber');
propertyList.Add('c');
propertyList.Add('samAccountName');
}
public boolean next()
{
Counter pos, posCount;
str memberOfStr;
CLRObject clrenumerator;
networkAlias = "";
company = "";
department = "";
title = "";
mail = "";
givenName = "";
manager = "";
departmentNumber = "";
telephoneNumber = "";
userId = "";
sn = "";
employeeType = "";
adGroups = conNull();
memberOf = conNull();
try
{
if (currentUserNumber == 0 && searchResultCollection == null)
{
prefix = 'LDAP://';
searchScope = System.DirectoryServices.SearchScope::Subtree;
entry = new System.DirectoryServices.DirectoryEntry(prefix+selectedDomainName);
directorySearcher = new System.DirectoryServices.DirectorySearcher(entry);
directorySearcher.set_PageSize(65535);
directorySearcher.set_CacheResults(false);
directorySearcher.set_SearchScope(searchScope);
directorySearcher.set_Filter(strFmt('(&(objectClass=user)(objectCategory=person)%1(userAccountControl:1.2.840.113556.1.4.803:=512))', criteria));
this.setSearchPropertyList(directorySearcher);
searchResultCollection = directorySearcher.FindAll();
numberOfUsers = searchResultCollection.get_Count();
}
do
{
currentUserNumber++;
if (currentUserNumber > numberOfUsers)
{
return false;
}
searchResult = searchResultCollection.get_Item(currentUserNumber-1);
entry = searchResult.GetDirectoryEntry();
if (entry)
{
PropertyCollection = entry.get_Properties();
}
if (!PropertyCollection)
{
entry.Dispose();
}
}
while (!PropertyCollection);
propertyValueCollection = PropertyCollection.get_Item('samaccountname');
if (PropertyValueCollection)
{
if (PropertyValueCollection.get_Value())
{
NetworkAlias = PropertyValueCollection.get_Value();
}
}
propertyValueCollection = PropertyCollection.get_Item('company');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
company = PropertyValueCollection.get_Value();
}
}
propertyValueCollection = PropertyCollection.get_Item('telephoneNumber');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
telephoneNumber = PropertyValueCollection.get_Value();
telephoneNumber = telephoneNumber; //str2int(telephoneNumber)==0 ? "" : telephoneNumber;
}
}
PropertyValueCollection = propertyCollection.get_Item('department');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
department = PropertyValueCollection.get_Value();
}
}
PropertyValueCollection = propertyCollection.get_Item('departmentNumber');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
departmentNumber = PropertyValueCollection.get_Value();
}
}
PropertyValueCollection = propertyCollection.get_Item('title');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
title = PropertyValueCollection.get_Value();
}
}
PropertyValueCollection = propertyCollection.get_Item('mail');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
mail = PropertyValueCollection.get_Value();
}
}
PropertyValueCollection = propertyCollection.get_Item('givenName');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
givenName = PropertyValueCollection.get_Value();
}
}
PropertyValueCollection = propertyCollection.get_Item('Userid');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
UserId = PropertyValueCollection.get_Value();
}
}
PropertyValueCollection = propertyCollection.get_Item('EmployeeNumber');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
employeeNumber = PropertyValueCollection.get_Value();
}
}
PropertyValueCollection = propertyCollection.get_Item('sn');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
sn = PropertyValueCollection.get_Value();
}
}
PropertyValueCollection = propertyCollection.get_Item('EmployeeType');
if (PropertyValueCollection && PropertyValueCollection.get_Count())
{
if (PropertyValueCollection.get_Value())
{
employeeType = PropertyValueCollection.get_Value();
}
}
propertyValueCollection = PropertyCollection.get_Item('manager');
if (PropertyValueCollection)
{
if (PropertyValueCollection.get_Value())
{
manager = PropertyValueCollection.get_Value();
}
}
propertyValueCollection = PropertyCollection.get_Item('c');
if (PropertyValueCollection)
{
if (PropertyValueCollection.get_Value())
{
country = PropertyValueCollection.get_Value();
}
}
propertyValueCollection = PropertyCollection.get_Item('samAccountName');
if (PropertyValueCollection)
{
if (PropertyValueCollection.get_Value())
{
samAccountName = PropertyValueCollection.get_Value();
}
}
propertyValueCollection = PropertyCollection.get_Item('userAccountControl');
if (PropertyValueCollection)
{
if (PropertyValueCollection.get_Value())
{
userAccountControl = PropertyValueCollection.get_Value();
}
}
propertyValueCollection = PropertyCollection.get_Item('memberOf');
if (PropertyValueCollection)
{
if (PropertyValueCollection.get_Value())
{
clrenumerator = PropertyValueCollection.GetEnumerator();
while (clrenumerator.MoveNext())
{
memberOfStr = clrenumerator.get_Current();
memberOf += [memberOfStr];
}
//posCount = PropertyValueCollection.get_Count();
//for (Pos=0; pos<posCount;pos++)
//{
//memberOfStr = PropertyValueCollection.get_Item(pos);
//memberOf += [memberOfStr];
//}
}
}
}
catch (Exception::CLRError)
{
//SRSProxy::handleClrException(Exception::Warning);
//warning(strFmt("@SYS117734"));
curext();
}
return true;
}
public str getCompany()
{
return company;
}
public str getCountry()
{
return country;
}
public str getDepartmentNumber()
{
return departmentNumber;
}
public boolean getDisabled()
{
// Added new conditions for when a user should be seen as disabled.
boolean ret;
;
//ACCOUNTDISABLE 0x0002 2
ret = bitTest(userAccountControl, 1 << 1);
if (!ret && title == 'SLUTAT')
ret = true;
if (!ret && (strLen(departmentNumber) > 0 && !strRem(departmentNumber,'0')) && (strLen(employeeNumber) > 0 && !strRem(employeeNumber,'0')))
ret = true;
return ret;
}
public str getEmployeeNumber()
{
return employeeNumber;
}
public str getEmployeeType()
{
return EmployeeType;
}
public str getGivenName()
{
return givenName;
}
public email getMail()
{
return mail;
}
public str getManager()
{
return subStr(manager
, strfind(manager, 'CN=', 1, strLen(manager))+3
, strfind(manager, ',', strfind(manager, 'CN=', 1, strLen(manager)), strLen(manager)) - strfind(manager, 'CN=',1 , strLen(manager)) - 3
);
}
public container getMemberOf()
{
return memberOf;
}
public str getSamAccountName()
{
return samAccountName;
}
public str getSn()
{
return sn;
}
public str getTelephoneNumber()
{
return telephoneNumber;
}
public str getUserId()
{
return UserId;
}
public int parmCurrentUserNumber(int _currentUserNumber = currentUserNumber)
{
currentUserNumber = _currentUserNumber;
return currentUserNumber;
}
public str parmFileName(Filename _filename = filename)
{
filename = _filename;
return filename;
}
public str parmselectedDomainName(str _selectedDomainName = selectedDomainName)
{
selectedDomainName = _selectedDomainName;
return selectedDomainName;
}
public void reset()
{
currentUserNumber = 0;
}
No comments:
Post a Comment
Thanks for visiting my blog,
I will reply for your comment within 48 hours.
Thanks,
krishna.