Compare commits

...

31 Commits

Author SHA1 Message Date
Schedel Pascal
a721b5ab6c and even more 2024-07-09 10:57:20 +02:00
Schedel Pascal
bd0053cc03 some fixes 2024-07-09 10:23:05 +02:00
Schedel Pascal
9e77090bd4 completely rework IDataProvider
- make it more dynamic
- prevent incremental method count increase
2024-07-09 10:13:38 +02:00
Schedel Pascal
f1deeeda6c work 2024-07-08 15:19:00 +02:00
Schedel Pascal
342c6dfcb2 more extension methods 2024-07-08 09:38:35 +02:00
Schedel Pascal
cd32786115 minor renames & updates 2024-07-05 11:56:55 +02:00
Schedel Pascal
444bc0b44f urg 2024-07-03 08:01:01 +02:00
2e9acf1578 asdjf 2024-06-30 18:33:23 +02:00
6a48628fd8 d 2024-06-30 18:32:07 +02:00
2f1e9716fe big big update 2024-06-30 18:19:00 +02:00
Zoe Fenris
b592dde7bb add owner-property to character 2024-06-21 15:10:41 +02:00
Schedel Pascal
e381f99b5a simpler nullcheck 2024-06-17 08:38:10 +02:00
Schedel Pascal
7ab05d41f0 fix null warning in CheckLogin() 2024-06-17 08:31:51 +02:00
Schedel Pascal
48b8faed4e simplyfy null checks 2024-06-17 08:30:40 +02:00
Schedel Pascal
d3b935bebc null checks 2024-06-17 08:27:18 +02:00
c453434c58 d 2024-06-13 22:03:52 +02:00
16038c9e6a add missing h 2024-06-13 22:01:01 +02:00
a17a21b12c save on logout & logout on close 2024-06-13 21:43:15 +02:00
089d6b466f character & group management 2024-06-11 22:06:56 +02:00
ca8cee9189 finish get-character implementation 2024-06-11 14:13:04 +02:00
60b38e5434 add fandom & fix initializeing database 2024-06-11 07:37:42 +02:00
6fd51f4b7a code cleanup 2024-06-11 06:59:34 +02:00
ff4c352dee fix json file not exists case 2024-06-10 21:33:10 +02:00
ef808ac62b UserProfile, NOT Character 2024-06-09 17:23:52 +02:00
d68ba85262 add methods GetCharacters() (not finished) 2024-06-09 17:17:26 +02:00
270a01791c implement some missing method content 2024-05-28 18:23:30 +02:00
69b3406c4b noice 2024-05-28 15:21:49 +02:00
f8d4fda4f6 fix prev commit 2024-05-28 15:18:47 +02:00
991987bb6d yea boy, now I got it! 2024-05-28 15:18:27 +02:00
b08041204d some work 2024-05-27 22:07:25 +02:00
b2f0a80872 add UserManagement.GetUserProfile() 2024-05-20 18:49:42 +02:00
46 changed files with 1540 additions and 313 deletions

View File

@@ -0,0 +1,5 @@
namespace OwnChar.Api.Exceptions;
public class LoginException(string message) : Exception(message)
{
}

View File

@@ -0,0 +1,12 @@
using OwnChar.Model;
namespace OwnChar.Api;
public interface ICharacterManager
{
Character? CreateCharacter(string? name);
Character? CreateCharacter(string? name, Group? destination);
bool DeleteCharacter(Character? character);
IQueryable<Character>? GetCharacters(Group? group);
IQueryable<Character>? GetCharacters(UserProfile? profile);
UserProfile? GetOwner(Character? character);
}

View File

@@ -0,0 +1,14 @@
using OwnChar.Model;
namespace OwnChar.Api;
public interface IGroupsManager
{
IQueryable<Group>? GetGroups(UserProfile? profile);
IQueryable<Group>? GetGroups();
bool AddMember(UserProfile? profile, Group? group);
Group? CreateGroup(string? name);
bool DeleteGroup(Group? group);
bool RemoveMember(UserProfile? profile, Group? group);
IQueryable<UserProfile>? GetMembers(Group? group);
UserProfile? GetOwner(Group? group);
}

View File

@@ -0,0 +1,18 @@
using OwnChar.Data;
using OwnChar.Model;
using Pilz.Cryptography;
namespace OwnChar.Api;
public interface IOwnCharManager
{
IDataManager? DataManager { get; set; }
UserAccount? CurrentUser { get; }
bool IsLoggedIn { get; }
IUserManager Users { get; }
IGroupsManager Groups { get; }
ICharacterManager Characters { get; }
bool Login(IDataManager? proxy, string? username, SecureString? password);
bool Logout();
}

View File

@@ -0,0 +1,11 @@
using OwnChar.Model;
using Pilz.Cryptography;
namespace OwnChar.Api;
public interface IUserManager
{
IQueryable<UserAccount>? GetUserAccounts();
UserAccount? CreateUserAccount(string? username, SecureString? password);
bool DeleteUserAccount(UserAccount? account);
UserProfile? GetOwnUserProfile();
}

View File

@@ -1,52 +0,0 @@
using OwnChar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OwnChar.Data
{
public class ClientServerDataProvider : IDataProvider
{
public bool Save(Character character)
{
}
public bool Save(UserProfile profile)
{
}
public bool Save(UserAccount account)
{
}
public bool Save(Group group)
{
}
public bool Save(PropertyCategory category)
{
}
public bool Delete(Character character)
{
}
public bool Delete(UserProfile profile)
{
}
public bool Delete(UserAccount account)
{
}
public bool Delete(Group group)
{
}
public bool Delete(PropertyCategory category)
{
}
}
}

View File

