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#

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