Compare commits

..

1 Commits

Author SHA1 Message Date
270a01791c implement some missing method content 2024-05-28 18:23:30 +02:00
12 changed files with 160 additions and 65 deletions

View File

@@ -11,9 +11,10 @@ namespace OwnChar.Data
// 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);
abstract IEnumerable<UserProfile>? GetMembers(Group group);
}
}

View File

@@ -22,7 +22,7 @@ namespace OwnChar.Data
// Gets
abstract UserAccount? GetUserAccount(string username, string password);
abstract UserProfile? GetUserProfile(string username);
abstract IEnumerable<UserProfile> GetGroupMembers(Group group);
abstract IEnumerable<UserProfile>? GetGroupMembers(Group group);
abstract UserProfile? GetOwner(Group group);
abstract UserProfile? GetOwner(Character character);
}

View File

@@ -2,42 +2,14 @@
namespace OwnChar.Data.Managers
{
public class DefaultDataManager : IDataManager
public class DefaultDataManager(IDataProvider dataProvider) : IDataManager
{
public IDataProvider DataProvider { get; }
private const string defaultUsername = "admin";
private const string defaultPassword = "admin";
public DefaultDataManager(IDataProvider dataProvider)
{
DataProvider = dataProvider;
}
public IDataProvider DataProvider { get; } = dataProvider;
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 IEnumerable<UserProfile> GetMembers(Group group)
public IEnumerable<UserProfile>? GetMembers(Group group)
{
return DataProvider.GetGroupMembers(group);
}
@@ -66,7 +38,41 @@ namespace OwnChar.Data.Managers
public bool Initialize(bool force)
{
if (force || !DataProvider.IsInitialized())
return CreateUserAccount("admin", "admin") != null;
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;
}
}

View File

@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using OwnChar.Data.Providers.JsonFile.Model;
using OwnChar.Model;
using System.Net.NetworkInformation;
namespace OwnChar.Data.Providers.JsonFile
{
@@ -90,59 +91,131 @@ namespace OwnChar.Data.Providers.JsonFile
return JsonFile.UserAccounts.Count != 0;
}
bool IDataProvider.Delete<T>(T obj)
public bool Delete<T>(T obj) where T : class, IOwnCharObject
{
throw new NotImplementedException();
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)
{
throw new NotImplementedException();
if (parent is JsonUserAccount jaccount && profile is JsonUserProfile jprofile)
{
jaccount.Profile = jprofile;
return true;
}
return false;
}
public bool SetParent(Character character, Group parent)
{
throw new NotImplementedException();
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)
{
throw new NotImplementedException();
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)
{
throw new NotImplementedException();
if (group is JsonGroup jgroup && owner is JsonUserProfile jprofile)
{
jgroup.Owner = jprofile;
return true;
}
return false;
}
public bool SetOwner(Character group, UserProfile owner)
public bool SetOwner(Character character, UserProfile owner)
{
throw new NotImplementedException();
if (character is JsonCharacter jcharacter && owner is JsonUserProfile jprofile)
{
jcharacter.Owner = jprofile;
return true;
}
return false;
}
public UserAccount? GetUserAccount(string username, string password)
{
throw new NotImplementedException();
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)
{
throw new NotImplementedException();
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username)?.Profile;
}
public IEnumerable<UserProfile> GetGroupMembers(Group group)
public IEnumerable<UserProfile>? GetGroupMembers(Group group)
{
throw new NotImplementedException();
if (group is JsonGroup jgroup)
return jgroup.Members;
return null;
}
public UserProfile? GetOwner(Group group)
{
throw new NotImplementedException();
if (group is JsonGroup jgroup)
return jgroup.Owner;
return null;
}
public UserProfile? GetOwner(Character character)
{
throw new NotImplementedException();
if (character is JsonCharacter jcharacter)
return jcharacter.Owner;
return null;
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -9,13 +9,13 @@ namespace OwnChar.Manager
public UserProfile? GetOwner(Group? group)
{
ArgumentNullException.ThrowIfNull(group, nameof(group));
return Manager.DataProxy?.GetOwner(group);
return Manager.DataManager?.GetOwner(group);
}
public IEnumerable<UserProfile>? GetMembers(Group? group)
{
ArgumentNullException.ThrowIfNull(group, nameof(group));
return Manager.DataProxy?.GetMembers(group);
return Manager.DataManager?.GetMembers(group);
}
}
}

View File

@@ -12,7 +12,7 @@ namespace OwnChar.Manager
public UserAccount? CurrentUser { get; private set; }
// Data Provider
public IDataManager? DataProxy { get; set; }
public IDataManager? DataManager { get; set; }
// Manager
public UserManager Users { get; }
@@ -42,6 +42,7 @@ namespace OwnChar.Manager
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
username = username.Trim().ToLower();
CurrentUser = proxy.Login(username, Utils.HashPassword(username, password));
return IsLoggedIn;
@@ -53,7 +54,7 @@ namespace OwnChar.Manager
/// <returns></returns>
public bool Logout()
{
return DataProxy?.Logout(CurrentUser) ?? true;
return DataManager?.Logout(CurrentUser) ?? true;
}
}
}

View File

@@ -7,17 +7,24 @@ namespace OwnChar.Manager
{
public OwnCharManager Manager { get; } = manager;
public UserProfile? GetOwnUserProfile(UserAccount account)
public UserProfile? GetOwnUserProfile()
{
Manager.CheckLogin();
return Manager.DataProxy!.GetUserProfile(Manager.CurrentUser!);
return Manager.DataManager!.GetUserProfile(Manager.CurrentUser!);
}
public UserAccount? CreateUserAccount(string? username, SecureString? password)
{
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
return Manager.DataProxy?.CreateUserAccount(username, Utils.HashPassword(username, password));
username = username.Trim().ToLower();
return Manager.DataManager?.CreateUserAccount(username, Utils.HashPassword(username, password));
}
public bool DeleteUserAccount(UserAccount? account)
{
ArgumentNullException.ThrowIfNull(account, nameof(account));
return Manager.DataManager?.DeleteUserAccount(account) ?? false;
}
}
}

View File

@@ -5,5 +5,6 @@
public virtual string? Username { get; set; }
public virtual string? Password { get; set; }
public virtual string? Email { get; set; }
public virtual UserType Type { get; set; }
}
}