@@ -0,0 +1,57 @@
namespace OwnChar.Data;
public class DataManagerAction(string id)
{
public DataManagerAction? BaseAction { get; }
public string ActionId
{
get
{
if (BaseAction is not null)
return $"{BaseAction.ActionId}.{id}";
return id;
}
}
public DataManagerAction(DataManagerAction baseAction, string id) : this(id)
{
BaseAction = baseAction;
}
public static bool operator ==(DataManagerAction? a, DataManagerAction? b)
{
if (a is null || b is null)
return false;
if (a.ActionId == b.ActionId)
return true;
if (a.BaseAction is not null && a.BaseAction.ActionId == b.ActionId)
return true;
if (b.BaseAction is not null && a.ActionId == b.BaseAction.ActionId)
return true;
return false;
}
public static bool operator !=(DataManagerAction? a, DataManagerAction? b)
{
return !(a == b);
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj))
return true;
if (obj is not DataManagerAction action)
return false;
return action == this;
}
public override int GetHashCode()
{
return ActionId.GetHashCode();
}
}

View File

@@ -0,0 +1,6 @@
namespace OwnChar.Data;
public record class DataManagerActionResult(bool HasSuccess, object? Result)
{
public static DataManagerActionResult NonSuccess { get; } = new(false, null);
}

View File

@@ -0,0 +1,9 @@
namespace OwnChar.Data;
public enum DataManagerActionType
{
Default,
Get,
Set,
Delete,
}

View File

@@ -0,0 +1,25 @@
namespace OwnChar.Data;
public static class DataManagerActions
{
public static DataManagerAction Get { get; } = new("get");
public static DataManagerAction Save { get; } = new("save");
public static DataManagerAction Delete { get; } = new("delete");
public static DataManagerAction Associate { get; } = new("associate");
public static class Getter
{
public static DataManagerAction UserAccount { get; } = new(Get, "useraccount");
public static DataManagerAction Group { get; } = new(Get, "group");
public static DataManagerAction Character { get; } = new(Get, "character");
public static DataManagerAction Property { get; } = new(Get, "property");
public static DataManagerAction PropertyCategory { get; } = new(Get, "propertycategory");
}
public static class Association
{
public static DataManagerAction Owner { get; } = new(Associate, "owner");
public static DataManagerAction Profile { get; } = new(Associate, "profile");
public static DataManagerAction Members { get; } = new(Associate, "members");
}
}

View File

@@ -0,0 +1,7 @@
namespace OwnChar.Data;
public enum DataProviderSetAction
{
Set,
Remove,
}

View File

@@ -0,0 +1,12 @@
namespace OwnChar.Data;
public static class HierarchyProperties
{
public static string PropProps => "Props";
public static string PropPropCats => "PropCats";
public static string PropCharacters => "Characters";
public static string PropProfile => "Profile";
public static string PropOwner => "Owner";
public static string PropMembers => "Members";
public static string PropGroups => "Groups";
}

View File

@@ -0,0 +1,19 @@
using OwnChar.Model;
namespace OwnChar.Data;
public interface IDataManager
{
public delegate void OnActionEventHandler(object sender, OnActionEventArgs e);
public delegate void OnCallbackEventHandler(object sender, OnCallbackEventArgs e);
event OnActionEventHandler? OnAction;
event OnCallbackEventHandler? OnCallback;
// Login
UserAccount? Login(string username, string password);
bool Logout(UserAccount? account);
// Action
DataManagerActionResult ExecuteAction(DataManagerAction action, DataManagerActionType actionType, UserAccount currentUser, object? obj, params object?[] parameters);
}

View File

@@ -1,12 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OwnChar.Model;
namespace OwnChar.Data
namespace OwnChar.Data;
public interface IDataProvider
{
public interface IDataProvider
{
}
bool IsInitialized();
void SetInitialized();
bool SaveDatabase();
// Model
T? Create<T>() where T : OwnCharObject;
bool Save<T>(T obj) where T : OwnCharObject;
bool Delete<T>(T obj) where T : OwnCharObject;
IQueryable<T>? GetAll<T>() where T : OwnCharObject;
// Hierarchy
bool SetChild(OwnCharObject parent, OwnCharObject? child, string property, DataProviderSetAction action);
T? GetChild<T>(OwnCharObject parent, string property) where T : OwnCharObject;
IQueryable<T>? GetChilds<T>(OwnCharObject parent, string property) where T : OwnCharObject;
T? GetParent<T>(OwnCharObject child, string property) where T : OwnCharObject;
// Explicite getters
UserAccount? GetUserAccount(string username, string password);
UserProfile? GetUserProfile(string username);
}

View File

@@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OwnChar.Data
{
public class JsonFileDataProvider : IDataProvider
{
}
}

View File

