character & group management

This commit is contained in:
2024-06-11 22:06:56 +02:00
parent ca8cee9189
commit 089d6b466f
7 changed files with 189 additions and 16 deletions

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}