Files
Library/OwnChar/Data/Managers/DefaultDataManager.cs

180 lines
5.8 KiB
C#

using OwnChar.Model;
namespace OwnChar.Data.Managers;
public class DefaultDataManager : IDataManager
{
private const string defaultUsername = "admin";
private const string defaultPassword = "admin";
public IDataProvider DataProvider { get; }
public DefaultDataManager(IDataProvider dataProvider)
{
DataProvider = dataProvider;
Initialize(false);
}
public IEnumerable<UserProfile>? GetMembers(UserAccount account, Group group)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetMembers(group);
}
public UserProfile? GetOwner(UserAccount account, Group group)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetOwner(group);
}
public UserProfile? GetOwner(UserAccount account, Character character)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetOwner(character);
}
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;
}
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;
}
public IEnumerable<Character>? GetCharacters(UserAccount account, Group group)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetCharacters(group);
}
public IEnumerable<Character>? GetCharacters(UserAccount account, UserProfile profile)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetCharacters(profile);
}
public bool AddMember(UserAccount account, Group group, UserProfile user)
{
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(group) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
return false;
return DataProvider.AddMember(group, user);
}
public bool RemoveMember(UserAccount account, Group group, UserProfile user)
{
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(group) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
return false;
return DataProvider.RemoveMember(group, user);
}
public 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.SetOwner(group, profile);
return group;
}
public bool DeleteGroup(UserAccount account, Group group)
{
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(group) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
return false;
return DataProvider.Delete(group);
}
public 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.SetOwner(character, profile);
if (group != null)
DataProvider.SetParent(character, group);
return character;
}
public bool DeleteCharacter(UserAccount account, Character character)
{
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(character) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
return false;
DataProvider.Delete(character);
return true;
}
}