@@ -0,0 +1,416 @@
using OwnChar.Manager.Modules;
using OwnChar.Model;
using System.Reflection.Metadata;
namespace OwnChar.Data.Managers;
public class DefaultDataManager : IDataManager
{
public event IDataManager.OnActionEventHandler? OnAction;
public event IDataManager.OnCallbackEventHandler? OnCallback;
private const string defaultUsername = "admin";
private const string defaultPassword = "admin";
public IDataProvider DataProvider { get; }
public DefaultDataManager(IDataProvider dataProvider)
{
DataProvider = dataProvider;
Initialize(false);
}
public virtual DataManagerActionResult ExecuteAction(DataManagerAction action, DataManagerActionType actionType, UserAccount currentUser, object? obj, params object?[] parameters)
{
var success = false;
var e = new OnActionEventArgs(action, actionType, currentUser, obj, parameters);
OnAction?.Invoke(this, e);
if (e.IsHandled)
return new(true, e.Result);
if (HandleGet(e)
|| HandleDelete(e)
|| HandleSave(e)
|| HandleAssociation(e))
success = true;
OnCallback?.Invoke(this, new(action, actionType, success, e.Result));
return new(success, e.Result);
}
protected virtual bool HandleGet(OnActionEventArgs e)
{
if (!e.Is(DataManagerActions.Get))
return false;
// Character
if (e.Is(DataManagerActions.Getter.Character))
{
// Get
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.User))
{
if (e.GetObject(out Group? group))
return e.SetResult(DataProvider.GetChilds<Character>(group, HierarchyProperties.PropCharacters));
if (e.GetObject(out UserProfile? profile))
return e.SetResult(DataProvider.GetChilds<Character>(profile, HierarchyProperties.PropCharacters));
}
if (e.Is(UserType.Admin))
return e.SetResult(DataProvider.GetAll<Character>());
}
// Create
else if (e.Is(DataManagerActionType.Set))
{
if (e.GetObject(out Group? group) && e.GetParam(0, out string? name))
return e.SetResult(CreateCharacter(e.CurrentUser, name, group));
}
}
// Group
else if (e.Is(DataManagerActions.Getter.Group))
{
// Get
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.User))
{
if (e.GetObject(out UserProfile? profile))
return e.SetResult(DataProvider.GetChilds<Group>(profile, HierarchyProperties.PropGroups));
}
if (e.Is(UserType.Admin))
return e.SetResult(DataProvider.GetAll<Group>());
}
// Create
else if (e.Is(DataManagerActionType.Set))
{
if (e.GetParam(0, out string? name))
return e.SetResult(CreateGroup(e.CurrentUser, name));
}
}
// User
else if (e.Is(DataManagerActions.Getter.UserAccount))
{
// Get
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.Admin))
return e.SetResult(DataProvider.GetAll<UserAccount>());
}
// 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));
}
}
// Property
else if (e.Is(DataManagerActions.Getter.Property))
{
// Get
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.User) && e.GetObject(out Character? character))
return e.SetResult(DataProvider.GetChilds<Property>(character, HierarchyProperties.PropProps));
}
// Create
if (e.Is(DataManagerActionType.Set))
{
if (e.Is(UserType.User) && e.GetObject(out Character? character))
return e.SetResult(CreateProperty(e.CurrentUser, character));
}
}
// 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(CreatePropertyCategory(e.CurrentUser, character));
}
}
return false;
}
protected virtual bool HandleSave(OnActionEventArgs e)
{
if (!e.Is(DataManagerActions.Save))
return false;
if (e.Is(UserType.User) && e.GetObject(out OwnCharObject? obj))
DataProvider.Save(obj);
return false;
}
protected virtual bool HandleDelete(OnActionEventArgs e)
{
if (!e.Is(DataManagerActions.Delete))
return false;
// Character
if (e.GetObject(out Character? character))
return DeleteCharacter(e.CurrentUser, character);
// Group
if (e.GetObject(out Group? group))
return DeleteGroup(e.CurrentUser, group);
// User
if (e.GetObject(out UserAccount? userAccount))
return DeleteUserAccount(userAccount);
// Property
if (e.GetObject(out Property? property))
{
if (e.GetParam(0, out Character? paramChar))
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;
}
protected virtual bool HandleAssociation(OnActionEventArgs e)
{
if (!e.Is(DataManagerActions.Associate))
return false;
if (e.Is(DataManagerActions.Association.Profile))
{
if (e.GetObject(out UserAccount? account))
{
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.Admin) || (e.Is(UserType.User) && e.CurrentUser == account))
return e.SetResult(DataProvider.GetChild<UserProfile>(account, HierarchyProperties.PropProfile));
}
}
}
else if (e.Is(DataManagerActions.Association.Owner))
{
if (e.GetObject(out Group? group))
{
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.User))
return e.SetResultT(DataProvider.GetChild<UserProfile>(group, HierarchyProperties.PropOwner));
}
else if (e.Is(DataManagerActionType.Set))
{
if (e.Is(UserType.User) && e.GetParam(0, out UserProfile? profile))
return DataProvider.SetChild(group, profile, HierarchyProperties.PropOwner, DataProviderSetAction.Set);
}
}
if (e.GetObject(out Character? character))
{
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.User))
return e.SetResultT(DataProvider.GetChild<UserProfile>(character, HierarchyProperties.PropOwner));
}
else if (e.Is(DataManagerActionType.Set))
{
if (e.Is(UserType.User) && e.GetParam(0, out UserProfile? profile))
return DataProvider.SetChild(character, profile, HierarchyProperties.PropOwner, DataProviderSetAction.Set);
}
}
}
return false;
}
public IEnumerable<UserProfile>? GetMembers(UserAccount account, Group group)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetChilds<UserProfile>(group, HierarchyProperties.PropMembers);
}
public UserProfile? GetOwner(UserAccount account, Group group)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetChild<UserProfile>(group, HierarchyProperties.PropOwner);
}
public UserProfile? GetOwner(UserAccount account, Character character)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetChild<UserProfile>(character, HierarchyProperties.PropOwner);
}
public UserProfile? GetUserProfile(UserAccount account)
{
ArgumentException.ThrowIfNullOrWhiteSpace(account.Username, nameof(account.Username));
return DataProvider.GetUserProfile(account.Username);
}
public UserAccount? Login(string username, string password)
{
return DataProvider.GetUserAccount(username, password);
}
public bool Logout(UserAccount? account)
{
if (account != null && account.HasPermission(UserType.User))
DataProvider.SaveDatabase();
return true;
}
public bool Initialize(bool force)
{
var result = false;
if (force || !DataProvider.IsInitialized())
{
result = CreateUserAccount(defaultUsername, Utils.HashPassword(defaultUsername, defaultPassword)) != null;
DataProvider.SetInitialized();
}
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));
if (!account.HasPermission(UserType.User))
return null;
var prop = DataProvider.Create<Property>();
ArgumentNullException.ThrowIfNull(prop, nameof(prop));
DataProvider.SetChild(character, prop, HierarchyProperties.PropProps, DataProviderSetAction.Set);
return prop;
}
protected virtual PropertyCategory? CreatePropertyCategory(UserAccount account, Character character)
{
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)
{
var account = DataProvider.Create<UserAccount>();
var profile = DataProvider.Create<UserProfile>();
var group = DataProvider.Create<Group>();
ArgumentNullException.ThrowIfNull(account, nameof(account));
ArgumentNullException.ThrowIfNull(profile, nameof(profile));
ArgumentNullException.ThrowIfNull(group, nameof(group));
account.Username = username;
account.Password = password;
profile.Name = username;
DataProvider.SetChild(account, profile, HierarchyProperties.PropProfile, DataProviderSetAction.Set);
group.IsInternal = true;
DataProvider.SetChild(group, profile, HierarchyProperties.PropOwner, DataProviderSetAction.Set);
DataProvider.Save(account);
DataProvider.Save(profile);
DataProvider.Save(group);
return account;
}
protected virtual bool DeleteUserAccount(UserAccount account)
{
if (!string.IsNullOrWhiteSpace(account.Username) && DataProvider.GetUserProfile(account.Username) is UserProfile userProfile)
userProfile.Name = "Deleted user";
DataProvider.Delete(account);
return true;
}
protected virtual 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.SetChild(group, profile, HierarchyProperties.PropOwner, DataProviderSetAction.Set);
return group;
}
protected virtual bool DeleteGroup(UserAccount account, Group group)
{
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetChild<UserProfile>(group, HierarchyProperties.PropOwner) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
return false;
return DataProvider.Delete(group);
}
protected virtual 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.SetChild(character, profile, HierarchyProperties.PropOwner, DataProviderSetAction.Set);
if (group != null)
DataProvider.SetChild(group, character, HierarchyProperties.PropCharacters, DataProviderSetAction.Set);
return character;
}
protected virtual bool DeleteCharacter(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(character);
}
}

