This commit is contained in:
Schedel Pascal
2024-07-08 15:19:00 +02:00
parent 342c6dfcb2
commit f1deeeda6c
5 changed files with 101 additions and 25 deletions

View File

@@ -12,6 +12,8 @@ public interface IDataProvider
abstract T? Create<T>() where T : OwnCharObject; abstract T? Create<T>() where T : OwnCharObject;
abstract bool Save<T>(T obj) where T : OwnCharObject; abstract bool Save<T>(T obj) where T : OwnCharObject;
abstract bool Delete<T>(T obj) where T : OwnCharObject; abstract bool Delete<T>(T obj) where T : OwnCharObject;
virtual IEnumerable<T>? Get<T>() where T : OwnCharObject => Get<T>(null);
abstract IEnumerable<T>? Get<T>(OwnCharObject? context) where T : OwnCharObject;
// Hierarchy // Hierarchy
abstract bool SetParent(UserProfile profile, UserAccount parent); abstract bool SetParent(UserProfile profile, UserAccount parent);
@@ -19,17 +21,18 @@ public interface IDataProvider
abstract bool SetParent(Property property, Character character); abstract bool SetParent(Property property, Character character);
abstract bool SetOwner(Group group, UserProfile owner); abstract bool SetOwner(Group group, UserProfile owner);
abstract bool SetOwner(Character group, UserProfile owner); abstract bool SetOwner(Character group, UserProfile owner);
abstract UserProfile? GetOwner(Group group);
abstract UserProfile? GetOwner(Character character);
// Collections
abstract bool AddMember(Group group, UserProfile user);
abstract bool RemoveMember(Group group, UserProfile user);
// Gets // Gets
abstract UserAccount? GetUserAccount(string username, string password); abstract UserAccount? GetUserAccount(string username, string password);
abstract UserProfile? GetUserProfile(string username); abstract UserProfile? GetUserProfile(string username);
abstract IEnumerable<UserAccount>? GetUserAccounts();
abstract IEnumerable<UserProfile>? GetMembers(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(Group group);
abstract IEnumerable<Character>? GetCharacters(UserProfile jprofile); abstract IEnumerable<Character>? GetCharacters(UserProfile jprofile);
// Sets
abstract bool AddMember(Group group, UserProfile user);
abstract bool RemoveMember(Group group, UserProfile user);
} }

View File

