aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
blob: 84108ae838c92db8a40bce66a01d824ecf6acf41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using DevHive.Data.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Builder;
using System;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using DevHive.Data;
using System.Linq;
using System.Threading.Tasks;

namespace DevHive.Web.Configurations.Extensions
{
	public static class DatabaseExtensions
	{
		public static void DatabaseConfiguration(this IServiceCollection services, IConfiguration configuration)
		{
			services.AddDbContext<DevHiveContext>(options =>
			{
				options.EnableSensitiveDataLogging(true);
				options.UseNpgsql(configuration.GetConnectionString("DEV"));
			});

			services.AddIdentity<User, Role>()
				.AddRoles<Role>()
				.AddEntityFrameworkStores<DevHiveContext>();

			services.Configure<IdentityOptions>(options =>
			{
				options.User.RequireUniqueEmail = true;

				options.Password.RequireDigit = true;
				options.Password.RequiredLength = 5;
				options.Password.RequiredUniqueChars = 0;
				options.Password.RequireLowercase = false;
				options.Password.RequireNonAlphanumeric = false;
				options.Password.RequireUppercase = false;

				options.Lockout.AllowedForNewUsers = true;
				options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
				options.Lockout.MaxFailedAccessAttempts = 5;
			});

			services.AddAuthorization(options =>
			{
				options.AddPolicy("User", options =>
				{
					options.RequireAuthenticatedUser();
					options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
					options.RequireRole("User");
				});

				options.AddPolicy("Administrator", options =>
				{
					options.RequireAuthenticatedUser();
					options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
					options.RequireRole("Admin");
				});
			});
		}

		public static async Task UseDatabaseConfiguration(this IApplicationBuilder app)
		{
			app.UseHttpsRedirection();
			app.UseRouting();

			app.UseAuthentication();
			app.UseAuthorization();

			using var serviceScope = app.ApplicationServices.CreateScope();
			using var dbContext = serviceScope.ServiceProvider.GetRequiredService<DevHiveContext>();

			dbContext.Database.Migrate();

			var roleManager = (RoleManager<Role>)serviceScope.ServiceProvider.GetService(typeof(RoleManager<Role>));

			if (!await dbContext.Roles.AnyAsync(x => x.Name == Role.DefaultRole))
			{
				Role defaultRole = new() { Name = Role.DefaultRole };

				await roleManager.CreateAsync(defaultRole);
			}

			if (!await dbContext.Roles.AnyAsync(x => x.Name == Role.AdminRole))
			{
				Role adminRole = new() { Name = Role.AdminRole };

				await roleManager.CreateAsync(adminRole);
			}

			await dbContext.SaveChangesAsync();
		}
	}
}