View File

@@ -0,0 +1,5 @@
namespace OwnChar.Data.Managers;
public class HttpClientDataManager
{
}

View File

@@ -0,0 +1,14 @@
using OwnChar.Model;
namespace OwnChar.Data;
public class OnActionEventArgs(DataManagerAction action, DataManagerActionType actionType, UserAccount currentUser, object? obj, object?[] parameters) : EventArgs
{
public DataManagerAction Action { get; } = action;
public DataManagerActionType ActionType { get; } = actionType;
public UserAccount CurrentUser { get; } = currentUser;
public object? Object { get; } = obj;
public object?[] Parameters { get; } = parameters;
public bool IsHandled { get; set; }
public object? Result { get; set; }
}

View File

@@ -0,0 +1,12 @@
using OwnChar.Model;
using System;
namespace OwnChar.Data;
public class OnCallbackEventArgs(DataManagerAction action, DataManagerActionType actionType, bool success, object? result) : EventArgs
{
public DataManagerAction Action { get; } = action;
public DataManagerActionType ActionType { get; } = actionType;
public bool Success { get; } = success;
public object? Result { get; } = result;
}

View File

@@ -0,0 +1,11 @@
using OwnChar.Data.Providers.JsonFile.Model;
namespace OwnChar.Data.Providers.JsonFile;
public class JsonFile
{
public bool IsInitialized { get; set; }
public List<JsonUserAccount> UserAccounts { get; } = [];
public List<JsonCharacter> Characters { get; } = [];
public List<JsonGroup> Groups { get; } = [];
}

View File

