Skip to content

Feature/new UI #38

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

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions samples/net5/SampleMvcWebWithUi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using DynamicAuthorization.Mvc.Core;
using DynamicAuthorization.Mvc.Core.Extensions;
using DynamicAuthorization.Mvc.JsonStore.Extensions;
using DynamicAuthorization.Mvc.Ui;
using DynamicAuthorization.Mvc.Ui.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -38,9 +38,10 @@ public void ConfigureServices(IServiceCollection services)
var mvcBuilder = services.AddControllersWithViews();
services.AddRazorPages();

services.AddDynamicAuthorization<ApplicationDbContext>(options => options.DefaultAdminUser = "[email protected]")
.AddJsonStore()
.AddUi(mvcBuilder);
services.AddDynamicAuthorization<ApplicationDbContext>(options =>
{
options.AddUi(uiOptions => uiOptions.AuthenticationType = AuthenticationType.Cookie);
}, "[email protected]");
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DynamicAuthorization.Mvc.Core;
using DynamicAuthorization.Mvc.Core.Models;
using Microsoft.AspNetCore.Razor.TagHelpers;
using SampleMvcWebWithUi.Data;
using SampleMvcWebWithUi.Models;
Expand All @@ -10,11 +9,8 @@ namespace SampleMvcWebWithUi
public class MySecureContentTagHelper : SecureContentTagHelper<ApplicationDbContext, ApplicationUser, ApplicationRole, int,
ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken>
{
public MySecureContentTagHelper(
ApplicationDbContext dbContext,
DynamicAuthorizationOptions authorizationOptions,
IRoleAccessStore roleAccessStore)
: base(dbContext, authorizationOptions, roleAccessStore)
public MySecureContentTagHelper(ApplicationDbContext dbContext, IRoleAccessStore roleAccessStore)
: base(dbContext, roleAccessStore)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DynamicAuthorization.Mvc.Core;
using DynamicAuthorization.Mvc.Core.Models;
using Microsoft.AspNetCore.Razor.TagHelpers;
using SampleMvcWebApp.Data;

Expand All @@ -8,12 +7,8 @@ namespace SampleMvcWebApp
[HtmlTargetElement("secure-content")]
public class MySecureContentTagHelper : SecureContentTagHelper<ApplicationDbContext>
{
public MySecureContentTagHelper(
ApplicationDbContext dbContext,
DynamicAuthorizationOptions authorizationOptions,
IRoleAccessStore roleAccessStore
)
: base(dbContext, authorizationOptions, roleAccessStore)
public MySecureContentTagHelper(ApplicationDbContext dbContext, IRoleAccessStore roleAccessStore)
: base(dbContext, roleAccessStore)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DynamicAuthorization.Mvc.Core;
using DynamicAuthorization.Mvc.Core.Models;
using Microsoft.AspNetCore.Razor.TagHelpers;
using SampleMvcWebAppWithUi.Data;
using SampleMvcWebAppWithUi.Models;
Expand All @@ -9,12 +8,9 @@ namespace SampleMvcWebAppWithUi
[HtmlTargetElement("secure-content")]
public class MySecureContentTagHelper : SecureContentTagHelper<ApplicationDbContext, ApplicationUser>
{
public MySecureContentTagHelper(
ApplicationDbContext dbContext,
DynamicAuthorizationOptions authorizationOptions,
IRoleAccessStore roleAccessStore
public MySecureContentTagHelper(ApplicationDbContext dbContext, IRoleAccessStore roleAccessStore
)
: base(dbContext, authorizationOptions, roleAccessStore)
: base(dbContext, roleAccessStore)
{
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Extensions.DependencyInjection;
using System;

namespace DynamicAuthorization.Mvc.Core
{
/// <inheritdoc cref="IDynamicAuthorizationOptionBuilder"/>
public class DynamicAuthorizationOptionBuilder : IDynamicAuthorizationOptionBuilder
{
private readonly IServiceCollection _services;

public DynamicAuthorizationOptionBuilder(IServiceCollection services)
{
_services = services;
}

IServiceCollection IDynamicAuthorizationOptionBuilder.Services => _services;

public IDynamicAuthorizationOptionBuilder AddDefaultAllowedAdminsAndRoles(Action<DynamicAuthorizationOptions> allowedUserRoleOptions)
{
if (allowedUserRoleOptions == null)
throw new ArgumentNullException(nameof(allowedUserRoleOptions));

var dynamicAuthorizationOptions = new DynamicAuthorizationOptions();
allowedUserRoleOptions.Invoke(dynamicAuthorizationOptions);

if (dynamicAuthorizationOptions.DefaultAllowedAdmins == null && dynamicAuthorizationOptions.DefaultAllowedRoles == null)
throw new InvalidOperationException($"One of \"{nameof(DynamicAuthorizationOptions.DefaultAllowedAdmins)}\" or" +
$" \"{nameof(DynamicAuthorizationOptions.DefaultAllowedRoles)}\" properties should be initialized.");

DynamicAuthorizationOptionsInternals.DefaultAllowedAdmins = dynamicAuthorizationOptions.DefaultAllowedAdmins;
DynamicAuthorizationOptionsInternals.DefaultAllowedRoles = dynamicAuthorizationOptions.DefaultAllowedRoles;

return this;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using System;

namespace DynamicAuthorization.Mvc.Core
{
/// <summary>
/// An interface for configuring dynamic authorization options.
/// </summary>
public interface IDynamicAuthorizationOptionBuilder
{
/// <summary>
/// Gets the <see cref="IServiceCollection"/> where essential services are configured.
/// </summary>
IServiceCollection Services { get; }

/// <summary>
/// Adds the default allowed admins and roles.
/// </summary>
/// <param name="allowedUserRoleOptions">The allowed user role options.</param>
/// <exception cref="ArgumentNullException">Throws if allowedUserRoleOptions if null.</exception>
/// <exception cref="InvalidOperationException">
/// Throws if non of DefaultAllowedAdmins and DefaultAllowedRoles are initialized.
/// </exception>
/// <returns>IDynamicAuthorizationOptionBuilder.</returns>
IDynamicAuthorizationOptionBuilder AddDefaultAllowedAdminsAndRoles(
Action<DynamicAuthorizationOptions> allowedUserRoleOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<Version>1.2.2</Version>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
using DynamicAuthorization.Mvc.Core.Builder;
using DynamicAuthorization.Mvc.Core.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace DynamicAuthorization.Mvc.Core.Extensions
namespace DynamicAuthorization.Mvc.Core
{
/// <summary>
/// Extension methods for setting up Dynamic Authorization related services in an <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(this IServiceCollection services,
Action<DynamicAuthorizationOptions> options)
where TDbContext : DbContext
/// <summary>
/// Registers the Dynamic Authorization as a service in the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The services.</param>
/// <param name="optionsBuilder">
/// An action to configure the <see cref="DynamicAuthorizationOptionBuilder"/> for the
/// Dynamic Authorization.
/// </param>
/// <param name="defaultAdminUser">
/// The default user to access all controllers without needs for creating role and related
/// accesses in database.
/// </param>
/// <exception cref="ArgumentNullException">services</exception>
/// <exception cref="ArgumentNullException">optionsBuilder</exception>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IDynamicAuthorizationOptionBuilder AddDynamicAuthorization<TDbContext>(
this IServiceCollection services,
Action<DynamicAuthorizationOptionBuilder> optionsBuilder,
string defaultAdminUser
) where TDbContext : DbContext
{
var dynamicAuthorizationOptions = new DynamicAuthorizationOptions();
options.Invoke(dynamicAuthorizationOptions);
services.AddSingleton(dynamicAuthorizationOptions);
if (services == null)
throw new ArgumentNullException(nameof(services));

if (optionsBuilder == null)
throw new ArgumentNullException(nameof(optionsBuilder));

if (defaultAdminUser == null)
throw new ArgumentNullException(nameof(defaultAdminUser));

var baseType = typeof(TDbContext).BaseType;
var paramsLength = baseType.GetGenericArguments().Length;
Expand All @@ -28,9 +51,9 @@ public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(t
{
case 1:
userType = baseType.GetGenericArguments()[0];
DynamicAuthorizationOptions.UserType = userType;
DynamicAuthorizationOptions.RoleType = typeof(IdentityRole);
DynamicAuthorizationOptions.KeyType = typeof(string);
DynamicAuthorizationOptionsInternals.UserType = userType;
DynamicAuthorizationOptionsInternals.RoleType = typeof(IdentityRole);
DynamicAuthorizationOptionsInternals.KeyType = typeof(string);
services.Configure<MvcOptions>(mvcOptions =>
{
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<,>).MakeGenericType(typeof(TDbContext), userType));
Expand All @@ -41,9 +64,9 @@ public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(t
userType = baseType.GetGenericArguments()[0];
roleType = baseType.GetGenericArguments()[1];
keyType = baseType.GetGenericArguments()[2];
DynamicAuthorizationOptions.UserType = userType;
DynamicAuthorizationOptions.RoleType = roleType;
DynamicAuthorizationOptions.KeyType = keyType;
DynamicAuthorizationOptionsInternals.UserType = userType;
DynamicAuthorizationOptionsInternals.RoleType = roleType;
DynamicAuthorizationOptionsInternals.KeyType = keyType;
services.Configure<MvcOptions>(mvcOptions =>
{
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<,,,>)
Expand All @@ -60,14 +83,14 @@ public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(t
var userLoginType = baseType.GetGenericArguments()[5];
var roleClaimType = baseType.GetGenericArguments()[6];
var userTokenType = baseType.GetGenericArguments()[7];
DynamicAuthorizationOptions.UserType = userType;
DynamicAuthorizationOptions.RoleType = roleType;
DynamicAuthorizationOptions.KeyType = keyType;
DynamicAuthorizationOptions.UserClaimType = userClaimType;
DynamicAuthorizationOptions.UserRoleType = userRoleType;
DynamicAuthorizationOptions.UserLoginType = userLoginType;
DynamicAuthorizationOptions.RoleClaimType = roleClaimType;
DynamicAuthorizationOptions.UserTokenType = userTokenType;
DynamicAuthorizationOptionsInternals.UserType = userType;
DynamicAuthorizationOptionsInternals.RoleType = roleType;
DynamicAuthorizationOptionsInternals.KeyType = keyType;
DynamicAuthorizationOptionsInternals.UserClaimType = userClaimType;
DynamicAuthorizationOptionsInternals.UserRoleType = userRoleType;
DynamicAuthorizationOptionsInternals.UserLoginType = userLoginType;
DynamicAuthorizationOptionsInternals.RoleClaimType = roleClaimType;
DynamicAuthorizationOptionsInternals.UserTokenType = userTokenType;
services.Configure<MvcOptions>(mvcOptions =>
{
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<,,,,,,,,>)
Expand All @@ -76,9 +99,9 @@ public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(t
break;

default:
DynamicAuthorizationOptions.UserType = typeof(IdentityUser);
DynamicAuthorizationOptions.RoleType = typeof(IdentityRole);
DynamicAuthorizationOptions.KeyType = typeof(string);
DynamicAuthorizationOptionsInternals.UserType = typeof(IdentityUser);
DynamicAuthorizationOptionsInternals.RoleType = typeof(IdentityRole);
DynamicAuthorizationOptionsInternals.KeyType = typeof(string);
services.Configure<MvcOptions>(mvcOptions =>
{
mvcOptions.Filters.Add(typeof(DynamicAuthorizationFilter<>).MakeGenericType(typeof(TDbContext)));
Expand All @@ -88,9 +111,9 @@ public static IDynamicAuthorizationBuilder AddDynamicAuthorization<TDbContext>(t

services.AddSingleton<IMvcControllerDiscovery, MvcControllerDiscovery>();

IDynamicAuthorizationBuilder builder = new DynamicAuthorizationBuilder(services);
IDynamicAuthorizationOptionBuilder builder = new DynamicAuthorizationOptionBuilder(services);

DynamicAuthorizationOptions.DbContextType = typeof(TDbContext);
DynamicAuthorizationOptionsInternals.DbContextType = typeof(TDbContext);

return builder;
}
Expand Down
Loading