-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
secure spa with openid working aureli cli middleware done Stubbed out item templates (one working) Stubbed out project templates (one working) Stubbed out snippets (few working) Finishing up wizard for project generation next Finish up item template that will generate routing table
- Loading branch information
1 parent
6173a52
commit 74efd1f
Showing
248 changed files
with
21,829 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using AspNet.Security.OpenIdConnect.Primitives; | ||
using Aurelia.DotNet.DataAccess; | ||
using Aurelia.DotNet.DataAccess.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Identity; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Security.Claims; | ||
using Aurelia.DotNet.Logic.Interfaces; | ||
using Aurelia.DotNet.DataAccess.Common; | ||
|
||
namespace Aurelia.DotNet.Logic | ||
{ | ||
|
||
public class AccountLogic : IAccountLogic | ||
{ | ||
private readonly ApplicationDbContext _context; | ||
private readonly UserManager<User> _userManager; | ||
private readonly RoleManager<Role> _roleManager; | ||
|
||
public AccountLogic( | ||
ApplicationDbContext context, | ||
UserManager<User> userManager, | ||
RoleManager<Role> roleManager, | ||
IHttpContextAccessor httpAccessor) | ||
{ | ||
_context = context; | ||
|
||
_context.UserId = int.Parse(httpAccessor.HttpContext?.User.FindFirst(OpenIdConnectConstants.Claims.Subject)?.Value ?? "-1"); | ||
_userManager = userManager; | ||
_roleManager = roleManager; | ||
} | ||
|
||
public async Task<User> FindByIdAsync(string userId) | ||
{ | ||
return await _userManager.FindByIdAsync(userId); | ||
} | ||
|
||
public async Task<User> FindByNameAsync(string userName) | ||
{ | ||
return await _userManager.FindByNameAsync(userName); | ||
} | ||
|
||
public async Task<User> FindByEmailAsync(string email) | ||
{ | ||
return await _userManager.FindByEmailAsync(email); | ||
} | ||
|
||
public async Task<IEnumerable<string>> GetRolesAsync(User user) | ||
{ | ||
return await _userManager.GetRolesAsync(user); | ||
} | ||
public async Task<IEnumerable<IdentityError>> CreateUserAsync(User user, string password, params string[] roles) | ||
{ | ||
roles = roles ?? new string[] { }; | ||
var result = await _userManager.CreateAsync(user, password); | ||
if (!result.Succeeded) | ||
return result.Errors; | ||
|
||
//ReUp the user from the DB | ||
user = await _userManager.FindByNameAsync(user.UserName); | ||
|
||
try | ||
{ | ||
result = await this._userManager.AddToRolesAsync(user, roles.Distinct()); | ||
} | ||
catch | ||
{ | ||
await DeleteUserAsync(user); | ||
throw; | ||
} | ||
|
||
if (!result.Succeeded) | ||
{ | ||
await DeleteUserAsync(user); | ||
} | ||
return result.Errors; | ||
} | ||
|
||
public async Task<IEnumerable<IdentityError>> DeleteUserAsync(User user) | ||
{ | ||
var result = await _userManager.DeleteAsync(user); | ||
return result.Errors; | ||
} | ||
|
||
public async Task<Role> FindRoleBuId(string roleId) | ||
{ | ||
return await _roleManager.FindByIdAsync(roleId); | ||
} | ||
public async Task<Role> FindRoleByNameAsync(string roleName) | ||
{ | ||
return await _roleManager.FindByNameAsync(roleName); | ||
} | ||
|
||
public async Task<IEnumerable<IdentityError>> CreateRoleAsync(Role role, params string[] claims) | ||
{ | ||
claims = claims ?? new string[] { }; | ||
var result = await _roleManager.CreateAsync(role); | ||
if (!result.Succeeded) | ||
{ | ||
return result.Errors; | ||
} | ||
// Re-Up Role | ||
role = await _roleManager.FindByNameAsync(role.Name); | ||
|
||
foreach (string claim in claims.Distinct()) | ||
{ | ||
result = await this._roleManager.AddClaimAsync(role, new Claim(Claims.Permission, Permissions.Admin)); | ||
|
||
if (!result.Succeeded) | ||
{ | ||
await DeleteRoleAsync(role); | ||
return result.Errors; | ||
} | ||
} | ||
|
||
return result.Errors; | ||
} | ||
|
||
public async Task<IEnumerable<IdentityError>> DeleteRoleAsync(Role role) | ||
{ | ||
var result = await _roleManager.DeleteAsync(role); | ||
return result.Errors; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Aurelia.DotNet.DataAccess.Interfaces; | ||
using Aurelia.DotNet.DataAccess.Models; | ||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Aurelia.DotNet.DataAccess | ||
{ | ||
public class ApplicationDbContext : IdentityDbContext<User, Role, int> | ||
{ | ||
public int UserId { get; set; } | ||
public DbSet<Vehicle> Vehicles { get; set; } | ||
public DbSet<Manufacturer> Manufacturers{ get; set; } | ||
|
||
public ApplicationDbContext(DbContextOptions options) : base(options) | ||
{ } | ||
|
||
protected override void OnModelCreating(ModelBuilder builder) | ||
{ | ||
base.OnModelCreating(builder); | ||
builder.Entity<User>().HasMany(u => u.Claims).WithOne().HasForeignKey(c => c.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade); | ||
builder.Entity<User>().HasMany(u => u.Roles).WithOne().HasForeignKey(r => r.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade); | ||
builder.Entity<Role>().HasMany(r => r.Claims).WithOne().HasForeignKey(c => c.RoleId).IsRequired().OnDelete(DeleteBehavior.Cascade); | ||
builder.Entity<Role>().HasMany(r => r.Users).WithOne().HasForeignKey(r => r.RoleId).IsRequired().OnDelete(DeleteBehavior.Cascade); | ||
builder.Entity<Vehicle>().HasOne(y => y.Manufacturer).WithMany(y => y.Vehicles).HasForeignKey(y=>y.ManufacturerId).HasConstraintName("FK_Vehicle_ManufacturerId_Manufacturer_Id"); | ||
} | ||
|
||
public override int SaveChanges() | ||
{ | ||
AuditEntitiesBeingSaved(); | ||
return base.SaveChanges(); | ||
} | ||
|
||
|
||
public override int SaveChanges(bool acceptAllChangesOnSuccess) | ||
{ | ||
AuditEntitiesBeingSaved(); | ||
return base.SaveChanges(acceptAllChangesOnSuccess); | ||
} | ||
|
||
|
||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
AuditEntitiesBeingSaved(); | ||
return base.SaveChangesAsync(cancellationToken); | ||
} | ||
|
||
|
||
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
AuditEntitiesBeingSaved(); | ||
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); | ||
} | ||
|
||
|
||
private void AuditEntitiesBeingSaved() | ||
{ | ||
var currentTime = DateTime.Now; | ||
ChangeTracker.Entries().Where(x => x.Entity is IAudit && (x.State == EntityState.Added || x.State == EntityState.Modified)).ToList().ForEach(changeEntry => | ||
{ | ||
var auditEntity = changeEntry.Entity as IAudit; | ||
if (changeEntry.State == EntityState.Added) | ||
{ | ||
auditEntity.CreateDate = currentTime; | ||
auditEntity.CreateDateUTC = currentTime.ToUniversalTime(); | ||
auditEntity.CreatedBy = UserId; | ||
} | ||
else | ||
{ | ||
// Whoa Nelly - You can't set these again! | ||
base.Entry(auditEntity).Property(x => x.CreatedBy).IsModified = false; | ||
base.Entry(auditEntity).Property(x => x.CreateDate).IsModified = false; | ||
base.Entry(auditEntity).Property(x => x.CreateDateUTC).IsModified = false; | ||
} | ||
|
||
auditEntity.LastModifyDate = currentTime; | ||
auditEntity.LastModifyDate = currentTime.ToUniversalTime(); | ||
auditEntity.LastModifiedBy = UserId; | ||
}); | ||
} | ||
|
||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Aurelia.DotNet.DataAccess/Aurelia.DotNet.DataAccess.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AspNet.Security.OpenIdConnect.Primitives" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Aurelia.DotNet.DataAccess.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Aurelia.DotNet.DataAccess | ||
{ | ||
public abstract class Audit : IAudit | ||
{ | ||
|
||
public Audit() | ||
{ | ||
this.CreateDate = new DateTime(); | ||
this.CreateDateUTC = this.CreateDate.ToUniversalTime(); | ||
} | ||
|
||
|
||
public DateTime CreateDate { get; set; } | ||
public DateTime CreateDateUTC { get; set; } | ||
public DateTime LastModifyDate { get; set; } | ||
public DateTime LastModifyDateUTC { get; set; } | ||
|
||
public int CreatedBy { get; set; } | ||
public int LastModifiedBy { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Aurelia.DotNet.DataAccess | ||
{ | ||
public static class Claims | ||
{ | ||
public const string Permission = "permission"; | ||
} | ||
} |
Oops, something went wrong.