@@ -0,0 +1,457 @@
using Newtonsoft.Json;
using OwnChar.Data.Providers.JsonFile.Model;
using OwnChar.Model;
using System.Collections;
using System.Linq;
namespace OwnChar.Data.Providers.JsonFile;
public class JsonFileDataProvider : IDataProvider
{
protected readonly Random random = new();
public JsonFile JsonFile { get; protected set; }
public string JsonFilePath { get; protected set; }
public JsonFileDataProvider(string filePath)
{
JsonFilePath = filePath;
LoadFile();
// Get rid of bad compiler warnings
if (JsonFile == null)
throw new Exception("Something went incredible wrong at initialization of the JsonFile.");
}
protected void LoadFile()
{
if (File.Exists(JsonFilePath) && JsonConvert.DeserializeObject<JsonFile>(File.ReadAllText(JsonFilePath), CreateJsonSerializerSettings()) is JsonFile jsonFile)
JsonFile = jsonFile;
JsonFile ??= new();
}
protected void SaveFile()
{
File.WriteAllText(JsonFilePath, JsonConvert.SerializeObject(JsonFile, CreateJsonSerializerSettings()));
}
protected int GenerateNewObjectId()
{
return random.Next();
}
protected virtual JsonSerializerSettings CreateJsonSerializerSettings()
{
return new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.All,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
TypeNameHandling = TypeNameHandling.Auto,
Formatting = Formatting.Indented,
};
}
public bool IsInitialized()
{
return JsonFile.IsInitialized;
}
public void SetInitialized()
{
JsonFile.IsInitialized = true;
}
public bool SaveDatabase()
{
SaveFile();
return true;
}
public T? Create<T>() where T : OwnCharObject
{
var t = typeof(T);
OwnCharObject? obj;
if (t == typeof(Property))
obj = new JsonProp();
else if (t == typeof(PropertyCategory))
obj = new JsonPropCat();
else if (t == typeof(Character))
obj = new JsonCharacter();
else if (t == typeof(Group))
obj = new JsonGroup();
else if (t == typeof(UserAccount))
obj = new JsonUserAccount();
else if (t == typeof(UserProfile))
obj = new JsonUserProfile();
else
obj = null;
if (obj is T objT)
{
objT.Id = GenerateNewObjectId();
return objT;
}
return null;
}
public bool Save<T>(T obj) where T : OwnCharObject
{
if (obj is JsonCharacter character)
{
if (!JsonFile.Characters.Contains(character))
JsonFile.Characters.Add(character);
}
else if (obj is JsonGroup group)
{
if (!JsonFile.Groups.Contains(group))
JsonFile.Groups.Add(group);
}
else if (obj is JsonUserAccount account)
{
if (!JsonFile.UserAccounts.Contains(account))
JsonFile.UserAccounts.Add(account);
}
return true;
}
public bool Delete<T>(T obj) where T : OwnCharObject
{
if (obj is JsonCharacter character)
{
JsonFile.Groups.ForEach(n => n.Characters.Remove(character));
return true;
}
else if (obj is JsonProp prop)
{
JsonFile.Characters.ForEach(n => n.Properties.Remove(prop));
return true;
}
else if (obj is JsonPropCat propCat)
{
JsonFile.Characters.ForEach(n => n.Properties.ForEach(m =>
{
if (m.Category == propCat)
m.Category = null;
}));
JsonFile.Characters.ForEach(n => n.PropertyCategories.Remove(propCat));
return true;
}
else if (obj is JsonGroup group)
{
JsonFile.Groups.Remove(group);
return true;
}
else if (obj is JsonUserAccount account)
{
JsonFile.UserAccounts.Remove(account);
return true;
}
else if (obj is JsonUserProfile profile)
{
// We don't delete profiles at the moment!
profile.Name = "Deleted user";
return true;
}
return false;
}
public IQueryable<T>? GetAll<T>() where T : OwnCharObject
{
var t = typeof(T);
static IQueryable<T>? asList(IEnumerable list) => list.AsQueryable() as IQueryable<T>;
if (t == typeof(Character))
return asList(JsonFile.Characters);
else if (t == typeof(Group))
return asList(JsonFile.Groups);
else if (t == typeof(UserAccount))
return asList(JsonFile.UserAccounts);
return null;
}
public bool SetChild(OwnCharObject parent, OwnCharObject? child, string property, DataProviderSetAction action)
{
// UserAccount
if (parent is JsonUserAccount parentAccount)
{
// User profil
if (property == HierarchyProperties.PropProfile)
{
// Remove
if (child is null || action == DataProviderSetAction.Remove)
{
parentAccount.Profile = null;
return true;
}
// Set
else if (child is JsonUserProfile childProfile)
{
parentAccount.Profile = childProfile;
return true;
}
}
}
// Group
if (parent is JsonGroup parentGroup)
{
// Characters
if (property == HierarchyProperties.PropCharacters)
{
// Clear
if (child is null && action == DataProviderSetAction.Remove)
{
parentGroup.Characters.Clear();
return true;
}
// Add/Remove
else if (child is JsonCharacter childCharacter)
{
// Remove
if (action == DataProviderSetAction.Remove)
{
parentGroup.Characters.Remove(childCharacter);
return true;
}
// Add
else
{
if (!parentGroup.Characters.Contains(childCharacter))
parentGroup.Characters.Add(childCharacter);
return true;
}
}
}
// 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)
{
// Remove
if (child is null || action == DataProviderSetAction.Remove)
{
parentGroup.Owner = null;
return true;
}
// Set
else if (child is JsonUserProfile childProfile)
{
parentGroup.Owner = childProfile;
return true;
}
}
}
// Character
if (parent is JsonCharacter parentCharacter)
{
// Properties
if (property == HierarchyProperties.PropProps)
{
// Clear
if (child is null && action == DataProviderSetAction.Remove)
{
parentCharacter.Properties.Clear();
return true;
}
// Add/Remove
else if (child is JsonProp childProperty)
{
// Remove
if (action == DataProviderSetAction.Remove)
{
parentCharacter.Properties.Remove(childProperty);
return true;
}
// Add
else
{
if (!parentCharacter.Properties.Contains(childProperty))
parentCharacter.Properties.Add(childProperty);
return true;
}
}
}
// Property categories
else if (property == HierarchyProperties.PropPropCats)
{
// Clear
if (child is null && action == DataProviderSetAction.Remove)
{
parentCharacter.PropertyCategories.Clear();
return true;
}
// Add/Remove
else if (child is JsonPropCat childPropertyCategory)
{
// Remove
if (action == DataProviderSetAction.Remove)
{
parentCharacter.PropertyCategories.Remove(childPropertyCategory);
return true;
}
// Add
else
{
if (!parentCharacter.PropertyCategories.Contains(childPropertyCategory))
parentCharacter.PropertyCategories.Add(childPropertyCategory);
return true;
}
}
}
// Owner
else if (property == HierarchyProperties.PropOwner)
{
// Remove
if (child is null || action == DataProviderSetAction.Remove)
{
parentCharacter.Owner = null;
return true;
}
// Set
else if (child is JsonUserProfile childProfile)
{
parentCharacter.Owner = childProfile;
return true;
}
}
}
return false;
}
public T? GetParent<T>(OwnCharObject child, string property) where T : OwnCharObject
{
throw new NotImplementedException("Not yet implemented as not needed.");
}
public T? GetChild<T>(OwnCharObject parent, string property) where T : OwnCharObject
{
// UserAccount
if (parent is JsonUserAccount parentAccount)
{
// User profile
if (property == HierarchyProperties.PropProfile)
return parentAccount.Profile as T;
}
// Group
else if (parent is JsonGroup parentGroup)
{
// Owner
if (property == HierarchyProperties.PropCharacters)
return parentGroup.Owner as T;
}
// Character
else if (parent is JsonCharacter parentCharacter)
{
// Owner
if (property == HierarchyProperties.PropOwner)
return parentCharacter.Owner as T;
}
return null;
}
public IQueryable<T>? GetChilds<T>(OwnCharObject parent, string property) where T : OwnCharObject
{
static IQueryable<T>? asList(IEnumerable list) => list.AsQueryable() as IQueryable<T>;
// Group
if (parent is JsonGroup parentGroup)
{
// Characters
if (property == HierarchyProperties.PropCharacters)
return asList(parentGroup.Characters);
// Members
if (property == HierarchyProperties.PropMembers)
return asList(parentGroup.Members);
}
// Character
else if (parent is JsonCharacter parentCharacter)
{
// Properties
if (property == HierarchyProperties.PropProps)
return asList(parentCharacter.Properties);
// Property categories
if (property == HierarchyProperties.PropPropCats)
return asList(parentCharacter.PropertyCategories);
}
// UserProfile
else if (parent is JsonUserProfile parentUserProfile)
{
// Characters
if (property == HierarchyProperties.PropCharacters)
return asList(JsonFile.Characters.Where(c => c.Owner == parentUserProfile));
// Groups
if (property == HierarchyProperties.PropGroups)
return asList(JsonFile.Groups.Where(g => g.Owner == parentUserProfile));
}
return null;
}
public UserAccount? GetUserAccount(string username, string password)
{
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username && n.Password == password);
}
public UserProfile? GetUserProfile(string username)
{
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username)?.Profile;
}
}

