and even more
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using OwnChar.Manager.Modules;
|
||||
using OwnChar.Model;
|
||||
using System.Reflection.Metadata;
|
||||
|
||||
namespace OwnChar.Data.Managers;
|
||||
|
||||
@@ -117,7 +118,7 @@ public class DefaultDataManager : IDataManager
|
||||
if (e.Is(DataManagerActionType.Get))
|
||||
{
|
||||
if (e.Is(UserType.User) && e.GetObject(out Character? character))
|
||||
return e.SetResult(DataProvider.GetChilds<Property>(character, HierarchyProperties.PropPropCats));
|
||||
return e.SetResult(DataProvider.GetChilds<Property>(character, HierarchyProperties.PropProps));
|
||||
}
|
||||
|
||||
// Create
|
||||
@@ -128,23 +129,23 @@ public class DefaultDataManager : IDataManager
|
||||
}
|
||||
}
|
||||
|
||||
//// PropertyCategory
|
||||
//else if (e.Is(DataManagerActions.Getter.PropertyCategory))
|
||||
//{
|
||||
// // Get
|
||||
// if (e.Is(DataManagerActionType.Get))
|
||||
// {
|
||||
// if (e.Is(UserType.User) && e.GetObject(out Character? character))
|
||||
// return e.SetResult(DataProvider.GetChilds<Property>(character, HierarchyProperties.PropPropCats));
|
||||
// }
|
||||
// PropertyCategory
|
||||
else if (e.Is(DataManagerActions.Getter.PropertyCategory))
|
||||
{
|
||||
// Get
|
||||
if (e.Is(DataManagerActionType.Get))
|
||||
{
|
||||
if (e.Is(UserType.User) && e.GetObject(out Character? character))
|
||||
return e.SetResult(DataProvider.GetChilds<PropertyCategory>(character, HierarchyProperties.PropPropCats));
|
||||
}
|
||||
|
||||
// // Create
|
||||
// if (e.Is(DataManagerActionType.Set))
|
||||
// {
|
||||
// if (e.Is(UserType.User) && e.GetObject(out Character? character))
|
||||
// return e.SetResult(CreateProperty(character));
|
||||
// }
|
||||
//}
|
||||
// Create
|
||||
if (e.Is(DataManagerActionType.Set))
|
||||
{
|
||||
if (e.Is(UserType.User) && e.GetObject(out Character? character))
|
||||
return e.SetResult(CreatePropertyCategory(e.CurrentUser, character));
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -154,7 +155,8 @@ public class DefaultDataManager : IDataManager
|
||||
if (!e.Is(DataManagerActions.Save))
|
||||
return false;
|
||||
|
||||
// ...
|
||||
if (e.Is(UserType.User) && e.GetObject(out OwnCharObject? obj))
|
||||
DataProvider.Save(obj);
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -165,22 +167,29 @@ public class DefaultDataManager : IDataManager
|
||||
return false;
|
||||
|
||||
// Character
|
||||
if (e.Object is Character character)
|
||||
if (e.GetObject(out Character? character))
|
||||
return DeleteCharacter(e.CurrentUser, character);
|
||||
|
||||
// Group
|
||||
if (e.Object is Group group)
|
||||
if (e.GetObject(out Group? group))
|
||||
return DeleteGroup(e.CurrentUser, group);
|
||||
|
||||
// User
|
||||
if (e.Object is UserAccount userAccount)
|
||||
if (e.GetObject(out UserAccount? userAccount))
|
||||
return DeleteUserAccount(userAccount);
|
||||
|
||||
// Property
|
||||
if (e.Object is Property property)
|
||||
if (e.GetObject(out Property? property))
|
||||
{
|
||||
if (e.GetParam(0, out Character? paramChar))
|
||||
return DeleteProperty(e.CurrentUser, paramChar, property);
|
||||
return DeleteIfOwnerOrAdmin(e.CurrentUser, paramChar, property);
|
||||
}
|
||||
|
||||
// Property
|
||||
if (e.GetObject(out PropertyCategory? propertyCategory))
|
||||
{
|
||||
if (e.GetParam(0, out Character? paramChar))
|
||||
return DeleteIfOwnerOrAdmin(e.CurrentUser, paramChar, propertyCategory);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -289,6 +298,13 @@ public class DefaultDataManager : IDataManager
|
||||
return result;
|
||||
}
|
||||
|
||||
protected virtual bool DeleteIfOwnerOrAdmin<T>(UserAccount account, Character character, T property) where T : OwnCharObject
|
||||
{
|
||||
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetChild<UserProfile>(character, HierarchyProperties.PropOwner) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
|
||||
return false;
|
||||
return DataProvider.Delete(property);
|
||||
}
|
||||
|
||||
protected virtual Property? CreateProperty(UserAccount account, Character character)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(character, nameof(character));
|
||||
@@ -304,11 +320,19 @@ public class DefaultDataManager : IDataManager
|
||||
return prop;
|
||||
}
|
||||
|
||||
protected virtual bool DeleteProperty(UserAccount account, Character character, Property property)
|
||||
protected virtual PropertyCategory? CreatePropertyCategory(UserAccount account, Character character)
|
||||
{
|
||||
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetChild<UserProfile>(character, HierarchyProperties.PropOwner) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
|
||||
return false;
|
||||
return DataProvider.Delete(property);
|
||||
ArgumentNullException.ThrowIfNull(character, nameof(character));
|
||||
|
||||
if (!account.HasPermission(UserType.User))
|
||||
return null;
|
||||
|
||||
var cat = DataProvider.Create<PropertyCategory>();
|
||||
ArgumentNullException.ThrowIfNull(cat, nameof(cat));
|
||||
|
||||
DataProvider.SetChild(character, cat, HierarchyProperties.PropPropCats, DataProviderSetAction.Set);
|
||||
|
||||
return cat;
|
||||
}
|
||||
|
||||
protected virtual UserAccount? CreateUserAccount(string username, string password)
|
||||
|
||||
@@ -163,7 +163,7 @@ public class JsonFileDataProvider : IDataProvider
|
||||
{
|
||||
var t = typeof(T);
|
||||
|
||||
static IQueryable<T>? asList(IEnumerable list) => list as IQueryable<T>;
|
||||
static IQueryable<T>? asList(IEnumerable list) => list.AsQueryable() as IQueryable<T>;
|
||||
|
||||
if (t == typeof(Character))
|
||||
return asList(JsonFile.Characters);
|
||||
@@ -233,7 +233,34 @@ public class JsonFileDataProvider : IDataProvider
|
||||
}
|
||||
|
||||
// Members
|
||||
// ...
|
||||
if (property == HierarchyProperties.PropMembers)
|
||||
{
|
||||
// Clear
|
||||
if (child is null && action == DataProviderSetAction.Remove)
|
||||
{
|
||||
parentGroup.Members.Clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Add/Remove
|
||||
else if (child is JsonUserProfile childProfile)
|
||||
{
|
||||
// Remove
|
||||
if (action == DataProviderSetAction.Remove)
|
||||
{
|
||||
parentGroup.Members.Remove(childProfile);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Add
|
||||
else
|
||||
{
|
||||
if (!parentGroup.Members.Contains(childProfile))
|
||||
parentGroup.Members.Add(childProfile);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Owner
|
||||
if (property == HierarchyProperties.PropOwner)
|
||||
@@ -375,7 +402,7 @@ public class JsonFileDataProvider : IDataProvider
|
||||
|
||||
public IQueryable<T>? GetChilds<T>(OwnCharObject parent, string property) where T : OwnCharObject
|
||||
{
|
||||
static IQueryable<T>? asList(IEnumerable list) => list as IQueryable<T>;
|
||||
static IQueryable<T>? asList(IEnumerable list) => list.AsQueryable() as IQueryable<T>;
|
||||
|
||||
// Group
|
||||
if (parent is JsonGroup parentGroup)
|
||||
|
||||
Reference in New Issue
Block a user