@@ -48,7 +48,14 @@ public class DefaultDataManager : IDataManager
if (e.Is(DataManagerActions.Getter.Character)) if (e.Is(DataManagerActions.Getter.Character))
{ {
// Get // Get
if (e.Is(DataManagerActionType.Set)) if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.User) && e.GetObject(out Group? group))
return e.SetResult(DataProvider.GetCharacters(group));
}
// Create
else if (e.Is(DataManagerActionType.Set))
{ {
if (e.Object is Group group && e.GetParam(0, out string? name)) if (e.Object is Group group && e.GetParam(0, out string? name))
return e.SetResult(CreateCharacter(e.CurrentUser, name, group)); return e.SetResult(CreateCharacter(e.CurrentUser, name, group));
@@ -56,10 +63,17 @@ public class DefaultDataManager : IDataManager
} }
// Group // Group
if (e.Is(DataManagerActions.Getter.Group)) else if (e.Is(DataManagerActions.Getter.Group))
{ {
// Get // Get
if (e.Is(DataManagerActionType.Get)) if (e.Is(DataManagerActionType.Get))
{
if (e.Object is UserProfile profile)
return e.SetResult(DataProvider.GetGroups(profile));
}
// Create
else if (e.Is(DataManagerActionType.Set))
{ {
if (e.GetParam(0, out string? name)) if (e.GetParam(0, out string? name))
return e.SetResult(CreateGroup(e.CurrentUser, name)); return e.SetResult(CreateGroup(e.CurrentUser, name));
@@ -67,10 +81,17 @@ public class DefaultDataManager : IDataManager
} }
// User // User
if (e.Is(DataManagerActions.Getter.UserAccount)) else if (e.Is(DataManagerActions.Getter.UserAccount))
{ {
// Get // Get
if (e.Is(DataManagerActionType.Get)) if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.Admin))
return e.SetResult(DataProvider.GetUserAccounts());
}
// Create
if (e.Is(DataManagerActionType.Set))
{ {
if (e.GetParam(0, out string? username) && e.GetParam(1, out string? password)) if (e.GetParam(0, out string? username) && e.GetParam(1, out string? password))
return e.SetResult(CreateUserAccount(username, password)); return e.SetResult(CreateUserAccount(username, password));

View File

@@ -2,6 +2,7 @@
using OwnChar.Api; using OwnChar.Api;
using OwnChar.Data.Providers.JsonFile.Model; using OwnChar.Data.Providers.JsonFile.Model;
using OwnChar.Model; using OwnChar.Model;
using System.Collections;
namespace OwnChar.Data.Providers.JsonFile; namespace OwnChar.Data.Providers.JsonFile;
@@ -32,7 +33,7 @@ public class JsonFileDataProvider : IDataProvider
File.WriteAllText(JsonFilePath, JsonConvert.SerializeObject(JsonFile, CreateJsonSerializerSettings())); File.WriteAllText(JsonFilePath, JsonConvert.SerializeObject(JsonFile, CreateJsonSerializerSettings()));
} }
protected JsonSerializerSettings CreateJsonSerializerSettings() protected virtual JsonSerializerSettings CreateJsonSerializerSettings()
{ {
return new JsonSerializerSettings return new JsonSerializerSettings
{ {
@@ -42,7 +43,18 @@ public class JsonFileDataProvider : IDataProvider
Formatting = Formatting.Indented, Formatting = Formatting.Indented,
}; };
} }
public T? Create<T>() where T : class, OwnCharObject
public bool IsInitialized()
{
return JsonFile.IsInitialized;
}
public void SetInitialized()
{
JsonFile.IsInitialized = true;
}
public T? Create<T>() where T : OwnCharObject
{ {
var t = typeof(T); var t = typeof(T);
OwnCharObject? obj; OwnCharObject? obj;
@@ -65,7 +77,7 @@ public class JsonFileDataProvider : IDataProvider
return obj as T; return obj as T;
} }
public bool Save<T>(T obj) where T : class, OwnCharObject public bool Save<T>(T obj) where T : OwnCharObject
{ {
if (obj is JsonCharacter character) if (obj is JsonCharacter character)
{ {
@@ -86,17 +98,7 @@ public class JsonFileDataProvider : IDataProvider
return true; return true;
} }
public bool IsInitialized() public bool Delete<T>(T obj) where T : OwnCharObject
{
return JsonFile.IsInitialized;
}
public void SetInitialized()
{
JsonFile.IsInitialized = true;
}
public bool Delete<T>(T obj) where T : class, OwnCharObject
{ {
if (obj is JsonCharacter character) if (obj is JsonCharacter character)
{ {
@@ -138,6 +140,39 @@ public class JsonFileDataProvider : IDataProvider
return false; return false;
} }
public IEnumerable<T>? Get<T>(OwnCharObject? context) where T : OwnCharObject
{
var t = typeof(T);
static IEnumerable<T>? asList(IList list) => list as IEnumerable<T>;
if (t == typeof(Property))
{
// ...
}
else if (t == typeof(PropertyCategory))
{
// ...
}
else if (t == typeof(Character))
{
if (context is JsonGroup group)
return (IEnumerable<T>)group.Characters;
if (context is UserProfile profile)
return (IEnumerable<T>)JsonFile.Characters.Where(n => n.Owner == profile);
return (IEnumerable<T>)JsonFile.Characters;
}
else if (t == typeof(Group))
{
if (context is UserProfile profile)
return (IEnumerable<T>)JsonFile.Groups.Where(g => g.Owner == profile);
return (IEnumerable<T>)JsonFile.Groups;
}
else if (t == typeof(UserAccount))
return (IEnumerable<T>)JsonFile.UserAccounts;
return null;
}
public bool SetParent(UserProfile profile, UserAccount parent) public bool SetParent(UserProfile profile, UserAccount parent)
{ {
if (parent is JsonUserAccount jaccount && profile is JsonUserProfile jprofile) if (parent is JsonUserAccount jaccount && profile is JsonUserProfile jprofile)

View File

@@ -41,9 +41,15 @@ public static class Extensions
return @this.Parameters.GetAt(index, out result); return @this.Parameters.GetAt(index, out result);
} }
public static bool Is(this OnActionEventArgs @this, UserType minLevel) public static bool GetObject<T>(this OnActionEventArgs @this, [NotNullWhen(true)] out T? result)
{ {
return @this.CurrentUser.HasPermission(minLevel); if (@this.Object is T t)
{
result = t;
return true;
}
result = default;
return false;
} }
public static bool Is(this OnActionEventArgs @this, DataManagerActionType actionType) public static bool Is(this OnActionEventArgs @this, DataManagerActionType actionType)
@@ -56,5 +62,15 @@ public static class Extensions
return @this.Action == action; return @this.Action == action;
} }
public static bool Is(this OnActionEventArgs @this, UserType minLevel)
{
return @this.CurrentUser.HasPermission(minLevel);
}
public static bool Is(this OnActionEventArgs @this, UserType maxLevel, UserType minLevel, Func<OnActionEventArgs, bool> isOwner)
{
return @this.Is(maxLevel) || (@this.Is(minLevel) && isOwner(@this));
}
#endregion #endregion
} }

View File

@@ -2,6 +2,7 @@
public enum UserType public enum UserType
{ {
None,
Guest, Guest,
User, User,
Admin Admin