Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit 2a7fe02

Browse files
committed
Users Entity
0 parents  commit 2a7fe02

Some content is hidden

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

60 files changed

+2546
-0
lines changed

backend/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/bin
2+
/obj
3+
4+
# migration
5+
/Migrations

backend/BackendAPI.csproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<LinkerRootAssemblies Include="Microsoft.AspNetCore.Mvc.Razor.Extensions;Microsoft.Extensions.FileProviders.Composite;Microsoft.Extensions.Primitives;Microsoft.AspNetCore.Diagnostics.Abstractions" />
9+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.9" NoWarn="NU1605" />
10+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" NoWarn="NU1605" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
12+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.12.0" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.9" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.9">
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
<PrivateAssets>all</PrivateAssets>
17+
</PackageReference>
18+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<Folder Include="Models\" />
23+
</ItemGroup>
24+
</Project>

backend/BackendAPI.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.808.3
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BackendAPI", "BackendAPI.csproj", "{A3E2E05C-8661-425E-87E9-C18D5F8645E1}"
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+
{A3E2E05C-8661-425E-87E9-C18D5F8645E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A3E2E05C-8661-425E-87E9-C18D5F8645E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A3E2E05C-8661-425E-87E9-C18D5F8645E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{A3E2E05C-8661-425E-87E9-C18D5F8645E1}.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 = {FCF92181-2A3F-4B97-9BEF-063E12B53E0F}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IdentityModel.Tokens.Jwt;
4+
using System.Linq;
5+
using System.Security.Claims;
6+
using System.Text;
7+
using BackendAPI.Models;
8+
using Microsoft.AspNetCore.Authorization;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Microsoft.Extensions.Configuration;
11+
using Microsoft.IdentityModel.Tokens;
12+
13+
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
14+
15+
namespace BackendAPI.Controllers
16+
{
17+
[ApiController]
18+
[Route("api/[controller]")]
19+
public class AdministrationController : Controller
20+
{
21+
private DataBaseContext user_data_context;
22+
public AdministrationController(DataBaseContext user_data_context)
23+
{
24+
this.user_data_context = user_data_context;
25+
}
26+
27+
28+
// CREATE, EDIT, DELETE OPERATIONS
29+
// INDEX
30+
[Authorize(Roles = "Admin")]
31+
[HttpGet]
32+
public IEnumerable<Users> Get()
33+
{
34+
return user_data_context.User.ToList();
35+
}
36+
37+
// DETAILS
38+
[HttpGet("{id}")]
39+
public Users Get(int id)
40+
{
41+
return this.user_data_context.User.Where(user => user.UserId == id).FirstOrDefault();
42+
}
43+
44+
// CREATE
45+
[HttpPost]
46+
public string Post([FromBody] Users New_User)
47+
{
48+
this.user_data_context.User.Add(New_User);
49+
this.user_data_context.SaveChanges();
50+
return "New User created successfully!";
51+
}
52+
53+
54+
// EDIT
55+
[HttpPut("{id}")]
56+
public void Put(int id, [FromBody] Users New_User)
57+
{
58+
this.user_data_context.User.Update(New_User);
59+
this.user_data_context.SaveChanges();
60+
}
61+
62+
// DELETE
63+
[HttpDelete("{id}")]
64+
public void Delete(int id)
65+
{
66+
this.user_data_context.User.Remove(this.user_data_context.User.Where(New_User => New_User.UserId == id).FirstOrDefault());
67+
this.user_data_context.SaveChanges();
68+
}
69+
70+
71+
// DETAILS-MAIL
72+
[HttpGet("email/{email}")]
73+
public Users GetMail(string email)
74+
{
75+
return this.user_data_context.User.Where(user => user.Email == email).FirstOrDefault();
76+
}
77+
78+
79+
// EDIT-BYEMAIL
80+
[HttpPut("email/{email}")]
81+
public void PutMail(string email, [FromBody] Users New_User)
82+
{
83+
this.user_data_context.User.Update(New_User);
84+
this.user_data_context.SaveChanges();
85+
}
86+
87+
// DELETE-BYEMAIL
88+
[HttpDelete("email/{email}")]
89+
public void DeleteMail(string email)
90+
{
91+
this.user_data_context.User.Remove(this.user_data_context.User.Where(New_User => New_User.Email == email).FirstOrDefault());
92+
this.user_data_context.SaveChanges();
93+
}
94+
95+
}
96+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IdentityModel.Tokens.Jwt;
4+
using System.Linq;
5+
using System.Security.Claims;
6+
using System.Text;
7+
using BackendAPI.Models;
8+
using Microsoft.AspNetCore.Authorization;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Microsoft.Extensions.Configuration;
11+
using Microsoft.IdentityModel.Tokens;
12+
13+
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
14+
15+
namespace BackendAPI.Controllers
16+
{
17+
[AllowAnonymous]
18+
[Route("api/[controller]")]
19+
public class AuthenticateController : Controller
20+
{
21+
private DataBaseContext data_context;
22+
public AuthenticateController(IConfiguration configuration, DataBaseContext data_context)
23+
{
24+
Configuration = configuration;
25+
this.data_context = data_context;
26+
}
27+
28+
public IConfiguration Configuration { get; }
29+
30+
public IActionResult Post()
31+
{
32+
33+
var authorizationHeader = Request.Headers["Authorization"].First();
34+
var key = authorizationHeader.Split(' ')[1];
35+
var credentials = Encoding.UTF8.GetString(Convert.FromBase64String(key)).Split(':');
36+
var serverSecret = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:ServerSecret"]));
37+
38+
Users user = this.data_context.User.Where(u => u.Email == credentials[0] && u.Password == credentials[1]).FirstOrDefault();
39+
40+
if (user != null)
41+
{
42+
var result = new
43+
{
44+
token = GenerateToken(serverSecret, user)
45+
};
46+
return Ok(result);//status code
47+
}
48+
return BadRequest("Invalid Email/Password");//status code
49+
}
50+
51+
private string GenerateToken(SecurityKey key, Users user)
52+
{
53+
var now = DateTime.UtcNow;
54+
var issuer = Configuration["JWT:Issuer"];
55+
var audience = Configuration["JWT:Audience"];
56+
var identity = new ClaimsIdentity(new Claim[]
57+
{
58+
new Claim(ClaimTypes.Email, user.Email),
59+
new Claim(ClaimTypes.Role, user.Role)
60+
});
61+
var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
62+
var handler = new JwtSecurityTokenHandler();
63+
var token = handler.CreateJwtSecurityToken(issuer, audience, identity,
64+
now, now.Add(TimeSpan.FromHours(100)), now, signingCredentials);
65+
var encodedJwt = handler.WriteToken(token);
66+
return encodedJwt;
67+
}
68+
}
69+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace BackendAPI.Models
5+
{
6+
public class DataBaseContext : DbContext
7+
{
8+
public DataBaseContext(DbContextOptions<DataBaseContext> dbContextOptions) : base(dbContextOptions)
9+
{
10+
}
11+
public DbSet<Users> User { get; set; }
12+
13+
// GIVING PREDIFINED DATA TO DATABASE
14+
// CREATING ADMIN USER
15+
protected override void OnModelCreating(ModelBuilder modelBuilder)
16+
{
17+
// CREATING SAMPLE USER
18+
modelBuilder.Entity<Users>().HasData(
19+
new Users
20+
{
21+
UserId = 1,
22+
Email = "admin@localhost",
23+
Password = "Passcode1",
24+
Role = Roles.Admin
25+
}
26+
);
27+
}
28+
}
29+
}

backend/Models/Users/Roles.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
namespace BackendAPI.Models
3+
{
4+
public class Roles
5+
{
6+
public const string Admin = "Admin";
7+
public const string Doctor = "Doctor";
8+
public const string Member = "Member";
9+
}
10+
}

backend/Models/Users/Users.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.ComponentModel.DataAnnotations;
5+
6+
namespace BackendAPI.Models
7+
{
8+
public class Users
9+
{
10+
public Users()
11+
{
12+
}
13+
[Key]
14+
public int UserId { get; set; }
15+
16+
// BASIC DATA FIELD
17+
public string FirstName { get; set; }
18+
public string LastName { get; set; }
19+
20+
21+
// AUTHENTICATION DATA FIELD
22+
public string Email { get; set; }
23+
public string Password { get; set; }
24+
25+
26+
// ROLE DATA FIELD
27+
public string Role { get; set; } = "Member";
28+
}
29+
}

backend/Program.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace BackendAPI
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
webBuilder.UseKestrel(opts =>
25+
{
26+
opts.ListenLocalhost(5001, opts => opts.UseHttps());
27+
});
28+
});
29+
}
30+
}

backend/Properties/HTTP/Login.http

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Login
2+
3+
POST https://localhost:5001/api/authenticate HTTP/1.1
4+
Authorization: Basic admin@localhost:Passcode1
5+

0 commit comments

Comments
 (0)