Skip to content

Commit 1102017

Browse files
committed
Do not log if new value equals old one
1 parent 379aa78 commit 1102017

18 files changed

Lines changed: 140 additions & 59 deletions

Core/Internal/Models/Account.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ string IAccount.Label
1919
itemName: ToString(),
2020
fieldName: nameof(Label),
2121
needsReview: false,
22+
oldValue: Label,
2223
value: value,
2324
readableValue: value);
2425
}
@@ -30,6 +31,7 @@ string[] IAccount.Identifiants
3031
itemName: ToString(),
3132
fieldName: nameof(Identifiants),
3233
needsReview: true,
34+
oldValue: Identifiants,
3335
value: value,
3436
readableValue: $"({string.Join(", ", value)})");
3537
}
@@ -39,8 +41,10 @@ string IAccount.Password
3941
get => Database.Get(Password);
4042
set
4143
{
42-
if (!string.IsNullOrEmpty(value))
44+
if (!string.IsNullOrEmpty(value)
45+
&& Password != value)
4346
{
47+
Dictionary<DateTime, string> oldPasswords = ISerializationCenter.Clone(Database.SerializationCenter, Passwords);
4448
Passwords[DateTime.Now] = Password = value;
4549

4650
if (_service != null)
@@ -49,6 +53,7 @@ string IAccount.Password
4953
itemName: ToString(),
5054
fieldName: nameof(Password),
5155
needsReview: true,
56+
oldValue: oldPasswords,
5257
value: Passwords,
5358
readableValue: string.Empty);
5459
}
@@ -65,6 +70,7 @@ string IAccount.Notes
6570
itemName: ToString(),
6671
fieldName: nameof(Notes),
6772
needsReview: false,
73+
oldValue: Notes,
6874
value: value,
6975
readableValue: value);
7076
}
@@ -76,6 +82,7 @@ int IAccount.PasswordUpdateReminderDelay
7682
itemName: ToString(),
7783
fieldName: nameof(PasswordUpdateReminderDelay),
7884
needsReview: false,
85+
oldValue: PasswordUpdateReminderDelay,
7986
value: value,
8087
readableValue: value.ToString());
8188
}
@@ -87,6 +94,7 @@ AccountOption IAccount.Options
8794
itemName: ToString(),
8895
fieldName: nameof(Options),
8996
needsReview: false,
97+
oldValue: Options,
9098
value: value,
9199
readableValue: value.ToString());
92100
}

Core/Internal/Models/AutoSave.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Upsilon.Apps.PassKey.Core.Internal.Utils;
2+
using Upsilon.Apps.PassKey.Core.Public.Interfaces;
23

34
namespace Upsilon.Apps.PassKey.Core.Internal.Models
45
{
@@ -13,9 +14,12 @@ internal Database Database
1314

1415
public Queue<Change> Changes { get; set; } = new();
1516

16-
internal T UpdateValue<T>(string itemId, string itemName, string fieldName, bool needsReview, T value, string readableValue) where T : notnull
17+
internal T UpdateValue<T>(string itemId, string itemName, string fieldName, bool needsReview, T oldValue, T value, string readableValue) where T : notnull
1718
{
18-
_addChange(itemId, itemName, string.Empty, fieldName, Database.SerializationCenter.Serialize(value), readableValue, needsReview, Change.Type.Update);
19+
if (ISerializationCenter.AreDifferent(Database.SerializationCenter, oldValue, value))
20+
{
21+
_addChange(itemId, itemName, string.Empty, fieldName, Database.SerializationCenter.Serialize(value), readableValue, needsReview, Change.Type.Update);
22+
}
1923

2024
return value;
2125
}
@@ -50,22 +54,12 @@ private void _addChange(string itemId, string itemName, string containerName, st
5054
}
5155

5256
Database.AutoSaveFileLocker.Save(this, Database.Passkeys);
53-
string logMessage;
54-
55-
switch (action)
57+
string logMessage = action switch
5658
{
57-
case Change.Type.Add:
58-
logMessage = $"{itemName} has been added to {containerName}";
59-
break;
60-
case Change.Type.Delete:
61-
logMessage = $"{itemName} has been removed from {containerName}";
62-
break;
63-
case Change.Type.Update:
64-
default:
65-
logMessage = $"{itemName}'s {fieldName.ToSentenceCase().ToLower()} has been {(string.IsNullOrWhiteSpace(readableValue) ? $"updated" : $"set to {readableValue}")}";
66-
break;
67-
}
68-
59+
Change.Type.Add => $"{itemName} has been added to {containerName}",
60+
Change.Type.Delete => $"{itemName} has been removed from {containerName}",
61+
_ => $"{itemName}'s {fieldName.ToSentenceCase().ToLower()} has been {(string.IsNullOrWhiteSpace(readableValue) ? $"updated" : $"set to {readableValue}")}",
62+
};
6963
Database.Logs.AddLog(logMessage, needsReview);
7064
}
7165

