Skip to content

Commit a29a8d9

Browse files
committed
Push to master
2 parents 321315c + 961cb19 commit a29a8d9

File tree

173 files changed

+20704
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+20704
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
##
44
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
55

6+
# Migrations folder
7+
/API/ACRS/Migrations/**
8+
!/API/ACRS/Migrations/.gitkeep
9+
10+
/API/ACRS/dist
11+
612
# User-specific files
713
*.rsuser
814
*.suo
915
*.user
1016
*.userosscache
1117
*.sln.docstates
1218

19+
*.db
20+
1321
# User-specific files (MonoDevelop/Xamarin Studio)
1422
*.userprefs
1523

API/ACRS.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.28803.202
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ACRS", "ACRS\ACRS.csproj", "{3FC5D79B-EC85-4DDB-950B-B89F726C1E78}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3FC5D79B-EC85-4DDB-950B-B89F726C1E78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3FC5D79B-EC85-4DDB-950B-B89F726C1E78}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3FC5D79B-EC85-4DDB-950B-B89F726C1E78}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3FC5D79B-EC85-4DDB-950B-B89F726C1E78}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {18C8C176-017A-4B84-9104-45A3B3C2FF92}
24+
EndGlobalSection
25+
EndGlobal

API/ACRS/.vscode/launch.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
// Use IntelliSense to find out which attributes exist for C# debugging
3+
// Use hover for the description of the existing attributes
4+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (web)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/ACRS.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
"stopAtEntry": false,
17+
"launchBrowser": {
18+
"enabled": true
19+
},
20+
"env": {
21+
"ASPNETCORE_ENVIRONMENT": "Development"
22+
},
23+
"sourceFileMap": {
24+
"/Views": "${workspaceFolder}/Views"
25+
}
26+
},
27+
{
28+
"name": ".NET Core Attach",
29+
"type": "coreclr",
30+
"request": "attach",
31+
"processId": "${command:pickProcess}"
32+
}
33+
]
34+
}

API/ACRS/.vscode/tasks.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/ACRS.csproj"
11+
],
12+
"problemMatcher": "$tsc"
13+
},
14+
{
15+
"label": "publish",
16+
"command": "dotnet",
17+
"type": "process",
18+
"args": [
19+
"publish",
20+
"${workspaceFolder}/ACRS.csproj"
21+
],
22+
"problemMatcher": "$tsc"
23+
},
24+
{
25+
"label": "watch",
26+
"command": "dotnet",
27+
"type": "process",
28+
"args": [
29+
"watch",
30+
"run",
31+
"${workspaceFolder}/ACRS.csproj"
32+
],
33+
"problemMatcher": "$tsc"
34+
}
35+
]
36+
}

API/ACRS/ACRS.csproj

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
6+
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="CsvHelper" Version="12.1.2" />
11+
<PackageReference Include="EPPlus" Version="4.5.3.1" />
12+
<!-- <PackageReference Include="EntityFramework" Version="6.2.0" /> -->
13+
<PackageReference Include="Microsoft.AspNetCore.App" />
14+
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.4" />
17+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
18+
<PackageReference Include="NSubsys" Version="1.0.0">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.0" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<Compile Remove="Migrations\20190509163856_9maydb.cs" />
35+
<Compile Remove="Migrations\20190513001144_12may2db.cs" />
36+
<Compile Remove="Migrations\20190513001144_12may2db.Designer.cs" />
37+
<Compile Remove="Migrations\ApplicationDbContextModelSnapshot.cs" />
38+
<Compile Remove="Migrations\20190513165328_13may2db.cs" />
39+
<Compile Remove="Migrations\20190513165328_13may2db.Designer.cs" />
40+
<Compile Remove="Migrations\20190514184724_14may2db.cs" />
41+
<Compile Remove="Migrations\20190514184724_14may2db.Designer.cs" />
42+
<Compile Remove="Migrations\ApplicationDbContextModelSnapshot.cs" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<None Remove="Database.db" />
46+
</ItemGroup>
47+
48+
<ItemGroup>
49+
<Folder Include="Migrations\" />
50+
</ItemGroup>
51+
</Project>

API/ACRS/ACRS.sln

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio 15
3+
VisualStudioVersion = 15.0.27004.2006
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ACRS", "ACRS.csproj", "{A33C253F-81E9-4031-BEBC-AE1F32603000}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{A33C253F-81E9-4031-BEBC-AE1F32603000}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{A33C253F-81E9-4031-BEBC-AE1F32603000}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{A33C253F-81E9-4031-BEBC-AE1F32603000}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{A33C253F-81E9-4031-BEBC-AE1F32603000}.Release|Any CPU.Build.0 = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(SolutionProperties) = preSolution
19+
HideSolutionNode = FALSE
20+
EndGlobalSection
21+
GlobalSection(ExtensibilityGlobals) = postSolution
22+
SolutionGuid = {5FD4277D-49E6-4F9A-A9EF-521D010A649E}
23+
EndGlobalSection
24+
EndGlobal
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Mvc.Rendering;
7+
using Microsoft.EntityFrameworkCore;
8+
using ACRS.Data;
9+
using ACRS.Models;
10+
using Microsoft.Extensions.Configuration;
11+
using Microsoft.AspNetCore.Identity;
12+
using System.IdentityModel.Tokens.Jwt;
13+
using System.Security.Claims;
14+
using Microsoft.IdentityModel.Tokens;
15+
using System.Text;
16+
using Microsoft.AspNetCore.Authorization;
17+
using Microsoft.AspNetCore.Cors;
18+
using Microsoft.EntityFrameworkCore.Metadata.Internal;
19+
using Microsoft.AspNetCore.Http;
20+
using System.Diagnostics;
21+
22+
namespace ACRS
23+
{
24+
[Route("api/[controller]")]
25+
[EnableCors("CORSPolicy")]
26+
public class AuthController : Controller
27+
{
28+
private readonly IConfiguration _configuration;
29+
private readonly UserManager<IdentityUser> _userManager;
30+
31+
public AuthController(IConfiguration configuration,
32+
UserManager<IdentityUser> userManager)
33+
{
34+
_configuration = configuration;
35+
_userManager = userManager;
36+
}
37+
38+
[HttpPost, Route("login")]
39+
public async Task<ActionResult<AuthLogin>> Login([FromBody] AuthLogin model)
40+
{
41+
if (model == null)
42+
{
43+
return BadRequest();
44+
}
45+
46+
var user = await _userManager.FindByNameAsync(model.UserName);
47+
48+
if (user != null && await _userManager.CheckPasswordAsync(user, model.Password))
49+
{
50+
var roles = await _userManager.GetRolesAsync(user);
51+
52+
var claims = new[]
53+
{
54+
new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
55+
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
56+
new Claim(JwtRegisteredClaimNames.Iat, DateTime.Now.ToString())
57+
};
58+
59+
ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims, "Token");
60+
claimsIdentity.AddClaims(roles.Select(role => new Claim(ClaimTypes.Role, role)));
61+
62+
var signinKey = new SymmetricSecurityKey(
63+
Encoding.UTF8.GetBytes(_configuration["Jwt:SigningKey"]));
64+
65+
int expiryInMinutes = Convert.ToInt32(_configuration["Jwt:ExpiryInMinutes"]);
66+
67+
var token = new JwtSecurityToken(
68+
issuer: _configuration["Jwt:Site"],
69+
audience: _configuration["Jwt:Site"],
70+
claims: claimsIdentity.Claims,
71+
expires: DateTime.UtcNow.AddMinutes(expiryInMinutes),
72+
signingCredentials: new SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256)
73+
);
74+
75+
return Ok(
76+
new
77+
{
78+
token = new JwtSecurityTokenHandler().WriteToken(token),
79+
expiration = token.ValidTo,
80+
role = await _userManager.GetRolesAsync(user)
81+
});
82+
}
83+
84+
return Unauthorized();
85+
}
86+
87+
[HttpPost, Route("register")]
88+
public async Task<ActionResult<AuthRegister>> Register([FromBody] AuthRegister user)
89+
{
90+
if (user == null)
91+
{
92+
return BadRequest();
93+
}
94+
95+
var passwordValidator = new PasswordValidator<IdentityUser>();
96+
if (!(await passwordValidator.ValidateAsync(_userManager, null, user.Password)).Succeeded)
97+
{
98+
return BadRequest("Password is too weak");
99+
}
100+
101+
if (await _userManager.FindByNameAsync(user.UserName) != null)
102+
{
103+
return BadRequest($"User with username: {user.UserName} already exists");
104+
}
105+
106+
IdentityUser newUser = new IdentityUser
107+
{
108+
UserName = user.UserName
109+
};
110+
111+
var result = await _userManager.CreateAsync(newUser);
112+
113+
if (result.Succeeded)
114+
{
115+
await _userManager.AddPasswordAsync(newUser, user.Password);
116+
117+
// Admin is the only role!
118+
await _userManager.AddToRoleAsync(newUser, "Admin");
119+
}
120+
else
121+
{
122+
return BadRequest(result.Errors);
123+
}
124+
125+
return CreatedAtAction("GetUsers", new { userName = newUser.UserName });
126+
}
127+
128+
[HttpGet, Route("users")]
129+
public async Task<ActionResult<IEnumerable<string>>> GetUsers()
130+
{
131+
return await _userManager.Users.Select(u => u.UserName).ToListAsync();
132+
}
133+
134+
135+
[HttpDelete("users/{username}")]
136+
public async Task<ActionResult<object>> DeleteUser(string username)
137+
{
138+
var user = await _userManager.FindByNameAsync(username);
139+
140+
if (user == null)
141+
{
142+
return NotFound();
143+
}
144+
145+
var result = await _userManager.DeleteAsync(user);
146+
147+
if (!result.Succeeded)
148+
{
149+
return NotFound();
150+
}
151+
152+
return new { userName = username };
153+
}
154+
155+
[HttpPut("users/{username}")]
156+
public async Task<IActionResult> UpdateUser(string username, [FromBody] AuthChangePassword request)
157+
{
158+
if (request == null || request.UserName != username)
159+
{
160+
return BadRequest();
161+
}
162+
163+
var user = await _userManager.FindByNameAsync(username);
164+
165+
if (user == null)
166+
{
167+
return NotFound();
168+
}
169+
170+
var result = await _userManager.ChangePasswordAsync(user, request.CurrentPassword, request.NewPassword);
171+
172+
if (!result.Succeeded)
173+
{
174+
return BadRequest(result.Errors);
175+
}
176+
177+
return NoContent();
178+
}
179+
}
180+
}

0 commit comments

Comments
 (0)