transactions
This commit is contained in:
47
back/services/engine/SystemUser/SystemUserGenerator.cs
Normal file
47
back/services/engine/SystemUser/SystemUserGenerator.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using back.DataModels;
|
||||
using back.persistance.blob;
|
||||
using back.persistance.data;
|
||||
using back.persistance.data.repositories.Abstracts;
|
||||
using back.services.engine.Crypto;
|
||||
using back.services.engine.PasswordGenerator;
|
||||
using Transactional.Abstractions.Interfaces;
|
||||
|
||||
namespace back.services.engine.SystemUser;
|
||||
|
||||
public class SystemUserGenerator(
|
||||
ITransactionalService<DataContext> transactional,
|
||||
IUserRepository userRepository,
|
||||
IPersonRepository personRepository,
|
||||
ICryptoService cryptoService,
|
||||
IBlobStorageService blobStorageService,
|
||||
IPasswordGenerator passwordGenerator) : ISystemUserGenerator
|
||||
{
|
||||
public async Task GenerateAsync()
|
||||
{
|
||||
var systemKey = new SystemKey() {
|
||||
Password = passwordGenerator.Generate(16),
|
||||
};
|
||||
var systemKeyJson = System.Text.Json.JsonSerializer.Serialize(systemKey);
|
||||
|
||||
using Stream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(systemKeyJson));
|
||||
|
||||
await blobStorageService.Delete("systemkey.lock");
|
||||
|
||||
await blobStorageService.Save(
|
||||
stream,
|
||||
"systemkey.lock"
|
||||
);
|
||||
|
||||
User.SystemUser.Password = systemKey.Password;
|
||||
User.SystemUser.Salt = cryptoService.Salt();
|
||||
User.SystemUser.Password = cryptoService.HashPassword(User.SystemUser.Password, User.SystemUser.Salt) ?? string.Empty;
|
||||
|
||||
if (!await userRepository.Exists(User.SystemUser.Id!))
|
||||
{
|
||||
await transactional.DoTransaction(async () => {
|
||||
await personRepository.Insert(Person.SystemPerson);
|
||||
await userRepository.Insert(User.SystemUser);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user