SharpRSS/SharpRSS.API/Models/DbUser.cs

50 lines
1.5 KiB
C#
Raw Normal View History

2023-10-08 00:46:42 +02:00
using Newtonsoft.Json;
using SharpRSS.API.Contracts.DTO;
using SharpRSS.API.Contracts.Payloads;
using SharpRSS.API.Cryptography;
using ToolQit.Extensions;
namespace SharpRSS.API.Models
{
// Database model
internal class DbUser : User
{
public DbUser() { }
public DbUser(ModifyUser user)
{
Uid = user.UserName;
DisplayName = user.DisplayName;
Email = user.Email;
Gid = user.GroupId;
Active = user.Active;
PswHash = Hasher.HashPassword(user.Password, out byte[] salt);
Salt = salt;
}
public byte[] PswHash { get; set; }
public byte[] Salt { get; set; }
public User ToDto()
{
string json = JsonConvert.SerializeObject(this);
return JsonConvert.DeserializeObject<User>(json) ?? new User();
}
public bool Update(ModifyUser user)
{
if (user == null) return false;
if (!user.DisplayName.IsNullEmptyWhiteSpace())
DisplayName = user.DisplayName;
if (!user.Email.IsNullEmptyWhiteSpace())
Email = user.Email;
if (!user.GroupId.IsNullEmptyWhiteSpace())
Gid = user.GroupId;
Active = user.Active;
if (!user.Password.IsNullEmptyWhiteSpace())
{
PswHash = Hasher.HashPassword(user.Password, out byte[] salt);
Salt = salt;
}
return true;
}
}
}