82 lines
3.3 KiB
C#
82 lines
3.3 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Contracts.Application;
|
|
using System.DirectoryServices;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.DirectoryServices.AccountManagement;
|
|
|
|
namespace DigitalData.Core.Application
|
|
{
|
|
public class DirectoryService : ServiceBase, IDirectoryService
|
|
{
|
|
protected IMapper _mapper;
|
|
protected readonly DirectorySearcher _groupSearcher;
|
|
|
|
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
|
public DirectoryService(IMapper mapper) {
|
|
_mapper = mapper;
|
|
_groupSearcher = new()
|
|
{
|
|
Filter = "(&(objectClass=group) (samAccountName=*))",
|
|
SearchScope = SearchScope.Subtree,
|
|
SizeLimit = 5000
|
|
};
|
|
}
|
|
|
|
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
|
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);
|
|
}
|
|
|
|
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
|
public IServiceResult<IEnumerable<Dictionary<string, object>>> ReadGroupByPropertyName(string propertyName)
|
|
{
|
|
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);
|
|
}
|
|
|
|
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
|
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);
|
|
}
|
|
}
|
|
} |