diff options
| author | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-04-09 19:51:35 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-09 19:51:35 +0300 |
| commit | 233f38915ba0079079233eff55434ef349c05c45 (patch) | |
| tree | 6c5f69017865bcab87355e910c87339453da1406 /src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs | |
| parent | f4a70c6430db923af9fa9958a11c2d6612cb52cc (diff) | |
| parent | a992357efcf1bc1ece81b95ecee5e05a0b73bfdc (diff) | |
| download | DevHive-233f38915ba0079079233eff55434ef349c05c45.tar DevHive-233f38915ba0079079233eff55434ef349c05c45.tar.gz DevHive-233f38915ba0079079233eff55434ef349c05c45.zip | |
Merge pull request #28 from Team-Kaleidoscope/devHEADv0.2mainheroku/main
Second stage: Complete
Diffstat (limited to 'src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs')
| -rw-r--r-- | src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs new file mode 100644 index 0000000..b4c49b4 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -0,0 +1,98 @@ +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"), options => + { + options.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery); + }); + }); + + 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 void 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 (!dbContext.Roles.Any(x => x.Name == Role.DefaultRole)) + { + Role defaultRole = new() { Name = Role.DefaultRole }; + + roleManager.CreateAsync(defaultRole).Wait(); + } + + if (!dbContext.Roles.Any(x => x.Name == Role.AdminRole)) + { + Role adminRole = new() { Name = Role.AdminRole }; + + roleManager.CreateAsync(adminRole).Wait(); + } + + dbContext.SaveChanges(); + } + } +} |
