diff --git a/OwnChar/Data/IDataManager.cs b/OwnChar/Data/IDataManager.cs index 6c049f3..e629549 100644 --- a/OwnChar/Data/IDataManager.cs +++ b/OwnChar/Data/IDataManager.cs @@ -11,9 +11,10 @@ namespace OwnChar.Data // User management abstract UserAccount? CreateUserAccount(string username, string password); abstract UserProfile? GetUserProfile(UserAccount account); + abstract bool DeleteUserAccount(UserAccount account); // Group management abstract UserProfile? GetOwner(Group group); - abstract IEnumerable GetMembers(Group group); + abstract IEnumerable? GetMembers(Group group); } } diff --git a/OwnChar/Data/IDataProvider.cs b/OwnChar/Data/IDataProvider.cs index 1266c1e..3550827 100644 --- a/OwnChar/Data/IDataProvider.cs +++ b/OwnChar/Data/IDataProvider.cs @@ -22,7 +22,7 @@ namespace OwnChar.Data // Gets abstract UserAccount? GetUserAccount(string username, string password); abstract UserProfile? GetUserProfile(string username); - abstract IEnumerable GetGroupMembers(Group group); + abstract IEnumerable? GetGroupMembers(Group group); abstract UserProfile? GetOwner(Group group); abstract UserProfile? GetOwner(Character character); } diff --git a/OwnChar/Data/Managers/DefaultDataManager.cs b/OwnChar/Data/Managers/DefaultDataManager.cs index 0985521..dafe058 100644 --- a/OwnChar/Data/Managers/DefaultDataManager.cs +++ b/OwnChar/Data/Managers/DefaultDataManager.cs @@ -2,42 +2,14 @@ namespace OwnChar.Data.Managers { - public class DefaultDataManager : IDataManager + public class DefaultDataManager(IDataProvider dataProvider) : IDataManager { - public IDataProvider DataProvider { get; } + private const string defaultUsername = "admin"; + private const string defaultPassword = "admin"; - public DefaultDataManager(IDataProvider dataProvider) - { - DataProvider = dataProvider; - } + public IDataProvider DataProvider { get; } = dataProvider; - public UserAccount? CreateUserAccount(string username, string password) - { - var account = DataProvider.Create(); - var profile = DataProvider.Create(); - var group = DataProvider.Create(); - - ArgumentNullException.ThrowIfNull(account, nameof(account)); - ArgumentNullException.ThrowIfNull(profile, nameof(profile)); - ArgumentNullException.ThrowIfNull(group, nameof(group)); - - account.Username = username; - account.Password = password; - - profile.Name = username; - DataProvider.SetParent(profile, account); - - group.IsInternal = true; - DataProvider.SetOwner(group, profile); - - DataProvider.Save(account); - DataProvider.Save(profile); - DataProvider.Save(group); - - return account; - } - - public IEnumerable GetMembers(Group group) + public IEnumerable? GetMembers(Group group) { return DataProvider.GetGroupMembers(group); } @@ -66,7 +38,41 @@ namespace OwnChar.Data.Managers public bool Initialize(bool force) { if (force || !DataProvider.IsInitialized()) - return CreateUserAccount("admin", "admin") != null; + return CreateUserAccount(defaultUsername, Utils.HashPassword(defaultUsername, defaultPassword)) != null; + return true; + } + + public UserAccount? CreateUserAccount(string username, string password) + { + var account = DataProvider.Create(); + var profile = DataProvider.Create(); + var group = DataProvider.Create(); + + ArgumentNullException.ThrowIfNull(account, nameof(account)); + ArgumentNullException.ThrowIfNull(profile, nameof(profile)); + ArgumentNullException.ThrowIfNull(group, nameof(group)); + + account.Username = username; + account.Password = password; + + profile.Name = username; + DataProvider.SetParent(profile, account); + + group.IsInternal = true; + DataProvider.SetOwner(group, profile); + + DataProvider.Save(account); + DataProvider.Save(profile); + DataProvider.Save(group); + + return account; + } + + public bool DeleteUserAccount(UserAccount account) + { + if (!string.IsNullOrWhiteSpace(account.Username) && DataProvider.GetUserProfile(account.Username) is UserProfile userProfile) + userProfile.Name = "Deleted user"; + DataProvider.Delete(account); return true; } } diff --git a/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs b/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs index b69e0b0..ae4189d 100644 --- a/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs +++ b/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json; using OwnChar.Data.Providers.JsonFile.Model; using OwnChar.Model; +using System.Net.NetworkInformation; namespace OwnChar.Data.Providers.JsonFile { @@ -90,59 +91,131 @@ namespace OwnChar.Data.Providers.JsonFile return JsonFile.UserAccounts.Count != 0; } - bool IDataProvider.Delete(T obj) + public bool Delete(T obj) where T : class, IOwnCharObject { - throw new NotImplementedException(); + if (obj is JsonCharacter character) + { + JsonFile.Groups.ForEach(n => n.Characters.Remove(character)); + return true; + } + else if (obj is JsonProp prop) + { + JsonFile.Characters.ForEach(n => n.Properties.Remove(prop)); + return true; + } + else if (obj is JsonPropCat propCat) + { + JsonFile.Characters.ForEach(n => n.Properties.ForEach(m => + { + if (m.Category == propCat) + m.Category = null; + })); + JsonFile.Characters.ForEach(n => n.PropertyCategories.Remove(propCat)); + return true; + } + else if (obj is JsonGroup group) + { + JsonFile.Groups.Remove(group); + return true; + } + else if (obj is JsonUserAccount account) + { + JsonFile.UserAccounts.Remove(account); + return true; + } + else if (obj is JsonUserProfile profile) + { + // We don't delete profiles at the moment! + profile.Name = "Deleted user"; + return false; + } + + return false; } public bool SetParent(UserProfile profile, UserAccount parent) { - throw new NotImplementedException(); + if (parent is JsonUserAccount jaccount && profile is JsonUserProfile jprofile) + { + jaccount.Profile = jprofile; + return true; + } + return false; } public bool SetParent(Character character, Group parent) { - throw new NotImplementedException(); + if (character is JsonCharacter jcharacter && parent is JsonGroup jgroup) + { + if (!jgroup.Characters.Contains(jcharacter)) + jgroup.Characters.Add(jcharacter); + return true; + } + return false; } public bool SetParent(Property property, Character character) { - throw new NotImplementedException(); + if (property is JsonProp jprop && character is JsonCharacter jcharacter) + { + if (!jcharacter.Properties.Contains(jprop)) + jcharacter.Properties.Add(jprop); + return true; + } + return false; } public bool SetOwner(Group group, UserProfile owner) { - throw new NotImplementedException(); + if (group is JsonGroup jgroup && owner is JsonUserProfile jprofile) + { + jgroup.Owner = jprofile; + return true; + } + return false; } - public bool SetOwner(Character group, UserProfile owner) + public bool SetOwner(Character character, UserProfile owner) { - throw new NotImplementedException(); + if (character is JsonCharacter jcharacter && owner is JsonUserProfile jprofile) + { + jcharacter.Owner = jprofile; + return true; + } + return false; } public UserAccount? GetUserAccount(string username, string password) { - throw new NotImplementedException(); + ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username)); + ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password)); + return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username && n.Password == password); } public UserProfile? GetUserProfile(string username) { - throw new NotImplementedException(); + return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username)?.Profile; } - public IEnumerable GetGroupMembers(Group group) + public IEnumerable? GetGroupMembers(Group group) { - throw new NotImplementedException(); + if (group is JsonGroup jgroup) + return jgroup.Members; + return null; } public UserProfile? GetOwner(Group group) { - throw new NotImplementedException(); + if (group is JsonGroup jgroup) + return jgroup.Owner; + return null; } public UserProfile? GetOwner(Character character) { - throw new NotImplementedException(); + if (character is JsonCharacter jcharacter) + return jcharacter.Owner; + return null; } } } diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs index 12cf538..ca9baa1 100644 --- a/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs @@ -4,7 +4,8 @@ namespace OwnChar.Data.Providers.JsonFile.Model { public class JsonCharacter : Character { - public List Properties { get; } = []; - public List PropertyCategories { get; } = []; + public virtual JsonUserProfile? Owner { get; set; } + public virtual List Properties { get; } = []; + public virtual List PropertyCategories { get; } = []; } } diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs index 8bb4461..3f4c0ec 100644 --- a/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs @@ -4,8 +4,8 @@ namespace OwnChar.Data.Providers.JsonFile.Model { public class JsonGroup : Group { - public List Owner { get; } = []; - public List Members { get; } = []; - public List Characters { get; } = []; + public virtual JsonUserProfile? Owner { get; set; } + public virtual List Members { get; } = []; + public virtual List Characters { get; } = []; } } diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs index ab75e75..42df6b8 100644 --- a/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs @@ -4,6 +4,6 @@ namespace OwnChar.Data.Providers.JsonFile.Model { public class JsonProp : Property { - public JsonPropCat? Category { get; set; } = null; + public virtual JsonPropCat? Category { get; set; } = null; } } diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs index d16aaa1..042c6a8 100644 --- a/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs @@ -1,9 +1,14 @@ -using OwnChar.Model; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using OwnChar.Model; namespace OwnChar.Data.Providers.JsonFile.Model { public class JsonUserAccount : UserAccount { - public JsonUserProfile? Profile { get; set; } + public virtual JsonUserProfile? Profile { get; set; } + + [JsonConverter(typeof(StringEnumConverter))] + public override UserType Type { get => base.Type; set => base.Type = value; } } } diff --git a/OwnChar/Manager/GroupsManager.cs b/OwnChar/Manager/GroupsManager.cs index b5ebbd9..d935b9c 100644 --- a/OwnChar/Manager/GroupsManager.cs +++ b/OwnChar/Manager/GroupsManager.cs @@ -9,13 +9,13 @@ namespace OwnChar.Manager public UserProfile? GetOwner(Group? group) { ArgumentNullException.ThrowIfNull(group, nameof(group)); - return Manager.DataProxy?.GetOwner(group); + return Manager.DataManager?.GetOwner(group); } public IEnumerable? GetMembers(Group? group) { ArgumentNullException.ThrowIfNull(group, nameof(group)); - return Manager.DataProxy?.GetMembers(group); + return Manager.DataManager?.GetMembers(group); } } } diff --git a/OwnChar/Manager/OwnCharManager.cs b/OwnChar/Manager/OwnCharManager.cs index 80802b3..1918a81 100644 --- a/OwnChar/Manager/OwnCharManager.cs +++ b/OwnChar/Manager/OwnCharManager.cs @@ -12,7 +12,7 @@ namespace OwnChar.Manager public UserAccount? CurrentUser { get; private set; } // Data Provider - public IDataManager? DataProxy { get; set; } + public IDataManager? DataManager { get; set; } // Manager public UserManager Users { get; } @@ -42,6 +42,7 @@ namespace OwnChar.Manager ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username)); ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password)); + username = username.Trim().ToLower(); CurrentUser = proxy.Login(username, Utils.HashPassword(username, password)); return IsLoggedIn; @@ -53,7 +54,7 @@ namespace OwnChar.Manager /// public bool Logout() { - return DataProxy?.Logout(CurrentUser) ?? true; + return DataManager?.Logout(CurrentUser) ?? true; } } } diff --git a/OwnChar/Manager/UserManager.cs b/OwnChar/Manager/UserManager.cs index f0f759e..7510f36 100644 --- a/OwnChar/Manager/UserManager.cs +++ b/OwnChar/Manager/UserManager.cs @@ -7,17 +7,24 @@ namespace OwnChar.Manager { public OwnCharManager Manager { get; } = manager; - public UserProfile? GetOwnUserProfile(UserAccount account) + public UserProfile? GetOwnUserProfile() { Manager.CheckLogin(); - return Manager.DataProxy!.GetUserProfile(Manager.CurrentUser!); + return Manager.DataManager!.GetUserProfile(Manager.CurrentUser!); } public UserAccount? CreateUserAccount(string? username, SecureString? password) { ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username)); ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password)); - return Manager.DataProxy?.CreateUserAccount(username, Utils.HashPassword(username, password)); + username = username.Trim().ToLower(); + return Manager.DataManager?.CreateUserAccount(username, Utils.HashPassword(username, password)); + } + + public bool DeleteUserAccount(UserAccount? account) + { + ArgumentNullException.ThrowIfNull(account, nameof(account)); + return Manager.DataManager?.DeleteUserAccount(account) ?? false; } } } diff --git a/OwnChar/Model/UserAccount.cs b/OwnChar/Model/UserAccount.cs index 08b45de..52d82df 100644 --- a/OwnChar/Model/UserAccount.cs +++ b/OwnChar/Model/UserAccount.cs @@ -5,5 +5,6 @@ public virtual string? Username { get; set; } public virtual string? Password { get; set; } public virtual string? Email { get; set; } + public virtual UserType Type { get; set; } } }