.Net and Active Directory - An OO solution to authority structure - Part 6

So, if we envision what we have represented so far, our Owner can "see" all those people in his immediate sphere of influence.  But if your boss's boss told you to do something, would you do it?  Well, maybe you wouldn't, but you should because ultimately he has authority over you.  That is what we will do in this post, expand our owner's vision so he can see not just the people and groups that directly report to him, but those that report to them, and so on and so forth.

Lets start with expanding the User object to capture, like our Owner, the Users and Groups that directly report to them.

private UserCollection directreports;
private GroupCollection groups;
 
/// <summary>
/// Collection of Users that report to this User
/// </summary>
public UserCollection DirectReports
{
    get 
    { return directreports;    }
    set 
    { directreports = value; }    
}
 
/// <summary>
/// Groups this User manages
/// </summary>
public GroupCollection Groups
{
    get{ return groups; }
    set{ groups = value;}
}

And much like our Owner, assign values to our new Collections:

public User(DirectoryEntry de, bool IterateBranch)
{
    .
    .
    .
 
    if(IterateBranch)
    {
        DirectReports = GetDirectReports();
        Groups = GetGroups();
    }
    else
    {
        DirectReports = new UserCollection();
        Groups = new GroupCollection();
    }
 
}

 

private UserCollection GetDirectReports()
{
    UserCollection rtn = new UserCollection();
    foreach( Object memberColl in this.originaldirectoryentry.Properties["directReports"])
    {
        DirectoryEntry userDE = new DirectoryEntry(DEFAULTDOMAIN +"/"+ memberColl, DEFAULTUSERNAME, DEFAULTPASSWORD, AuthenticationTypes.Secure);
            
        User user = new User(userDE, true);
        rtn.Add(user);
    }
    return rtn;
}
 
private GroupCollection GetGroups()
{
    GroupCollection rtn = new GroupCollection();
 
    foreach( Object memberColl in this.originaldirectoryentry.Properties["managedObjects"])
    {
        DirectoryEntry groupDE = new DirectoryEntry(DEFAULTDOMAIN +"/"+ memberColl, DEFAULTUSERNAME, DEFAULTPASSWORD, AuthenticationTypes.Secure);
        
        if(groupDE.SchemaClassName.ToString().ToLower() == "group")
        {
            Group grp = new Group(groupDE);
            rtn.Add(grp);
        }
    }
 
    return rtn;
}

And so, in the above we have drilled into the User's direct reports, and when we instantiate a new User object to represent them, they will in turn iterate over their direct reports.  In this way we recursively walk the tree of authority established by the organization until we get down to the people who can claim no one under their authority.  In other words, the people who actually work for a living. Laughing

You probably noticed that our User constructor takes a Boolean "IterateBranch".  And if you were paying especially close attention you'll have noticed that when we got our Owner's Peers we passed in "false" so as not to get get their Groups and DirectReports.  Now why is that?

The reason is that while our Owner has authority over his direct reports, and may delegate access to a peer, he has no authority over his peer's direct reports.

Alright, now lets apply the same logic to Groups:

private UserCollection users = null;
 
/// <summary>
/// Members of this group
/// </summary>
public UserCollection Users
{
    get { return users;    }
    set { users = value; } 
}
 
public Group(DirectoryEntry de)
{
    .
    .
    .
    Users = GetUsers();
    .
    .
    .
}
 
private UserCollection GetUsers()
{
    UserCollection userColl = new UserCollection();
 
    foreach( Object memberColl in this.originaldirectoryentry.Properties["member"])
    {
        DirectoryEntry userDE = new DirectoryEntry(DEFAULTDOMAIN +"/"+ memberColl, DEFAULTUSERNAME, DEFAULTPASSWORD, AuthenticationTypes.Secure);
            
        if(userDE.SchemaClassName.ToString().ToLower() == "user")
        {
            User user = new User(userDE, true);
            userColl.Add(user);
        }
    }
 
    return userColl;
}
 

Now when we create a Group object it will automatically enumerate it's members. And since those members are User objects and we pass in true for IterateBranch, we will walk this leg of the tree as well.

So, what we have now is a robust object which logically walks the organization by nature of the linkage established by direct reportage and membership in a group from our Owner down to the level of non-management.  I hope you're not tired yet because although we have gotten down to the roots of the organizational tree, the leaves lie above us.  In the next post we will be walking the tree UP and examining the person who is our owner's boss, and so on until we get to the head honcho, usually a CEO or something.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: AaronZalewski
Posted on: 1/23/2008 at 11:20 AM
Tags: , ,
Categories: Active Directory
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Comments are closed