Skip to content

Commit

Permalink
Identity migration done
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-atharva committed Apr 28, 2023
1 parent daccf84 commit 5fe7f2a
Show file tree
Hide file tree
Showing 36 changed files with 1,063 additions and 415 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BlazorEcommerce.Shared;
using BlazorEcommerce.Shared.User;

namespace BlazorEcommerce.Application.Contracts.Identity;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using BlazorEcommerce.Shared;
using BlazorEcommerce.Shared.AccessControl;
using BlazorEcommerce.Shared.Authorization;
using BlazorEcommerce.Shared.User;

namespace BlazorEcommerce.Application.Contracts.Identity;

Expand All @@ -14,21 +12,11 @@ public interface IIdentityService

Task<Result> DeleteUserAsync(string userId);

Task<IList<RoleDto>> GetRolesAsync(CancellationToken cancellationToken);

Task UpdateRolePermissionsAsync(string roleId, Permissions permissions);

Task<IList<UserDto>> GetUsersAsync(CancellationToken cancellationToken);

Task<UserDto> GetUserAsync(string id);

Task UpdateUserAsync(UserDto updatedUser);

Task CreateRoleAsync(RoleDto newRole);

Task UpdateRoleAsync(RoleDto updatedRole);

Task DeleteRoleAsync(string roleId);

Task<IResponse> ChangePassword(string userId, string currentPassword, string newPassword);
}
5 changes: 0 additions & 5 deletions src/BlazorEcommerce.Identity/BlazorEcommerce.Identity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
</ItemGroup>

Expand Down
28 changes: 28 additions & 0 deletions src/BlazorEcommerce.Identity/Configurations/RoleConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using BlazorEcommerce.Shared.Constant;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace BlazorEcommerce.Identity.Configurations
{
public class RoleConfiguration : IEntityTypeConfiguration<IdentityRole>
{
public void Configure(EntityTypeBuilder<IdentityRole> builder)
{
builder.HasData(
new IdentityRole
{
Id = "cac43a6e-f7bb-4448-baaf-1add431ccbbf",
Name = "User",
NormalizedName = "USER"
},
new IdentityRole
{
Id = "cbc43a8e-f7bb-4445-baaf-1add431ffbbf",
Name = Constants.AdminRoleName,
NormalizedName = Constants.AdminRoleName.ToUpper()
}
);
}
}
}
29 changes: 29 additions & 0 deletions src/BlazorEcommerce.Identity/Configurations/UserConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using BlazorEcommerce.Shared.Constant;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace BlazorEcommerce.Identity.Configurations
{
public class UserConfiguration : IEntityTypeConfiguration<ApplicationUser>
{
public void Configure(EntityTypeBuilder<ApplicationUser> builder)
{
var hasher = new PasswordHasher<ApplicationUser>();
builder.HasData(
new ApplicationUser
{
Id = "8e445865-a24d-4543-a6c6-9443d048cdb9",
Email = Constants.AdminEmail,
NormalizedEmail = Constants.AdminEmail.ToUpper(),
FirstName = "System",
LastName = "Admin",
UserName = Constants.AdminEmail,
NormalizedUserName = Constants.AdminEmail.ToUpper(),
PasswordHash = hasher.HashPassword(null, "Atharva@123"),
EmailConfirmed = true
}
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;


namespace BlazorEcommerce.Identity.Configurations
{
public class UserRoleConfiguration : IEntityTypeConfiguration<IdentityUserRole<string>>
{
public void Configure(EntityTypeBuilder<IdentityUserRole<string>> builder)
{
builder.HasData(
new IdentityUserRole<string>
{
RoleId = "cbc43a8e-f7bb-4445-baaf-1add431ffbbf",
UserId = "8e445865-a24d-4543-a6c6-9443d048cdb9"
}
);
}
}
}
14 changes: 6 additions & 8 deletions src/BlazorEcommerce.Identity/ConfigureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using BlazorEcommerce.Application.Model;
using BlazorEcommerce.Identity.Contexts;
using BlazorEcommerce.Identity.Service;
using BlazorEcommerce.Persistence.Contexts;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -19,15 +21,11 @@ public static IServiceCollection AddIdentityServices(this IServiceCollection ser

var connectionString = configuration.GetConnectionString("Default") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");

services.AddDbContext<ApplicationDbContext>(options =>
services.AddDbContext<UserIdentityDbContext>(options =>
options.UseSqlServer(connectionString));

services.AddScoped<ApplicationDbContextInitialiser>();

services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddRoles<ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddClaimsPrincipalFactory<ApplicationUserClaimsPrincipalFactory>();
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<UserIdentityDbContext>().AddDefaultTokenProviders();

services.AddAuthentication(options =>
{
Expand All @@ -50,8 +48,8 @@ public static IServiceCollection AddIdentityServices(this IServiceCollection ser
});

services.AddScoped<IIdentityService, IdentityService>();

services.AddScoped<IAuthService, AuthService>();
services.AddScoped<UserIdentityDbContextInitialiser>();

return services;
}
Expand Down
27 changes: 0 additions & 27 deletions src/BlazorEcommerce.Identity/Contexts/ApiAuthorizationDbContext.cs

This file was deleted.

29 changes: 0 additions & 29 deletions src/BlazorEcommerce.Identity/Contexts/ApplicationDbContext.cs

This file was deleted.

This file was deleted.

23 changes: 23 additions & 0 deletions src/BlazorEcommerce.Identity/Contexts/UserIdentityDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace BlazorEcommerce.Identity.Contexts;

public class UserIdentityDbContext : IdentityDbContext<ApplicationUser>
{
public UserIdentityDbContext(DbContextOptions<UserIdentityDbContext> options) : base(options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.ApplyConfigurationsFromAssembly(typeof(UserIdentityDbContext).Assembly);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using BlazorEcommerce.Identity.Contexts;
using Microsoft.EntityFrameworkCore;

namespace BlazorEcommerce.Persistence.Contexts;

public class UserIdentityDbContextInitialiser
{
private readonly UserIdentityDbContext _context;

public UserIdentityDbContextInitialiser(UserIdentityDbContext context)
{
_context = context;
}

public async Task InitialiseAsync()
{
await InitialiseWithMigrationsAsync();
}

private async Task InitialiseWithMigrationsAsync()
{
if (_context.Database.IsSqlServer())
{
await _context.Database.MigrateAsync();
}
else
{
await _context.Database.EnsureCreatedAsync();
}
}
}
9 changes: 0 additions & 9 deletions src/BlazorEcommerce.Identity/Identity/ApplicationRole.cs

This file was deleted.

Loading

0 comments on commit 5fe7f2a

Please sign in to comment.