Fixed controller adding from Api library
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,4 +2,3 @@ bin/
|
||||
obj/
|
||||
/packages/
|
||||
riderModule.iml
|
||||
/_ReSharper.Caches/
|
||||
1
DatabaseTest/.gitignore
vendored
1
DatabaseTest/.gitignore
vendored
@@ -2,4 +2,3 @@
|
||||
bin
|
||||
Migrations
|
||||
appsettings.Development.json
|
||||
test.db
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using HopFrame.Api.Controller;
|
||||
using HopFrame.Security.Authorization;
|
||||
using HopFrame.Security.Claims;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -6,7 +5,8 @@ using Microsoft.AspNetCore.Mvc;
|
||||
namespace DatabaseTest.Controllers;
|
||||
|
||||
[ApiController]
|
||||
public class TestController(DatabaseContext context, ITokenContext userContext) : SecurityController<DatabaseContext>(context) {
|
||||
[Route("test")]
|
||||
public class TestController(ITokenContext userContext) : ControllerBase {
|
||||
|
||||
[HttpGet("permissions"), Authorized]
|
||||
public ActionResult<IList<string>> Permissions() {
|
||||
|
||||
@@ -7,6 +7,6 @@ public class DatabaseContext : HopDbContextBase {
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
|
||||
optionsBuilder.UseSqlite("Data Source=C:\\Users\\Remote\\Documents\\Projekte\\HopFrame\\DatabaseTest\\test.db;Mode=ReadWrite;");
|
||||
optionsBuilder.UseSqlite("Data Source=C:\\Users\\Remote\\Documents\\Projekte\\HopFrame\\DatabaseTest\\bin\\Debug\\net8.0\\test.db;Mode=ReadWrite;");
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,18 @@
|
||||
using DatabaseTest;
|
||||
using HopFrame.Api;
|
||||
using HopFrame.Api.Controller;
|
||||
using HopFrame.Security.Authentication;
|
||||
using HopFrame.Api.Extensions;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers()
|
||||
.AddController<SecurityController<DatabaseContext>>();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddHopFrame<DatabaseContext>();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddDbContext<DatabaseContext>();
|
||||
builder.Services.AddHopFrameAuthentication<DatabaseContext>();
|
||||
//builder.Logging.AddFilter<HopFrameAuthentication<DatabaseContext>>(options => options == LogLevel.None);
|
||||
|
||||
builder.Services.AddSwaggerGen(c => {
|
||||
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme {
|
||||
@@ -51,7 +47,7 @@ if (app.Environment.IsDevelopment()) {
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
Binary file not shown.
@@ -1,13 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HopFrame.Api;
|
||||
|
||||
public static class ControllerExtensions {
|
||||
|
||||
public static IMvcBuilder AddController<TController>(this IMvcBuilder builder) where TController : ControllerBase {
|
||||
//TODO: Change implementation method
|
||||
return builder.AddApplicationPart(typeof(TController).Assembly);
|
||||
}
|
||||
|
||||
}
|
||||
86
HopFrame.Api/Extensions/MvcExtensions.cs
Normal file
86
HopFrame.Api/Extensions/MvcExtensions.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
//Source: https://gist.github.com/damianh/5d69be0e3004024f03b6cc876d7b0bd3
|
||||
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using IMvcCoreBuilder = Microsoft.Extensions.DependencyInjection.IMvcCoreBuilder;
|
||||
|
||||
namespace HopFrame.Api.Extensions;
|
||||
|
||||
public static class MvcExtensions {
|
||||
/// <summary>
|
||||
/// Finds the appropriate controllers
|
||||
/// </summary>
|
||||
/// <param name="partManager">The manager for the parts</param>
|
||||
/// <param name="controllerTypes">The controller types that are allowed. </param>
|
||||
public static void UseSpecificControllers(this ApplicationPartManager partManager, params Type[] controllerTypes) {
|
||||
partManager.FeatureProviders.Add(new InternalControllerFeatureProvider());
|
||||
//partManager.ApplicationParts.Clear();
|
||||
partManager.ApplicationParts.Add(new SelectedControllersApplicationParts(controllerTypes));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Only allow selected controllers
|
||||
/// </summary>
|
||||
/// <param name="mvcCoreBuilder">The builder that configures mvc core</param>
|
||||
/// <param name="controllerTypes">The controller types that are allowed. </param>
|
||||
public static IMvcCoreBuilder
|
||||
UseSpecificControllers(this IMvcCoreBuilder mvcCoreBuilder, params Type[] controllerTypes) =>
|
||||
mvcCoreBuilder.ConfigureApplicationPartManager(partManager =>
|
||||
partManager.UseSpecificControllers(controllerTypes));
|
||||
|
||||
/// <summary>
|
||||
/// Only instantiates selected controllers, not all of them. Prevents application scanning for controllers.
|
||||
/// </summary>
|
||||
private class SelectedControllersApplicationParts : ApplicationPart, IApplicationPartTypeProvider {
|
||||
public SelectedControllersApplicationParts() {
|
||||
Name = "Only allow selected controllers";
|
||||
}
|
||||
|
||||
public SelectedControllersApplicationParts(Type[] types) {
|
||||
Types = types.Select(x => x.GetTypeInfo()).ToArray();
|
||||
}
|
||||
|
||||
public override string Name { get; }
|
||||
|
||||
public IEnumerable<TypeInfo> Types { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensure that internal controllers are also allowed. The default ControllerFeatureProvider hides internal controllers, but this one allows it.
|
||||
/// </summary>
|
||||
private class InternalControllerFeatureProvider : ControllerFeatureProvider {
|
||||
private const string ControllerTypeNameSuffix = "Controller";
|
||||
|
||||
/// <summary>
|
||||
/// Determines if a given <paramref name="typeInfo"/> is a controller. The default ControllerFeatureProvider hides internal controllers, but this one allows it.
|
||||
/// </summary>
|
||||
/// <param name="typeInfo">The <see cref="TypeInfo"/> candidate.</param>
|
||||
/// <returns><code>true</code> if the type is a controller; otherwise <code>false</code>.</returns>
|
||||
protected override bool IsController(TypeInfo typeInfo) {
|
||||
if (!typeInfo.IsClass) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeInfo.IsAbstract) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeInfo.ContainsGenericParameters) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeInfo.IsDefined(typeof(Microsoft.AspNetCore.Mvc.NonControllerAttribute))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!typeInfo.Name.EndsWith(ControllerTypeNameSuffix, StringComparison.OrdinalIgnoreCase) &&
|
||||
!typeInfo.IsDefined(typeof(Microsoft.AspNetCore.Mvc.ControllerAttribute))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
HopFrame.Api/Extensions/ServiceCollectionExtensions.cs
Normal file
15
HopFrame.Api/Extensions/ServiceCollectionExtensions.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using HopFrame.Api.Controller;
|
||||
using HopFrame.Database;
|
||||
using HopFrame.Security.Authentication;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HopFrame.Api.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions {
|
||||
|
||||
public static void AddHopFrame<TDbContext>(this IServiceCollection services) where TDbContext : HopDbContextBase {
|
||||
services.AddMvcCore().UseSpecificControllers(typeof(SecurityController<TDbContext>));
|
||||
services.AddHopFrameAuthentication<TDbContext>();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user