docker files fixes from liaqat
This commit is contained in:
88
src/Managing.Application/Users/UserService.cs
Normal file
88
src/Managing.Application/Users/UserService.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Users;
|
||||
|
||||
namespace Managing.Application.Users;
|
||||
|
||||
public class UserService : IUserService
|
||||
{
|
||||
private readonly IEvmManager _evmManager;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IAccountService _accountService;
|
||||
|
||||
public UserService(
|
||||
IEvmManager evmManager,
|
||||
IUserRepository userRepository,
|
||||
IAccountService accountService)
|
||||
{
|
||||
_evmManager = evmManager;
|
||||
_userRepository = userRepository;
|
||||
_accountService = accountService;
|
||||
}
|
||||
|
||||
public async Task<User> Authenticate(string name, string address, string message, string signature)
|
||||
{
|
||||
var recoveredAddress = _evmManager.VerifySignature(signature, message);
|
||||
|
||||
if (recoveredAddress == null || !recoveredAddress.Equals(address))
|
||||
throw new Exception("Address not corresponding");
|
||||
|
||||
// Check if account exist
|
||||
var account = await _accountService.GetAccountByKey(recoveredAddress, true, false);
|
||||
var user = await _userRepository.GetUserByNameAsync(name);
|
||||
|
||||
if (account != null && user != null)
|
||||
{
|
||||
// User and account found
|
||||
user.Accounts = _accountService.GetAccountsByUser(user).ToList();
|
||||
|
||||
if (!user.Name.Equals(name))
|
||||
throw new Exception("Name not corresponding");
|
||||
|
||||
return user;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No account and no
|
||||
account = new Account
|
||||
{
|
||||
Name = $"Auth-{new Random().Next(1, 99)}",
|
||||
Key = recoveredAddress,
|
||||
Secret = "",
|
||||
Exchange = Common.Enums.TradingExchanges.Evm,
|
||||
Type = Common.Enums.AccountType.Auth,
|
||||
};
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
_ = await _accountService.CreateAccount(user, account);
|
||||
user.Accounts = _accountService.GetAccountsByUser(user).ToList();
|
||||
return user;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No user found, we create one
|
||||
// Create user if not existing
|
||||
user = new User()
|
||||
{
|
||||
Name = name,
|
||||
Accounts = new List<Account> { account },
|
||||
};
|
||||
|
||||
_ = await _accountService.CreateAccount(user, account);
|
||||
await _userRepository.InsertUserAsync(user);
|
||||
}
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<User> GetUserByAddressAsync(string address)
|
||||
{
|
||||
var account = await _accountService.GetAccountByKey(address, true, false);
|
||||
var user = await _userRepository.GetUserByNameAsync(account.User.Name);
|
||||
user.Accounts = _accountService.GetAccountsByUser(user).ToList();
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user