tesses-cms/Tesses.CMS/UserAccount.cs

119 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
namespace Tesses.CMS
{
public class UserAccount
{
[JsonIgnore]
public bool IsVerified {get;set;}=false;
[JsonIgnore]
public bool IsInvited {get;set;}=false;
[JsonIgnore]
public bool IsAdmin {get;set;}=false;
public void NewSalt()
{
byte[] bytes=new byte[32];
using(var rnd = RandomNumberGenerator.Create())
rnd.GetBytes(bytes);
Salt = Convert.ToBase64String(bytes);
}
public string GetPasswordHash(string password)
{
string pass = $"{password}{Salt}";
using(var sha256Managed = SHA256Managed.Create())
{
return Convert.ToBase64String(sha256Managed.ComputeHash(Encoding.UTF8.GetBytes(pass)));
}
}
public bool PasswordCorrect(string password)
{
string hash=GetPasswordHash(password);
return PasswordHash == hash ;
}
[JsonIgnore]
public long Id {get;set;}
[JsonProperty("username")]
public string Username {get;set;}="";
[JsonIgnore]
public string PasswordHash {get;set;}="";
[JsonIgnore]
public string Email {get;set;}="";
[JsonIgnore]
public string Salt {get;set;}="";
[JsonProperty("proper_name")]
public string ProperName {get;set;}="";
[JsonProperty("about_me")]
public string AboutMe {get;set;}="";
[JsonIgnore]
public List<MailEntry> AccountsToMail {get;set;}=new List<MailEntry> ();
[JsonIgnore]
public List<WebHook> Webhooks {get;set;}=new List<WebHook>();
public object Scriban()
{
return new {
Username = System.Web.HttpUtility.HtmlEncode(Username),
Proper = System.Web.HttpUtility.HtmlEncode(ProperName),
About = System.Web.HttpUtility.HtmlEncode(AboutMe)
};
}
}
public class MailEntry
{
public long UserId {get;set;}
public bool EnableUpdates {get;set;}
public bool EnableMovies {get;set;}
public bool EnableAlbums {get;set;}
public bool EnableSingles {get;set;}
public bool EnableShows {get;set;}
public bool EnableSoftware {get;set;}
public bool EnableOther {get;set;}
}
public enum WebHookType
{
Ntfy,
Gotify,
Other
}
public class WebHook
{
public string WebhookName {get;set;}="";
public string Username {get;set;}="";
public string Url {get;set;}="";
public string Key {get;set;}="";
public int Priority {get;set;}=2;
public WebHookType Type {get;set;}= WebHookType.Other;
public bool EnabledMovies {get;set;}
public bool EnabledAlbums {get;set;}
public bool EnabledMusicVideos {get;set;}
public bool EnabledShows {get;set;}
public bool EnabledSoftware {get;set;}
public bool EnabledOther {get;set;}
}
}