character & group management
This commit is contained in:
@@ -14,10 +14,17 @@ public interface IDataManager
|
||||
abstract bool DeleteUserAccount(UserAccount account);
|
||||
|
||||
// Group management
|
||||
abstract UserProfile? GetOwner(Group group);
|
||||
abstract IEnumerable<UserProfile>? GetMembers(Group group);
|
||||
abstract UserProfile? GetOwner(UserAccount account, Group group);
|
||||
abstract IEnumerable<UserProfile>? 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<Character>? GetCharacters(Group group);
|
||||
abstract IEnumerable<Character>? GetCharacters(UserProfile profile);
|
||||
abstract UserProfile? GetOwner(UserAccount account, Character group);
|
||||
abstract IEnumerable<Character>? GetCharacters(UserAccount account, Group group);
|
||||
abstract IEnumerable<Character>? GetCharacters(UserAccount account, UserProfile profile);
|
||||
abstract Character? CreateCharacter(UserAccount account, string name, Group? group);
|
||||
abstract bool DeleteCharacter(UserAccount account, Character character);
|
||||
}
|
||||
|
||||
@@ -23,9 +23,13 @@ public interface IDataProvider
|
||||
// Gets
|
||||
abstract UserAccount? GetUserAccount(string username, string password);
|
||||
abstract UserProfile? GetUserProfile(string username);
|
||||
abstract IEnumerable<UserProfile>? GetGroupMembers(Group group);
|
||||
abstract IEnumerable<UserProfile>? GetMembers(Group group);
|
||||
abstract UserProfile? GetOwner(Group group);
|
||||
abstract UserProfile? GetOwner(Character character);
|
||||
abstract IEnumerable<Character>? GetCharacters(Group group);
|
||||
abstract IEnumerable<Character>? GetCharacters(UserProfile jprofile);
|
||||
|
||||
// Sets
|
||||
abstract bool AddMember(Group group, UserProfile user);
|
||||
abstract bool RemoveMember(Group group, UserProfile user);
|
||||
}
|
||||
|
||||
@@ -15,16 +15,27 @@ public class DefaultDataManager : IDataManager
|
||||
Initialize(false);
|
||||
}
|
||||
|
||||
public IEnumerable<UserProfile>? GetMembers(Group group)
|
||||
public IEnumerable<UserProfile>? 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<Character>? GetCharacters(Group group)
|
||||
public IEnumerable<Character>? GetCharacters(UserAccount account, Group group)
|
||||
{
|
||||
if (!account.HasPermission(UserType.Guest))
|
||||
return null;
|
||||
return DataProvider.GetCharacters(group);
|
||||
}
|
||||
|
||||
public IEnumerable<Character>? GetCharacters(UserProfile profile)
|
||||
public IEnumerable<Character>? 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<Group>() 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<Character>() 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<UserProfile>? GetGroupMembers(Group group)
|
||||
public IEnumerable<UserProfile>? 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;
|
||||
}
|
||||
}
|
||||
|
||||
11
OwnChar/Extensions.cs
Normal file
11
OwnChar/Extensions.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<UserProfile>? 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user