Skip to content

Commit

Permalink
Added Manager and DirectReports properties and function GetUserByDist…
Browse files Browse the repository at this point in the history
…inguishedName
  • Loading branch information
muneeb456 committed Dec 3, 2016
1 parent fbccfe8 commit 5a78b7d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
41 changes: 39 additions & 2 deletions DirectoryLib/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// SPDX-License-Identifier: MIT

using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;
Expand Down Expand Up @@ -147,6 +148,26 @@ public UserAccount GetUserByUpn(string upn, string baseDistinguishedName = null)
}
}

public UserAccount GetUserByDistinguishedName(string distinguishedName)
{
string path = GetDirectoryPath();
string filter = $"(&(objectClass=person)(distinguishedName={distinguishedName}))";

using (var entry = new DirectoryEntry(path))
using (var search = new DirectorySearcher(entry, filter))
{
SearchResult result = search.FindOne();
if (result == null)
{
return null;
}
else
{
return GetUserFromResult(result);
}
}
}

/// <summary>
/// Get user by GetUserByGuid
/// </summary>
Expand Down Expand Up @@ -229,7 +250,7 @@ public UserAccount GetUserByNtName(string ntName)
}
return GetUser(domain, samName);
}

/// <summary>
/// Perform authentication and return the status for the account.
/// </summary>
Expand Down Expand Up @@ -413,7 +434,9 @@ private UserAccount GetUserFromResult(SearchResult result)
DistinguishedName = TryGetResult<string>(result, "distinguishedName"),
Domain = GetDomainFromDistinguishedName(TryGetResult<string>(result, "distinguishedName")),
SamAccountName = TryGetResult<string>(result, "samAccountName"),
UserPrincipalName = TryGetResult<string>(result, "userPrincipalName")
UserPrincipalName = TryGetResult<string>(result, "userPrincipalName"),
Manager = TryGetResult<string>(result, "manager"),
DirectReports = TryGetResultList<string>(result, "directReports"),
};
}

Expand All @@ -426,6 +449,20 @@ private T TryGetResult<T>(SearchResult result, string key)
return default(T);
}

private List<T> TryGetResultList<T>(SearchResult result, string key)
{
var list = new List<T>();
var valueCollection = result.Properties[key];
if (valueCollection.Count > 0)
{
foreach (T val in valueCollection)
{
list.Add(val);
}
}
return list;
}

/// <summary>
/// Extracts the domain component (exmpl.wdc.com) from distinguished name (...DC=exmpl,DC=wdc,DC=com)
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions DirectoryLib/Types/UserAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// THE SOFTWARE.
// SPDX-License-Identifier: MIT

using System.Collections.Generic;
using System.Text;

namespace Wdc.DirectoryLib.Types
Expand Down Expand Up @@ -183,5 +184,17 @@ public string ObjectGuidAsString
/// Used to store an image of a person using the JPEG File Interchange Format.
/// </summary>
public byte[] JpegPhoto { get; set; }

/// <summary>
/// Contains the distinguished name of the user who is the user's manager.
/// </summary>
public string Manager { get; set; }

/// <summary>
/// Contains the list of users that directly report to the user. The users that are listed as
/// reports are those that have the property manager property set to this user. Each item in the
/// list is a linked reference to the object that represents the user.
/// </summary>
public List<string> DirectReports { get; set; }
}
}

0 comments on commit 5a78b7d

Please sign in to comment.