Compare commits
1 Commits
b2f0a80872
...
b08041204d
| Author | SHA1 | Date | |
|---|---|---|---|
| b08041204d |
30
OwnChar/Data/ClientServer/ClientServerDataProvider.cs
Normal file
30
OwnChar/Data/ClientServer/ClientServerDataProvider.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using OwnChar.Model;
|
||||
using Pilz.Cryptography;
|
||||
|
||||
namespace OwnChar.Data
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
14
OwnChar/Data/JsonFile/JsonFile.cs
Normal file
14
OwnChar/Data/JsonFile/JsonFile.cs
Normal 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; } = [];
|
||||
}
|
||||
}
|
||||
87
OwnChar/Data/JsonFile/JsonFileDataProvider.cs
Normal file
87
OwnChar/Data/JsonFile/JsonFileDataProvider.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ namespace OwnChar.Manager
|
||||
public OwnCharManager Manager { get; } = manager;
|
||||
|
||||
/// <summary>
|
||||
/// Gets personal characters.
|
||||
/// Gets all personal characters (private & within groups).
|
||||
/// </summary>
|
||||
/// <param name="group"></param>
|
||||
/// <returns>Returns a collection with personal characters.</returns>
|
||||
@@ -21,7 +21,7 @@ namespace OwnChar.Manager
|
||||
if (!IsLoggedIn || UserProfile == null)
|
||||
return null;
|
||||
|
||||
return GetCharacters(UserProfile.Group);
|
||||
return GetCharacters(Manager.CurrentUser);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
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,4 +1,5 @@
|
||||
using OwnChar.Data;
|
||||
using OwnChar.Manager.Exceptions;
|
||||
using OwnChar.Model;
|
||||
using Pilz.Cryptography;
|
||||
|
||||
@@ -7,32 +8,50 @@ namespace OwnChar.Manager
|
||||
public class OwnCharManager
|
||||
{
|
||||
// User
|
||||
public bool IsLoggedIn { get; set; }
|
||||
public UserAccount? CurrentUser { get; set; }
|
||||
public bool IsLoggedIn => DataProvider != null && DataProvider.IsLoggedIn;
|
||||
public UserAccount? CurrentUser => DataProvider?.CurrentUserAccount;
|
||||
|
||||
// Data Provider
|
||||
public IDataProvider DataProvider { get; set; }
|
||||
public IDataProvider? DataProvider { get; set; }
|
||||
|
||||
// Manager
|
||||
public UserManager Users { get; }
|
||||
public GroupsManager Groups { get; }
|
||||
public CharacterManager Characters { get; }
|
||||
|
||||
public OwnCharManager(IDataProvider dataProvider)
|
||||
public OwnCharManager()
|
||||
{
|
||||
DataProvider = dataProvider;
|
||||
Users = new(this);
|
||||
Groups = new(this);
|
||||
Characters = new(this);
|
||||
}
|
||||
|
||||
private void CheckLogin()
|
||||
{
|
||||
if (!IsLoggedIn)
|
||||
throw new LoginException("You are already logged in!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to login on the server.
|
||||
/// 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(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,15 +3,9 @@
|
||||
public class UserAccount
|
||||
{
|
||||
public ulong Id { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
|
||||
internal UserAccount(string email, string username, string password)
|
||||
{
|
||||
Username = username;
|
||||
Email = email;
|
||||
Password = password;
|
||||
}
|
||||
public string? Username { get; set; }
|
||||
public string? Password { get; set; }
|
||||
public string? Email { get; set; }
|
||||
public UserProfile? Profile { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
namespace OwnChar.Model
|
||||
{
|
||||
public class UserProfile(string name)
|
||||
public class UserProfile
|
||||
{
|
||||
public ulong Id { get; set; }
|
||||
public string Name { get; set; } = name;
|
||||
public string? Name { get; set; }
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user