Skip to content

Commit

Permalink
The first version of the application
Browse files Browse the repository at this point in the history
  • Loading branch information
tanure committed Nov 1, 2019
1 parent 6da12b3 commit 0af4b6d
Show file tree
Hide file tree
Showing 67 changed files with 40,430 additions and 2 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
119 changes: 117 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,117 @@
# dotnetcoreaudit
This repo it's a POC about the use of the Audit.Net Library
# Dotnet Core Audit

This repo it's a POC about the use of the Audit.Net Library with .net Core

# Prerequisites

- .net core 3.0
- docker
- Azure Data Studio / Sql Management Studio

# How to use

Create an instance of database using docker:

```bash

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Pa$$w0rd" -p 1433:1433 -d mcr.microsoft.com/mssql/server:latest

```

Create database and tables

Connect in database use Sql Management Studio or Azure Data Studio and execute the following scripts:

```sql

CREATE DATABASE pocaidit;

```


```sql

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AuditLog](
[AuditId] [int] IDENTITY(1,1) NOT NULL,
[TablePk] [varchar](100) NOT NULL,
[AuditAction] [varchar](100) NOT NULL,
[AuditUser] [varchar](150) NOT NULL,
[AuditDate] [datetime] NOT NULL,
[AuditData] [varchar](max) NOT NULL,
[EntityType] [varchar](250) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[AuditLog] ADD PRIMARY KEY CLUSTERED
(
[AuditId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Person](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](150) NOT NULL,
[Email] [varchar](250) NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Person] ADD PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


```

# Configurations

In Statup.cs file we have all configurations from connecting Database and use the Audit.net Library:

```csharp

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultDatabase"))); // Configure DbContext
/*
Configuring the Audit DataProvider with EntityFramewor and a generic method to create audit for all of the tables of the application
*/
Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, entity) =>
{
entity.AuditData = entry.ToJson();
entity.EntityType = entry.EntityType.Name;
entity.AuditAction = entry.Action;
entity.AuditDate = DateTime.Now;
entity.AuditUser = Environment.UserName;
entity.TablePk = entry.PrimaryKey.First().Value.ToString();
})
.IgnoreMatchedProperties(true));

services.AddControllersWithViews();

```

# Execute

Execute the following command on the bash, at the POC.Audit folder, to run the application:

```bash

dotnet run

```

Access the Person menu, create and person an then access the Audit menu to see the audities.

# Reference

[Audit.Net](https://github.com/thepirat000/Audit.NET)
25 changes: 25 additions & 0 deletions src/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
25 changes: 25 additions & 0 deletions src/POC.Audit.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29424.173
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "POC.Audit", "POC.Audit\POC.Audit.csproj", "{0BDBC89F-1981-4B40-88B9-1710E0360600}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0BDBC89F-1981-4B40-88B9-1710E0360600}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0BDBC89F-1981-4B40-88B9-1710E0360600}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0BDBC89F-1981-4B40-88B9-1710E0360600}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0BDBC89F-1981-4B40-88B9-1710E0360600}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BAF6C03D-BDCF-4314-B57E-1AC82F80F4E2}
EndGlobalSection
EndGlobal
51 changes: 51 additions & 0 deletions src/POC.Audit/Controllers/AuditLogsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using POC.Audit.Models;

namespace POC.Audit.Controllers
{
public class AuditLogsController : Controller
{
private readonly ApplicationDbContext _context;

public AuditLogsController(ApplicationDbContext context)
{
_context = context;
}

// GET: AuditLogs
public async Task<IActionResult> Index()
{
return View(await _context.AuditLogs.ToListAsync());
}

// GET: AuditLogs/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}

var auditLog = await _context.AuditLogs
.FirstOrDefaultAsync(m => m.AuditId == id);
if (auditLog == null)
{
return NotFound();
}

return View(auditLog);
}


private bool AuditLogExists(int id)
{
return _context.AuditLogs.Any(e => e.AuditId == id);
}
}
}
37 changes: 37 additions & 0 deletions src/POC.Audit/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using POC.Audit.Models;

namespace POC.Audit.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
return View();
}

public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
Loading

0 comments on commit 0af4b6d

Please sign in to comment.