Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

feat:add utils for ldap with novell #31

Merged
merged 5 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions Masa.Utils.sln
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Caller.Core.Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Caller.IntegratedTests", "test\Masa.Utils.Caller.IntegratedTests\Masa.Utils.Caller.IntegratedTests.csproj", "{D5E09920-081B-478C-B77B-EAB77931F47D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Utils.Caller.Tests", "test\Masa.Utils.Caller.Tests\Masa.Utils.Caller.Tests.csproj", "{276B4CDC-BD8F-4563-A82A-B46724C9F4B0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Caller.Tests", "test\Masa.Utils.Caller.Tests\Masa.Utils.Caller.Tests.csproj", "{276B4CDC-BD8F-4563-A82A-B46724C9F4B0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Data.EntityFrameworkCore.SqlServer", "src\Data\Masa.Utils.Data.EntityFrameworkCore.SqlServer\Masa.Utils.Data.EntityFrameworkCore.SqlServer.csproj", "{9004B687-3EB7-4156-A3F7-078A342F2BEF}"
EndProject
Expand All @@ -97,11 +97,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Data.EntityFrame
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Data.EntityFrameworkCore.PostgreSql", "src\Data\Masa.Utils.Data.EntityFrameworkCore.PostgreSql\Masa.Utils.Data.EntityFrameworkCore.PostgreSql.csproj", "{D682F08B-6586-4C33-815D-8F98C50A0843}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Utils.Data.EntityFrameworkCore.Pomelo.MySql", "src\Data\Masa.Utils.Data.EntityFrameworkCore.Pomelo.MySql\Masa.Utils.Data.EntityFrameworkCore.Pomelo.MySql.csproj", "{2AB5DFD8-B537-4066-8366-E3F371BD5EE2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Data.EntityFrameworkCore.Pomelo.MySql", "src\Data\Masa.Utils.Data.EntityFrameworkCore.Pomelo.MySql\Masa.Utils.Data.EntityFrameworkCore.Pomelo.MySql.csproj", "{2AB5DFD8-B537-4066-8366-E3F371BD5EE2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Utils.Data.EntityFrameworkCore.Cosmos", "src\Data\Masa.Utils.Data.EntityFrameworkCore.Cosmos\Masa.Utils.Data.EntityFrameworkCore.Cosmos.csproj", "{B8CB480B-2608-46A8-8528-37EDD5E1EC1E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Data.EntityFrameworkCore.Cosmos", "src\Data\Masa.Utils.Data.EntityFrameworkCore.Cosmos\Masa.Utils.Data.EntityFrameworkCore.Cosmos.csproj", "{B8CB480B-2608-46A8-8528-37EDD5E1EC1E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Utils.Data.EntityFrameworkCore.Tests", "test\Masa.Utils.Data.EntityFrameworkCore.Tests\Masa.Utils.Data.EntityFrameworkCore.Tests.csproj", "{41C2A208-4DF8-4BD0-B844-77E6F58FE000}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Data.EntityFrameworkCore.Tests", "test\Masa.Utils.Data.EntityFrameworkCore.Tests\Masa.Utils.Data.EntityFrameworkCore.Tests.csproj", "{41C2A208-4DF8-4BD0-B844-77E6F58FE000}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Ldap", "Ldap", "{59124A98-14AF-4201-87D7-6DC9C92D9849}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Ldap.Novell", "src\Ldap\Masa.Utils.Ldap.Novell\Masa.Utils.Ldap.Novell.csproj", "{E62E5E11-7119-4782-BCF4-A5913F4048FE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Ldap.Novell.Tests", "test\Masa.Utils.Ldap.Tests\Masa.Utils.Ldap.Novell.Tests\Masa.Utils.Ldap.Novell.Tests.csproj", "{F6242694-66A5-4E59-9ECD-510D3D81C8C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -257,6 +263,14 @@ Global
{41C2A208-4DF8-4BD0-B844-77E6F58FE000}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41C2A208-4DF8-4BD0-B844-77E6F58FE000}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41C2A208-4DF8-4BD0-B844-77E6F58FE000}.Release|Any CPU.Build.0 = Release|Any CPU
{E62E5E11-7119-4782-BCF4-A5913F4048FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E62E5E11-7119-4782-BCF4-A5913F4048FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E62E5E11-7119-4782-BCF4-A5913F4048FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E62E5E11-7119-4782-BCF4-A5913F4048FE}.Release|Any CPU.Build.0 = Release|Any CPU
{F6242694-66A5-4E59-9ECD-510D3D81C8C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F6242694-66A5-4E59-9ECD-510D3D81C8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6242694-66A5-4E59-9ECD-510D3D81C8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6242694-66A5-4E59-9ECD-510D3D81C8C6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -297,8 +311,8 @@ Global
{1A8FBE3D-B004-4D25-8FE0-A3632A0FDE9E} = {4F908878-0EB8-43E4-96E4-8B1F32E9B635}
{4B351F95-5919-46A9-A02F-F4409C9EA79A} = {4F908878-0EB8-43E4-96E4-8B1F32E9B635}
{D5E09920-081B-478C-B77B-EAB77931F47D} = {4F908878-0EB8-43E4-96E4-8B1F32E9B635}
{9004B687-3EB7-4156-A3F7-078A342F2BEF} = {F844C2A1-C36D-400E-A0D8-7658EF9C3B93}
{276B4CDC-BD8F-4563-A82A-B46724C9F4B0} = {4F908878-0EB8-43E4-96E4-8B1F32E9B635}
{9004B687-3EB7-4156-A3F7-078A342F2BEF} = {F844C2A1-C36D-400E-A0D8-7658EF9C3B93}
{4C3C99D6-A832-4D14-B1C8-54B6E0780F24} = {F844C2A1-C36D-400E-A0D8-7658EF9C3B93}
{8AE29CEB-89D3-4AE3-8F97-D03D19F0B6FB} = {F844C2A1-C36D-400E-A0D8-7658EF9C3B93}
{95A1CAC2-42F5-4916-99D2-64D9C830B1B3} = {F844C2A1-C36D-400E-A0D8-7658EF9C3B93}
Expand All @@ -307,6 +321,9 @@ Global
{2AB5DFD8-B537-4066-8366-E3F371BD5EE2} = {F844C2A1-C36D-400E-A0D8-7658EF9C3B93}
{B8CB480B-2608-46A8-8528-37EDD5E1EC1E} = {F844C2A1-C36D-400E-A0D8-7658EF9C3B93}
{41C2A208-4DF8-4BD0-B844-77E6F58FE000} = {4F908878-0EB8-43E4-96E4-8B1F32E9B635}
{59124A98-14AF-4201-87D7-6DC9C92D9849} = {D956582F-4071-47E6-A8E7-4C5A83770045}
{E62E5E11-7119-4782-BCF4-A5913F4048FE} = {59124A98-14AF-4201-87D7-6DC9C92D9849}
{F6242694-66A5-4E59-9ECD-510D3D81C8C6} = {4F908878-0EB8-43E4-96E4-8B1F32E9B635}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D7DAA0E6-098F-4B18-8775-64FDA96F1FF0}
Expand Down
16 changes: 16 additions & 0 deletions src/Ldap/Masa.Utils.Ldap.Novell/Entries/LdapAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Masa.Utils.Ldap.Novell.Entries;

public class LdapAddress
{
public string Street { get; set; } = string.Empty;

public string PostalCode { get; set; } = string.Empty;

public string City { get; set; } = string.Empty;

public string StateName { get; set; } = string.Empty;

public string CountryName { get; set; } = string.Empty;

public string CountryCode { get; set; } = string.Empty;
}
44 changes: 44 additions & 0 deletions src/Ldap/Masa.Utils.Ldap.Novell/Entries/LdapUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace Masa.Utils.Ldap.Novell.Entries;

public class LdapUser
{
public string SecurityIdentifier { get; set; } = string.Empty;

public string UniqueIdentifier { get; set; } = string.Empty;

public string Category { get; set; } = string.Empty;

public string Class { get; set; } = string.Empty;

public string Name { get; set; } = string.Empty;

public string CommonName { get; set; } = string.Empty;

public string DistinguishedName { get; set; } = string.Empty;

public string SamAccountName { get; set; } = string.Empty;

public int SamAccountType { get; set; }

public string[] MemberOf { get; set; } = Array.Empty<string>();

public bool IsDomainAdmin { get; set; }

public string UserPrincipalName { get; set; } = string.Empty;

public string DisplayName { get; set; } = string.Empty;

public string FirstName { get; set; } = string.Empty;

public string LastName { get; set; } = string.Empty;

public string FullName => $"{FirstName} {LastName}";

public string EmailAddress { get; set; } = string.Empty;

public string Description { get; set; } = string.Empty;

public string Phone { get; set; } = string.Empty;

public LdapAddress Address { get; set; } = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Masa.Utils.Ldap.Novell.Extensions;

public static class AsyncEnumerableExtensions
{
/// <summary>
/// Asynchronously materializes the subject <see cref="IAsyncEnumerable{T}"/> into a list.
/// </summary>
public static async Task<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> enumerable, CancellationToken cancellationToken = default)
{
var list = new List<T>();
await foreach (var element in enumerable.WithCancellation(cancellationToken))
{
list.Add(element);
}

return list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Masa.Utils.Ldap.Novell.Extensions;

public static class LdapAttributeSetExtensions
{
public static void AddAttribute(this LdapAttributeSet ldapAttributes, string name, string value)
{
if (!string.IsNullOrEmpty(value))
{
ldapAttributes.Add(new LdapAttribute(name, value));
}
}

public static string GetString(this LdapAttributeSet ldapAttributes, string name)
{
ldapAttributes.TryGetValue(name, out var value);
return value?.StringValue ?? "";
}

public static string[] GetStringArray(this LdapAttributeSet ldapAttributes, string name)
{
ldapAttributes.TryGetValue(name, out var value);
return value?.StringValueArray ?? new string[] { };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Masa.Utils.Ldap.Novell.Extensions;

public static class ServiceCollectionExtensions
{
private static IServiceCollection AddLadpContext(this IServiceCollection services, Action<LdapOptions> optionsAction)
{
services.Configure(optionsAction);
services.AddSingleton(typeof(ILdapProvider), typeof(LdapProvider));
return services;
}

public static IServiceCollection AddLadpContext(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<LdapOptions>(configuration);
services.AddSingleton(typeof(ILdapProvider), typeof(LdapProvider));
return services;
}
}
22 changes: 22 additions & 0 deletions src/Ldap/Masa.Utils.Ldap.Novell/ILdapProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Masa.Utils.Ldap.Novell;

public interface ILdapProvider
{
Task<LdapEntry?> GetGroupAsync(string groupName);

IAsyncEnumerable<LdapUser> GetUsersInGroupAsync(string groupName);

Task<LdapUser?> GetUsersByEmailAddressAsync(string emailAddress);

Task<LdapUser?> GetUserByUserNameAsync(string userName);

IAsyncEnumerable<LdapUser> GetAllUserAsync();

Task<List<LdapEntry>> GetPagingUserAsync(int pageSize);

Task AddUserAsync(LdapUser user, string password);

Task DeleteUserAsync(string distinguishedName);

Task<bool> AuthenticateAsync(string distinguishedName, string password);
}
46 changes: 46 additions & 0 deletions src/Ldap/Masa.Utils.Ldap.Novell/LdapOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Masa.Utils.Ldap.Novell;

public class LdapOptions
{
public string ServerAddress { get; set; } = null!;

public int ServerPort { get; set; }

public int ServerPortSsl { get; set; }

public string BaseDn { get; set; } = null!;

private string _userSearchBaseDn = string.Empty;

public string UserSearchBaseDn
{
get
{
if (string.IsNullOrEmpty(_userSearchBaseDn))
{
return BaseDn;
}
return _userSearchBaseDn;
}
set { _userSearchBaseDn = value; }
}

private string _groupSearchBaseDn = string.Empty;

public string GroupSearchBaseDn
{
get
{
if (string.IsNullOrEmpty(_groupSearchBaseDn))
{
return BaseDn;
}
return _groupSearchBaseDn;
}
set { _groupSearchBaseDn = value; }
}

public string RootUserDn { get; set; } = null!;

public string RootUserPassword { get; set; } = null!;
}
Loading