Rules Extensions – Helper Functions


 

This post is focused on Helper Functions  that Multiple Methods can call to complete a task, additionally talks about Function Overloading, “Overloaded functions enable programmers to supply different semantics for a function, depending on the types and number of arguments.”

See Referenced Documents:

Understanding the Helper Function

Function Overloading

Account-Expires attribute

Pwd-Last-Set attribute

Last-Logon-Timestamp attribute

When-Created attribute

The following is a snippet of code which I use to allow multiple methods to call the same functions with out the need to copy the function into each method. This way if I need to update a function I am only updating the function in one place.

The following code can be found on on the “Rules Extensions – MA Extension” Post I use a reference example to detail what the completed MA Extension should look like (as in format and placement of the code not the actual code, all environments are different and this code is to be used as a guide only)

#region helper functions

//1st GetDateString Function

private static void GetDateString(CSEntry csentry, MVEntry mventry, long dtInt, string mvAttrib, string sourceFormat, string targetFormat, int days = 0)
{
if (dtInt == 0 || dtInt == 9223372036854775807)
{
// This is a special condition, do not contribute and delete any current value
mventry[mvAttrib].Delete();
}
else
{
DateTime dtFileTime = DateTime.FromFileTime(dtInt).AddDays(days);
if (targetFormat.Equals("LONG", StringComparison.OrdinalIgnoreCase))
{
mventry[mvAttrib].Value = dtFileTime.ToLongDateString();

}
else if (targetFormat.Equals("SHORT", StringComparison.OrdinalIgnoreCase))
{
mventry[mvAttrib].Value = dtFileTime.ToShortDateString();
}
else
mventry[mvAttrib].Value = dtFileTime.ToString(targetFormat);
// mventry[mvAttrib].Value = DateTime.FromFileTimeUtc(dtInt).ToString(targetFormat);
}
}
// 2nd GetDateString function

//(CSEntry csentry, MVEntry mventry, long dtInt, string mvAttrib, string targetFormat, int days = 0)
private static void GetDateString(CSEntry csentry, MVEntry mventry, string dateStr, string mvAttrib, string sourceFormat, string targetFormat, int days = 0)
{
DateTime dt = DateTime.ParseExact(dateStr, sourceFormat, CultureInfo.InvariantCulture);

// drops into 1st GetDateString Function
GetDateString(csentry, mventry, dt.ToFileTime(), mvAttrib, sourceFormat, targetFormat, days);
}
private static string ConvertFileTimeToFimTimeStamp(long fileTime)
{
return DateTime.FromFileTimeUtc(fileTime).ToString("yyyy-MM-ddTHH:mm:ss.000");
}

private static string ConvertSidToString(byte[] objectSid)
{
string objectSidString = "";
SecurityIdentifier SI = new SecurityIdentifier(objectSid, 0);
objectSidString = SI.ToString();
return objectSidString;
}

#endregion

 

Now the above Snippet shows the helper functions which can be called but now lets look at how these functions are called.

Lets start with the first helper function and lets looks at the first couple of lines

private static void GetDateString(CSEntry csentry, MVEntry mventry, long dtInt, string mvAttrib, string sourceFormat, string targetFormat, int days = 0)

now lets look at a method that calls this function which can be found Rules Extensions – MapAttributesForImport

case “employeeEndDate”:
csAttrib = “accountExpires”;
mvAttrib = “employeeEndDate”;
dtInt = csentry[csAttrib].IntegerValue;
//targetFormat = “yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss’.000′”;
targetFormat = “yyyy-MM-ddTHH:mm:ss.000”;
//targetFormat = “M/d/yyyy h:mm tt”;
sourceFormat = string.Empty;
GetDateString(csentry, mventry, dtInt, mvAttrib, sourceFormat, targetFormat);
break;

Notice the Highlighted section GetDateString(csentry, mventry, dtInt, mvAttrib, sourceFormat, targetFormat);

and now look at the first line of the helper function private static void GetDateString(CSEntry csentry, MVEntry mventry, long dtInt, string mvAttrib, string sourceFormat, string targetFormat, int days = 0)

What do you notice? The Method supplies 6 arguments but the Helper Function has 7 arguments 1 being a constant int days = 0 we will get deeper into that in a minute but for now just know that because it is a constant you don't need to send that in as an argument from the method unless the value is different than the default constant value which in this example is 0 Zero.

As long as the Method that is calling the function by the function name which in the example is GetDateString and sending at a minimal of 6 arguments in the same order that the Helper Function is expecting them, you should be able to call the function within the method.

if you notice in the referenced post of Rules Extensions – MapAttributesForImport there are several methods that all call the same function.

case “employeeEndDate”:

case “pwdLastSet”:

case “pwdExpires”:

case “lastLogonTimestamp”:

case “createdDate”:

 

The first 4 methods all have the same type of source attribute that represents the Date Time but the 5th method createdDate uses the source Active Directory attribute which is a UTC String attribute which defers from the other values which are a value that represents the number of 100-nanosecond intervals since January 1, 1601 (UTC). A value of 0 or 0x7FFFFFFFFFFFFFFF (9223372036854775807) indicates that the account never expires. So in order to be able to use the same function across all Methods I need to do what is called Function Overloading which looks at the incoming arguments and drops the call into the corresponding function with the same name.

Function 1

private static void GetDateString(CSEntry csentry, MVEntry mventry, long dtInt, string mvAttrib, string sourceFormat, string targetFormat, int days = 0)

Function 2

private static void GetDateString(CSEntry csentry, MVEntry mventry, string dateStr, string mvAttrib, string sourceFormat, string targetFormat, int days = 0)

Notice they both have the same Function Name of GetDateString

If you look into the 2nd Function you will see that it takes the arguments being fed into and prepares it to be dumped into the 1st Function

GetDateString(csentry, mventry, dt.ToFileTime(), mvAttrib, sourceFormat, targetFormat, days);

 

 

 

 

Comments (0)

Skip to main content