SharpRSS/SharpRSS.API/Models/DbUser.cs

55 lines
1.8 KiB
C#
Raw Normal View History

using System;
2023-10-08 00:46:42 +02:00
using Newtonsoft.Json;
using SharpRSS.API.Contracts.DTOs.Users;
2023-10-08 00:46:42 +02:00
using SharpRSS.API.Cryptography;
using ToolQit.Extensions;
namespace SharpRSS.API.Models
{
// Database model
internal class DbUser
2023-10-08 00:46:42 +02:00
{
public DbUser() { }
public DbUser(InsertUser user)
2023-10-08 00:46:42 +02:00
{
Uid = user.Uid;
2023-10-08 00:46:42 +02:00
DisplayName = user.DisplayName;
Email = user.Email;
Gid = user.GroupId;
Active = user.Active;
PswHash = Hasher.HashPassword(user.Password, out byte[] salt);
Salt = salt;
}
public string Uid { get; set; } = string.Empty;
public string DisplayName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Gid { get; set; } = string.Empty;
public bool Active { get; set; }
public DateTime DateCreated { get; set; } = DateTime.Now;
2023-10-08 00:46:42 +02:00
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 void Update(InsertUser user)
2023-10-08 00:46:42 +02:00
{
if (user == null) return;
2023-10-08 00:46:42 +02:00
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;
}
}
}
}