Skip to content

Commit

Permalink
Isolate all EF<>Identity related bits in new project Piranha.AspNetCo…
Browse files Browse the repository at this point in the history
…re.Identity.EntityFrameworkCore

Use standard functionality of UserManager and RoleManager classes in UserController and RoleController (so remove dependency on IDB)
  • Loading branch information
tedvanderveen committed May 28, 2021
1 parent 9f60f19 commit 43b9eef
Show file tree
Hide file tree
Showing 25 changed files with 500 additions and 597 deletions.
2 changes: 2 additions & 0 deletions Piranha.sln
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "identity", "identity", "{9C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Piranha.Manager.LocalAuth", "core\Piranha.Manager.LocalAuth\Piranha.Manager.LocalAuth.csproj", "{A88D6D8D-725E-494F-91CD-440D87BFF21B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Piranha.AspNetCore.Identity.EntityFrameworkCore", "identity\Piranha.AspNetCore.Identity.EntityFrameworkCore\Piranha.AspNetCore.Identity.EntityFrameworkCore.csproj", "{52E9EC4E-1E63-47E1-92EC-EE6BB09AEFFC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
5 changes: 4 additions & 1 deletion examples/MvcWeb/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Piranha;
using Piranha.AspNetCore.Identity;
using Piranha.AspNetCore.Identity.EntityFrameworkCore;
using Piranha.Data.EF.SQLite;
using Piranha.AspNetCore.Identity.SQLite;
using Piranha.AttributeBuilder;
Expand All @@ -39,8 +41,9 @@ public void ConfigureServices(IServiceCollection services)
options.UseEF<SQLiteDb>(db =>
db.UseSqlite("Filename=./piranha.mvcweb.db"));
options.UseIdentityWithSeed<IdentitySQLiteDb>(db =>
options.UseIdentityEF<IdentitySQLiteDb>(db =>
db.UseSqlite("Filename=./piranha.mvcweb.db"));
options.UseIdentityWithSeed();
options.UseSecurity(o =>
{
Expand Down
5 changes: 0 additions & 5 deletions examples/RazorWeb/RazorWeb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@
<ProjectReference Include="..\..\core\Piranha\Piranha.csproj" />
<ProjectReference Include="..\..\core\Piranha.AspNetCore\Piranha.AspNetCore.csproj" />
<ProjectReference Include="..\..\core\Piranha.AttributeBuilder\Piranha.AttributeBuilder.csproj" />
<ProjectReference Include="..\..\core\Piranha.Azure.BlobStorage\Piranha.Azure.BlobStorage.csproj" />
<ProjectReference Include="..\..\core\Piranha.ImageSharp\Piranha.ImageSharp.csproj" />
<ProjectReference Include="..\..\core\Piranha.Local.FileStorage\Piranha.Local.FileStorage.csproj" />
<ProjectReference Include="..\..\core\Piranha.Manager\Piranha.Manager.csproj" />
<ProjectReference Include="..\..\core\Piranha.Manager.TinyMCE\Piranha.Manager.TinyMCE.csproj" />
<ProjectReference Include="..\..\data\Piranha.Data.EF.SQLite\Piranha.Data.EF.SQLite.csproj" />
<ProjectReference Include="..\..\data\Piranha.Data.EF.SQLServer\Piranha.Data.EF.SQLServer.csproj" />
<ProjectReference Include="..\..\data\Piranha.Data.EF.MySql\Piranha.Data.EF.MySql.csproj" />
<ProjectReference Include="..\..\data\Piranha.Data.EF.PostgreSql\Piranha.Data.EF.PostgreSql.csproj" />
<ProjectReference Include="..\..\identity\Piranha.AspNetCore.Identity.SQLite\Piranha.AspNetCore.Identity.SQLite.csproj" />
<ProjectReference Include="..\..\identity\Piranha.AspNetCore.Identity.SQLServer\Piranha.AspNetCore.Identity.SQLServer.csproj" />
</ItemGroup>
</Project>
6 changes: 4 additions & 2 deletions examples/RazorWeb/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Piranha;
using Piranha.AspNetCore.Identity;
using Piranha.AspNetCore.Identity.EntityFrameworkCore;
using Piranha.Data.EF.SQLite;
using Piranha.AspNetCore.Identity.SQLite;
using Piranha.AttributeBuilder;
Expand All @@ -39,8 +41,8 @@ public void ConfigureServices(IServiceCollection services)
options.UseEF<SQLiteDb>(db =>
db.UseSqlite("Filename=./piranha.razorweb.db"));
options.UseIdentityWithSeed<IdentitySQLiteDb>(db =>
db.UseSqlite("Filename=./piranha.razorweb.db"));
options.UseIdentityEF<IdentitySQLiteDb>(db =>
db.UseSqlite("Filename=./piranha.mvcweb.db"));
options.UseSecurity(o =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using Microsoft.EntityFrameworkCore;
using Piranha.AspNetCore.Identity.Data;

namespace Piranha.AspNetCore.Identity
namespace Piranha.AspNetCore.Identity.EntityFrameworkCore
{
public abstract class Db<T> :
IdentityDbContext<User, Role, Guid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using Microsoft.EntityFrameworkCore;
using Piranha.AspNetCore.Identity.Data;

namespace Piranha.AspNetCore.Identity
namespace Piranha.AspNetCore.Identity.EntityFrameworkCore
{
public interface IDb
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) .NET Foundation and Contributors
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
* http://github.com/piranhacms/piranha.core
*
*/

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Piranha.AspNetCore.Identity.Data;

namespace Piranha.AspNetCore.Identity.EntityFrameworkCore
{
public static class IdentityModuleExtensions
{
/// <summary>
/// Adds the Piranha identity module.
/// </summary>
/// <param name="services">The current service collection</param>
/// <param name="dbOptions">Options for configuring the database</param>
/// <returns>The services</returns>
public static IServiceCollection AddPiranhaIdentityEF<T>(this IServiceCollection services, Action<DbContextOptionsBuilder> dbOptions) where T : DbContext, IDb
{
services.AddDbContext<T>(dbOptions);
services.AddScoped<IDb, T>();
services.AddScoped<T, T>();
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<T>();

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) .NET Foundation and Contributors
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
* http://github.com/piranhacms/piranha.core
*
*/

using System;
using Microsoft.EntityFrameworkCore;

namespace Piranha.AspNetCore.Identity.EntityFrameworkCore
{
public static class IdentityStartupExtensions
{
/// <summary>
/// Extensions method for simplified setup.
/// </summary>
/// <param name="serviceBuilder">The current service builder</param>
/// <param name="dbOptions">The db options</param>
/// <typeparam name="T">The DbContext type</typeparam>
/// <returns>The builder</returns>
public static PiranhaServiceBuilder UseIdentityEF<T>(this PiranhaServiceBuilder serviceBuilder, Action<DbContextOptionsBuilder> dbOptions)
where T : Db<T>
{
serviceBuilder.Services.AddPiranhaIdentityEF<T>(dbOptions);

return serviceBuilder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<NoWarn>$(NoWarn);1591</NoWarn>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<Description>Piranha CMS security provider for AspNetCore Identity</Description>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
<ProjectReference Include="..\Piranha.AspNetCore.Identity\Piranha.AspNetCore.Identity.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Piranha.AspNetCore.Identity.MySQL
{
public class IdentityMySQLDb : Identity.Db<IdentityMySQLDb>
public class IdentityMySQLDb : EntityFrameworkCore.Db<IdentityMySQLDb>
{
/// <summary>
/// Default constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.0-alpha.1" />
<ProjectReference Include="..\Piranha.AspNetCore.Identity\Piranha.AspNetCore.Identity.csproj" />
<ProjectReference Include="..\Piranha.AspNetCore.Identity.EntityFrameworkCore\Piranha.AspNetCore.Identity.EntityFrameworkCore.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Piranha.AspNetCore.Identity.PostgreSQL
{
public class IdentityPostgreSQLDb : Db<IdentityPostgreSQLDb>
public class IdentityPostgreSQLDb : EntityFrameworkCore.Db<IdentityPostgreSQLDb>
{
/// <summary>
/// Default constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.0-rc2" />
<ProjectReference Include="..\Piranha.AspNetCore.Identity\Piranha.AspNetCore.Identity.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Piranha.AspNetCore.Identity.EntityFrameworkCore\Piranha.AspNetCore.Identity.EntityFrameworkCore.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Piranha.AspNetCore.Identity.SQLite
{
public class IdentitySQLiteDb : Db<IdentitySQLiteDb>
public class IdentitySQLiteDb : EntityFrameworkCore.Db<IdentitySQLiteDb>
{
/// <summary>
/// Default constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.0" />
<ProjectReference Include="..\Piranha.AspNetCore.Identity\Piranha.AspNetCore.Identity.csproj" />
<ProjectReference Include="..\Piranha.AspNetCore.Identity.EntityFrameworkCore\Piranha.AspNetCore.Identity.EntityFrameworkCore.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 43b9eef

Please sign in to comment.