Core/Internal/Models/Database.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ private Warning[] _lookAtPasswordUpdateReminderWarnings()
374374
.Where(x => x.PasswordExpired)
375375
.ToArray();
376376

377-
return accounts.Length != 0 ? ([new Warning(WarningType.PasswordUpdateReminderWarning, accounts)]) : ([]);
377+
return accounts.Length != 0 ? [new Warning(WarningType.PasswordUpdateReminderWarning, accounts)] : [];
378378
}
379379

380380
private Warning[] _lookAtPasswordLeakedWarnings()
@@ -386,7 +386,7 @@ private Warning[] _lookAtPasswordLeakedWarnings()
386386
.Where(x => x.PasswordLeaked)
387387
.ToArray();
388388

389-
return accounts.Length != 0 ? ([new Warning(WarningType.PasswordLeakedWarning, accounts)]) : ([]);
389+
return accounts.Length != 0 ? [new Warning(WarningType.PasswordLeakedWarning, accounts)] : [];
390390
}
391391

392392
private Warning[] _lookAtDuplicatedPasswordsWarnings()

Core/Internal/Models/Service.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ string IService.ServiceName
1818
itemName: ToString(),
1919
fieldName: nameof(ServiceName),
2020
needsReview: true,
21+
oldValue: ServiceName,
2122
value: value,
2223
readableValue: value);
2324
}
@@ -29,6 +30,7 @@ string IService.Url
2930
itemName: ToString(),
3031
fieldName: nameof(Url),
3132
needsReview: false,
33+
oldValue: Url,
3234
value: value,
3335
readableValue: value);
3436
}
@@ -40,6 +42,7 @@ string IService.Notes
4042
itemName: ToString(),
4143
fieldName: nameof(Notes),
4244
needsReview: false,
45+
oldValue: Notes,
4346
value: value,
4447
readableValue: value);
4548
}

Core/Internal/Models/User.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ string IUser.Username
1919
itemName: ToString(),
2020
fieldName: nameof(Username),
2121
needsReview: true,
22+
oldValue: Username,
2223
value: value,
2324
readableValue: value);
2425
}
@@ -30,6 +31,7 @@ string[] IUser.Passkeys
3031
itemName: ToString(),
3132
fieldName: nameof(Passkeys),
3233
needsReview: true,
34+
oldValue: Passkeys,
3335
value: value,
3436
readableValue: string.Empty);
3537
}
@@ -41,6 +43,7 @@ int IUser.LogoutTimeout
4143
itemName: ToString(),
4244
fieldName: nameof(LogoutTimeout),
4345
needsReview: false,
46+
oldValue: LogoutTimeout,
4447
value: value,
4548
readableValue: value.ToString());
4649
}
@@ -54,6 +57,7 @@ int IUser.CleaningClipboardTimeout
5457
itemName: ToString(),
5558
fieldName: nameof(CleaningClipboardTimeout),
5659
needsReview: false,
60+
oldValue: CleaningClipboardTimeout,
5761
value: value,
5862
readableValue: value.ToString());
5963
}
@@ -65,6 +69,7 @@ WarningType IUser.WarningsToNotify
6569
itemName: ToString(),
6670
fieldName: nameof(WarningsToNotify),
6771
needsReview: true,
72+
oldValue: WarningsToNotify,
6873
value: value,
6974
readableValue: value.ToString());
7075
}
@@ -169,9 +174,9 @@ private void _timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
169174

170175
private void _cleanClipboard()
171176
{
172-
var passwords = Services.SelectMany(x => x.Accounts).Select(x => x.Password).ToArray();
177+
string[] passwords = [.. Services.SelectMany(x => x.Accounts).Select(x => x.Password)];
173178

174-
var cleanedPasswordsCount = ClipboardManager.RemoveAllOccurence(passwords);
179+
int cleanedPasswordsCount = ClipboardManager.RemoveAllOccurence(passwords);
175180

176181
if (cleanedPasswordsCount != 0)
177182
{

Core/Internal/Utils/LogCenter.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ internal Database Database
1414
}
1515

1616
[JsonIgnore]
17-
public ILog[]? Logs
18-
{
19-
get
20-
{
21-
return Database.User == null
17+
public ILog[]? Logs => Database.User == null
2218
? null
2319
: LogList.Select(x =>
2420
{
@@ -27,8 +23,6 @@ public ILog[]? Logs
2723
})
2824
.OrderByDescending(x => x.DateTime)
2925
.ToArray();
30-
}
31-
}
3226

3327
public List<string> LogList { get; set; } = [];
3428
public string Username { get; set; } = string.Empty;

Core/Public/Interfaces/ILog.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ public interface ILog
88
/// <summary>
99
/// The date and time the event occured.
1010
/// </summary>
11-
public DateTime DateTime { get; }
11+
DateTime DateTime { get; }
1212

1313
/// <summary>
1414
/// The event message.
1515
/// </summary>
16-
public string Message { get; }
16+
string Message { get; }
1717

1818
/// <summary>
1919
/// Indicate if the current log needs review.
2020
/// </summary>
21-
public bool NeedsReview { get; set; }
21+
bool NeedsReview { get; set; }
2222
}
2323
}