View File

@@ -0,0 +1,10 @@
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonCharacter : Character
{
public virtual JsonUserProfile? Owner { get; set; }
public virtual List<JsonProp> Properties { get; } = [];
public virtual List<JsonPropCat> PropertyCategories { get; } = [];
}

View File

@@ -0,0 +1,10 @@
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonGroup : Group
{
public virtual JsonUserProfile? Owner { get; set; }
public virtual List<JsonUserProfile> Members { get; } = [];
public virtual List<JsonCharacter> Characters { get; } = [];
}

View File

@@ -0,0 +1,8 @@
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonProp : Property
{
public virtual JsonPropCat? Category { get; set; } = null;
}

View File

@@ -0,0 +1,7 @@
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonPropCat : PropertyCategory
{
}

View File

@@ -0,0 +1,13 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonUserAccount : UserAccount
{
public virtual JsonUserProfile? Profile { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public override UserType Type { get => base.Type; set => base.Type = value; }
}

View File

@@ -0,0 +1,7 @@
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonUserProfile : UserProfile
{
}

76
OwnChar/Extensions.cs Normal file
View File

@@ -0,0 +1,76 @@
using OwnChar.Data;
using OwnChar.Model;
using System.Diagnostics.CodeAnalysis;
namespace OwnChar;
public static class Extensions
{
public static bool GetAt<T>(this object?[] @this, int index, [NotNullWhen(true)] out T? result)
{
if (@this.ElementAtOrDefault(index) is T obj)
{
result = obj;
return true;
}
result = default;
return false;
}
public static bool HasPermission(this UserAccount account, UserType permissions)
{
return account.Type >= permissions;
}
#region OnActionEventArgs
public static bool SetResult(this OnActionEventArgs @this, object? data)
{
@this.Result = data;
return @this.Result != null;
}
public static bool SetResultT(this OnActionEventArgs @this, object? data)
{
@this.Result = data;
return true;
}
public static bool GetParam<T>(this OnActionEventArgs @this, int index, [NotNullWhen(true)] out T? result)
{
return @this.Parameters.GetAt(index, out result);
}
public static bool GetObject<T>(this OnActionEventArgs @this, [NotNullWhen(true)] out T? result)
{
if (@this.Object is T t)
{
result = t;
return true;
}
result = default;
return false;
}
public static bool Is(this OnActionEventArgs @this, DataManagerActionType actionType)
{
return @this.ActionType == actionType;
}
public static bool Is(this OnActionEventArgs @this, DataManagerAction 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
}

View File

@@ -1,54 +0,0 @@
using OwnChar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OwnChar.Manager
{
public class CharacterManager(OwnCharManager manager)
{
public OwnCharManager Manager { get; } = manager;
/// <summary>
/// Gets personal characters.
/// </summary>
/// <param name="group"></param>
/// <returns>Returns a collection with personal characters.</returns>
public IEnumerable<Character>? GetCharacters()
{
if (!IsLoggedIn || UserProfile == null)
return null;
return GetCharacters(UserProfile.Group);
}
/// <summary>
/// Gets all public ore accessable (e.g. via shares) characters from the specified group.
/// </summary>
/// <param name="group"></param>
/// <returns>Returns a collection with characters for the specified group.</returns>
public IEnumerable<Character> GetCharacters(Group group)
{
}
/// <summary>
/// Saves a character. If it doesn't exists, it will be created.
/// </summary>
/// <param name="character">The caracter to save.</param>
/// <returns><see cref="true"/> if success, otherwise <see cref="false"/>.</returns>
public bool SaveCharacter(Character character)
{
}
/// <summary>
/// Deletes a given character. If it doesn't exists, it will be ignored.
/// </summary>
/// <param name="character"></param>
/// <returns><see cref="true"/> if the given character has been deleted successfully or doesn't exist, otherwise <see cref="false"/>.</returns>
public bool DeleteCharacter(Character character)
{
}
}
}

View File

@@ -1,50 +0,0 @@
using OwnChar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OwnChar.Manager
{
public class GroupsManager(OwnCharManager manager)
{
public OwnCharManager Manager { get; } = manager;
public Group? GetGroup()
{
if (!IsLoggedIn || UserProfile == null)
return null;
// ...
}
public IEnumerable<Group>? GetGroups()
{
if (!IsLoggedIn || UserProfile == null)
return null;
return GetGroups(UserProfile);
}
public IEnumerable<Group>? GetGroups(UserProfile profile)
{
if (!IsLoggedIn)
return null;
// ...
}
public Group? CreateGroup(UserProfile owner)
{
}
public bool SaveGroup(Group group)
{
}
public bool DeleteGroup(Group group)
{
}
}
}

View File

@@ -0,0 +1,54 @@
using OwnChar.Api;
using OwnChar.Data;
using OwnChar.Model;
namespace OwnChar.Manager.Modules;
public class CharacterManager(OwnCharManager manager) : OwnCharManagerModule(manager), ICharacterManager
{
public IQueryable<Character>? GetCharacters(Group? group)
{
Manager.CheckLogin();
if (group != null)
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.Character, DataManagerActionType.Get, Manager.CurrentUser, group).Result as IQueryable<Character>;
return null;
}
public IQueryable<Character>? GetCharacters(UserProfile? profile)
{
Manager.CheckLogin();
if (profile != null)
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.Character, DataManagerActionType.Get, Manager.CurrentUser, profile).Result as IQueryable<Character>;
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.ExecuteAction(DataManagerActions.Getter.PropertyCategory, DataManagerActionType.Set, Manager.CurrentUser, null, name, destination).Result as Character;
}
public bool DeleteCharacter(Character? character)
{
ArgumentNullException.ThrowIfNull(character, nameof(character));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Delete, DataManagerActionType.Default, Manager.CurrentUser, character).HasSuccess;
}
public UserProfile? GetOwner(Character? character)
{
ArgumentNullException.ThrowIfNull(character, nameof(character));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Owner, DataManagerActionType.Get, Manager.CurrentUser, character).Result as UserProfile;
}
}

