Stringbuilder to handle passwords
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -12,9 +12,16 @@ namespace Aberwyn.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var baseConnStr = $"server={request.Host};port={request.Port};user={request.User};password={request.Pass};";
|
var builder = new MySqlConnectionStringBuilder
|
||||||
|
{
|
||||||
|
Server = request.Host,
|
||||||
|
Port = uint.Parse(request.Port),
|
||||||
|
UserID = request.User,
|
||||||
|
Password = request.Pass,
|
||||||
|
Database = "information_schema"
|
||||||
|
};
|
||||||
|
|
||||||
using (var conn = new MySqlConnection(baseConnStr + "database=information_schema;"))
|
using (var conn = new MySqlConnection(builder.ConnectionString))
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
|
|
||||||
|
|||||||
@@ -1,37 +1,35 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
|
using System.Text.Json;
|
||||||
|
using Aberwyn.Data;
|
||||||
|
using Aberwyn.Models;
|
||||||
|
|
||||||
namespace Aberwyn.Controllers
|
namespace Aberwyn.Controllers
|
||||||
{
|
{
|
||||||
using System.Text.Json;
|
|
||||||
using System.IO;
|
|
||||||
using Aberwyn.Models;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
|
||||||
using MySql.Data.MySqlClient;
|
|
||||||
using Aberwyn.Data;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
[Route("setup")]
|
[Route("setup")]
|
||||||
public class SetupController : Controller
|
public class SetupController : Controller
|
||||||
{
|
{
|
||||||
private readonly IWebHostEnvironment _env;
|
private readonly IWebHostEnvironment _env;
|
||||||
|
private readonly ILogger<SetupController> _logger;
|
||||||
|
|
||||||
|
public SetupController(IWebHostEnvironment env, ILogger<SetupController> logger)
|
||||||
|
{
|
||||||
|
_env = env;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public override void OnActionExecuting(ActionExecutingContext context)
|
public override void OnActionExecuting(ActionExecutingContext context)
|
||||||
{
|
{
|
||||||
ViewBag.IsSetupMode = true;
|
ViewBag.IsSetupMode = true;
|
||||||
base.OnActionExecuting(context);
|
base.OnActionExecuting(context);
|
||||||
}
|
}
|
||||||
public SetupController(IWebHostEnvironment env)
|
|
||||||
{
|
|
||||||
_env = env;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult Index()
|
public IActionResult Index() => View(new SetupSettings());
|
||||||
{
|
|
||||||
return View(new SetupSettings());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Authorize(Roles = "Admin")]
|
[Authorize(Roles = "Admin")]
|
||||||
[HttpPost("reset")]
|
[HttpPost("reset")]
|
||||||
@@ -58,7 +56,6 @@ namespace Aberwyn.Controllers
|
|||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPost("")]
|
[HttpPost("")]
|
||||||
public async Task<IActionResult> Setup([FromBody] SetupSettings model)
|
public async Task<IActionResult> Setup([FromBody] SetupSettings model)
|
||||||
{
|
{
|
||||||
@@ -73,9 +70,18 @@ namespace Aberwyn.Controllers
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Skapa databasen om den inte finns
|
// Bygg connection string säkert
|
||||||
var baseConnStr = $"server={model.DbHost};port={model.DbPort};user={model.DbUser};password={model.DbPassword};";
|
var baseConnBuilder = new MySqlConnectionStringBuilder
|
||||||
using (var conn = new MySqlConnection(baseConnStr + "database=information_schema;"))
|
{
|
||||||
|
Server = model.DbHost,
|
||||||
|
Port = (uint)model.DbPort,
|
||||||
|
UserID = model.DbUser,
|
||||||
|
Password = model.DbPassword,
|
||||||
|
Database = "information_schema"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Kontrollera om databasen redan finns
|
||||||
|
using (var conn = new MySqlConnection(baseConnBuilder.ConnectionString))
|
||||||
{
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
var cmd = new MySqlCommand("SELECT SCHEMA_NAME FROM SCHEMATA WHERE SCHEMA_NAME = @dbName", conn);
|
var cmd = new MySqlCommand("SELECT SCHEMA_NAME FROM SCHEMATA WHERE SCHEMA_NAME = @dbName", conn);
|
||||||
@@ -91,26 +97,41 @@ namespace Aberwyn.Controllers
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
_logger.LogError(ex, "Kunde inte skapa databasen.");
|
||||||
return BadRequest(new { error = "Databasen finns inte och kunde inte skapas.", details = ex.Message });
|
return BadRequest(new { error = "Databasen finns inte och kunde inte skapas.", details = ex.Message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bygg services temporärt för att skapa admin
|
// Bygg EF-connection
|
||||||
var connectionString = $"server={model.DbHost};port={model.DbPort};database={model.DbName};user={model.DbUser};password={model.DbPassword}";
|
var efConnBuilder = new MySqlConnectionStringBuilder
|
||||||
var tempProvider = SetupService.BuildTemporaryServices(connectionString);
|
{
|
||||||
|
Server = model.DbHost,
|
||||||
|
Port = (uint)model.DbPort,
|
||||||
|
UserID = model.DbUser,
|
||||||
|
Password = model.DbPassword,
|
||||||
|
Database = model.DbName
|
||||||
|
};
|
||||||
|
|
||||||
|
var tempProvider = SetupService.BuildTemporaryServices(efConnBuilder.ConnectionString);
|
||||||
using var scope = tempProvider.CreateScope();
|
using var scope = tempProvider.CreateScope();
|
||||||
|
|
||||||
// Skapa databastabeller via EF
|
// Skapa databastabeller
|
||||||
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||||
await db.Database.MigrateAsync();
|
await db.Database.MigrateAsync();
|
||||||
|
|
||||||
|
// Sätt konfig-flagga tidigt
|
||||||
|
model.IsConfigured = true;
|
||||||
|
|
||||||
|
// Spara setup.json
|
||||||
|
var filePath = Path.Combine(_env.ContentRootPath, "infrastructure", "setup.json");
|
||||||
|
var json = JsonSerializer.Serialize(model, new JsonSerializerOptions { WriteIndented = true });
|
||||||
|
System.IO.File.WriteAllText(filePath, json);
|
||||||
|
|
||||||
|
// Roller och admin
|
||||||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||||
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
||||||
|
|
||||||
// Skapa roller
|
|
||||||
string[] roles = { "Admin", "Chef", "Budget" };
|
string[] roles = { "Admin", "Chef", "Budget" };
|
||||||
foreach (var role in roles)
|
foreach (var role in roles)
|
||||||
{
|
{
|
||||||
@@ -118,42 +139,32 @@ namespace Aberwyn.Controllers
|
|||||||
await roleManager.CreateAsync(new IdentityRole(role));
|
await roleManager.CreateAsync(new IdentityRole(role));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skapa adminanvändare
|
|
||||||
var adminUser = new ApplicationUser
|
|
||||||
{
|
|
||||||
UserName = model.AdminUsername,
|
|
||||||
Email = model.AdminEmail
|
|
||||||
};
|
|
||||||
|
|
||||||
var existingUser = await userManager.FindByNameAsync(model.AdminUsername);
|
var existingUser = await userManager.FindByNameAsync(model.AdminUsername);
|
||||||
if (existingUser == null)
|
if (existingUser == null)
|
||||||
{
|
{
|
||||||
|
var adminUser = new ApplicationUser
|
||||||
|
{
|
||||||
|
UserName = model.AdminUsername,
|
||||||
|
Email = model.AdminEmail,
|
||||||
|
EmailConfirmed = true
|
||||||
|
};
|
||||||
|
|
||||||
var result = await userManager.CreateAsync(adminUser, model.AdminPassword);
|
var result = await userManager.CreateAsync(adminUser, model.AdminPassword);
|
||||||
if (!result.Succeeded)
|
if (!result.Succeeded)
|
||||||
{
|
|
||||||
return BadRequest(new { error = "Kunde inte skapa administratör", details = result.Errors });
|
return BadRequest(new { error = "Kunde inte skapa administratör", details = result.Errors });
|
||||||
}
|
|
||||||
|
|
||||||
await userManager.AddToRoleAsync(adminUser, "Admin");
|
await userManager.AddToRoleAsync(adminUser, "Admin");
|
||||||
}
|
}
|
||||||
|
|
||||||
model.IsConfigured = true;
|
|
||||||
|
|
||||||
// Spara inställningarna
|
|
||||||
var json = JsonSerializer.Serialize(model, new JsonSerializerOptions { WriteIndented = true });
|
|
||||||
var filePath = Path.Combine(_env.ContentRootPath, "infrastructure", "setup.json");
|
|
||||||
System.IO.File.WriteAllText(filePath, json);
|
|
||||||
|
|
||||||
return Ok(new { message = "Installation slutförd!" });
|
return Ok(new { message = "Installation slutförd!" });
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
_logger.LogError(ex, "Fel vid installation.");
|
||||||
return BadRequest(new { error = "Fel vid installation", details = ex.Message });
|
return BadRequest(new { error = "Fel vid installation", details = ex.Message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IActionResult SetupComplete() => View();
|
public IActionResult SetupComplete() => View();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
11
Aberwyn/Infrastructure/setup.json
Normal file
11
Aberwyn/Infrastructure/setup.json
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"AdminUsername": "admin",
|
||||||
|
"AdminEmail": "admin@localhost",
|
||||||
|
"AdminPassword": "Admin123!",
|
||||||
|
"IsConfigured": false,
|
||||||
|
"DbHost": null,
|
||||||
|
"DbPort": 3306,
|
||||||
|
"DbName": null,
|
||||||
|
"DbUser": null,
|
||||||
|
"DbPassword": null
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user