diff --git a/OwnChar/Data/IDataManager.cs b/OwnChar/Data/IDataManager.cs index 1013d01..3e6cbe2 100644 --- a/OwnChar/Data/IDataManager.cs +++ b/OwnChar/Data/IDataManager.cs @@ -14,10 +14,17 @@ public interface IDataManager abstract bool DeleteUserAccount(UserAccount account); // Group management - abstract UserProfile? GetOwner(Group group); - abstract IEnumerable? GetMembers(Group group); + abstract UserProfile? GetOwner(UserAccount account, Group group); + abstract IEnumerable? GetMembers(UserAccount account, Group group); + abstract bool AddMember(UserAccount account, Group group, UserProfile user); + abstract bool RemoveMember(UserAccount account, Group group, UserProfile user); + abstract Group? CreateGroup(UserAccount account, string name); + abstract bool DeleteGroup(UserAccount account, Group group); // Character management - abstract IEnumerable? GetCharacters(Group group); - abstract IEnumerable? GetCharacters(UserProfile profile); + abstract UserProfile? GetOwner(UserAccount account, Character group); + abstract IEnumerable? GetCharacters(UserAccount account, Group group); + abstract IEnumerable? GetCharacters(UserAccount account, UserProfile profile); + abstract Character? CreateCharacter(UserAccount account, string name, Group? group); + abstract bool DeleteCharacter(UserAccount account, Character character); } diff --git a/OwnChar/Data/IDataProvider.cs b/OwnChar/Data/IDataProvider.cs index 01716fa..d7bdfcd 100644 --- a/OwnChar/Data/IDataProvider.cs +++ b/OwnChar/Data/IDataProvider.cs @@ -23,9 +23,13 @@ public interface IDataProvider // Gets abstract UserAccount? GetUserAccount(string username, string password); abstract UserProfile? GetUserProfile(string username); - abstract IEnumerable? GetGroupMembers(Group group); + abstract IEnumerable? GetMembers(Group group); abstract UserProfile? GetOwner(Group group); abstract UserProfile? GetOwner(Character character); abstract IEnumerable? GetCharacters(Group group); abstract IEnumerable? GetCharacters(UserProfile jprofile); + + // Sets + abstract bool AddMember(Group group, UserProfile user); + abstract bool RemoveMember(Group group, UserProfile user); } diff --git a/OwnChar/Data/Managers/DefaultDataManager.cs b/OwnChar/Data/Managers/DefaultDataManager.cs index d172b60..51c390d 100644 --- a/OwnChar/Data/Managers/DefaultDataManager.cs +++ b/OwnChar/Data/Managers/DefaultDataManager.cs @@ -15,16 +15,27 @@ public class DefaultDataManager : IDataManager Initialize(false); } - public IEnumerable? GetMembers(Group group) + public IEnumerable? GetMembers(UserAccount account, Group group) { - return DataProvider.GetGroupMembers(group); + if (!account.HasPermission(UserType.Guest)) + return null; + return DataProvider.GetMembers(group); } - public UserProfile? GetOwner(Group group) + public UserProfile? GetOwner(UserAccount account, Group group) { + if (!account.HasPermission(UserType.Guest)) + return null; return DataProvider.GetOwner(group); } + public UserProfile? GetOwner(UserAccount account, Character character) + { + if (!account.HasPermission(UserType.Guest)) + return null; + return DataProvider.GetOwner(character); + } + public UserProfile? GetUserProfile(UserAccount account) { ArgumentException.ThrowIfNullOrWhiteSpace(account.Username, nameof(account.Username)); @@ -88,13 +99,79 @@ public class DefaultDataManager : IDataManager return true; } - public IEnumerable? GetCharacters(Group group) + public IEnumerable? GetCharacters(UserAccount account, Group group) { + if (!account.HasPermission(UserType.Guest)) + return null; return DataProvider.GetCharacters(group); } - public IEnumerable? GetCharacters(UserProfile profile) + public IEnumerable? GetCharacters(UserAccount account, UserProfile profile) { + if (!account.HasPermission(UserType.Guest)) + return null; return DataProvider.GetCharacters(profile); } + + public bool AddMember(UserAccount account, Group group, UserProfile user) + { + if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(group) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin)) + return false; + return DataProvider.AddMember(group, user); + } + + public bool RemoveMember(UserAccount account, Group group, UserProfile user) + { + if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(group) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin)) + return false; + return DataProvider.RemoveMember(group, user); + } + + public Group? CreateGroup(UserAccount account, string name) + { + if (!account.HasPermission(UserType.User) || GetUserProfile(account) is not UserProfile profile || DataProvider.Create() is not Group group) + return null; + + group.Name = name; + + DataProvider.Save(group); + + DataProvider.SetOwner(group, profile); + + return group; + } + + public bool DeleteGroup(UserAccount account, Group group) + { + if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(group) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin)) + return false; + return DataProvider.Delete(group); + } + + public Character? CreateCharacter(UserAccount account, string name, Group? group) + { + if (!account.HasPermission(UserType.User) || GetUserProfile(account) is not UserProfile profile || DataProvider.Create() is not Character character) + return null; + + character.Name = name; + + DataProvider.Save(character); + + DataProvider.SetOwner(character, profile); + + if (group != null) + DataProvider.SetParent(character, group); + + return character; + } + + public bool DeleteCharacter(UserAccount account, Character character) + { + if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(character) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin)) + return false; + + DataProvider.Delete(character); + + return true; + } } diff --git a/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs b/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs index 7608e8e..047d7d2 100644 --- a/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs +++ b/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs @@ -131,7 +131,7 @@ public class JsonFileDataProvider : IDataProvider { // We don't delete profiles at the moment! profile.Name = "Deleted user"; - return false; + return true; } return false; @@ -201,7 +201,7 @@ public class JsonFileDataProvider : IDataProvider return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username)?.Profile; } - public IEnumerable? GetGroupMembers(Group group) + public IEnumerable? GetMembers(Group group) { if (group is JsonGroup jgroup) return jgroup.Members; @@ -235,4 +235,25 @@ public class JsonFileDataProvider : IDataProvider return JsonFile.Characters.Where(n => n.Owner == profile); return null; } + + public bool AddMember(Group group, UserProfile user) + { + if (group is JsonGroup jgroup && user is JsonUserProfile jprofile) + { + if (!jgroup.Members.Contains(jprofile)) + jgroup.Members.Add(jprofile); + return true; + } + return false; + } + + public bool RemoveMember(Group group, UserProfile user) + { + if (group is JsonGroup jgroup && user is JsonUserProfile jprofile) + { + jgroup.Members.Remove(jprofile); + return true; + } + return false; + } } diff --git a/OwnChar/Extensions.cs b/OwnChar/Extensions.cs new file mode 100644 index 0000000..ee3bda6 --- /dev/null +++ b/OwnChar/Extensions.cs @@ -0,0 +1,11 @@ +using OwnChar.Model; + +namespace OwnChar; + +public static class Extensions +{ + public static bool HasPermission(this UserAccount account, UserType permissions) + { + return account.Type >= permissions; + } +} diff --git a/OwnChar/Manager/CharacterManager.cs b/OwnChar/Manager/CharacterManager.cs index 501bdd9..568f7be 100644 --- a/OwnChar/Manager/CharacterManager.cs +++ b/OwnChar/Manager/CharacterManager.cs @@ -11,7 +11,7 @@ public class CharacterManager(OwnCharManager manager) Manager.CheckLogin(); if (group != null) - return Manager.DataManager?.GetCharacters(group); + return Manager.DataManager?.GetCharacters(Manager.CurrentUser!, group); return null; } @@ -21,8 +21,31 @@ public class CharacterManager(OwnCharManager manager) Manager.CheckLogin(); if (profile != null) - return Manager.DataManager?.GetCharacters(profile); + return Manager.DataManager?.GetCharacters(Manager.CurrentUser!, profile); return null; } + + public Character? CreateCharacter(string? name) + { + return CreateCharacter(name, null); + } + + public Character? CreateCharacter(string? name, Group? destination) + { + ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name)); + + Manager.CheckLogin(); + + return Manager.DataManager?.CreateCharacter(Manager.CurrentUser!, name, destination); + } + + public bool DeleteCharacter(Character? character) + { + ArgumentNullException.ThrowIfNull(character, nameof(character)); + + Manager.CheckLogin(); + + return Manager.DataManager?.DeleteCharacter(Manager.CurrentUser!, character) ?? false; + } } diff --git a/OwnChar/Manager/GroupsManager.cs b/OwnChar/Manager/GroupsManager.cs index d5287e6..eea146b 100644 --- a/OwnChar/Manager/GroupsManager.cs +++ b/OwnChar/Manager/GroupsManager.cs @@ -9,12 +9,42 @@ public class GroupsManager(OwnCharManager manager) public UserProfile? GetOwner(Group? group) { ArgumentNullException.ThrowIfNull(group, nameof(group)); - return Manager.DataManager?.GetOwner(group); + return Manager.DataManager?.GetOwner(Manager.CurrentUser!, group); } public IEnumerable? GetMembers(Group? group) { ArgumentNullException.ThrowIfNull(group, nameof(group)); - return Manager.DataManager?.GetMembers(group); + return Manager.DataManager?.GetMembers(Manager.CurrentUser!, group); + } + + public bool AddMember(UserProfile? profile, Group? group) + { + Manager.CheckLogin(); + ArgumentNullException.ThrowIfNull(profile, nameof(profile)); + ArgumentNullException.ThrowIfNull(group, nameof(group)); + return Manager.DataManager?.AddMember(Manager.CurrentUser!, group, profile) ?? false; + } + + public bool DeleteMember(UserProfile? profile, Group? group) + { + Manager.CheckLogin(); + ArgumentNullException.ThrowIfNull(profile, nameof(profile)); + ArgumentNullException.ThrowIfNull(group, nameof(group)); + return Manager.DataManager?.RemoveMember(Manager.CurrentUser!, group, profile) ?? false; + } + + public Group? CreateGroup(string? name) + { + Manager.CheckLogin(); + ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name)); + return Manager.DataManager?.CreateGroup(Manager.CurrentUser!, name); + } + + public bool DeleteGroup(Group? group) + { + Manager.CheckLogin(); + ArgumentNullException.ThrowIfNull(group, nameof(group)); + return Manager.DataManager?.DeleteGroup(Manager.CurrentUser!, group) ?? false; } }