Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Commit

Permalink
Audit Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMatt57 committed Oct 26, 2016
1 parent 26bc0fe commit f5f29ea
Show file tree
Hide file tree
Showing 26 changed files with 535 additions and 19 deletions.
3 changes: 2 additions & 1 deletion DOL.WHD.Section14c.Api/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ public async Task<IHttpActionResult> Register(RegisterBindingModel model)
}

// Add User
var now = DateTime.UtcNow;
var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
user.Organizations.Add(new OrganizationMembership { EIN = model.EIN, IsAdmin = true});
user.Organizations.Add(new OrganizationMembership { EIN = model.EIN, IsAdmin = true, CreatedAt = now, LastModifiedAt = now, CreatedBy_Id = user.Id, LastModifiedBy_Id = user.Id });

IdentityResult result = await UserManager.CreateAsync(user, model.Password);

Expand Down
47 changes: 47 additions & 0 deletions DOL.WHD.Section14c.DataAccess/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
using DOL.WHD.Section14c.Domain.Models;
using DOL.WHD.Section14c.Domain.Models.Submission;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity;
using System;
using System.Linq;
using System.Web;
using System.Security.Claims;

namespace DOL.WHD.Section14c.DataAccess
{
Expand Down Expand Up @@ -46,5 +51,47 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
.Map(m => m.ToTable("WorkSiteWorkSiteType"));

}

public override int SaveChanges()
{
var addedAuditedEntities = ChangeTracker.Entries<IAuditedEntity>()
.Where(p => p.State == EntityState.Added)
.Select(p => p.Entity);

var modifiedAuditedEntities = ChangeTracker.Entries<IAuditedEntity>()
.Where(p => p.State == EntityState.Modified)
.Select(p => p.Entity);

var now = DateTime.UtcNow;
var zeroTime = new DateTime();

var userId = Guid.Empty.ToString();

if (HttpContext.Current != null && HttpContext.Current.User != null)
{
userId = HttpContext.Current.User.Identity.GetUserId();
}

foreach (var added in addedAuditedEntities)
{
if (added.CreatedAt == zeroTime) { added.CreatedAt = now; }
added.LastModifiedAt = now;
added.CreatedBy_Id = userId;
added.LastModifiedBy_Id = userId;
}

foreach (var modified in modifiedAuditedEntities)
{
if (modified.CreatedAt == zeroTime)
{
modified.CreatedAt = now;
modified.CreatedBy_Id = userId;
}
modified.LastModifiedAt = now;
modified.LastModifiedBy_Id = userId;
}

return base.SaveChanges();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -166,6 +167,10 @@
<Compile Include="Migrations\201610252128299_EmployerUpdate.Designer.cs">
<DependentUpon>201610252128299_EmployerUpdate.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\201610261540007_AuditFields.cs" />
<Compile Include="Migrations\201610261540007_AuditFields.Designer.cs">
<DependentUpon>201610261540007_AuditFields.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repositories\FileRepository.cs" />
Expand Down Expand Up @@ -229,6 +234,9 @@
<EmbeddedResource Include="Migrations\201610252128299_EmployerUpdate.resx">
<DependentUpon>201610252128299_EmployerUpdate.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Migrations\201610261540007_AuditFields.resx">
<DependentUpon>201610261540007_AuditFields.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f5f29ea

Please sign in to comment.