From 2d8bcef1a429add6f7173d161c3ea0e76402c69e Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Wed, 11 Oct 2023 20:46:42 +0330 Subject: [PATCH] Refactor, Support for role and permissions --- .../Contexts/AuthenticationsContext.cs | 28 +-- .../Database/Entities/RoleEntity.cs | 16 ++ .../Entities/RoleServicePermissionEntity.cs | 13 ++ .../Entities/ServicePermissionEntity.cs | 12 ++ .../Database/Entities/UserEntity.cs | 6 +- .../Database/Entities/UserRoleEntity.cs | 13 ++ .../Database/Schemas/RoleSchema.cs | 9 + .../Schemas/ServicePermissionSchema.cs | 11 + ...uthenticationsMicroservice.Database.csproj | 1 - .../Contracts/Common/RoleContract.cs | 10 + .../Common/RoleServicePermissionContract.cs | 14 ++ .../Common/ServicePermissionContract.cs | 9 + .../Contracts/Common/UserRoleContract.cs | 8 + .../Requests/AddRoleRequestContract.cs | 8 + ...AddRoleServicePermissionRequestContract.cs | 14 ++ .../AddServicePermissionRequestContract.cs | 9 + .../Requests/AddUserRoleRequestContract.cs | 8 + ....AuthenticationsMicroservice.Domain.csproj | 5 +- .../Helpers/ApplicationManager.cs | 16 -- .../Helpers/AuthenticationHelper.cs | 7 +- .../Interfaces/IDependencyManager.cs | 16 -- .../JWTManager.cs | 19 +- .../DatabaseBuilder.cs | 13 +- .../DependencyManager.cs | 66 ------ ...AuthenticationsMicroservice.StartUp.csproj | 1 - .../StartUp.cs | 19 -- .../WhiteLabelManager.cs | 113 ---------- .../Controllers/RoleController.cs | 15 ++ .../RoleServicePermissionController.cs | 15 ++ .../ServicePermissionController.cs | 15 ++ .../Controllers/UserController.cs | 27 +-- .../Controllers/UserRoleController.cs | 15 ++ ....AuthenticationsMicroservice.WebApi.csproj | 1 - .../Program.cs | 199 ++++-------------- .../Properties/launchSettings.json | 6 +- .../appsettings.Development.json | 2 +- .../appsettings.json | 2 +- 37 files changed, 302 insertions(+), 459 deletions(-) create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleEntity.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleServicePermissionEntity.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/ServicePermissionEntity.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/UserRoleEntity.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/RoleSchema.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/ServicePermissionSchema.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/RoleContract.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/RoleServicePermissionContract.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/ServicePermissionContract.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/UserRoleContract.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddRoleRequestContract.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddRoleServicePermissionRequestContract.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddServicePermissionRequestContract.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddUserRoleRequestContract.cs delete mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Helpers/ApplicationManager.cs delete mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Interfaces/IDependencyManager.cs delete mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/DependencyManager.cs delete mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/StartUp.cs delete mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/WhiteLabelManager.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleController.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleServicePermissionController.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/ServicePermissionController.cs create mode 100644 src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserRoleController.cs diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Contexts/AuthenticationsContext.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Contexts/AuthenticationsContext.cs index ac0c801..4588360 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Contexts/AuthenticationsContext.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Contexts/AuthenticationsContext.cs @@ -1,37 +1,39 @@ using EasyMicroservices.AuthenticationsMicroservice.Database.Entities; -using Microsoft.EntityFrameworkCore; using EasyMicroservices.Cores.Relational.EntityFrameworkCore; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; +using Microsoft.EntityFrameworkCore; namespace EasyMicroservices.AuthenticationsMicroservice.Database.Contexts { public class AuthenticationsContext : RelationalCoreContext { - IDatabaseBuilder _builder; - public AuthenticationsContext(IDatabaseBuilder builder) + public AuthenticationsContext(IEntityFrameworkCoreDatabaseBuilder builder) : base(builder) { - _builder = builder; } public DbSet Users { get; set; } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (_builder != null) - _builder.OnConfiguring(optionsBuilder); - base.OnConfiguring(optionsBuilder); - } + public DbSet Roles { get; set; } + public DbSet UserRoles { get; set; } + public DbSet ServicePermissions { get; set; } + public DbSet RoleServicePermissions { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { - base.OnModelCreating(modelBuilder); + base.AutoModelCreating(modelBuilder); modelBuilder.Entity(model => { - model.HasKey(x => x.Id); model.HasIndex(u => u.UserName) .IsUnique(); }); + modelBuilder.Entity(model => + { + model.HasIndex(u => u.MicroserviceName); + model.HasIndex(u => u.ServiceName); + model.HasIndex(u => u.MethodName); + model.HasIndex(u => new { u.MicroserviceName, u.ServiceName, u.MethodName }).IsUnique(); + }); } } } \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleEntity.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleEntity.cs new file mode 100644 index 0000000..b0f095c --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleEntity.cs @@ -0,0 +1,16 @@ +using EasyMicroservices.AuthenticationsMicroservice.Database.Schemas; +using EasyMicroservices.Cores.Interfaces; +using System.Collections.Generic; + +namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities +{ + public class RoleEntity : RoleSchema, IIdSchema + { + public long Id { get; set; } + public long? ParentId { get; set; } + public RoleEntity Parent { get; set; } + public ICollection Children { get; set; } + public ICollection UserRoles { get; set; } + public ICollection RoleServicePermissions { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleServicePermissionEntity.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleServicePermissionEntity.cs new file mode 100644 index 0000000..4e1caa0 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleServicePermissionEntity.cs @@ -0,0 +1,13 @@ +using EasyMicroservices.Cores.Database.Schemas; + +namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities +{ + public class RoleServicePermissionEntity : FullAbilityIdSchema + { + public long RoleId { get; set; } + public long ServicePermissionId { get; set; } + + public RoleEntity Role { get; set; } + public ServicePermissionEntity ServicePermission { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/ServicePermissionEntity.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/ServicePermissionEntity.cs new file mode 100644 index 0000000..9a5ea2f --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/ServicePermissionEntity.cs @@ -0,0 +1,12 @@ +using EasyMicroservices.AuthenticationsMicroservice.Database.Schemas; +using EasyMicroservices.Cores.Interfaces; +using System.Collections.Generic; + +namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities +{ + public class ServicePermissionEntity : ServicePermissionSchema, IIdSchema + { + public long Id { get; set; } + public ICollection RoleServicePermissions { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/UserEntity.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/UserEntity.cs index 1355443..fb364df 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/UserEntity.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/UserEntity.cs @@ -1,16 +1,12 @@ using EasyMicroservices.AuthenticationsMicroservice.Database.Schemas; using EasyMicroservices.Cores.Interfaces; -using System; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities { public class UserEntity : UserSchema, IIdSchema { public long Id { get; set; } + public ICollection UserRoles { get; set; } } } diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/UserRoleEntity.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/UserRoleEntity.cs new file mode 100644 index 0000000..f6e1985 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/UserRoleEntity.cs @@ -0,0 +1,13 @@ +using EasyMicroservices.Cores.Database.Schemas; + +namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities +{ + public class UserRoleEntity : FullAbilityIdSchema + { + public long RoleId { get; set; } + public long UserId { get; set; } + + public RoleEntity Role { get; set; } + public UserEntity User { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/RoleSchema.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/RoleSchema.cs new file mode 100644 index 0000000..181accb --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/RoleSchema.cs @@ -0,0 +1,9 @@ +using EasyMicroservices.Cores.Database.Schemas; + +namespace EasyMicroservices.AuthenticationsMicroservice.Database.Schemas +{ + public class RoleSchema : FullAbilitySchema + { + public string Name { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/ServicePermissionSchema.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/ServicePermissionSchema.cs new file mode 100644 index 0000000..b050f66 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/ServicePermissionSchema.cs @@ -0,0 +1,11 @@ +using EasyMicroservices.Cores.Database.Schemas; + +namespace EasyMicroservices.AuthenticationsMicroservice.Database.Schemas +{ + public class ServicePermissionSchema : FullAbilitySchema + { + public string MicroserviceName { get; set; } + public string ServiceName { get; set; } + public string MethodName { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/EasyMicroservices.AuthenticationsMicroservice.Database.csproj b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/EasyMicroservices.AuthenticationsMicroservice.Database.csproj index d21909f..70cdf6d 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/EasyMicroservices.AuthenticationsMicroservice.Database.csproj +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/EasyMicroservices.AuthenticationsMicroservice.Database.csproj @@ -11,7 +11,6 @@ - diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/RoleContract.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/RoleContract.cs new file mode 100644 index 0000000..eac78b3 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/RoleContract.cs @@ -0,0 +1,10 @@ +using EasyMicroservices.Cores.Database.Schemas; + +namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Common +{ + public class RoleContract : FullAbilityIdSchema + { + public string Name { get; set; } + public long? ParentId { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/RoleServicePermissionContract.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/RoleServicePermissionContract.cs new file mode 100644 index 0000000..3e10b05 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/RoleServicePermissionContract.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Common +{ + public class RoleServicePermissionContract + { + public long RoleId { get; set; } + public long ServicePermissionId { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/ServicePermissionContract.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/ServicePermissionContract.cs new file mode 100644 index 0000000..2f0d6f6 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/ServicePermissionContract.cs @@ -0,0 +1,9 @@ +namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Common +{ + public class ServicePermissionContract + { + public string MicroserviceName { get; set; } + public string ServiceName { get; set; } + public string MethodName { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/UserRoleContract.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/UserRoleContract.cs new file mode 100644 index 0000000..9914003 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Common/UserRoleContract.cs @@ -0,0 +1,8 @@ +namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Common +{ + public class UserRoleContract + { + public long RoleId { get; set; } + public long UserId { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddRoleRequestContract.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddRoleRequestContract.cs new file mode 100644 index 0000000..1c50563 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddRoleRequestContract.cs @@ -0,0 +1,8 @@ +namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests +{ + public class AddRoleRequestContract + { + public string Name { get; set; } + public long? ParentId { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddRoleServicePermissionRequestContract.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddRoleServicePermissionRequestContract.cs new file mode 100644 index 0000000..36e89d7 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddRoleServicePermissionRequestContract.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests +{ + public class AddRoleServicePermissionRequestContract + { + public long RoleId { get; set; } + public long ServicePermissionId { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddServicePermissionRequestContract.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddServicePermissionRequestContract.cs new file mode 100644 index 0000000..4223c27 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddServicePermissionRequestContract.cs @@ -0,0 +1,9 @@ +namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests +{ + public class AddServicePermissionRequestContract + { + public string MicroserviceName { get; set; } + public string ServiceName { get; set; } + public string MethodName { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddUserRoleRequestContract.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddUserRoleRequestContract.cs new file mode 100644 index 0000000..7aa2a58 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Requests/AddUserRoleRequestContract.cs @@ -0,0 +1,8 @@ +namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests +{ + public class AddUserRoleRequestContract + { + public long RoleId { get; set; } + public long UserId { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/EasyMicroservices.AuthenticationsMicroservice.Domain.csproj b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/EasyMicroservices.AuthenticationsMicroservice.Domain.csproj index 2aae2db..095ba76 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/EasyMicroservices.AuthenticationsMicroservice.Domain.csproj +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/EasyMicroservices.AuthenticationsMicroservice.Domain.csproj @@ -1,4 +1,4 @@ - + net7.0 @@ -11,8 +11,7 @@ - - + diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Helpers/ApplicationManager.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Helpers/ApplicationManager.cs deleted file mode 100644 index 97b9804..0000000 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Helpers/ApplicationManager.cs +++ /dev/null @@ -1,16 +0,0 @@ -using EasyMicroservices.AuthenticationsMicroservice.Interfaces; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EasyMicroservices.AuthenticationsMicroservice.Helpers -{ - public class ApplicationManager - { - public static ApplicationManager Instance { get; private set; } = new ApplicationManager(); - public IDependencyManager DependencyManager { get; set; } - - } -} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Helpers/AuthenticationHelper.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Helpers/AuthenticationHelper.cs index 7191430..37acc78 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Helpers/AuthenticationHelper.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Helpers/AuthenticationHelper.cs @@ -1,11 +1,6 @@ -using Microsoft.AspNetCore.Cryptography.KeyDerivation; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; -using static System.Runtime.InteropServices.JavaScript.JSType; namespace EasyMicroservices.AuthenticationsMicroservice.Helpers { diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Interfaces/IDependencyManager.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Interfaces/IDependencyManager.cs deleted file mode 100644 index f43fcd1..0000000 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Logics/Interfaces/IDependencyManager.cs +++ /dev/null @@ -1,16 +0,0 @@ -using EasyMicroservices.Configuration.Interfaces; -using EasyMicroservices.Cores.Database.Interfaces; -using EasyMicroservices.Cores.Database.Managers; -using EasyMicroservices.Database.Interfaces; -using EasyMicroservices.Mapper.Interfaces; - -namespace EasyMicroservices.AuthenticationsMicroservice.Interfaces -{ - public interface IDependencyManager - { - IDatabase GetDatabase(); - IMapperProvider GetMapper(); - IConfigProvider GetConfigProvider(); - IUniqueIdentityManager GetUniqueIdentityManager(); - } -} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Services/JWTManager.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Services/JWTManager.cs index 1ffd2b6..e98a2c8 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Services/JWTManager.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Services/JWTManager.cs @@ -4,6 +4,7 @@ using EasyMicroservices.AuthenticationsMicroservice.Database.Entities; using EasyMicroservices.AuthenticationsMicroservice.Helpers; using EasyMicroservices.AuthenticationsMicroservice.Interfaces; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.Cores.Database.Interfaces; using EasyMicroservices.ServiceContracts; using Microsoft.Extensions.Configuration; @@ -20,17 +21,18 @@ namespace EasyMicroservices.AuthenticationsMicroservice public class JWTManager : IJWTManager { private readonly IConfiguration _config; - private readonly IContractLogic _userLogic; + private readonly IUnitOfWork _unitOfWork; - public JWTManager(IContractLogic userLogic, IConfiguration config) + public JWTManager(IUnitOfWork unitOfWork, IConfiguration config) { _config = config; - _userLogic = userLogic; + _unitOfWork = unitOfWork; } public virtual async Task> Login(UserSummaryContract cred) { - var userRecords = await _userLogic.GetAll(); + var logic = _unitOfWork.GetLongContractLogic(); + var userRecords = await logic.GetAll(); var user = userRecords.Result.Where(x => x.UserName == cred.UserName && x.Password == cred.Password); if (!user.Any()) return (FailedReasonType.AccessDenied, "Username or password is invalid."); //"Username or password is invalid." @@ -41,12 +43,12 @@ public virtual async Task> Login(UserSummaryContract cred) public virtual async Task> GenerateToken(UserClaimContract cred) { - var response = await Login(cred); if (!response) return response.ToContract(); - var user = await _userLogic.GetBy(x => x.UserName == cred.UserName && x.Password == cred.Password); + var logic = _unitOfWork.GetLongContractLogic(); + var user = await logic.GetBy(x => x.UserName == cred.UserName && x.Password == cred.Password); var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.UTF8.GetBytes(_config.GetValue("JWT:Key")); @@ -74,12 +76,13 @@ public virtual async Task> Register(AddUserRequestContract string Password = input.Password; input.Password = await AuthenticationHelper.HashPassword(input.Password); - var usersRecords = await _userLogic.GetBy(x => x.UserName == input.UserName.ToLower()); + var logic = _unitOfWork.GetLongContractLogic(); + var usersRecords = await logic.GetBy(x => x.UserName == input.UserName.ToLower()); if (usersRecords.IsSuccess) return (FailedReasonType.Duplicate, "User already exists!"); - var user = await _userLogic.Add(input); + var user = await logic.Add(input); return user; } diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/DatabaseBuilder.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/DatabaseBuilder.cs index 7105d68..307e8ee 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/DatabaseBuilder.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/DatabaseBuilder.cs @@ -1,15 +1,10 @@ -using EasyMicroservices.AuthenticationsMicroservice.Database; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace EasyMicroservices.AuthenticationsMicroservice { - public class DatabaseBuilder : IDatabaseBuilder + public class DatabaseBuilder : IEntityFrameworkCoreDatabaseBuilder { IConfiguration _configuration; public DatabaseBuilder(IConfiguration configuration) @@ -19,8 +14,8 @@ public DatabaseBuilder(IConfiguration configuration) public void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - //optionsBuilder.UseInMemoryDatabase("AuthDb"); - optionsBuilder.UseSqlServer(_configuration.GetConnectionString("local")); + optionsBuilder.UseInMemoryDatabase("AuthDb"); + //optionsBuilder.UseSqlServer(_configuration.GetConnectionString("local")); } } } diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/DependencyManager.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/DependencyManager.cs deleted file mode 100644 index 67ff702..0000000 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/DependencyManager.cs +++ /dev/null @@ -1,66 +0,0 @@ -using EasyMicroservices.AuthenticationsMicroservice.Interfaces; -using EasyMicroservices.AuthenticationsMicroservice; -using EasyMicroservices.Configuration.Interfaces; -using EasyMicroservices.Cores.Database.Interfaces; -using EasyMicroservices.Cores.Database.Logics; -using EasyMicroservices.Cores.Database.Managers; -using EasyMicroservices.Cores.Interfaces; -using EasyMicroservices.Database.EntityFrameworkCore.Providers; -using EasyMicroservices.Database.Interfaces; -using EasyMicroservices.Mapper.CompileTimeMapper.Interfaces; -using EasyMicroservices.Mapper.CompileTimeMapper.Providers; -using EasyMicroservices.Mapper.Interfaces; -using EasyMicroservices.AuthenticationsMicroservice.Database.Contexts; -using System; -using System.Linq; -using Microsoft.Extensions.Configuration; - -namespace EasyMicroservices.AuthenticationsMicroservice -{ - public class DependencyManager : IDependencyManager - { - public virtual IConfigProvider GetConfigProvider() - { - throw new NotImplementedException(); - } - - public virtual IContractLogic GetContractLogic() - where TEntity : class, IIdSchema - where TResponseContract : class - { - return new LongIdMappedDatabaseLogicBase(GetDatabase().GetReadableOf(), GetDatabase().GetWritableOf(), GetMapper(), GetUniqueIdentityManager()); - } - - public virtual IDatabase GetDatabase() - { - var _config = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build(); - - return new EntityFrameworkCoreDatabaseProvider(new AuthenticationsContext(new DatabaseBuilder(_config))); - } - - public static string DefaultUniqueIdentity { get; set; } - public static long MicroserviceId { get; set; } - public static IUniqueIdentityManager UniqueIdentityManager { get; set; } - public virtual IUniqueIdentityManager GetUniqueIdentityManager() - { - if (UniqueIdentityManager == null) - UniqueIdentityManager = new DefaultUniqueIdentityManager(DefaultUniqueIdentity, MicroserviceId); - return UniqueIdentityManager; - } - - public virtual IMapperProvider GetMapper() - { - var mapper = new CompileTimeMapperProvider(new EasyMicroservices.Mapper.SerializerMapper.Providers.SerializerMapperProvider(new EasyMicroservices.Serialization.Newtonsoft.Json.Providers.NewtonsoftJsonProvider())); - foreach (var type in typeof(IDependencyManager).Assembly.GetTypes()) - { - if (typeof(IMapper).IsAssignableFrom(type)) - { - var instance = Activator.CreateInstance(type, mapper); - var returnTypes = type.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Where(x => x.ReturnType != typeof(object) && x.Name == "Map").Select(x => x.ReturnType).ToArray(); - mapper.AddMapper(returnTypes[0], returnTypes[1], (IMapper)instance); - } - } - return mapper; - } - } -} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/EasyMicroservices.AuthenticationsMicroservice.StartUp.csproj b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/EasyMicroservices.AuthenticationsMicroservice.StartUp.csproj index 279378f..3011649 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/EasyMicroservices.AuthenticationsMicroservice.StartUp.csproj +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/EasyMicroservices.AuthenticationsMicroservice.StartUp.csproj @@ -6,7 +6,6 @@ - diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/StartUp.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/StartUp.cs deleted file mode 100644 index 7efc023..0000000 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/StartUp.cs +++ /dev/null @@ -1,19 +0,0 @@ -using EasyMicroservices.AuthenticationsMicroservice.Helpers; -using EasyMicroservices.AuthenticationsMicroservice.Interfaces; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EasyMicroservices.AuthenticationsMicroservice -{ - public class StartUp - { - public Task Run(IDependencyManager dependencyManager) - { - ApplicationManager.Instance.DependencyManager = dependencyManager; - return Task.CompletedTask; - } - } -} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/WhiteLabelManager.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/WhiteLabelManager.cs deleted file mode 100644 index 23b159e..0000000 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.StartUp/WhiteLabelManager.cs +++ /dev/null @@ -1,113 +0,0 @@ -using EasyMicroservices.Cores.Database.Managers; -using EasyMicroservices.AuthenticationsMicroservice.Interfaces; -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Text; -using System.Threading.Tasks; -using WhiteLables.GeneratedServices; -using EasyMicroservices.AuthenticationsMicroservice; -using EasyMicroservices.AuthenticationsMicroservice; - -namespace EasyMicroservices.AuthenticationsMicroservice -{ - public class WhiteLabelManager - { - public WhiteLabelManager(IServiceProvider serviceProvider, IDependencyManager dependencyManager) - { - _serviceProvider = serviceProvider; - _dependencyManager = dependencyManager; - } - - private readonly IServiceProvider _serviceProvider; - private readonly IDependencyManager _dependencyManager; - public static HttpClient HttpClient { get; set; } = new HttpClient(); - - string GetDefaultUniqueIdentity(ICollection whiteLables, long? parentId) - { - var found = whiteLables.FirstOrDefault(x => x.ParentId == parentId); - if (found == null) - { - return ""; - } - return $"{DefaultUniqueIdentityManager.GenerateUniqueIdentity(found.Id)}-{GetDefaultUniqueIdentity(whiteLables, found.Id)}".Trim('-'); - } - - public async Task Initialize(string microserviceName, string whiteLableRoute, params Type[] dbContextTypes) - { - if (dbContextTypes.IsEmpty()) - return; - var whiteLabelClient = new WhiteLables.GeneratedServices.WhiteLabelClient(whiteLableRoute, HttpClient); - var whiteLabels = await whiteLabelClient.GetAllAsync(); - DependencyManager.DefaultUniqueIdentity = GetDefaultUniqueIdentity(whiteLabels.Result, null); - - var microserviceClient = new WhiteLables.GeneratedServices.MicroserviceClient(whiteLableRoute, HttpClient); - var microservices = await microserviceClient.GetAllAsync(); - var foundMicroservice = microservices.Result.FirstOrDefault(x => x.Name.Equals(microserviceName, StringComparison.OrdinalIgnoreCase)); - if (foundMicroservice == null) - { - foundMicroservice = new WhiteLables.GeneratedServices.MicroserviceContract() - { - InstanceIndex = 1, - Name = microserviceName, - Description = "Automatically added" - }; - var addMicroservice = await microserviceClient.AddAsync(foundMicroservice); - foundMicroservice.Id = addMicroservice.Result; - } - DependencyManager.MicroserviceId = foundMicroservice.Id; - - var uniqueIdentityManager = _dependencyManager.GetUniqueIdentityManager() as DefaultUniqueIdentityManager; - - var microserviceContextTableClient = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(whiteLableRoute, HttpClient); - var microserviceContextTables = await microserviceContextTableClient.GetAllAsync(); - - HashSet addedInWhitLabels = new HashSet(); - foreach (var contextTableContract in microserviceContextTables.Result) - { - uniqueIdentityManager.InitializeTables(contextTableContract.MicroserviceId, contextTableContract.ContextName, contextTableContract.TableName, contextTableContract.ContextTableId); - addedInWhitLabels.Add(uniqueIdentityManager.GetContextTableName(contextTableContract.MicroserviceId, contextTableContract.ContextName, contextTableContract.TableName)); - } - - foreach (var contextType in dbContextTypes) - { - var contextTableClient = new WhiteLables.GeneratedServices.ContextTableClient(whiteLableRoute, HttpClient); - var contextTables = await contextTableClient.GetAllAsync(); - using var insctanceOfContext = _serviceProvider.GetService(contextType) as DbContext; - string contextName = uniqueIdentityManager.GetContextName(contextType); - foreach (var entityType in insctanceOfContext.Model.GetEntityTypes()) - { - string tableName = entityType.ServiceOnlyConstructorBinding.RuntimeType.Name; - var tableFullName = uniqueIdentityManager.GetContextTableName(foundMicroservice.Id, contextType.Name, tableName); - if (!addedInWhitLabels.Contains(tableFullName)) - { - if (microserviceContextTables.Result.Any(x => x.ContextName == contextName && x.TableName == tableName && x.MicroserviceId == foundMicroservice.Id)) - continue; - var contextTable = contextTables.Result.FirstOrDefault(x => x.ContextName == contextName && x.TableName == tableName); - if (contextTable == null) - { - contextTable = new WhiteLables.GeneratedServices.ContextTableContract() - { - ContextName = contextName, - TableName = tableName, - }; - var contextTableResult = await contextTableClient.AddAsync(contextTable); - contextTable.Id = contextTableResult.Result; - } - var addedMicroserviceContextTable = await microserviceContextTableClient.AddAsync(new WhiteLables.GeneratedServices.MicroserviceContextTableContract() - { - ContextName = contextName, - TableName = tableName, - MicroserviceName = microserviceName, - MicroserviceId = foundMicroservice.Id, - ContextTableId = contextTable.Id - }); - uniqueIdentityManager.InitializeTables(foundMicroservice.Id, contextName, tableName, contextTable.Id); - } - } - } - } - } -} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleController.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleController.cs new file mode 100644 index 0000000..7b20ca4 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleController.cs @@ -0,0 +1,15 @@ +using EasyMicroservices.AuthenticationsMicroservice.Contracts.Common; +using EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests; +using EasyMicroservices.AuthenticationsMicroservice.Database.Entities; +using EasyMicroservices.Cores.AspCoreApi; +using EasyMicroservices.Cores.Interfaces; + +namespace EasyMicroservices.AuthenticationsMicroservice.WebApi.Controllers +{ + public class RoleController : SimpleQueryServiceController + { + public RoleController(IBaseUnitOfWork unitOfWork) : base(unitOfWork) + { + } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleServicePermissionController.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleServicePermissionController.cs new file mode 100644 index 0000000..6bceb46 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleServicePermissionController.cs @@ -0,0 +1,15 @@ +using EasyMicroservices.AuthenticationsMicroservice.Contracts.Common; +using EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests; +using EasyMicroservices.AuthenticationsMicroservice.Database.Entities; +using EasyMicroservices.Cores.AspCoreApi; +using EasyMicroservices.Cores.Interfaces; + +namespace EasyMicroservices.AuthenticationsMicroservice.WebApi.Controllers +{ + public class RoleServicePermissionController : SimpleQueryServiceController + { + public RoleServicePermissionController(IBaseUnitOfWork unitOfWork) : base(unitOfWork) + { + } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/ServicePermissionController.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/ServicePermissionController.cs new file mode 100644 index 0000000..4e30dac --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/ServicePermissionController.cs @@ -0,0 +1,15 @@ +using EasyMicroservices.AuthenticationsMicroservice.Contracts.Common; +using EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests; +using EasyMicroservices.AuthenticationsMicroservice.Database.Entities; +using EasyMicroservices.Cores.AspCoreApi; +using EasyMicroservices.Cores.Interfaces; + +namespace EasyMicroservices.AuthenticationsMicroservice.WebApi.Controllers +{ + public class ServicePermissionController : SimpleQueryServiceController + { + public ServicePermissionController(IBaseUnitOfWork unitOfWork) : base(unitOfWork) + { + } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserController.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserController.cs index 7dae15c..63cb44b 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserController.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserController.cs @@ -5,43 +5,35 @@ using EasyMicroservices.AuthenticationsMicroservice.Helpers; using EasyMicroservices.AuthenticationsMicroservice.Interfaces; using EasyMicroservices.Cores.AspCoreApi; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.Cores.Database.Interfaces; using EasyMicroservices.ServiceContracts; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; -using Microsoft.IdentityModel.Tokens; -using System.IdentityModel.Tokens.Jwt; -using System.Runtime.CompilerServices; -using System.Security.Claims; -using System.Text; namespace EasyMicroservices.AuthenticationsMicroservice.WebApi.Controllers { - [Route("api/[controller]/[action]")] - [ApiController] public class UsersController : SimpleQueryServiceController { - private readonly IContractLogic _contractLogic; + private readonly IUnitOfWork _unitOfWork; private readonly IJWTManager _jwtManager; - public UsersController(IContractLogic contractLogic, IJWTManager jwtManager) : base(contractLogic) + public UsersController(IUnitOfWork unitOfWork, IJWTManager jwtManager) : base(unitOfWork) { - _contractLogic = contractLogic; + _unitOfWork = unitOfWork; _jwtManager = jwtManager; } [HttpPost] public async Task> VerifyUserName(VerifyEmailAddressContract request) { - var user = await _contractLogic.GetById(new Cores.Contracts.Requests.GetIdRequestContract { Id = request.UserId }); + var logic = _unitOfWork.GetLongContractLogic(); + var user = await logic.GetById(new Cores.Contracts.Requests.GetIdRequestContract { Id = request.UserId }); if (!user.IsSuccess) return (FailedReasonType.Incorrect, "UserId is incorrect"); if (user.Result.IsUsernameVerified) return true; - var updateUser = await _contractLogic.Update(new UserContract + var updateUser = await logic.Update(new UserContract { CreationDateTime = user.Result.CreationDateTime, DeletedDateTime = user.Result.DeletedDateTime, @@ -58,7 +50,7 @@ public async Task> VerifyUserName(VerifyEmailAddressContra return (FailedReasonType.Incorrect, "An error has occurred"); return true; - + } [HttpPost] public async Task> Register(AddUserRequestContract request) @@ -94,7 +86,8 @@ public async Task> GenerateToken(UserClaim [HttpPost] public async Task> RegenerateToken(RegenerateTokenContract request) { - var user = await _contractLogic.GetById(new Cores.Contracts.Requests.GetIdRequestContract + var logic = _unitOfWork.GetLongContractLogic(); + var user = await logic.GetById(new Cores.Contracts.Requests.GetIdRequestContract { Id = request.UserId }); diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserRoleController.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserRoleController.cs new file mode 100644 index 0000000..ec3f0c7 --- /dev/null +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserRoleController.cs @@ -0,0 +1,15 @@ +using EasyMicroservices.AuthenticationsMicroservice.Contracts.Common; +using EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests; +using EasyMicroservices.AuthenticationsMicroservice.Database.Entities; +using EasyMicroservices.Cores.AspCoreApi; +using EasyMicroservices.Cores.Interfaces; + +namespace EasyMicroservices.AuthenticationsMicroservice.WebApi.Controllers +{ + public class UserRoleController : SimpleQueryServiceController + { + public UserRoleController(IBaseUnitOfWork unitOfWork) : base(unitOfWork) + { + } + } +} diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/EasyMicroservices.AuthenticationsMicroservice.WebApi.csproj b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/EasyMicroservices.AuthenticationsMicroservice.WebApi.csproj index 8ec5274..f554fed 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/EasyMicroservices.AuthenticationsMicroservice.WebApi.csproj +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/EasyMicroservices.AuthenticationsMicroservice.WebApi.csproj @@ -6,7 +6,6 @@ - diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Program.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Program.cs index d2fc0fa..2b41493 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Program.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Program.cs @@ -1,49 +1,33 @@ - -using EasyMicroservices.AuthenticationsMicroservice.Contracts.Common; -using EasyMicroservices.AuthenticationsMicroservice.Contracts.Requests; -using EasyMicroservices.AuthenticationsMicroservice.Database; using EasyMicroservices.AuthenticationsMicroservice.Database.Contexts; -using EasyMicroservices.AuthenticationsMicroservice.Database.Entities; using EasyMicroservices.AuthenticationsMicroservice.Interfaces; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.IdentityModel.Tokens; -using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; -using Swashbuckle.AspNetCore.SwaggerGen; -using System.Reflection; using System.Text; namespace EasyMicroservices.AuthenticationsMicroservice.WebApi { public class Program { - public static async Task Main(string[] args) { - var builder = WebApplication.CreateBuilder(args); - - IConfiguration config = new ConfigurationBuilder() - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .Build(); - - var MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; + var app = CreateBuilder(args); + var build = await app.Build(true, true); - builder.Services.AddCors(options => - { - options.AddPolicy(name: MyAllowSpecificOrigins, - policy => - { - policy.AllowAnyOrigin(); - }); - }); + //build.UseAuthentication(); + build.MapControllers(); + build.UseCors(MyAllowSpecificOrigins); + build.Run(); + } + static readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; - builder.Services.AddControllers(); - // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle - builder.Services.AddEndpointsApiExplorer(); - builder.Services.AddSwaggerGen(options => + static WebApplicationBuilder CreateBuilder(string[] args) + { + var app = StartUpExtensions.Create(args); + app.Services.Builder(options => { options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { @@ -71,32 +55,24 @@ public static async Task Main(string[] args) new List() } }); - - options.SchemaFilter(); - options.SchemaFilter(); }); + app.Services.AddTransient((serviceProvider) => new UnitOfWork(serviceProvider)); + app.Services.AddTransient(serviceProvider => new AuthenticationsContext(serviceProvider.GetService())); + app.Services.AddTransient(); + StartUpExtensions.AddWhiteLabel("Authentication", "RootAddresses:WhiteLabel"); + app.Services.AddCors(options => + { + options.AddPolicy(name: MyAllowSpecificOrigins, + policy => + { + policy.AllowAnyOrigin(); + }); + }); - builder.Services.AddDbContext(options => - options.UseSqlServer(builder.Configuration.GetConnectionString(config.GetConnectionString("local"))) - ); - - //builder.Services.AddScoped((serviceProvider) => new DependencyManager().GetContractLogic()); - string webRootPath = @Directory.GetCurrentDirectory(); - - var _config = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build(); - - builder.Services.AddHttpContextAccessor(); - builder.Services.AddScoped(service => new DependencyManager()); - builder.Services.AddScoped(serviceProvider => new DatabaseBuilder(_config)); - builder.Services.AddScoped(service => new WhiteLabelManager(service, service.GetService())); - builder.Services.AddScoped(); - builder.Services.AddScoped((serviceProvider) => new DependencyManager().GetContractLogic()); - builder.Services.AddTransient(serviceProvider => new AuthenticationsContext(serviceProvider.GetService())); - builder.Services.AddTransient(serviceProvider => _config); - + app.Services.AddScoped(); - builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + app.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters @@ -105,120 +81,23 @@ public static async Task Main(string[] args) ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, - ValidIssuer = builder.Configuration["JWT:Issuer"], - ValidAudience = builder.Configuration["JWT:Audience"], - IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])) + ValidIssuer = app.Configuration["JWT:Issuer"], + ValidAudience = app.Configuration["JWT:Audience"], + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(app.Configuration["Jwt:Key"])) }; }); - - - - var app = builder.Build(); - app.UseDeveloperExceptionPage(); - // Configure the HTTP request pipeline. - app.UseSwagger(); - app.UseSwaggerUI(); - - app.UseHttpsRedirection(); - app.UseAuthentication(); - app.UseAuthorization(); - app.MapControllers(); - app.UseCors(MyAllowSpecificOrigins); - - //CreateDatabase(); - - using (var scope = app.Services.CreateScope()) - { - using var context = scope.ServiceProvider.GetService(); - //await context.Database.EnsureCreatedAsync(); - //await context.Database.MigrateAsync(); - await context.DisposeAsync(); - var service = scope.ServiceProvider.GetService(); - await service.Initialize("Authentication", config.GetValue("RootAddresses:whitelabel"), typeof(AuthenticationsContext)); - } - - StartUp startUp = new StartUp(); - await startUp.Run(new DependencyManager()); - app.Run(); - } - - static void CreateDatabase() - { - var _config = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build(); - - using (var context = new AuthenticationsContext(new DatabaseBuilder(_config))) - { - if (context.Database.EnsureCreated()) - { - //auto migration when database created first time - - //add migration history table - - string createEFMigrationsHistoryCommand = $@" -USE [{context.Database.GetDbConnection().Database}]; -SET ANSI_NULLS ON; -SET QUOTED_IDENTIFIER ON; -CREATE TABLE [dbo].[__EFMigrationsHistory]( - [MigrationId] [nvarchar](150) NOT NULL, - [ProductVersion] [nvarchar](32) NOT NULL, - CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY CLUSTERED -( - [MigrationId] ASC -)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] -) ON [PRIMARY]; -"; - context.Database.ExecuteSqlRaw(createEFMigrationsHistoryCommand); - - //insert all of migrations - var dbAssebmly = context.GetType().Assembly; - foreach (var item in dbAssebmly.GetTypes()) - { - if (item.BaseType == typeof(Migration)) - { - string migrationName = item.GetCustomAttributes().First().Id; - var version = typeof(Migration).Assembly.GetName().Version; - string efVersion = $"{version.Major}.{version.Minor}.{version.Build}"; - context.Database.ExecuteSqlRaw("INSERT INTO __EFMigrationsHistory(MigrationId,ProductVersion) VALUES ({0},{1})", migrationName, efVersion); - } - } - } - context.Database.Migrate(); - } + return app; } - } - public class GenericFilter : ISchemaFilter - { - public void Apply(OpenApiSchema schema, SchemaFilterContext context) + public static async Task Run(string[] args, Action use) { - var type = context.Type; - - if (type.IsGenericType == false) - return; - - schema.Title = $"{type.Name[0..^2]}<{type.GenericTypeArguments[0].Name}>"; - } - } + var app = CreateBuilder(args); + use?.Invoke(app.Services); + var build = await app.Build(true, true); + build.MapControllers(); - public class XEnumNamesSchemaFilter : ISchemaFilter - { - private const string NAME = "x-enumNames"; - public void Apply(OpenApiSchema model, SchemaFilterContext context) - { - var typeInfo = context.Type; - // Chances are something in the pipeline might generate this automatically at some point in the future - // therefore it's best to check if it exists. - if (typeInfo.IsEnum && !model.Extensions.ContainsKey(NAME)) - { - var names = Enum.GetNames(context.Type); - var arr = new OpenApiArray(); - arr.AddRange(names.Select(name => new OpenApiString(name))); - model.Extensions.Add(NAME, arr); - } + build.Run(); } } -} - - - +} \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Properties/launchSettings.json b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Properties/launchSettings.json index 21ec2cd..12d460c 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Properties/launchSettings.json +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Properties/launchSettings.json @@ -18,7 +18,7 @@ "ASPNETCORE_ENVIRONMENT": "Development" }, "dotnetRunMessages": true, - "applicationUrl": "https://localhost:7183" + "applicationUrl": "http://localhost:7183" }, "IIS Express": { "commandName": "IISExpress", @@ -31,10 +31,10 @@ "WSL": { "commandName": "WSL2", "launchBrowser": true, - "launchUrl": "https://localhost:7183/swagger", + "launchUrl": "http://localhost:1044/swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", - "ASPNETCORE_URLS": "https://localhost:7183" + "ASPNETCORE_URLS": "http://localhost:1044" }, "distributionName": "" } diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.Development.json b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.Development.json index 4326007..c86d716 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.Development.json +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.Development.json @@ -16,7 +16,7 @@ }, "JWT": { "Key": "VGhpc0lzQVNlY3JldEtleUZvckp3dEF1dGhlbnRpY2F0aW9u=", - "Issuer": "https://githubecom/easymicroservices", + "Issuer": "https://github.com/easymicroservices", "Audience": "easymicroservices", "TokenExpireTimeInSeconds": 86400 diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.json b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.json index 4326007..c86d716 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.json +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.json @@ -16,7 +16,7 @@ }, "JWT": { "Key": "VGhpc0lzQVNlY3JldEtleUZvckp3dEF1dGhlbnRpY2F0aW9u=", - "Issuer": "https://githubecom/easymicroservices", + "Issuer": "https://github.com/easymicroservices", "Audience": "easymicroservices", "TokenExpireTimeInSeconds": 86400