ceed44f997
Tests using local LLM AI to add comments in the C# files For the flights tunnel, show the total to day/hours For the jump, add the equipment (now just with the wingsuit) Reviewed-on: #8 Co-authored-by: sandre <perso@sebastienandre.com> Co-committed-by: sandre <perso@sebastienandre.com>
117 lines
4.0 KiB
C#
117 lines
4.0 KiB
C#
using skydiveLogs_api.Domain;
|
|
using skydiveLogs_api.DomainBusiness.Interfaces;
|
|
using skydiveLogs_api.DomainService.Repositories;
|
|
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace skydiveLogs_api.DomainBusiness
|
|
{
|
|
public class UserService : IUserService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public UserService(IUserRepository userRepository,
|
|
IGearService gearService)
|
|
{
|
|
_userRepository = userRepository;
|
|
_gearService = gearService;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Adds a new user to the system.
|
|
/// </summary>
|
|
/// <param name="newUser">The User entity containing the new user data.</param>
|
|
/// <param name="isAdmin">Whether the new user should have admin privileges.</param>
|
|
public bool AddNewUser(User newUser, bool isAdmin = false)
|
|
{
|
|
newUser.Password = EncryptPassword(newUser.Password);
|
|
var foundUser = _userRepository.GetByLogin(newUser.Login, newUser.Password);
|
|
var result = false;
|
|
|
|
if (foundUser == null)
|
|
{
|
|
newUser.IsAdmin = isAdmin;
|
|
var newUserId = _userRepository.Add(newUser);
|
|
result = (newUserId > 0);
|
|
|
|
if (result)
|
|
{
|
|
newUser.Id = newUserId;
|
|
_gearService.AddRentalGear(newUser);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves a user by their ID.
|
|
/// </summary>
|
|
/// <param name="userId">The user ID to retrieve.</param>
|
|
/// <returns>A User entity containing the user details.</returns>
|
|
public User GetById(int userId)
|
|
{
|
|
return _userRepository.GetById(userId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves a user by their login and password.
|
|
/// </summary>
|
|
/// <param name="login">The login name of the user.</param>
|
|
/// <param name="password">The password to verify the user.</param>
|
|
/// <returns>A User entity containing the user details.</returns>
|
|
public User GetByLogin(string login, string password)
|
|
{
|
|
return _userRepository.GetByLogin(login, EncryptPassword(password));
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Methods
|
|
|
|
private string EncryptPassword(string password)
|
|
{
|
|
var encryptionKey = "skydivelogsangular"; //we can change the code converstion key as per our requirement
|
|
byte[] clearBytes = Encoding.Unicode.GetBytes(password);
|
|
var encryptedPassword = string.Empty;
|
|
|
|
using (Aes encryptor = Aes.Create())
|
|
{
|
|
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey,
|
|
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 },
|
|
3,
|
|
HashAlgorithmName.SHA256);
|
|
encryptor.Key = pdb.GetBytes(32);
|
|
encryptor.IV = pdb.GetBytes(16);
|
|
using (MemoryStream ms = new MemoryStream())
|
|
{
|
|
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
|
|
{
|
|
cs.Write(clearBytes, 0, clearBytes.Length);
|
|
cs.Close();
|
|
}
|
|
|
|
encryptedPassword = Convert.ToBase64String(ms.ToArray());
|
|
}
|
|
}
|
|
|
|
return encryptedPassword;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Private Fields
|
|
|
|
private readonly IGearService _gearService;
|
|
private readonly IUserRepository _userRepository;
|
|
|
|
#endregion Private Fields
|
|
}
|
|
}
|