Skip to content

Commit

Permalink
update to new structure and multi tenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Dec 29, 2023
1 parent b6fe280 commit 9e8e9a9
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace EasyMicroservices.IdentityMicroservice.Contracts.Common
{
public class UserSummaryContract
{
public string WhiteLabelKey { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public class AddUserRequestContract
{
public string UserName { get; set; }
public string Password { get; set; }
public string WhiteLabelKey { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace EasyMicroservices.IdentityMicroservice.Contracts.Common
{
public class ApplicationInitializeRequestContract
{
public string WhiteLabelKey { get; set; }
public string Language { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.ContentsMicroservice.Clients" Version="0.0.0.19" />
<PackageReference Include="EasyMicroservices.Cores.AspEntityFrameworkCoreApi" Version="0.0.0.63" />
<PackageReference Include="EasyMicroservices.Logger" Version="0.0.0.5" />
<PackageReference Include="EasyMicroservices.Cores.AspEntityFrameworkCoreApi" Version="0.0.0.79" />
<PackageReference Include="EasyMicroservices.Logger" Version="0.0.0.6" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="EasyMicroservices.Mapper.CompileTimeMapper" Version="0.0.0.6" />
<PackageReference Include="EasyMicroservices.Mapper.SerializerMapper" Version="0.0.0.3" />
<PackageReference Include="EasyMicroservices.Serialization.Newtonsoft.Json" Version="0.0.0.8" />
<PackageReference Include="EasyMicroservices.Serialization.Newtonsoft.Json" Version="0.0.0.9" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using WhiteLables.GeneratedServices;

namespace EasyMicroservices.IdentityMicroservice.Helpers
{
Expand Down Expand Up @@ -41,10 +43,12 @@ public ClaimManager GetClaimManager()
{
return _service.GetService<ClaimManager>();
}

static List<ServiceAddressInfo> ServiceAddresses;
string GetValue(string key)
{
return GetConfiguration().GetValue<string>(key);
if (ServiceAddresses == null)
ServiceAddresses = GetConfiguration().GetSection("ServiceAddresses").Get<List<ServiceAddressInfo>>();
return ServiceAddresses.FirstOrDefault(x => x.Name.Equals(key, StringComparison.OrdinalIgnoreCase)).Address;
}

public IHttpContextAccessor GetHttpContextAccessor()
Expand All @@ -64,27 +68,32 @@ T InternalLogin<T>(T client)

public LanguageClient GetLanguageClient()
{
return InternalLogin(new LanguageClient(GetValue("RootAddresses:Contents"), CurrentHttpClient));
return InternalLogin(new LanguageClient(GetValue("Content"), CurrentHttpClient));
}

public UserClient GetUserClient()
{
return InternalLogin(new UserClient(GetValue("RootAddresses:Authentications"), CurrentHttpClient));
return InternalLogin(new UserClient(GetValue("Authentication"), CurrentHttpClient));
}

public RoleClient GetRoleClient()
{
return InternalLogin(new RoleClient(GetValue("RootAddresses:Authentications"), CurrentHttpClient));
return InternalLogin(new RoleClient(GetValue("Authentication"), CurrentHttpClient));
}

public PersonalAccessTokenClient GetPersonalAccessTokenClientClient()
{
return InternalLogin(new PersonalAccessTokenClient(GetValue("RootAddresses:Authentications"), CurrentHttpClient));
return InternalLogin(new PersonalAccessTokenClient(GetValue("Authentication"), CurrentHttpClient));
}

public ILoggerProvider GetLogger()
{
return ServiceProvider.GetService<ILoggerProvider>();
}

public WhiteLabelClient GetWhiteLabelClient()
{
return InternalLogin(new WhiteLabelClient(GetValue("WhiteLabel"), CurrentHttpClient));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Authentications.GeneratedServices;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.IdentityMicroservice.Contracts.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
Expand Down Expand Up @@ -116,7 +117,7 @@ public List<ClaimContract> SetUniqueIdentity(string value, List<ClaimContract> c
if (value != null)
claims.Add(new ClaimContract
{
Name = "UniqueIdentity",
Name = nameof(IUniqueIdentitySchema.UniqueIdentity),
Value = value
});
return claims;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Authentications.GeneratedServices;
using EasyMicroservices.IdentityMicroservice.Contracts.Common;
using EasyMicroservices.IdentityMicroservice.Contracts.Common;
using EasyMicroservices.IdentityMicroservice.Contracts.Responses;
using EasyMicroservices.IdentityMicroservice.Interfaces;
using EasyMicroservices.ServiceContracts;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using FailedReasonType = EasyMicroservices.ServiceContracts.FailedReasonType;

namespace EasyMicroservices.IdentityMicroservice.Helpers
{
Expand All @@ -24,31 +23,46 @@ public IdentityHelper(IAppUnitOfWork appUnitOfWork)

public async Task<MessageContract<RegisterResponseContract>> Register(Contracts.Requests.AddUserRequestContract request)
{
var client = _appUnitOfWork.GetUserClient();
var usersRecords = await client.GetUserByUserNameAsync(new GetUserByUserNameRequestContract
var whiteLabelClient = _appUnitOfWork.GetWhiteLabelClient();
var uniqueIdentityOfBusiness = await whiteLabelClient.GetUniqueIdentityByKeyAsync(new WhiteLables.GeneratedServices.GuidGetByIdRequestContract()
{
Username = request.UserName.ToLower()
Id = System.Guid.Parse(request.WhiteLabelKey)
}).AsCheckedResult(x => x.Result);

var user = await client.AddAsync(new AddUserRequestContract
var client = _appUnitOfWork.GetUserClient();
var user = await client.GetUserByUserNameAsync(new Authentications.GeneratedServices.GetUserByUserNameRequestContract
{
UserName = request.UserName,
Password = request.Password
UserName = request.UserName.ToLower(),
UniqueIdentity = uniqueIdentityOfBusiness
});
if (user.IsSuccess)
return (FailedReasonType.Duplicate, $"User with UserName: {request.UserName} already exists!");
var addedUserId = await client.AddAsync(new Authentications.GeneratedServices.AddUserRequestContract
{
UserName = request.UserName,
Password = request.Password,
UniqueIdentity = uniqueIdentityOfBusiness
}).AsCheckedResult(x => x.Result);

return new RegisterResponseContract
{
UserId = user.Result,
UserId = addedUserId,
};
}

public virtual async Task<LoginResponseContract> Login(Contracts.Common.UserSummaryContract cred)
public virtual async Task<LoginResponseContract> Login(Contracts.Common.UserSummaryContract request)
{
var _whiteLabelClient = _appUnitOfWork.GetWhiteLabelClient();
var uniqueIdentity = await _whiteLabelClient.GetUniqueIdentityByKeyAsync(new WhiteLables.GeneratedServices.GuidGetByIdRequestContract
{
Id = Guid.Parse(request.WhiteLabelKey)
}).AsCheckedResult(x => x.Result);
var client = _appUnitOfWork.GetUserClient();
var user = await client.VerifyUserIdentityAsync(new Authentications.GeneratedServices.UserSummaryContract
{
UserName = cred.UserName,
Password = cred.Password
UserName = request.UserName,
Password = request.Password,
UniqueIdentity = uniqueIdentity
}).AsCheckedResult(x => x.Result);


Expand All @@ -71,9 +85,10 @@ public async Task<string> GetFullAccessPersonalAccessToken(string personalAccess
Value = personalAccessToken
}).AsCheckedResult(x => x.Result);

var roles = await _appUnitOfWork.GetRoleClient().GetRolesByUserIdAsync(new Authentications.GeneratedServices.Int64GetIdRequestContract
var roles = await _appUnitOfWork.GetRoleClient().GetRolesByUserIdAsync(new Authentications.GeneratedServices.GetByIdAndUniqueIdentityRequestContract
{
Id = user.Id
Id = user.Id,
//UniqueIdentity = user.UniqueIdentity
}).AsCheckedResult(x => x.Result);

List<ClaimContract> claims = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class JWTManager : IJWTManager
public JWTManager(IConfiguration config)
{
_config = config;
_authRoot = _config.GetValue<string>("RootAddresses:Authentications");
_authRoot = _config.GetValue<string>("RootAddresses:Authentication");
_userClient = new(_authRoot, new System.Net.Http.HttpClient());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Authentications.GeneratedServices;
using Contents.GeneratedServices;
using EasyMicroservices.Logger.Interfaces;
using WhiteLables.GeneratedServices;

namespace EasyMicroservices.IdentityMicroservice.Interfaces
{
Expand All @@ -26,5 +27,6 @@ public interface IAppUnitOfWork : IUnitOfWork
public UserClient GetUserClient();
public RoleClient GetRoleClient();
public PersonalAccessTokenClient GetPersonalAccessTokenClientClient();
public WhiteLabelClient GetWhiteLabelClient();
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces;
using EasyMicroservices.Cores.Relational.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace EasyMicroservices.IdentityMicroservice
{
public class DatabaseBuilder : IEntityFrameworkCoreDatabaseBuilder
public class DatabaseBuilder : EntityFrameworkCoreDatabaseBuilder
{
IConfiguration _configuration;
public DatabaseBuilder(IConfiguration configuration)
public DatabaseBuilder(IConfiguration configuration) : base(configuration)
{
_configuration = configuration;
}

public void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
public override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//optionsBuilder.UseInMemoryDatabase("IdentityDatabase");
optionsBuilder.UseSqlServer(_configuration.GetConnectionString("local"));
var entity = GetEntity();
if (entity.IsSqlServer())
optionsBuilder.UseSqlServer(entity.ConnectionString);
else if (entity.IsInMemory())
optionsBuilder.UseInMemoryDatabase("Identity");
}
}
}
Loading

0 comments on commit 9e8e9a9

Please sign in to comment.