Compare commits
5 Commits
b2f0a80872
...
270a01791c
| Author | SHA1 | Date | |
|---|---|---|---|
| 270a01791c | |||
| 69b3406c4b | |||
| f8d4fda4f6 | |||
| 991987bb6d | |||
| b08041204d |
@@ -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)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
20
OwnChar/Data/IDataManager.cs
Normal file
20
OwnChar/Data/IDataManager.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using OwnChar.Model;
|
||||||
|
|
||||||
|
namespace OwnChar.Data
|
||||||
|
{
|
||||||
|
public interface IDataManager
|
||||||
|
{
|
||||||
|
// Login
|
||||||
|
abstract UserAccount? Login(string username, string password);
|
||||||
|
abstract bool Logout(UserAccount? account);
|
||||||
|
|
||||||
|
// User management
|
||||||
|
abstract UserAccount? CreateUserAccount(string username, string password);
|
||||||
|
abstract UserProfile? GetUserProfile(UserAccount account);
|
||||||
|
abstract bool DeleteUserAccount(UserAccount account);
|
||||||
|
|
||||||
|
// Group management
|
||||||
|
abstract UserProfile? GetOwner(Group group);
|
||||||
|
abstract IEnumerable<UserProfile>? GetMembers(Group group);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,29 @@
|
|||||||
using System;
|
using OwnChar.Model;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace OwnChar.Data
|
namespace OwnChar.Data
|
||||||
{
|
{
|
||||||
public interface IDataProvider
|
public interface IDataProvider
|
||||||
{
|
{
|
||||||
|
// General
|
||||||
|
abstract bool IsInitialized();
|
||||||
|
|
||||||
|
// Model
|
||||||
|
abstract T? Create<T>() where T : class, IOwnCharObject;
|
||||||
|
abstract bool Save<T>(T obj) where T : class, IOwnCharObject;
|
||||||
|
abstract bool Delete<T>(T obj) where T : class, IOwnCharObject;
|
||||||
|
|
||||||
|
// Hierarchy
|
||||||
|
abstract bool SetParent(UserProfile profile, UserAccount parent);
|
||||||
|
abstract bool SetParent(Character character, Group parent);
|
||||||
|
abstract bool SetParent(Property property, Character character);
|
||||||
|
abstract bool SetOwner(Group group, UserProfile owner);
|
||||||
|
abstract bool SetOwner(Character group, UserProfile owner);
|
||||||
|
|
||||||
|
// Gets
|
||||||
|
abstract UserAccount? GetUserAccount(string username, string password);
|
||||||
|
abstract UserProfile? GetUserProfile(string username);
|
||||||
|
abstract IEnumerable<UserProfile>? GetGroupMembers(Group group);
|
||||||
|
abstract UserProfile? GetOwner(Group group);
|
||||||
|
abstract UserProfile? GetOwner(Character character);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
6
OwnChar/Data/Managers/ClientDataManager.cs
Normal file
6
OwnChar/Data/Managers/ClientDataManager.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace OwnChar.Data.Managers
|
||||||
|
{
|
||||||
|
public class ClientDataManager
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
79
OwnChar/Data/Managers/DefaultDataManager.cs
Normal file
79
OwnChar/Data/Managers/DefaultDataManager.cs
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
using OwnChar.Model;
|
||||||
|
|
||||||
|
namespace OwnChar.Data.Managers
|
||||||
|
{
|
||||||
|
public class DefaultDataManager(IDataProvider dataProvider) : IDataManager
|
||||||
|
{
|
||||||
|
private const string defaultUsername = "admin";
|
||||||
|
private const string defaultPassword = "admin";
|
||||||
|
|
||||||
|
public IDataProvider DataProvider { get; } = dataProvider;
|
||||||
|
|
||||||
|
public IEnumerable<UserProfile>? GetMembers(Group group)
|
||||||
|
{
|
||||||
|
return DataProvider.GetGroupMembers(group);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserProfile? GetOwner(Group group)
|
||||||
|
{
|
||||||
|
return DataProvider.GetOwner(group);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Initialize(bool force)
|
||||||
|
{
|
||||||
|
if (force || !DataProvider.IsInitialized())
|
||||||
|
return CreateUserAccount(defaultUsername, Utils.HashPassword(defaultUsername, defaultPassword)) != null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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.SetParent(profile, account);
|
||||||
|
|
||||||
|
group.IsInternal = true;
|
||||||
|
DataProvider.SetOwner(group, profile);
|
||||||
|
|
||||||
|
DataProvider.Save(account);
|
||||||
|
DataProvider.Save(profile);
|
||||||
|
DataProvider.Save(group);
|
||||||
|
|
||||||
|
return account;
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
OwnChar/Data/Providers/JsonFile/JsonFile.cs
Normal file
12
OwnChar/Data/Providers/JsonFile/JsonFile.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using OwnChar.Data.Providers.JsonFile.Model;
|
||||||
|
using OwnChar.Model;
|
||||||
|
|
||||||
|
namespace OwnChar.Data.Providers.JsonFile
|
||||||
|
{
|
||||||
|
public class JsonFile
|
||||||
|
{
|
||||||
|
public List<JsonUserAccount> UserAccounts { get; } = [];
|
||||||
|
public List<JsonCharacter> Characters { get; } = [];
|
||||||
|
public List<JsonGroup> Groups { get; } = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
221
OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs
Normal file
221
OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using OwnChar.Data.Providers.JsonFile.Model;
|
||||||
|
using OwnChar.Model;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
|
||||||
|
namespace OwnChar.Data.Providers.JsonFile
|
||||||
|
{
|
||||||
|
public class JsonFileDataProvider : IDataProvider
|
||||||
|
{
|
||||||
|
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 (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 JsonSerializerSettings CreateJsonSerializerSettings()
|
||||||
|
{
|
||||||
|
return new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||||
|
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
|
||||||
|
TypeNameHandling = TypeNameHandling.Auto,
|
||||||
|
Formatting = Formatting.Indented,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public T? Create<T>() where T : class, IOwnCharObject
|
||||||
|
{
|
||||||
|
var t = typeof(T);
|
||||||
|
IOwnCharObject? 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;
|
||||||
|
|
||||||
|
return obj as T;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Save<T>(T obj) where T : class, IOwnCharObject
|
||||||
|
{
|
||||||
|
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 IsInitialized()
|
||||||
|
{
|
||||||
|
return JsonFile.UserAccounts.Count != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Delete<T>(T obj) where T : class, IOwnCharObject
|
||||||
|
{
|
||||||
|
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 false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool SetParent(UserProfile profile, UserAccount parent)
|
||||||
|
{
|
||||||
|
if (parent is JsonUserAccount jaccount && profile is JsonUserProfile jprofile)
|
||||||
|
{
|
||||||
|
jaccount.Profile = jprofile;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool SetParent(Character character, Group parent)
|
||||||
|
{
|
||||||
|
if (character is JsonCharacter jcharacter && parent is JsonGroup jgroup)
|
||||||
|
{
|
||||||
|
if (!jgroup.Characters.Contains(jcharacter))
|
||||||
|
jgroup.Characters.Add(jcharacter);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool SetParent(Property property, Character character)
|
||||||
|
{
|
||||||
|
if (property is JsonProp jprop && character is JsonCharacter jcharacter)
|
||||||
|
{
|
||||||
|
if (!jcharacter.Properties.Contains(jprop))
|
||||||
|
jcharacter.Properties.Add(jprop);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool SetOwner(Group group, UserProfile owner)
|
||||||
|
{
|
||||||
|
if (group is JsonGroup jgroup && owner is JsonUserProfile jprofile)
|
||||||
|
{
|
||||||
|
jgroup.Owner = jprofile;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool SetOwner(Character character, UserProfile owner)
|
||||||
|
{
|
||||||
|
if (character is JsonCharacter jcharacter && owner is JsonUserProfile jprofile)
|
||||||
|
{
|
||||||
|
jcharacter.Owner = jprofile;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<UserProfile>? GetGroupMembers(Group group)
|
||||||
|
{
|
||||||
|
if (group is JsonGroup jgroup)
|
||||||
|
return jgroup.Members;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserProfile? GetOwner(Group group)
|
||||||
|
{
|
||||||
|
if (group is JsonGroup jgroup)
|
||||||
|
return jgroup.Owner;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserProfile? GetOwner(Character character)
|
||||||
|
{
|
||||||
|
if (character is JsonCharacter jcharacter)
|
||||||
|
return jcharacter.Owner;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs
Normal file
11
OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
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; } = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
11
OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs
Normal file
11
OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
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; } = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
9
OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs
Normal file
9
OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using OwnChar.Model;
|
||||||
|
|
||||||
|
namespace OwnChar.Data.Providers.JsonFile.Model
|
||||||
|
{
|
||||||
|
public class JsonProp : Property
|
||||||
|
{
|
||||||
|
public virtual JsonPropCat? Category { get; set; } = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
8
OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs
Normal file
8
OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using OwnChar.Model;
|
||||||
|
|
||||||
|
namespace OwnChar.Data.Providers.JsonFile.Model
|
||||||
|
{
|
||||||
|
public class JsonPropCat : PropertyCategory
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
14
OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs
Normal file
14
OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
8
OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs
Normal file
8
OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using OwnChar.Model;
|
||||||
|
|
||||||
|
namespace OwnChar.Data.Providers.JsonFile.Model
|
||||||
|
{
|
||||||
|
public class JsonUserProfile : UserProfile
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,54 +1,7 @@
|
|||||||
using OwnChar.Model;
|
namespace OwnChar.Manager
|
||||||
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 class CharacterManager(OwnCharManager manager)
|
||||||
{
|
{
|
||||||
public OwnCharManager Manager { get; } = 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)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
OwnChar/Manager/Exceptions/LoginException.cs
Normal file
6
OwnChar/Manager/Exceptions/LoginException.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace OwnChar.Manager.Exceptions
|
||||||
|
{
|
||||||
|
public class LoginException(string message) : Exception(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,4 @@
|
|||||||
using OwnChar.Model;
|
using OwnChar.Model;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace OwnChar.Manager
|
namespace OwnChar.Manager
|
||||||
{
|
{
|
||||||
@@ -11,40 +6,16 @@ namespace OwnChar.Manager
|
|||||||
{
|
{
|
||||||
public OwnCharManager Manager { get; } = manager;
|
public OwnCharManager Manager { get; } = manager;
|
||||||
|
|
||||||
public Group? GetGroup()
|
public UserProfile? GetOwner(Group? group)
|
||||||
{
|
{
|
||||||
if (!IsLoggedIn || UserProfile == null)
|
ArgumentNullException.ThrowIfNull(group, nameof(group));
|
||||||
return null;
|
return Manager.DataManager?.GetOwner(group);
|
||||||
|
|
||||||
// ...
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Group>? GetGroups()
|
public IEnumerable<UserProfile>? GetMembers(Group? group)
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(group, nameof(group));
|
||||||
|
return Manager.DataManager?.GetMembers(group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using OwnChar.Data;
|
using OwnChar.Data;
|
||||||
|
using OwnChar.Manager.Exceptions;
|
||||||
using OwnChar.Model;
|
using OwnChar.Model;
|
||||||
using Pilz.Cryptography;
|
using Pilz.Cryptography;
|
||||||
|
|
||||||
@@ -7,32 +8,53 @@ namespace OwnChar.Manager
|
|||||||
public class OwnCharManager
|
public class OwnCharManager
|
||||||
{
|
{
|
||||||
// User
|
// User
|
||||||
public bool IsLoggedIn { get; set; }
|
public bool IsLoggedIn => CurrentUser != null;
|
||||||
public UserAccount? CurrentUser { get; set; }
|
public UserAccount? CurrentUser { get; private set; }
|
||||||
|
|
||||||
// Data Provider
|
// Data Provider
|
||||||
public IDataProvider DataProvider { get; set; }
|
public IDataManager? DataManager { get; set; }
|
||||||
|
|
||||||
// Manager
|
// Manager
|
||||||
public UserManager Users { get; }
|
public UserManager Users { get; }
|
||||||
public GroupsManager Groups { get; }
|
public GroupsManager Groups { get; }
|
||||||
public CharacterManager Characters { get; }
|
public CharacterManager Characters { get; }
|
||||||
|
|
||||||
public OwnCharManager(IDataProvider dataProvider)
|
public OwnCharManager()
|
||||||
{
|
{
|
||||||
DataProvider = dataProvider;
|
|
||||||
Users = new(this);
|
Users = new(this);
|
||||||
Groups = new(this);
|
Groups = new(this);
|
||||||
Characters = new(this);
|
Characters = new(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal protected void CheckLogin()
|
||||||
|
{
|
||||||
|
if (!IsLoggedIn)
|
||||||
|
throw new LoginException("You are already logged in!");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries to login on the server.
|
/// Tries to login on the given data provider.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Returns <see cref="true"/> if the login was successfull and <see cref="false"/> if not.</returns>
|
/// <returns>Returns <see cref="true"/> if the login was successfull and <see cref="false"/> if not.</returns>
|
||||||
public bool Login(string username, SecureString password)
|
public bool Login(IDataManager? proxy, string? username, SecureString? password)
|
||||||
{
|
{
|
||||||
return IsLoggedIn = true; // TODO: Change `true` to the real check.
|
ArgumentNullException.ThrowIfNull(proxy, nameof(proxy));
|
||||||
|
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
|
||||||
|
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
|
||||||
|
|
||||||
|
username = username.Trim().ToLower();
|
||||||
|
CurrentUser = proxy.Login(username, Utils.HashPassword(username, password));
|
||||||
|
|
||||||
|
return IsLoggedIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Closes the session on the current data provider.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool Logout()
|
||||||
|
{
|
||||||
|
return DataManager?.Logout(CurrentUser) ?? true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,50 +7,24 @@ namespace OwnChar.Manager
|
|||||||
{
|
{
|
||||||
public OwnCharManager Manager { get; } = manager;
|
public OwnCharManager Manager { get; } = manager;
|
||||||
|
|
||||||
public UserProfile? GetUserProfile(UserAccount account)
|
public UserProfile? GetOwnUserProfile()
|
||||||
{
|
{
|
||||||
|
Manager.CheckLogin();
|
||||||
|
return Manager.DataManager!.GetUserProfile(Manager.CurrentUser!);
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserAccount? CreateAccount(string username, SecureString password, string email, string displayName)
|
public UserAccount? CreateUserAccount(string? username, SecureString? password)
|
||||||
{
|
{
|
||||||
// Create account
|
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
|
||||||
// ...
|
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
|
||||||
|
username = username.Trim().ToLower();
|
||||||
// Create profile
|
return Manager.DataManager?.CreateUserAccount(username, Utils.HashPassword(username, password));
|
||||||
// ...
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public bool DeleteUserAccount(UserAccount? account)
|
||||||
/// 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()
|
|
||||||
{
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(account, nameof(account));
|
||||||
|
return Manager.DataManager?.DeleteUserAccount(account) ?? false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
namespace OwnChar.Model
|
namespace OwnChar.Model
|
||||||
{
|
{
|
||||||
public class Character(Group group, string name)
|
public abstract class Character : IOwnCharObject
|
||||||
{
|
{
|
||||||
public ulong Id { get; set; }
|
public virtual string? Name { 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; } = [];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,8 @@
|
|||||||
using System;
|
namespace OwnChar.Model
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace OwnChar.Model
|
|
||||||
{
|
{
|
||||||
public class Group(UserProfile owner)
|
public abstract class Group : IOwnCharObject
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public virtual string? Name { get; set; }
|
||||||
public UserProfile Owner { get; set; } = owner;
|
public virtual bool IsInternal { get; set; }
|
||||||
public List<UserProfile> Members { get; set; } = [];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
OwnChar/Model/IOwnCharObject.cs
Normal file
6
OwnChar/Model/IOwnCharObject.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace OwnChar.Model
|
||||||
|
{
|
||||||
|
public interface IOwnCharObject
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
namespace OwnChar.Model
|
namespace OwnChar.Model
|
||||||
{
|
{
|
||||||
public class Property(string name)
|
public abstract class Property : IOwnCharObject
|
||||||
{
|
{
|
||||||
public ulong Id { get; set; }
|
public virtual string? Name { get; set; }
|
||||||
public string Name { get; set; } = name;
|
public virtual object? Value { get; set; }
|
||||||
public PropertyCategory? Category { get; set; }
|
|
||||||
public object? Value { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
namespace OwnChar.Model
|
namespace OwnChar.Model
|
||||||
{
|
{
|
||||||
public class PropertyCategory(string name)
|
public abstract class PropertyCategory : IOwnCharObject
|
||||||
{
|
{
|
||||||
public ulong Id { get; set; }
|
public virtual string? Name { get; set; }
|
||||||
public string Name { get; set; } = name;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,10 @@
|
|||||||
namespace OwnChar.Model
|
namespace OwnChar.Model
|
||||||
{
|
{
|
||||||
public class UserAccount
|
public abstract class UserAccount : IOwnCharObject
|
||||||
{
|
{
|
||||||
public ulong Id { get; set; }
|
public virtual string? Username { get; set; }
|
||||||
public string Username { get; set; }
|
public virtual string? Password { get; set; }
|
||||||
public string Email { get; set; }
|
public virtual string? Email { get; set; }
|
||||||
public string Password { get; set; }
|
public virtual UserType Type { get; set; }
|
||||||
|
|
||||||
internal UserAccount(string email, string username, string password)
|
|
||||||
{
|
|
||||||
Username = username;
|
|
||||||
Email = email;
|
|
||||||
Password = password;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
namespace OwnChar.Model
|
namespace OwnChar.Model
|
||||||
{
|
{
|
||||||
public class UserProfile(string name)
|
public abstract class UserProfile : IOwnCharObject
|
||||||
{
|
{
|
||||||
public ulong Id { get; set; }
|
public virtual string? Name { get; set; }
|
||||||
public string Name { get; set; } = name;
|
|
||||||
public UserAccount? Account { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
OwnChar/Model/UserType.cs
Normal file
9
OwnChar/Model/UserType.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace OwnChar.Model
|
||||||
|
{
|
||||||
|
public enum UserType
|
||||||
|
{
|
||||||
|
Guest,
|
||||||
|
User,
|
||||||
|
Admin
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,4 +10,8 @@
|
|||||||
<PackageReference Include="Pilz.Cryptography" Version="2.0.1" />
|
<PackageReference Include="Pilz.Cryptography" Version="2.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Data\Providers\SqlDb\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
13
OwnChar/Utils.cs
Normal file
13
OwnChar/Utils.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using Pilz.Cryptography;
|
||||||
|
|
||||||
|
namespace OwnChar
|
||||||
|
{
|
||||||
|
public static class Utils
|
||||||
|
{
|
||||||
|
public static string HashPassword(string username, SecureString password)
|
||||||
|
{
|
||||||
|
// TODO: Implement a good hasing algorythmus (like MD5) BEFORE going productive!
|
||||||
|
return (username + ":" + password).GetHashCode().ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user