View File

@@ -0,0 +1,65 @@
using OwnChar.Api;
using OwnChar.Data;
using OwnChar.Model;
namespace OwnChar.Manager.Modules;
public class GroupsManager(OwnCharManager manager) : OwnCharManagerModule(manager), IGroupsManager
{
public UserProfile? GetOwner(Group? group)
{
ArgumentNullException.ThrowIfNull(group, nameof(group));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Owner, DataManagerActionType.Get, Manager.CurrentUser, group).Result as UserProfile;
}
public IQueryable<Group>? GetGroups(UserProfile? profile)
{
ArgumentNullException.ThrowIfNull(profile, nameof(profile));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.Group, DataManagerActionType.Get, Manager.CurrentUser, profile) as IQueryable<Group>;
}
public IQueryable<Group>? GetGroups()
{
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.Group, DataManagerActionType.Get, Manager.CurrentUser, null) as IQueryable<Group>;
}
public IQueryable<UserProfile>? GetMembers(Group? group)
{
ArgumentNullException.ThrowIfNull(group, nameof(group));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Members, DataManagerActionType.Get, Manager.CurrentUser, group).Result as IQueryable<UserProfile>;
}
public bool AddMember(UserProfile? profile, Group? group)
{
ArgumentNullException.ThrowIfNull(profile, nameof(profile));
ArgumentNullException.ThrowIfNull(group, nameof(group));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Members, DataManagerActionType.Set, Manager.CurrentUser, group, profile).HasSuccess;
}
public bool RemoveMember(UserProfile? profile, Group? group)
{
ArgumentNullException.ThrowIfNull(profile, nameof(profile));
ArgumentNullException.ThrowIfNull(group, nameof(group));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Members, DataManagerActionType.Delete, Manager.CurrentUser, group, profile).HasSuccess;
}
public Group? CreateGroup(string? name)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.Group, DataManagerActionType.Set, Manager.CurrentUser, null, name).Result as Group;
}
public bool DeleteGroup(Group? group)
{
ArgumentNullException.ThrowIfNull(group, nameof(group));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Delete, DataManagerActionType.Default, Manager.CurrentUser, group).HasSuccess;
}
}

View File

@@ -0,0 +1,37 @@
using OwnChar.Api;
using OwnChar.Data;
using OwnChar.Model;
using Pilz.Cryptography;
namespace OwnChar.Manager.Modules;
public class UserManager(OwnCharManager manager) : OwnCharManagerModule(manager), IUserManager
{
public UserProfile? GetOwnUserProfile()
{
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Profile, DataManagerActionType.Get, Manager.CurrentUser, Manager.CurrentUser).Result as UserProfile;
}
public IQueryable<UserAccount>? GetUserAccounts()
{
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.UserAccount, DataManagerActionType.Get, Manager.CurrentUser, null).Result as IQueryable<UserAccount>;
}
public UserAccount? CreateUserAccount(string? username, SecureString? password)
{
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
Manager.CheckLogin();
username = username.Trim().ToLower();
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.UserAccount, DataManagerActionType.Set, Manager.CurrentUser, null, username, Utils.HashPassword(username, password)).Result as UserAccount;
}
public bool DeleteUserAccount(UserAccount? account)
{
ArgumentNullException.ThrowIfNull(account, nameof(account));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Delete, DataManagerActionType.Default, Manager.CurrentUser, account).HasSuccess;
}
}

View File

