Skip to content

Commit 47af67b

Browse files
committed
Use GUID for Credentials... Fix edit profile/credential
1 parent 638fb91 commit 47af67b

16 files changed

Lines changed: 153 additions & 74 deletions

‎Source/NETworkManager/Models/Settings/CredentialInfo.cs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
using System.Security;
1+
using System;
2+
using System.Security;
23

34
namespace NETworkManager.Models.Settings
45
{
56
public class CredentialInfo
67
{
7-
public int ID { get; set; }
8+
public Guid ID { get; set; }
89
public string Name { get; set; }
910
public string Username { get; set; }
1011
public SecureString Password { get; set; }
1112

1213
public CredentialInfo()
1314
{
14-
15+
ID = Guid.NewGuid();
1516
}
1617

17-
public CredentialInfo(int id)
18+
public CredentialInfo(Guid id)
1819
{
1920
ID = id;
2021
}
2122

22-
public CredentialInfo(int id, string name)
23+
public CredentialInfo(Guid id, string name)
2324
{
2425
ID = id;
2526
Name = name;
2627
}
2728

28-
public CredentialInfo(int id, string name, string username, SecureString password)
29+
public CredentialInfo(Guid id, string name, string username, SecureString password)
2930
{
3031
ID = id;
3132
Name = name;

‎Source/NETworkManager/Models/Settings/CredentialInfoSerializable.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
namespace NETworkManager.Models.Settings
1+
using System;
2+
3+
namespace NETworkManager.Models.Settings
24
{
35
public class CredentialInfoSerializable
46
{
5-
public int ID { get; set; }
7+
public Guid ID { get; set; }
68
public string Name { get; set; }
79
public string Username { get; set; }
810
public string Password { get; set; }
@@ -12,7 +14,7 @@ public CredentialInfoSerializable()
1214

1315
}
1416

15-
public CredentialInfoSerializable(int id, string name, string username, string password)
17+
public CredentialInfoSerializable(Guid id, string name, string username, string password)
1618
{
1719
ID = id;
1820
Name = name;

‎Source/NETworkManager/Models/Settings/CredentialManager.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Contains code from: https://stackoverflow.com/questions/10168240/encrypting-decrypting-a-string-in-c-sharp
22

3+
using System;
34
using NETworkManager.Utilities;
45
using System.Collections.Generic;
56
using System.Collections.ObjectModel;
@@ -26,7 +27,7 @@ public static IEnumerable<CredentialInfo> CredentialInfoList
2627
get { return Credentials.Select(x => new CredentialInfo(x.ID, x.Name)); }
2728
}
2829

29-
public static CredentialInfo GetCredentialByID(int id)
30+
public static CredentialInfo GetCredentialByID(Guid id)
3031
{
3132
return Credentials.FirstOrDefault(x => x.ID == id);
3233
}
@@ -137,16 +138,6 @@ private static void Credentials_CollectionChanged(object sender, System.Collecti
137138
CredentialsChanged = true;
138139
}
139140

140-
public static int GetNextID()
141-
{
142-
if (Credentials.Count == 0)
143-
return 0;
144-
145-
var credential = Credentials.OrderByDescending(x => x.ID).FirstOrDefault();
146-
147-
return credential?.ID + 1 ?? 0;
148-
}
149-
150141
public static void SetMasterPassword(SecureString masterPasword)
151142
{
152143
_masterPassword = masterPasword;

‎Source/NETworkManager/Models/Settings/ProfileInfo.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using static NETworkManager.Models.PuTTY.PuTTY;
34

45
namespace NETworkManager.Models.Settings
@@ -8,7 +9,7 @@ public class ProfileInfo
89
{
910
public string Name { get; set; }
1011
public string Host { get; set; }
11-
public int CredentialID { get; set; } = -1;
12+
public Guid CredentialID { get; set; } = Guid.Empty;
1213
public string Group { get; set; }
1314
public string Tags { get; set; }
1415

‎Source/NETworkManager/NETworkManager.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@
284284
<Compile Include="Models\Update\UpdateAvailableArgs.cs" />
285285
<Compile Include="Models\Update\Updater.cs" />
286286
<Compile Include="Validators\IPv4IPv6SubnetmaskOrCIDRValidator.cs" />
287+
<Compile Include="Validators\IsCredentialNameUnique.cs" />
287288
<Compile Include="Validators\NoSpacesValidator.cs" />
288289
<Compile Include="Validators\BaudValidator.cs" />
289290
<Compile Include="Validators\PrivacyAESValidator.cs" />
@@ -1123,6 +1124,7 @@
11231124
<EmbeddedResource Include="Resources\Localization\Strings.resx">
11241125
<Generator>PublicResXFileCodeGenerator</Generator>
11251126
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
1127+
<SubType>Designer</SubType>
11261128
</EmbeddedResource>
11271129
<EmbeddedResource Include="Resources\Localization\Strings.de-DE.resx" />
11281130
<EmbeddedResource Include="Resources\Localization\Strings.ru-RU.resx" />

‎Source/NETworkManager/Resources/Localization/Strings.Designer.cs‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Source/NETworkManager/Resources/Localization/Strings.resx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,4 +1932,7 @@
19321932
<data name="ProfileWithThisNameAlreadyExists" xml:space="preserve">
19331933
<value>A profile with this name already exists!</value>
19341934
</data>
1935+
<data name="CredentialWithThisNameAlreadyExists" xml:space="preserve">
1936+
<value>Credential with this name already exists!</value>
1937+
</data>
19351938
</root>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Linq;
4+
using System.Windows.Controls;
5+
using NETworkManager.Models.Settings;
6+
7+
namespace NETworkManager.Validators
8+
{
9+
public class IsCredentialNameUnique : ValidationRule
10+
{
11+
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
12+
{
13+
return CredentialManager.Credentials.Any(x => string.Equals(x.Name, value as string, StringComparison.OrdinalIgnoreCase)) ? new ValidationResult(false, Resources.Localization.Strings.CredentialWithThisNameAlreadyExists) : ValidationResult.ValidResult;
14+
}
15+
}
16+
}

‎Source/NETworkManager/ViewModels/CredentialViewModel.cs‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class CredentialViewModel : ViewModelBase
1414

1515
public ICommand CancelCommand { get; }
1616

17-
private int _id;
18-
public int Id
17+
private Guid _id;
18+
public Guid Id
1919
{
2020
get => _id;
2121
set
@@ -115,36 +115,36 @@ public bool PasswordIsEmpty
115115
}
116116
}
117117

118-
private bool _isBeingEdited;
119-
public bool IsBeingEdited
118+
private bool _isEdited;
119+
public bool IsEdited
120120
{
121-
get => _isBeingEdited;
121+
get => _isEdited;
122122
set
123123
{
124-
if (value == _isBeingEdited)
124+
if (value == _isEdited)
125125
return;
126126

127-
_isBeingEdited = value;
127+
_isEdited = value;
128128
OnPropertyChanged();
129129
}
130130
}
131131

132-
public CredentialViewModel(Action<CredentialViewModel> saveCommand, Action<CredentialViewModel> cancelHandler, int id, CredentialInfo credentialInfo = null)
132+
public CredentialViewModel(Action<CredentialViewModel> saveCommand, Action<CredentialViewModel> cancelHandler, CredentialInfo credentialInfo = null)
133133
{
134134
_isLoading = true;
135135

136136
SaveCommand = new RelayCommand(p => saveCommand(this));
137137
CancelCommand = new RelayCommand(p => cancelHandler(this));
138138

139+
_isEdited = credentialInfo != null;
140+
139141
_credentialInfo = credentialInfo ?? new CredentialInfo();
140142

141-
Id = id;
143+
Id = _credentialInfo.ID;
142144
Name = _credentialInfo.Name;
143145
Username = _credentialInfo.Username;
144146
Password = _credentialInfo.Password;
145147

146-
_isBeingEdited = credentialInfo != null;
147-
148148
_isLoading = false;
149149
}
150150

‎Source/NETworkManager/ViewModels/CredentialsViewModel.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public async void Add()
358358
}, instance =>
359359
{
360360
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
361-
}, CredentialManager.GetNextID());
361+
});
362362

363363
customDialog.Content = new CredentialDialog
364364
{
@@ -395,7 +395,7 @@ public async void Edit()
395395
}, instance =>
396396
{
397397
_dialogCoordinator.HideMetroDialogAsync(this, customDialog);
398-
}, SelectedCredential.ID, SelectedCredential);
398+
}, SelectedCredential);
399399

400400
customDialog.Content = new CredentialDialog
401401
{

0 commit comments

Comments
 (0)