From b08041204da0cc799d2d813e6f1beeaf45afeea9 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Mon, 27 May 2024 22:07:25 +0200 Subject: [PATCH] some work --- .../ClientServer/ClientServerDataProvider.cs | 30 +++++++ OwnChar/Data/ClientServerDataProvider.cs | 52 ----------- OwnChar/Data/IDataProvider.cs | 20 +++-- OwnChar/Data/JsonFile/JsonFile.cs | 14 +++ OwnChar/Data/JsonFile/JsonFileDataProvider.cs | 87 +++++++++++++++++++ OwnChar/Data/JsonFileDataProvider.cs | 12 --- OwnChar/Manager/CharacterManager.cs | 4 +- OwnChar/Manager/Exceptions/LoginException.cs | 6 ++ OwnChar/Manager/OwnCharManager.cs | 35 ++++++-- OwnChar/Model/UserAccount.cs | 14 +-- OwnChar/Model/UserProfile.cs | 4 +- OwnChar/Model/UserType.cs | 9 ++ 12 files changed, 196 insertions(+), 91 deletions(-) create mode 100644 OwnChar/Data/ClientServer/ClientServerDataProvider.cs delete mode 100644 OwnChar/Data/ClientServerDataProvider.cs create mode 100644 OwnChar/Data/JsonFile/JsonFile.cs create mode 100644 OwnChar/Data/JsonFile/JsonFileDataProvider.cs delete mode 100644 OwnChar/Data/JsonFileDataProvider.cs create mode 100644 OwnChar/Manager/Exceptions/LoginException.cs create mode 100644 OwnChar/Model/UserType.cs diff --git a/OwnChar/Data/ClientServer/ClientServerDataProvider.cs b/OwnChar/Data/ClientServer/ClientServerDataProvider.cs new file mode 100644 index 0000000..3731ccc --- /dev/null +++ b/OwnChar/Data/ClientServer/ClientServerDataProvider.cs @@ -0,0 +1,30 @@ +using OwnChar.Model; +using Pilz.Cryptography; + +namespace OwnChar.Data.ClientServer +{ + public class ClientServerDataProvider : IDataProvider + { + public UserAccount? CurrentUserAccount => throw new NotImplementedException(); + + public ClientServerDataProvider(string serverAddress) + { + throw new NotImplementedException(); + } + + public UserProfile? GetUserProfile(string username) + { + throw new NotImplementedException(); + } + + public UserAccount? Login(string username, SecureString password) + { + throw new NotImplementedException(); + } + + public bool Logout() + { + throw new NotImplementedException(); + } + } +} diff --git a/OwnChar/Data/ClientServerDataProvider.cs b/OwnChar/Data/ClientServerDataProvider.cs deleted file mode 100644 index d72181e..0000000 --- a/OwnChar/Data/ClientServerDataProvider.cs +++ /dev/null @@ -1,52 +0,0 @@ -using OwnChar.Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OwnChar.Data -{ - public class ClientServerDataProvider : IDataProvider - { - public bool Save(Character character) - { - } - - public bool Save(UserProfile profile) - { - } - - public bool Save(UserAccount account) - { - } - - public bool Save(Group group) - { - } - - public bool Save(PropertyCategory category) - { - } - - public bool Delete(Character character) - { - } - - public bool Delete(UserProfile profile) - { - } - - public bool Delete(UserAccount account) - { - } - - public bool Delete(Group group) - { - } - - public bool Delete(PropertyCategory category) - { - } - } -} diff --git a/OwnChar/Data/IDataProvider.cs b/OwnChar/Data/IDataProvider.cs index b71390b..7513991 100644 --- a/OwnChar/Data/IDataProvider.cs +++ b/OwnChar/Data/IDataProvider.cs @@ -1,12 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using OwnChar.Model; +using Pilz.Cryptography; namespace OwnChar.Data { public interface IDataProvider { + // Properties + public bool IsLoggedIn => CurrentUserAccount != null; + public UserProfile? CurrentUserProfile => CurrentUserAccount?.Profile; + + // Properties (abstract) + abstract UserAccount? CurrentUserAccount { get; } + + // Methods (abstract) + abstract UserAccount? Initialize(bool force); + abstract UserProfile? GetUserProfile(string username); + abstract UserAccount? Login(string username, string password); + abstract bool Logout(); + abstract UserAccount? CreateUser(string username, string password); } } diff --git a/OwnChar/Data/JsonFile/JsonFile.cs b/OwnChar/Data/JsonFile/JsonFile.cs new file mode 100644 index 0000000..581e8b1 --- /dev/null +++ b/OwnChar/Data/JsonFile/JsonFile.cs @@ -0,0 +1,14 @@ +using OwnChar.Model; + +namespace OwnChar.Data.JsonFile +{ + public class JsonFile + { + public List UserAccounts { get; } = []; + public List UserProfiles { get; } = []; + public List Characters { get; } = []; + public List Groups { get; } = []; + public List Properties { get; } = []; + public List PropertyCategories { get; } = []; + } +} diff --git a/OwnChar/Data/JsonFile/JsonFileDataProvider.cs b/OwnChar/Data/JsonFile/JsonFileDataProvider.cs new file mode 100644 index 0000000..3ebab76 --- /dev/null +++ b/OwnChar/Data/JsonFile/JsonFileDataProvider.cs @@ -0,0 +1,87 @@ +using Newtonsoft.Json; +using OwnChar.Model; + +namespace OwnChar.Data.JsonFile +{ + public class JsonFileDataProvider : IDataProvider + { + public JsonFile JsonFile { get; protected set; } + public string JsonFilePath { get; protected set; } + public UserAccount? CurrentUserAccount { get; protected set; } + + public JsonFileDataProvider(string filePath) + { + JsonFilePath = filePath; + LoadFile(); + + // Get rid of bad compiler warnings + if (JsonFile == null) + throw new Exception("Something went incredible wrong at initialization of the JsonFile."); + } + + public UserAccount? Initialize(bool force) + { + if (force || JsonFile.UserAccounts.Count == 0) + CreateUser("admin", "admin"); + return CurrentUserAccount; + } + + protected void LoadFile() + { + if (JsonConvert.DeserializeObject(File.ReadAllText(JsonFilePath), CreateJsonSerializerSettings()) is JsonFile jsonFile) + JsonFile = jsonFile; + JsonFile ??= new(); + CurrentUserAccount = Initialize(false); + } + + protected void SaveFile() + { + File.WriteAllText(JsonFilePath, JsonConvert.SerializeObject(JsonFile, CreateJsonSerializerSettings())); + } + + protected JsonSerializerSettings CreateJsonSerializerSettings() + { + return new JsonSerializerSettings + { + PreserveReferencesHandling = PreserveReferencesHandling.All, + ReferenceLoopHandling = ReferenceLoopHandling.Serialize, + TypeNameHandling = TypeNameHandling.Auto, + Formatting = Formatting.Indented, + }; + } + + public UserAccount? Login(string username, string password) + { + return CurrentUserAccount = JsonFile.UserAccounts.FirstOrDefault(n => !string.IsNullOrWhiteSpace(n.Username) && !string.IsNullOrWhiteSpace(n.Password) && n.Username == username && n.Password == password); + } + + public bool Logout() + { + CurrentUserAccount = null; + return true; + } + + public UserAccount? CreateUser(string username, string password) + { + var account = new UserAccount + { + Username = username, + Password = password, + Profile = new() + { + Name = username, + } + }; + + JsonFile.UserAccounts.Add(account); + JsonFile.UserProfiles.Add(account.Profile); + + return account; + } + + public UserProfile? GetUserProfile(string username) + { + return JsonFile.UserProfiles.FirstOrDefault(n => !string.IsNullOrWhiteSpace(n.Account?.Username) && n.Account.Username == username); + } + } +} diff --git a/OwnChar/Data/JsonFileDataProvider.cs b/OwnChar/Data/JsonFileDataProvider.cs deleted file mode 100644 index 73023d1..0000000 --- a/OwnChar/Data/JsonFileDataProvider.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OwnChar.Data -{ - public class JsonFileDataProvider : IDataProvider - { - } -} diff --git a/OwnChar/Manager/CharacterManager.cs b/OwnChar/Manager/CharacterManager.cs index a745458..10f806d 100644 --- a/OwnChar/Manager/CharacterManager.cs +++ b/OwnChar/Manager/CharacterManager.cs @@ -12,7 +12,7 @@ namespace OwnChar.Manager public OwnCharManager Manager { get; } = manager; /// - /// Gets personal characters. + /// Gets all personal characters (private & within groups). /// /// /// Returns a collection with personal characters. @@ -21,7 +21,7 @@ namespace OwnChar.Manager if (!IsLoggedIn || UserProfile == null) return null; - return GetCharacters(UserProfile.Group); + return GetCharacters(Manager.CurrentUser); } /// diff --git a/OwnChar/Manager/Exceptions/LoginException.cs b/OwnChar/Manager/Exceptions/LoginException.cs new file mode 100644 index 0000000..7bc6712 --- /dev/null +++ b/OwnChar/Manager/Exceptions/LoginException.cs @@ -0,0 +1,6 @@ +namespace OwnChar.Manager.Exceptions +{ + public class LoginException(string message) : Exception(message) + { + } +} diff --git a/OwnChar/Manager/OwnCharManager.cs b/OwnChar/Manager/OwnCharManager.cs index 043d1e8..e2232c5 100644 --- a/OwnChar/Manager/OwnCharManager.cs +++ b/OwnChar/Manager/OwnCharManager.cs @@ -1,4 +1,5 @@ using OwnChar.Data; +using OwnChar.Manager.Exceptions; using OwnChar.Model; using Pilz.Cryptography; @@ -7,32 +8,50 @@ namespace OwnChar.Manager public class OwnCharManager { // User - public bool IsLoggedIn { get; set; } - public UserAccount? CurrentUser { get; set; } + public bool IsLoggedIn => DataProvider != null && DataProvider.IsLoggedIn; + public UserAccount? CurrentUser => DataProvider?.CurrentUserAccount; // Data Provider - public IDataProvider DataProvider { get; set; } + public IDataProvider? DataProvider { get; set; } // Manager public UserManager Users { get; } public GroupsManager Groups { get; } public CharacterManager Characters { get; } - public OwnCharManager(IDataProvider dataProvider) + public OwnCharManager() { - DataProvider = dataProvider; Users = new(this); Groups = new(this); Characters = new(this); } + private void CheckLogin() + { + if (!IsLoggedIn) + throw new LoginException("You are already logged in!"); + } + /// - /// Tries to login on the server. + /// Tries to login on the given data provider. /// /// Returns if the login was successfull and if not. - public bool Login(string username, SecureString password) + public bool Login(IDataProvider? dataProvider, string username, SecureString password) { - return IsLoggedIn = true; // TODO: Change `true` to the real check. + CheckLogin(); + ArgumentNullException.ThrowIfNull(dataProvider); + return dataProvider.Login(username, password) != null; + } + + /// + /// Closes the session on the current data provider. + /// + /// + public bool Logout() + { + if (DataProvider != null) + return DataProvider.Logout(); + return true; } } } diff --git a/OwnChar/Model/UserAccount.cs b/OwnChar/Model/UserAccount.cs index ff69c91..0cdeb96 100644 --- a/OwnChar/Model/UserAccount.cs +++ b/OwnChar/Model/UserAccount.cs @@ -3,15 +3,9 @@ public class UserAccount { public ulong Id { get; set; } - public string Username { get; set; } - public string Email { get; set; } - public string Password { get; set; } - - internal UserAccount(string email, string username, string password) - { - Username = username; - Email = email; - Password = password; - } + public string? Username { get; set; } + public string? Password { get; set; } + public string? Email { get; set; } + public UserProfile? Profile { get; set; } } } diff --git a/OwnChar/Model/UserProfile.cs b/OwnChar/Model/UserProfile.cs index 767b19f..2c8d0c8 100644 --- a/OwnChar/Model/UserProfile.cs +++ b/OwnChar/Model/UserProfile.cs @@ -1,9 +1,9 @@ namespace OwnChar.Model { - public class UserProfile(string name) + public class UserProfile { public ulong Id { get; set; } - public string Name { get; set; } = name; + public string? Name { get; set; } public UserAccount? Account { get; set; } } } diff --git a/OwnChar/Model/UserType.cs b/OwnChar/Model/UserType.cs new file mode 100644 index 0000000..c0fd354 --- /dev/null +++ b/OwnChar/Model/UserType.cs @@ -0,0 +1,9 @@ +namespace OwnChar.Model +{ + public enum UserType + { + Guest, + User, + Admin + } +}