115 lines
4.5 KiB
C#

using AutoMapper;
using DigitalData.Core.Contracts.Application;
using System.DirectoryServices;
using System.Diagnostics.CodeAnalysis;
using System.DirectoryServices.AccountManagement;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Configuration;
namespace DigitalData.Core.Application
{
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
public class DirectoryService : ServiceBase, IDirectoryService
{
protected IMapper _mapper;
protected readonly DirectorySearcher _groupSearcher;
IConfiguration _configuration;
public readonly string SearchRootPath;
public DirectoryService(IMapper mapper, IConfiguration configuration, ILogger<DirectoryService> logger) {
_mapper = mapper;
_configuration = configuration;
var searchRoot = configuration["DirectorySearch:SearchRootPath"];
logger.LogInformation($"Search Root is {searchRoot}.");
SearchRootPath = _configuration["DirectorySearch:SearchRootPath"] ?? throw new ConfigurationErrorsException("SearchRootPath configuration is missing.");
_groupSearcher = new DirectorySearcher()
{
Filter = "(&(objectClass=group) (samAccountName=*))",
SearchScope = SearchScope.Subtree,
SizeLimit = 5000,
SearchRoot = new DirectoryEntry(searchRoot)
};
}
public IServiceResult<IEnumerable<ResultPropertyCollection>> ReadAllGroupAsCollection()
{
List<ResultPropertyCollection> list = new();
foreach (SearchResult result in _groupSearcher.FindAll())
{
ResultPropertyCollection rpc = result.Properties;
list.Add(rpc);
}
return Successful<IEnumerable<ResultPropertyCollection>>(list);
}
public IServiceResult<IEnumerable<ResultPropertyCollection>> ReadAllGroupAsCollection(string serverAddress, string username, string password)
{
var searcher = new DirectorySearcher()
{
Filter = "(&(objectClass=user)(sAMAccountName=*))",
SearchScope = SearchScope.Subtree,
SizeLimit = 5000,
SearchRoot = new DirectoryEntry($"LDAP://{serverAddress}/DC=dd-gan,DC=local,DC=digitaldata,DC=works", username, password)
};
List<ResultPropertyCollection> list = new();
foreach (SearchResult result in searcher.FindAll())
{
ResultPropertyCollection rpc = result.Properties;
list.Add(rpc);
}
return Successful<IEnumerable<ResultPropertyCollection>>(list);
}
public IServiceResult<IEnumerable<Dictionary<string, object>>> ReadGroupByPropertyName(string propertyName = "samaccountname")
{
List<Dictionary<string, object>> list = new();
foreach (SearchResult result in _groupSearcher.FindAll())
{
var value = result.Properties[propertyName];
if (value is not null)
list.Add(new Dictionary<string, object>()
{
[propertyName] = value
});
}
return Successful<IEnumerable<Dictionary<string, object>>>(list);
}
public IServiceResult<IEnumerable<UserPrincipalDto>> ReadUserByGroup<UserPrincipalDto>(string groupIdentityValue, IdentityType groupIdentityType = IdentityType.Name, bool recursive = true)
{
List<UserPrincipalDto> upDTOs = new();
using PrincipalContext context = new(ContextType.Domain);
using GroupPrincipal? groupPrincipal = GroupPrincipal.FindByIdentity(context, groupIdentityType, groupIdentityValue);
if (groupPrincipal is null)
return Failed<IEnumerable<UserPrincipalDto>>();
using PrincipalSearchResult<Principal> principalSearchResult = groupPrincipal.GetMembers(recursive);
foreach (Principal principal in principalSearchResult)
{
if (principal is UserPrincipal userPrincipal)
{
var upDto = _mapper.MapOrThrow<UserPrincipalDto>(userPrincipal);
upDTOs.Add(upDto);
}
}
return Successful<IEnumerable<UserPrincipalDto>>(upDTOs);
}
}
}