mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-04 08:00:16 -04:00
Moved application code to the Application layer
Imported Win10 project Code cleanup WIP - Use common UWP services for Win8.1 and Win10
This commit is contained in:
149
ModernKeePass10/ViewModels/UserControls/CredentialsViewModel.cs
Normal file
149
ModernKeePass10/ViewModels/UserControls/CredentialsViewModel.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Autofac;
|
||||
using ModernKeePass.Domain.AOP;
|
||||
using ModernKeePass.Domain.Dtos;
|
||||
using ModernKeePass.Domain.Enums;
|
||||
using ModernKeePass.Domain.Interfaces;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class CredentialsViewModel: NotifyPropertyChangedBase
|
||||
{
|
||||
|
||||
private bool _hasPassword;
|
||||
private bool _hasKeyFile;
|
||||
private bool _hasUserAccount;
|
||||
private bool _isOpening;
|
||||
private string _password = string.Empty;
|
||||
private string _status;
|
||||
private CredentialStatusTypes _statusType;
|
||||
private string _keyFilePath = string.Empty;
|
||||
private string _keyFileText;
|
||||
private readonly IResourceService _resourceService;
|
||||
private readonly IDatabaseService _databaseService;
|
||||
|
||||
public bool HasPassword
|
||||
{
|
||||
get => _hasPassword;
|
||||
set
|
||||
{
|
||||
SetProperty(ref _hasPassword, value);
|
||||
OnPropertyChanged(nameof(IsValid));
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasKeyFile
|
||||
{
|
||||
get => _hasKeyFile;
|
||||
set
|
||||
{
|
||||
SetProperty(ref _hasKeyFile, value);
|
||||
OnPropertyChanged(nameof(IsValid));
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasUserAccount
|
||||
{
|
||||
get => _hasUserAccount;
|
||||
set
|
||||
{
|
||||
SetProperty(ref _hasUserAccount, value);
|
||||
OnPropertyChanged(nameof(IsValid));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsValid => !_isOpening && (HasPassword || HasKeyFile && KeyFilePath != string.Empty || HasUserAccount);
|
||||
|
||||
public string Status
|
||||
{
|
||||
get => _status;
|
||||
set => SetProperty(ref _status, value);
|
||||
}
|
||||
|
||||
public int StatusType
|
||||
{
|
||||
get => (int)_statusType;
|
||||
set => SetProperty(ref _statusType, (CredentialStatusTypes)value);
|
||||
}
|
||||
|
||||
public string Password
|
||||
{
|
||||
get => _password;
|
||||
set
|
||||
{
|
||||
_password = value;
|
||||
StatusType = (int)CredentialStatusTypes.Normal;
|
||||
Status = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public string KeyFilePath
|
||||
{
|
||||
get => _keyFilePath;
|
||||
set
|
||||
{
|
||||
_keyFilePath = value;
|
||||
OnPropertyChanged(nameof(IsValid));
|
||||
}
|
||||
}
|
||||
|
||||
public string KeyFileText
|
||||
{
|
||||
get => _keyFileText;
|
||||
set => SetProperty(ref _keyFileText, value);
|
||||
}
|
||||
|
||||
|
||||
public CredentialsViewModel() : this(App.Container.Resolve<IDatabaseService>(), App.Container.Resolve<IResourceService>()) { }
|
||||
|
||||
public CredentialsViewModel(IDatabaseService databaseService, IResourceService resourceService)
|
||||
{
|
||||
_databaseService = databaseService;
|
||||
_resourceService = resourceService;
|
||||
_keyFileText = _resourceService.GetResourceValue("CompositeKeyDefaultKeyFile");
|
||||
}
|
||||
|
||||
public async Task<bool> OpenDatabase(FileInfo fileInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
_isOpening = true;
|
||||
OnPropertyChanged(nameof(IsValid));
|
||||
var credentials = new Credentials
|
||||
{
|
||||
KeyFilePath = HasKeyFile ? KeyFilePath : string.Empty,
|
||||
Password = HasPassword ? Password : string.Empty
|
||||
};
|
||||
await Task.Run(() => _databaseService.Open(fileInfo, credentials));
|
||||
return true;
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
var errorMessage = new StringBuilder($"{_resourceService.GetResourceValue("CompositeKeyErrorOpen")}\n");
|
||||
if (HasPassword) errorMessage.AppendLine(_resourceService.GetResourceValue("CompositeKeyErrorUserPassword"));
|
||||
if (HasKeyFile) errorMessage.AppendLine(_resourceService.GetResourceValue("CompositeKeyErrorUserKeyFile"));
|
||||
if (HasUserAccount) errorMessage.AppendLine(_resourceService.GetResourceValue("CompositeKeyErrorUserAccount"));
|
||||
UpdateStatus(errorMessage.ToString(), CredentialStatusTypes.Error);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var error = $"{_resourceService.GetResourceValue("CompositeKeyErrorOpen")}{e.Message}";
|
||||
UpdateStatus(error, CredentialStatusTypes.Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isOpening = false;
|
||||
OnPropertyChanged(nameof(IsValid));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void UpdateStatus(string text, CredentialStatusTypes type)
|
||||
{
|
||||
Status = text;
|
||||
StatusType = (int)type;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,93 @@
|
||||
using Autofac;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using ModernKeePass.Domain.AOP;
|
||||
using ModernKeePass.Domain.Dtos;
|
||||
using ModernKeePass.Domain.Enums;
|
||||
using ModernKeePass.Domain.Interfaces;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class UpdateCredentialsViewModel : NotifyPropertyChangedBase
|
||||
{
|
||||
private readonly ISecurityService _securityService;
|
||||
private bool _hasPassword;
|
||||
private bool _hasKeyFile;
|
||||
private bool _hasUserAccount;
|
||||
private string _confirmPassword;
|
||||
private string _password;
|
||||
private string _keyFileText;
|
||||
private string _status;
|
||||
private CredentialStatusTypes _statusType;
|
||||
|
||||
public string Password
|
||||
{
|
||||
get => _password;
|
||||
set
|
||||
{
|
||||
SetProperty(ref _password, value);
|
||||
OnPropertyChanged(nameof(IsValid));
|
||||
}
|
||||
}
|
||||
|
||||
public string ConfirmPassword
|
||||
{
|
||||
get => _confirmPassword;
|
||||
set => SetProperty(ref _confirmPassword, value);
|
||||
}
|
||||
|
||||
public string KeyFilePath { get; set; }
|
||||
|
||||
public bool HasPassword
|
||||
{
|
||||
get => _hasPassword;
|
||||
set => SetProperty(ref _hasPassword, value);
|
||||
}
|
||||
|
||||
public bool HasKeyFile
|
||||
{
|
||||
get => _hasKeyFile;
|
||||
set => SetProperty(ref _hasKeyFile, value);
|
||||
}
|
||||
|
||||
public bool HasUserAccount
|
||||
{
|
||||
get => _hasUserAccount;
|
||||
set => SetProperty(ref _hasUserAccount, value);
|
||||
}
|
||||
|
||||
public string KeyFileText
|
||||
{
|
||||
get => _keyFileText;
|
||||
set => SetProperty(ref _keyFileText, value);
|
||||
}
|
||||
|
||||
public string Status
|
||||
{
|
||||
get => _status;
|
||||
set => SetProperty(ref _status, value);
|
||||
}
|
||||
|
||||
public int StatusType
|
||||
{
|
||||
get => (int)_statusType;
|
||||
set => SetProperty(ref _statusType, (CredentialStatusTypes)value);
|
||||
}
|
||||
|
||||
public double PasswordComplexityIndicator => _securityService.EstimatePasswordComplexity(Password);
|
||||
|
||||
public bool IsValid => HasPassword && Password == ConfirmPassword || HasKeyFile && KeyFilePath != string.Empty || HasUserAccount;
|
||||
|
||||
public UpdateCredentialsViewModel(): this(App.Container.Resolve<ISecurityService>()) { }
|
||||
|
||||
public UpdateCredentialsViewModel(ISecurityService securityService)
|
||||
{
|
||||
_securityService = securityService;
|
||||
}
|
||||
|
||||
internal Task<bool> CreateDatabase(FileInfo fileInfo)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user