@@ -1,38 +1,64 @@
using OwnChar.Data;
using OwnChar.Api;
using OwnChar.Api.Exceptions;
using OwnChar.Data;
using OwnChar.Manager.Modules;
using OwnChar.Model;
using Pilz.Cryptography;
using System.Diagnostics.CodeAnalysis;
namespace OwnChar.Manager
namespace OwnChar.Manager;
public class OwnCharManager : IOwnCharManager
{
public class OwnCharManager
// User
public bool IsLoggedIn => CurrentUser != null;
public UserAccount? CurrentUser { get; private set; }
// Data Provider
public IDataManager? DataManager { get; set; }
// Manager
public IUserManager Users { get; }
public IGroupsManager Groups { get; }
public ICharacterManager Characters { get; }
public OwnCharManager()
{
// User
public bool IsLoggedIn { get; set; }
public UserAccount? CurrentUser { get; set; }
Users = new UserManager(this);
Groups = new GroupsManager(this);
Characters = new CharacterManager(this);
}
// Data Provider
public IDataProvider DataProvider { get; set; }
[MemberNotNull(nameof(CurrentUser), nameof(DataManager))]
internal protected void CheckLogin()
{
if (!IsLoggedIn || DataManager == null)
throw new LoginException("You are already logged in!");
}
// Manager
public UserManager Users { get; }
public GroupsManager Groups { get; }
public CharacterManager Characters { get; }
/// <summary>
/// Tries to login on the given data provider.
/// </summary>
/// <returns>Returns <see cref="true"/> if the login was successfull and <see cref="false"/> if not.</returns>
public bool Login(IDataManager? proxy, string? username, SecureString? password)
{
ArgumentNullException.ThrowIfNull(proxy, nameof(proxy));
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
public OwnCharManager(IDataProvider dataProvider)
{
DataProvider = dataProvider;
Users = new(this);
Groups = new(this);
Characters = new(this);
}
username = username.Trim().ToLower();
CurrentUser = proxy.Login(username, Utils.HashPassword(username, password));
DataManager = proxy;
/// <summary>
/// Tries to login on the server.
/// </summary>
/// <returns>Returns <see cref="true"/> if the login was successfull and <see cref="false"/> if not.</returns>
public bool Login(string username, SecureString password)
{
return IsLoggedIn = true; // TODO: Change `true` to the real check.
}
return IsLoggedIn;
}
/// <summary>
/// Closes the session on the current data provider.
/// </summary>
/// <returns></returns>
public bool Logout()
{
return DataManager?.Logout(CurrentUser) ?? true;
}
}

View File

@@ -0,0 +1,6 @@
namespace OwnChar.Manager;
public abstract class OwnCharManagerModule(OwnCharManager manager)
{
public OwnCharManager Manager { get; } = manager;
}

View File

@@ -1,52 +0,0 @@
using OwnChar.Model;
using Pilz.Cryptography;
namespace OwnChar.Manager
{
public class UserManager(OwnCharManager manager)
{
public OwnCharManager Manager { get; } = manager;
public UserAccount? CreateAccount(string username, SecureString password, string email, string displayName)
{
// Create account
// ...
// Create profile
// ...
}
/// <summary>
/// Saves the current logged in user account.
/// </summary>
/// <returns><see cref="true"/> if success, otherwise <see cref="false"/>.</returns>
public bool SaveAccount()
{
}
public bool DeleteAccount()
{
}
public UserProfile? CreateProfile()
{
// Create profile
// ...
// Create pre-defined group
// ...
}
/// <summary>
/// Saves the current logged in user profile.
/// </summary>
/// <returns><see cref="true"/> if success, otherwise <see cref="false"/>.</returns>
public bool SaveProfile()
{
}
public bool DeleteProfile()
{
}
}
}

View File

@@ -1,11 +1,7 @@
namespace OwnChar.Model
namespace OwnChar.Model;
public abstract class Character : OwnCharObject
{
public class Character(Group group, string name)
{
public ulong Id { get; set; }
public string Name { get; set; } = name;
public Group Group { get; set; } = group;
public List<Property> Properties { get; set; } = [];
public List<PropertyCategory> PropertyCategories { get; set; } = [];
}
public virtual string? Name { get; set; }
public virtual string? Fandom { get; set; }
}

View File

@@ -1,15 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OwnChar.Model;
namespace OwnChar.Model
public abstract class Group : OwnCharObject
{
public class Group(UserProfile owner)
{
public int Id { get; set; }
public UserProfile Owner { get; set; } = owner;
public List<UserProfile> Members { get; set; } = [];
}
public virtual string? Name { get; set; }
public virtual string? Fandom { get; set; }
public virtual bool IsInternal { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace OwnChar.Model;
public class OwnCharObject
{
public virtual int Id { get; set; }
}

View File

@@ -1,10 +1,7 @@
namespace OwnChar.Model
namespace OwnChar.Model;
public abstract class Property : OwnCharObject
{
public class Property(string name)
{
public ulong Id { get; set; }
public string Name { get; set; } = name;
public PropertyCategory? Category { get; set; }
public object? Value { get; set; }
}
public virtual string? Name { get; set; }
public virtual object? Value { get; set; }
}

View File

@@ -1,8 +1,6 @@
namespace OwnChar.Model
namespace OwnChar.Model;
public abstract class PropertyCategory : OwnCharObject
{
public class PropertyCategory(string name)
{
public ulong Id { get; set; }
public string Name { get; set; } = name;
}
public virtual string? Name { get; set; }
}

View File

@@ -1,17 +1,9 @@
namespace OwnChar.Model
{
public class UserAccount
{
public ulong Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
namespace OwnChar.Model;
internal UserAccount(string email, string username, string password)
{
Username = username;
Email = email;
Password = password;
}
}
public abstract class UserAccount : OwnCharObject
{
public virtual string? Username { get; set; }
public virtual string? Password { get; set; }
public virtual string? Email { get; set; }
public virtual UserType Type { get; set; }
}

View File

@@ -1,9 +1,6 @@
namespace OwnChar.Model
namespace OwnChar.Model;
public abstract class UserProfile : OwnCharObject
{
public class UserProfile(string name)
{
public ulong Id { get; set; }
public string Name { get; set; } = name;
public UserAccount? Account { get; set; }
}
public virtual string? Name { get; set; }
}

View File

@@ -0,0 +1,9 @@
namespace OwnChar.Model;
public enum UserType
{
None,
Guest,
User,
Admin
}

View File

@@ -10,4 +10,8 @@
<PackageReference Include="Pilz.Cryptography" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Data\Providers\SqlDb\" />
</ItemGroup>
</Project>

12
OwnChar/Utils.cs Normal file
View File

@@ -0,0 +1,12 @@
using Pilz.Cryptography;
namespace OwnChar;
public static class Utils
{
public static string HashPassword(string username, SecureString password)
{
// TODO: Implement a good hashing algorythmus (like MD5) BEFORE going productive.
return (username + ":" + password).GetHashCode().ToString();
}
}