Compare commits

...

1 Commits

Author SHA1 Message Date
b08041204d some work 2024-05-27 22:07:25 +02:00
12 changed files with 196 additions and 91 deletions

View File

@@ -0,0 +1,30 @@
using OwnChar.Model;
using Pilz.Cryptography;
namespace OwnChar.Data.ClientServer
{
public class ClientServerDataProvider : IDataProvider
{
public UserAccount? CurrentUserAccount => throw new NotImplementedException();
public ClientServerDataProvider(string serverAddress)
{
throw new NotImplementedException();
}
public UserProfile? GetUserProfile(string username)
{
throw new NotImplementedException();
}
public UserAccount? Login(string username, SecureString password)
{
throw new NotImplementedException();
}
public bool Logout()
{
throw new NotImplementedException();
}
}
}

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

@@ -1,12 +1,22 @@
using System; using OwnChar.Model;
using System.Collections.Generic; using Pilz.Cryptography;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OwnChar.Data namespace OwnChar.Data
{ {
public interface IDataProvider public interface IDataProvider
{ {
// Properties
public bool IsLoggedIn => CurrentUserAccount != null;
public UserProfile? CurrentUserProfile => CurrentUserAccount?.Profile;
// Properties (abstract)
abstract UserAccount? CurrentUserAccount { get; }
// Methods (abstract)
abstract UserAccount? Initialize(bool force);
abstract UserProfile? GetUserProfile(string username);
abstract UserAccount? Login(string username, string password);
abstract bool Logout();
abstract UserAccount? CreateUser(string username, string password);
} }
} }

View File

@@ -0,0 +1,14 @@
using OwnChar.Model;
namespace OwnChar.Data.JsonFile
{
public class JsonFile
{
public List<UserAccount> UserAccounts { get; } = [];
public List<UserProfile> UserProfiles { get; } = [];
public List<Character> Characters { get; } = [];
public List<Group> Groups { get; } = [];
public List<Property> Properties { get; } = [];
public List<PropertyCategory> PropertyCategories { get; } = [];
}
}

View File

@@ -0,0 +1,87 @@
using Newtonsoft.Json;
using OwnChar.Model;
namespace OwnChar.Data.JsonFile
{
public class JsonFileDataProvider : IDataProvider
{
public JsonFile JsonFile { get; protected set; }
public string JsonFilePath { get; protected set; }
public UserAccount? CurrentUserAccount { 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.");
}
public UserAccount? Initialize(bool force)
{
if (force || JsonFile.UserAccounts.Count == 0)
CreateUser("admin", "admin");
return CurrentUserAccount;
}
protected void LoadFile()
{
if (JsonConvert.DeserializeObject<JsonFile>(File.ReadAllText(JsonFilePath), CreateJsonSerializerSettings()) is JsonFile jsonFile)
JsonFile = jsonFile;
JsonFile ??= new();
CurrentUserAccount = Initialize(false);
}
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 UserAccount? Login(string username, string password)
{
return CurrentUserAccount = JsonFile.UserAccounts.FirstOrDefault(n => !string.IsNullOrWhiteSpace(n.Username) && !string.IsNullOrWhiteSpace(n.Password) && n.Username == username && n.Password == password);
}
public bool Logout()
{
CurrentUserAccount = null;
return true;
}
public UserAccount? CreateUser(string username, string password)
{
var account = new UserAccount
{
Username = username,
Password = password,
Profile = new()
{
Name = username,
}
};
JsonFile.UserAccounts.Add(account);
JsonFile.UserProfiles.Add(account.Profile);
return account;
}
public UserProfile? GetUserProfile(string username)
{
return JsonFile.UserProfiles.FirstOrDefault(n => !string.IsNullOrWhiteSpace(n.Account?.Username) && n.Account.Username == 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

@@ -12,7 +12,7 @@ namespace OwnChar.Manager
public OwnCharManager Manager { get; } = manager; public OwnCharManager Manager { get; } = manager;
/// <summary> /// <summary>
/// Gets personal characters. /// Gets all personal characters (private & within groups).
/// </summary> /// </summary>
/// <param name="group"></param> /// <param name="group"></param>
/// <returns>Returns a collection with personal characters.</returns> /// <returns>Returns a collection with personal characters.</returns>
@@ -21,7 +21,7 @@ namespace OwnChar.Manager
if (!IsLoggedIn || UserProfile == null) if (!IsLoggedIn || UserProfile == null)
return null; return null;
return GetCharacters(UserProfile.Group); return GetCharacters(Manager.CurrentUser);
} }
/// <summary> /// <summary>

View File

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

View File

@@ -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,50 @@ namespace OwnChar.Manager
public class OwnCharManager public class OwnCharManager
{ {
// User // User
public bool IsLoggedIn { get; set; } public bool IsLoggedIn => DataProvider != null && DataProvider.IsLoggedIn;
public UserAccount? CurrentUser { get; set; } public UserAccount? CurrentUser => DataProvider?.CurrentUserAccount;
// Data Provider // Data Provider
public IDataProvider DataProvider { get; set; } public IDataProvider? DataProvider { 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);
} }
private 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(IDataProvider? dataProvider, string username, SecureString password)
{ {
return IsLoggedIn = true; // TODO: Change `true` to the real check. CheckLogin();
ArgumentNullException.ThrowIfNull(dataProvider);
return dataProvider.Login(username, password) != null;
}
/// <summary>
/// Closes the session on the current data provider.
/// </summary>
/// <returns></returns>
public bool Logout()
{
if (DataProvider != null)
return DataProvider.Logout();
return true;
} }
} }
} }

View File

@@ -3,15 +3,9 @@
public class UserAccount public class UserAccount
{ {
public ulong Id { get; set; } public ulong Id { get; set; }
public string Username { get; set; } public string? Username { get; set; }
public string Email { get; set; } public string? Password { get; set; }
public string Password { get; set; } public string? Email { get; set; }
public UserProfile? Profile { get; set; }
internal UserAccount(string email, string username, string password)
{
Username = username;
Email = email;
Password = password;
}
} }
} }

View File

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

View File

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