Core/Public/Interfaces/IPasswordFactory.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ public interface IPasswordFactory
88
/// <summary>
99
/// The letters used by the factory.
1010
/// </summary>
11-
public string Alphabetic { get; }
11+
string Alphabetic { get; }
1212

1313
/// <summary>
1414
/// The digits used by the factory.
1515
/// </summary>
16-
public string Numeric { get; }
16+
string Numeric { get; }
1717

1818
/// <summary>
1919
/// The special characters used by the factory.
2020
/// </summary>
21-
public string SpecialChars { get; }
21+
string SpecialChars { get; }
2222

2323
/// <summary>
2424
/// Generate a random password.
@@ -31,7 +31,7 @@ public interface IPasswordFactory
3131
/// <param name="excludedChars">Exclude some specific characters.</param>
3232
/// <param name="checkIfLeaked">Ensure that the generated password has been already leaked.</param>
3333
/// <returns>The random geenrated password.</returns>
34-
public string GeneratePassword(int length,
34+
string GeneratePassword(int length,
3535
bool includeUpperCaseAlphabeticChars = true,
3636
bool includeLowerCaseAlphabeticChars = true,
3737
bool includeNumericChars = true,
@@ -46,7 +46,7 @@ public string GeneratePassword(int length,
4646
/// <param name="alphabet">The alphabet used.</param>
4747
/// <param name="checkIfLeaked">Ensure that the generated password has been already leaked.</param>
4848
/// <returns>The random geenrated password.</returns>
49-
public string GeneratePassword(int length,
49+
string GeneratePassword(int length,
5050
string alphabet,
5151
bool checkIfLeaked = true);
5252

@@ -55,6 +55,6 @@ public string GeneratePassword(int length,
5555
/// </summary>
5656
/// <param name="password">The password to check.</param>
5757
/// <returns>Returns true if the password has been leaked.</returns>
58-
public bool PasswordLeaked(string password);
58+
bool PasswordLeaked(string password);
5959
}
6060
}

Core/Public/Interfaces/ISerializationCenter.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,29 @@ public interface ISerializationCenter
2020
/// <param name="toDeserialize">The serialised string.</param>
2121
/// <returns>The deserialized object.</returns>
2222
T Deserialize<T>(string toDeserialize) where T : notnull;
23+
24+
/// <summary>
25+
/// Check if two objects are different or not.
26+
/// </summary>
27+
/// <param name="serializationCenter">The Serialization Center.</param>
28+
/// <param name="object1">The first object.</param>
29+
/// <param name="object2">The second object.</param>
30+
/// <returns>True if the two objects are different, False else.</returns>
31+
static bool AreDifferent(ISerializationCenter serializationCenter, object object1, object object2)
32+
{
33+
return serializationCenter.Serialize(object1) != serializationCenter.Serialize(object2);
34+
}
35+
36+
/// <summary>
37+
/// Clone the given object.
38+
/// </summary>
39+
/// <typeparam name="T">The type of the object to clone.</typeparam>
40+
/// <param name="serializationCenter">The Serialization Center.</param>
41+
/// <param name="source">The object to clone.</param>
42+
/// <returns>The clone of the object.</returns>
43+
static T Clone<T>(ISerializationCenter serializationCenter, T source) where T : notnull
44+
{
45+
return serializationCenter.Deserialize<T>(serializationCenter.Serialize(source));
46+
}
2347
}
2448
}

Core/Public/Utils/ClipboardManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ public static int RemoveAllOccurence(string[] removeList)
88
{
99
int cleanedPasswordCount = 0;
1010

11-
var clipboardHistory = Clipboard.GetHistoryItemsAsync().AsTask().GetAwaiter().GetResult().Items;
11+
IReadOnlyList<ClipboardHistoryItem> clipboardHistory = Clipboard.GetHistoryItemsAsync().AsTask().GetAwaiter().GetResult().Items;
1212

13-
foreach (var item in clipboardHistory)
13+
foreach (ClipboardHistoryItem? item in clipboardHistory)
1414
{
15-
var content = item.Content;
15+
DataPackageView content = item.Content;
1616
if (content.Contains(StandardDataFormats.Text))
1717
{
1818
string text = content.GetTextAsync().AsTask().GetAwaiter().GetResult();
1919

2020
if (removeList.Any(x => x == text))
2121
{
22-
Clipboard.DeleteItemFromHistory(item);
22+
_ = Clipboard.DeleteItemFromHistory(item);
2323
cleanedPasswordCount++;
2424
}
2525
}

0 commit comments

Comments
 (0)