Skip to content

Commit

Permalink
#8 Добавил конфигурирование для AD service
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryGhost committed Apr 14, 2020
1 parent e85585c commit 0cfcf36
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Idone/Idone.Back/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).Services
.AddIdoneIdentity()
.AddIdoneDb(connString)
.AddSecurityDi();
.AddSecurityDi(Configuration);
}
}
}
3 changes: 3 additions & 0 deletions Idone/Idone.Back/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
},
"ConnectionStrings": {
"default": "Server=(localdb)\\mssqllocaldb;Database=Idone;Trusted_Connection=True;ConnectRetryCount=0;"
},
"ActiveDirectory": {
"domain": "tomskasu"
}
}
8 changes: 6 additions & 2 deletions Idone/Idone.Security/SecurityApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using Idone.Security.Services;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

/// <summary>
Expand All @@ -14,10 +15,13 @@ public static class SecurityApp
/// </summary>
/// <param name="services"> Сервисы. </param>
/// <returns> Возвращает сервисы. </returns>
public static IServiceCollection AddSecurityDi(this IServiceCollection services)
public static IServiceCollection AddSecurityDi(this IServiceCollection services, IConfiguration config)
{
//TODO: при разрастании параметров выделить в отдельный класс десериализации настроек
var adDomain = config.GetSection("ActiveDirectory").GetSection("domain").Value;

services.AddScoped<UserService>();
services.AddScoped<AdService>();
services.AddScoped(s => new AdService(adDomain));

return services;
}
Expand Down
30 changes: 26 additions & 4 deletions Idone/Idone.Security/Services/AdService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace Idone.Security.Services
{
using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Net;

using Idone.DAL.Dictionaries;
using Idone.DAL.DTO;
Expand All @@ -18,7 +20,28 @@ namespace Idone.Security.Services
/// </summary>
internal class AdService
{
private const string DOMAIN = "tomskasu";
/// <summary>
/// Домен сервиса Active Directory.
/// </summary>
private readonly string _domain;

/// <summary>
/// Инициализировать зависимости.
/// </summary>
/// <param name="domain">Домен сервиса Active Directory.</param>
public AdService(string domain)
{
if (string.IsNullOrEmpty(domain))
{
throw new NullReferenceException($"Пустой аргумент {nameof(domain)}");
}
if (!Dns.GetHostAddresses(domain).Any())
{
var msg = $"Не найден домен сервиса Active Directory для переданного аргумента {nameof(domain)}";
throw new ArgumentException(msg);
}
_domain = domain;
}

/// <summary>
/// Найти пользователей по отображаемому имени.
Expand All @@ -27,8 +50,7 @@ internal class AdService
/// <returns> Возращает монаду с найденными совпадениями пользователей по отображаемому имени. </returns>
public Either<Error, IEnumerable<DtoAdUser>> FindUsersByDisplayName(string searchExpression)
{
//TODO: вынести домен в настройки
using (var ctx = new PrincipalContext(ContextType.Domain, DOMAIN))
using (var ctx = new PrincipalContext(ContextType.Domain, _domain))
using (var query = new UserPrincipal(ctx)
{
DisplayName = searchExpression
Expand Down Expand Up @@ -57,7 +79,7 @@ internal class AdService
/// <returns>Результат операции.</returns>
public Either<Error, Success> CreateUser(DtoNewAdUser newUser)
{
using (var ctx = new PrincipalContext(ContextType.Domain, DOMAIN))
using (var ctx = new PrincipalContext(ContextType.Domain, _domain))
using (var query = new UserPrincipal(ctx))
{
query.SamAccountName = newUser.Nickname;
Expand Down
5 changes: 4 additions & 1 deletion Idone/Idone.Tests/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
},
"ConnectionStrings": {
"default": "Server=172.17.0.3,1433;Database=Idone.Tests;User ID=SA;pwd=<qweQWE1234>;Trusted_Connection=False;"
},
"ActiveDirectory": {
"domain": "tomskasu"
}
}
}

0 comments on commit 0cfcf36

Please sign in to comment.