Added dirty behavior

Removed restore action (-> Move action wip)
Added additional check on DB size before auto saving
Code cleanup
This commit is contained in:
Geoffroy BONNEVILLE
2020-04-14 13:44:07 +02:00
parent d972b6cb5a
commit a2eba91a3b
34 changed files with 236 additions and 190 deletions

View File

@@ -75,6 +75,7 @@
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="Common\Behaviors\SetDirtyBehavior.cs" />
<Compile Include="Common\Interfaces\ICryptographyClient.cs" />
<Compile Include="Common\Interfaces\IDatabaseProxy.cs" />
<Compile Include="Common\Interfaces\IEntityVm.cs" />

View File

@@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using MediatR;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Database.Commands.CloseDatabase;
using ModernKeePass.Application.Database.Commands.SaveDatabase;
namespace ModernKeePass.Application.Common.Behaviors
{
public class SetDirtyBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly List<string> _excludedCommands = new List<string>
{nameof(SaveDatabaseCommand), nameof(CloseDatabaseCommand)};
private readonly IDatabaseProxy _database;
public SetDirtyBehavior(IDatabaseProxy database)
{
_database = database;
}
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next)
{
var response = await next();
var queryName = typeof(TRequest).Name;
if (queryName.Contains("Command") && !_excludedCommands.Contains(queryName))
{
_database.IsDirty = true;
}
return response;
}
}
}

View File

@@ -7,7 +7,6 @@ namespace ModernKeePass.Application.Common.Interfaces
{
public interface IDatabaseProxy
{
string ZeroId { get; }
bool IsOpen { get; }
string Name { get; }
string RootGroupId { get; }
@@ -17,6 +16,8 @@ namespace ModernKeePass.Application.Common.Interfaces
string Compression { get; set; }
bool IsRecycleBinEnabled { get; set; }
string FileAccessToken { get; set; }
int Size { get; set; }
bool IsDirty { get; set; }
Task Open(byte[] file, Credentials credentials);
Task ReOpen(byte[] file);

View File

@@ -48,8 +48,10 @@ namespace ModernKeePass.Application.Database.Commands.SaveDatabase
_file.ReleaseFile(_database.FileAccessToken);
_database.FileAccessToken = message.FilePath;
}
_database.IsDirty = false;
}
catch (Exception exception)
catch (ArgumentException exception)
{
throw new SaveException(exception);
}

View File

@@ -10,5 +10,7 @@
public string Compression { get; set; }
public string CipherId { get; set; }
public string KeyDerivationId { get; set; }
public int Size { get; internal set; }
public bool IsDirty { get; internal set; }
}
}

View File

@@ -31,6 +31,8 @@ namespace ModernKeePass.Application.Database.Queries.GetDatabase
database.Compression = _databaseProxy.Compression;
database.CipherId = _databaseProxy.CipherId;
database.KeyDerivationId = _databaseProxy.KeyDerivationId;
database.Size = _databaseProxy.Size;
database.IsDirty = _databaseProxy.IsDirty;
}
return database;
}

View File

@@ -33,6 +33,7 @@ namespace ModernKeePass.Application.Database.Queries.OpenDatabase
KeyFileContents = !string.IsNullOrEmpty(message.KeyFilePath) ? await _file.OpenBinaryFile(message.KeyFilePath): null,
Password = message.Password
});
_database.Size = file.Length;
_database.FileAccessToken = message.FilePath;
}

View File

@@ -2,6 +2,7 @@
using FluentValidation;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Behaviors;
namespace ModernKeePass.Application
{
@@ -11,6 +12,7 @@ namespace ModernKeePass.Application
{
var assembly = typeof(DependencyInjection).GetTypeInfo().Assembly;
services.AddMediatR(assembly);
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(SetDirtyBehavior<,>));
//services.AddValidatorsFromAssembly(assembly);
return services;

View File

@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using MediatR;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Domain.Common;
using ModernKeePass.Domain.Exceptions;
namespace ModernKeePass.Application.Group.Commands.DeleteEntry
@@ -24,7 +25,7 @@ namespace ModernKeePass.Application.Group.Commands.DeleteEntry
{
if (!_database.IsOpen) throw new DatabaseClosedException();
if (_database.IsRecycleBinEnabled && (string.IsNullOrEmpty(_database.RecycleBinId) || _database.RecycleBinId.Equals(_database.ZeroId)))
if (_database.IsRecycleBinEnabled && (string.IsNullOrEmpty(_database.RecycleBinId) || _database.RecycleBinId.Equals(Constants.EmptyId)))
{
_database.CreateGroup(_database.RootGroupId, message.RecycleBinName, true);
}

View File

@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using MediatR;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Domain.Common;
using ModernKeePass.Domain.Exceptions;
namespace ModernKeePass.Application.Group.Commands.DeleteGroup
@@ -24,7 +25,7 @@ namespace ModernKeePass.Application.Group.Commands.DeleteGroup
{
if (!_database.IsOpen) throw new DatabaseClosedException();
if (_database.IsRecycleBinEnabled && (string.IsNullOrEmpty(_database.RecycleBinId) || _database.RecycleBinId.Equals(_database.ZeroId)))
if (_database.IsRecycleBinEnabled && (string.IsNullOrEmpty(_database.RecycleBinId) || _database.RecycleBinId.Equals(Constants.EmptyId)))
{
_database.CreateGroup(_database.RootGroupId, message.RecycleBinName, true);
}