Compare commits
1 Commits
342c6dfcb2
...
f1deeeda6c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f1deeeda6c |
@@ -12,6 +12,8 @@ public interface IDataProvider
|
||||
abstract T? Create<T>() where T : OwnCharObject;
|
||||
abstract bool Save<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
|
||||
abstract bool SetParent(UserProfile profile, UserAccount parent);
|
||||
@@ -19,17 +21,18 @@ public interface IDataProvider
|
||||
abstract bool SetParent(Property property, Character character);
|
||||
abstract bool SetOwner(Group 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
|
||||
abstract UserAccount? GetUserAccount(string username, string password);
|
||||
abstract UserProfile? GetUserProfile(string username);
|
||||
abstract IEnumerable<UserAccount>? GetUserAccounts();
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,14 @@ public class DefaultDataManager : IDataManager
|
||||
if (e.Is(DataManagerActions.Getter.Character))
|
||||
{
|
||||
// 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))
|
||||
return e.SetResult(CreateCharacter(e.CurrentUser, name, group));
|
||||
@@ -56,10 +63,17 @@ public class DefaultDataManager : IDataManager
|
||||
}
|
||||
|
||||
// Group
|
||||
if (e.Is(DataManagerActions.Getter.Group))
|
||||
else if (e.Is(DataManagerActions.Getter.Group))
|
||||
{
|
||||
// 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))
|
||||
return e.SetResult(CreateGroup(e.CurrentUser, name));
|
||||
@@ -67,10 +81,17 @@ public class DefaultDataManager : IDataManager
|
||||
}
|
||||
|
||||
// User
|
||||
if (e.Is(DataManagerActions.Getter.UserAccount))
|
||||
else if (e.Is(DataManagerActions.Getter.UserAccount))
|
||||
{
|
||||
// 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))
|
||||
return e.SetResult(CreateUserAccount(username, password));
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using OwnChar.Api;
|
||||
using OwnChar.Data.Providers.JsonFile.Model;
|
||||
using OwnChar.Model;
|
||||
using System.Collections;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile;
|
||||
|
||||
@@ -32,7 +33,7 @@ public class JsonFileDataProvider : IDataProvider
|
||||
File.WriteAllText(JsonFilePath, JsonConvert.SerializeObject(JsonFile, CreateJsonSerializerSettings()));
|
||||
}
|
||||
|
||||
protected JsonSerializerSettings CreateJsonSerializerSettings()
|
||||
protected virtual JsonSerializerSettings CreateJsonSerializerSettings()
|
||||
{
|
||||
return new JsonSerializerSettings
|
||||
{
|
||||
@@ -42,7 +43,18 @@ public class JsonFileDataProvider : IDataProvider
|
||||
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);
|
||||
OwnCharObject? obj;
|
||||
@@ -65,7 +77,7 @@ public class JsonFileDataProvider : IDataProvider
|
||||
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)
|
||||
{
|
||||
@@ -86,17 +98,7 @@ public class JsonFileDataProvider : IDataProvider
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsInitialized()
|
||||
{
|
||||
return JsonFile.IsInitialized;
|
||||
}
|
||||
|
||||
public void SetInitialized()
|
||||
{
|
||||
JsonFile.IsInitialized = true;
|
||||
}
|
||||
|
||||
public bool Delete<T>(T obj) where T : class, OwnCharObject
|
||||
public bool Delete<T>(T obj) where T : OwnCharObject
|
||||
{
|
||||
if (obj is JsonCharacter character)
|
||||
{
|
||||
@@ -138,6 +140,39 @@ public class JsonFileDataProvider : IDataProvider
|
||||
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)
|
||||
{
|
||||
if (parent is JsonUserAccount jaccount && profile is JsonUserProfile jprofile)
|
||||
|
||||
@@ -41,9 +41,15 @@ public static class Extensions
|
||||
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)
|
||||
@@ -56,5 +62,15 @@ public static class Extensions
|
||||
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
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
public enum UserType
|
||||
{
|
||||
None,
|
||||
Guest,
|
||||
User,
|
||||
Admin
|
||||
|
||||
Reference in New Issue
Block a user