Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add AuthenticationRouting #48

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
using EasyMicroservices.AuthenticationsMicroservice.Database.Entities;
using EasyMicroservices.AuthenticationsMicroservice.SeedData;
using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Builders;
using Microsoft.EntityFrameworkCore;

namespace EasyMicroservices.AuthenticationsMicroservice.Database.Builders;

public class AuthenticationDatabaseBuilder
{
public void OnModelCreating(ModelBuilder modelBuilder)
public void OnModelCreating(ModelBuilder modelBuilder, string suffix = "", string prefix = "")
{
modelBuilder.Entity<UserEntity>(model =>
{
model.HasIndex(u => new { u.BusinessUniqueIdentity, u.UserName })
.IsUnique();
model.ToTable("Users");
model.ToTable(GetTableName("Users", suffix, prefix));
});

modelBuilder.Entity<RoleServicePermissionEntity>(model =>
{
model.HasKey(u => new { u.RoleId, u.ServicePermissionId });
model.ToTable("RoleServicePermissions");
model.ToTable(GetTableName("RoleServicePermissions", suffix, prefix));
});

modelBuilder.Entity<UserRoleEntity>(model =>
{
model.HasKey(u => new { u.RoleId, u.UserId });
model.ToTable("UserRoles");
model.ToTable(GetTableName("UserRoles", suffix, prefix));
});

modelBuilder.Entity<ServicePermissionEntity>(model =>
Expand All @@ -34,7 +33,7 @@ public void OnModelCreating(ModelBuilder modelBuilder)
model.HasIndex(u => u.ServiceName);
model.HasIndex(u => u.MethodName);
model.HasIndex(u => new { u.MicroserviceName, u.ServiceName, u.MethodName }).IsUnique();
model.ToTable("ServicePermissions");
model.ToTable(GetTableName("ServicePermissions", suffix, prefix));
});

modelBuilder.Entity<RoleParentChildEntity>(model =>
Expand All @@ -50,24 +49,29 @@ public void OnModelCreating(ModelBuilder modelBuilder)
.WithMany(u => u.Children)
.HasForeignKey(u => u.ChildId)
.OnDelete(DeleteBehavior.Restrict);
model.ToTable("RoleParentChildren");
model.ToTable(GetTableName("RoleParentChildren", suffix, prefix));
});

modelBuilder.Entity<RoleEntity>(model =>
{
model.ToTable("Roles");
model.ToTable(GetTableName("Roles", suffix, prefix));
});

modelBuilder.Entity<PersonalAccessTokenEntity>(model =>
{
model.ToTable("PersonalAccessTokens");
model.ToTable(GetTableName("PersonalAccessTokens", suffix, prefix));
});

modelBuilder.Entity<RegisterUserDefaultRoleEntity>(model =>
{
model.ToTable("RegisterUserDefaultRoles");
model.ToTable(GetTableName("RegisterUserDefaultRoles", suffix, prefix));
});

var result = new RelationalCoreModelBuilder().AutoModelCreating(modelBuilder);
}

string GetTableName(string name, string suffix = "", string prefix = "")
{
return string.Concat(suffix, name, prefix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.1</Version>
<Version>0.0.0.2</Version>
<Description>Authentications Microservice</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>module,database,services,asp,aspnet,auth,authentication,authentications</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.1</Version>
<Version>0.0.0.2</Version>
<Description>Authentications Microservice</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>module,domain,services,asp,aspnet,auth,authentication,authentications</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.1</Version>
<Version>0.0.0.2</Version>
<Description>Authentications Microservice</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>module,logics,asp,aspnet,auth,authentication,authentications</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using EasyMicroservices.AuthenticationsMicroservice.WebApi.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;

namespace EasyMicroservices.AuthenticationsMicroservice.Module;
public static class AuthenticationRouting
{
public static void ConfigureServices(IServiceCollection services, string prefix = "")
{
IMvcBuilder mvcBuilder;
if (prefix.HasAny())
{
mvcBuilder = services.AddControllersWithViews(options =>
{
options.Conventions.Add(new RoutePrefixConvention(new RouteAttribute("authentication")));
});
}
else
{
mvcBuilder = services.AddMvc();
}
mvcBuilder.AddApplicationPart(typeof(RoleController).Assembly);
}
}

public class RoutePrefixConvention : IApplicationModelConvention
{
private readonly AttributeRouteModel _prefix;

public RoutePrefixConvention(IRouteTemplateProvider routeTemplateProvider)
{
_prefix = new AttributeRouteModel(routeTemplateProvider);
}

public void Apply(ApplicationModel application)
{
foreach (var controller in application.Controllers)
{
var matchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList();
if (matchedSelectors.Any())
{
foreach (var selectorModel in matchedSelectors)
{
selectorModel.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_prefix,
selectorModel.AttributeRouteModel);
}
}
else
{
controller.Selectors.Add(new SelectorModel
{
AttributeRouteModel = _prefix
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.1</Version>
<Version>0.0.0.2</Version>
<Description>Authentications Microservice</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>module,services,asp,aspnet,auth,authentication,authentications</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.AuthenticationsMicroservice.Database.Contexts;
using EasyMicroservices.AuthenticationsMicroservice.Module;
using EasyMicroservices.AuthenticationsMicroservice.WebApi.Controllers;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi;
using EasyMicroservices.Cores.Interfaces;
Expand All @@ -25,8 +26,7 @@ static WebApplicationBuilder CreateBuilder(string[] args)
app.Services.AddTransient(serviceProvider => new AuthenticationsContext(serviceProvider.GetService<IEntityFrameworkCoreDatabaseBuilder>()));
app.Services.AddTransient<IEntityFrameworkCoreDatabaseBuilder, DatabaseBuilder>();
app.Services.AddTransient<IBaseUnitOfWork, UnitOfWork>();
app.Services.AddMvc()
.AddApplicationPart(typeof(RoleController).Assembly);
AuthenticationRouting.ConfigureServices(app.Services);
return app;
}

Expand Down
Loading