Files
modernkeepass/WinAppCommon/ViewModels/UserControls/SetCredentialsVm.cs

154 lines
5.4 KiB
C#
Raw Normal View History

2020-04-22 18:12:28 +02:00
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MediatR;
using Messages;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Security.Commands.GenerateKeyFile;
using ModernKeePass.Domain.Common;
using ModernKeePass.Domain.Dtos;
2020-04-22 18:12:28 +02:00
namespace ModernKeePass.ViewModels
{
public class SetCredentialsVm : ViewModelBase
2020-04-22 18:12:28 +02:00
{
private readonly IMediator _mediator;
private readonly IResourceProxy _resource;
private readonly IFileProxy _file;
2020-04-22 18:12:28 +02:00
public bool HasPassword
{
get { return _hasPassword; }
set
{
Set(() => HasPassword, ref _hasPassword, value);
2020-04-30 15:32:42 +02:00
RaisePropertyChanged(nameof(IsPasswordValid));
2020-04-22 18:12:28 +02:00
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public bool HasKeyFile
{
get { return _hasKeyFile; }
set
{
Set(() => HasKeyFile, ref _hasKeyFile, value);
2020-04-30 15:32:42 +02:00
RaisePropertyChanged(nameof(IsKeyFileValid));
2020-04-22 18:12:28 +02:00
RaisePropertyChanged(nameof(IsValid));
OpenKeyFileCommand.RaiseCanExecuteChanged();
CreateKeyFileCommand.RaiseCanExecuteChanged();
2020-04-22 18:12:28 +02:00
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string Password
{
get { return _password; }
set
{
Set(() => Password, ref _password, value);
2020-04-30 15:32:42 +02:00
RaisePropertyChanged(nameof(IsPasswordValid));
2020-04-22 18:12:28 +02:00
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string ConfirmPassword
{
get { return _confirmPassword; }
set
{
Set(() => ConfirmPassword, ref _confirmPassword, value);
2020-04-30 15:32:42 +02:00
RaisePropertyChanged(nameof(IsPasswordValid));
2020-04-22 18:12:28 +02:00
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string KeyFilePath
{
get { return _keyFilePath; }
set
{
_keyFilePath = value;
2020-04-30 15:32:42 +02:00
RaisePropertyChanged(nameof(IsKeyFileValid));
2020-04-22 18:12:28 +02:00
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string KeyFileText
{
get { return _keyFileText; }
set { Set(() => KeyFileText, ref _keyFileText, value); }
}
2020-04-30 15:32:42 +02:00
public bool IsPasswordValid => HasPassword && Password == ConfirmPassword || !HasPassword;
public bool IsKeyFileValid => HasKeyFile && !string.IsNullOrEmpty(KeyFilePath) || !HasKeyFile;
public bool IsValid => HasPassword && Password == ConfirmPassword || HasKeyFile && !string.IsNullOrEmpty(KeyFilePath) && (HasPassword || HasKeyFile);
2020-04-22 18:12:28 +02:00
public RelayCommand OpenKeyFileCommand { get; }
public RelayCommand CreateKeyFileCommand { get; }
2020-04-22 18:12:28 +02:00
public RelayCommand GenerateCredentialsCommand{ get; }
private bool _hasPassword;
private bool _hasKeyFile;
private string _password = string.Empty;
2020-04-30 15:32:42 +02:00
private string _confirmPassword = string.Empty;
2020-04-22 18:12:28 +02:00
private string _keyFilePath;
private string _keyFileText;
public SetCredentialsVm(IMediator mediator, IResourceProxy resource, IFileProxy file)
2020-04-22 18:12:28 +02:00
{
_mediator = mediator;
_resource = resource;
_file = file;
OpenKeyFileCommand = new RelayCommand(async () => await OpenKeyFile(), () => HasKeyFile);
CreateKeyFileCommand = new RelayCommand(async () => await CreateKeyFile(), () => HasKeyFile);
2020-04-22 18:12:28 +02:00
GenerateCredentialsCommand = new RelayCommand(GenerateCredentials, () => IsValid);
_keyFileText = resource.GetResourceValue("CompositeKeyDefaultKeyFile");
MessengerInstance.Register<PasswordGeneratedMessage>(this, message =>
{
Password = message.Password;
ConfirmPassword = message.Password;
});
2020-04-22 18:12:28 +02:00
}
private async Task OpenKeyFile()
{
var file = await _file.OpenFile(string.Empty, Constants.Extensions.Any, false);
if (file == null) return;
SetKeyFileInfo(file);
}
2020-04-22 18:12:28 +02:00
private async Task CreateKeyFile()
2020-04-22 18:12:28 +02:00
{
var file = await _file.CreateFile(_resource.GetResourceValue("CompositeKeyFileNameSuggestion"),
Constants.Extensions.Key, _resource.GetResourceValue("CompositeKeyFileTypeDesc"),
false);
if (file == null) return;
SetKeyFileInfo(file);
await _mediator.Send(new GenerateKeyFileCommand { KeyFilePath = KeyFilePath });
2020-04-22 18:12:28 +02:00
}
private void SetKeyFileInfo(FileInfo file)
{
KeyFilePath = file.Id;
KeyFileText = file.Name;
HasKeyFile = true;
}
2020-04-22 18:12:28 +02:00
private void GenerateCredentials()
{
MessengerInstance.Send(new CredentialsSetMessage
2020-04-22 18:12:28 +02:00
{
Password = HasPassword ? Password : null,
KeyFilePath = HasKeyFile ? KeyFilePath : null
});
}
}
}