This repository has been archived on 2023-02-13. You can view files and clone it, but cannot push or open issues or pull requests.
gswi-server/gswi/DataSeeder.cs

60 lines
2.1 KiB
C#
Raw Normal View History

2022-02-20 19:04:11 +01:00
using System;
using System.Linq;
2022-02-21 18:27:33 +01:00
using System.Security.Cryptography;
2022-02-20 19:04:11 +01:00
using System.Text;
using System.Threading.Tasks;
using gswi.Data;
using gswi.Model;
2022-02-21 18:27:33 +01:00
using Microsoft.EntityFrameworkCore;
2022-02-20 19:04:11 +01:00
2022-02-21 18:27:33 +01:00
namespace gswi {
public class DataSeeder {
2022-02-20 19:04:11 +01:00
private readonly DatabaseContext _databaseContext;
2022-02-21 18:27:33 +01:00
public DataSeeder(DatabaseContext databaseContext) {
2022-02-20 19:04:11 +01:00
_databaseContext = databaseContext;
}
2022-02-21 18:27:33 +01:00
public void SeedData() {
2022-02-20 19:04:11 +01:00
_databaseContext.Database.EnsureCreated();
2022-02-21 18:27:33 +01:00
if (!_databaseContext.AuthUsers.Any()) {
var admin = new AuthUser() {
2022-02-20 19:04:11 +01:00
FirstName = "Admin",
2022-02-21 18:27:33 +01:00
LastName = "Admin",
EMail = "admin@localhost.local",
Password = ComputeHash("localhost.local", new SHA256CryptoServiceProvider()),
2022-02-20 19:04:11 +01:00
AuthRole = AuthRoles.Admin
};
2022-02-21 18:27:33 +01:00
var support = new AuthUser() {
FirstName = "Support",
LastName = "Supporter",
EMail = "support@localhost.local",
Password = ComputeHash("localhost.local", new SHA256CryptoServiceProvider()),
AuthRole = AuthRoles.Supporter
2022-02-20 19:04:11 +01:00
};
2022-02-21 18:27:33 +01:00
var user = new AuthUser() {
FirstName = "User",
LastName = "User",
EMail = "user@localhost.local",
Password = ComputeHash("localhost.local", new SHA256CryptoServiceProvider()),
AuthRole = AuthRoles.User
2022-02-20 19:04:11 +01:00
};
_databaseContext.AuthUsers.Add(admin);
2022-02-21 18:27:33 +01:00
_databaseContext.AuthUsers.Add(support);
_databaseContext.AuthUsers.Add(user);
2022-02-20 19:04:11 +01:00
}
_databaseContext.SaveChanges();
}
2022-02-21 18:27:33 +01:00
public string ComputeHash(string input, HashAlgorithm algorithm) {
2022-02-20 19:04:11 +01:00
Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
return BitConverter.ToString(hashedBytes);
}
}
}