Skip to content

Commit

Permalink
refactored a bit to use the Maybe pattern
Browse files Browse the repository at this point in the history
implemented via ienumerable<t> so we can make use of the existing ienum monads
  • Loading branch information
sparerd committed Jul 28, 2017
1 parent 7a2ab59 commit 792edd9
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using mRemoteNG.Credential;
using mRemoteNG.Security;
using mRemoteNG.Security.Factories;
using mRemoteNG.Tools;
using NSubstitute;
using NUnit.Framework;

Expand Down Expand Up @@ -79,7 +80,7 @@ private class TestCaseDataSource
Username = "myuser",
Domain = "superdomain",
Password = "pass",
CredentialRecordId = Guid.Empty,
CredentialRecordId = Guid.Empty.Maybe(),
Hostname = "somehost",
ExtApp = "myextapp",
PreExtApp = "preext1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using mRemoteNG.Connection;
using mRemoteNG.Credential;
using mRemoteNG.Security;
using mRemoteNG.Tools;
using mRemoteNG.Tree;
using NSubstitute;
using NUnit.Framework;
Expand Down Expand Up @@ -87,7 +88,7 @@ private ConnectionInfo BuildConnectionInfo()
return new ConnectionInfo
{
Name = ConnectionName,
CredentialRecordId = Guid.NewGuid(),
CredentialRecordId = Guid.NewGuid().Maybe(),
Inheritance = {Colors = true}
};
}
Expand Down
23 changes: 23 additions & 0 deletions mRemoteNGTests/Tools/MaybeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using mRemoteNG.Tools;
using NUnit.Framework;

namespace mRemoteNGTests.Tools
{
public class MaybeTests
{
[Test]
public void MaybeReturnsEmptyListWhenGivenNullValue()
{
var sut = new Maybe<object>(null);
Assert.That(sut, Is.Empty);
}

[Test]
public void MaybeReturnsValueIfNotNull()
{
var expected = new object();
var sut = new Maybe<object>(expected);
Assert.That(sut, Has.Member(expected));
}
}
}
1 change: 1 addition & 0 deletions mRemoteNGTests/mRemoteNGTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
<Compile Include="TestHelpers\FileTestHelpers.cs" />
<Compile Include="Tools\ExternalToolsArgumentParserTests.cs" />
<Compile Include="Tools\FullyObservableCollectionTests.cs" />
<Compile Include="Tools\MaybeTests.cs" />
<Compile Include="Tree\ClickHandlers\TreeNodeCompositeClickHandlerTests.cs" />
<Compile Include="Tree\ConnectionTreeDragAndDropHandlerTests.cs" />
<Compile Include="Tree\ConnectionTreeModelTests.cs" />
Expand Down
3 changes: 2 additions & 1 deletion mRemoteV1/App/Initialization/CredsAndConsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using mRemoteNG.Connection;
using mRemoteNG.Credential;
using mRemoteNG.Tools;

namespace mRemoteNG.App.Initialization
{
Expand Down Expand Up @@ -37,7 +38,7 @@ private void LoadDefaultConnectionCredentials()
{
var defaultCredId = Settings.Default.ConDefaultCredentialRecord;
var matchedCredentials = _credentialsService.GetCredentialRecords().Where(record => record.Id.Equals(defaultCredId)).ToArray();
DefaultConnectionInfo.Instance.CredentialRecordId = matchedCredentials.Any() ? matchedCredentials.First().Id : default(Guid?);
DefaultConnectionInfo.Instance.CredentialRecordId = matchedCredentials.FirstOrDefault()?.Id.Maybe();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using mRemoteNG.Credential;
using mRemoteNG.Messages;
using mRemoteNG.Security;
using mRemoteNG.Tools;
using mRemoteNG.Tree;
using mRemoteNG.Tree.Root;
using mRemoteNG.UI.Forms;
Expand Down Expand Up @@ -508,7 +509,7 @@ private ConnectionInfo GetConnectionInfoFromXml(XmlNode xmlnode)
if (_confVersion >= 2.7)
{
connectionInfo.Inheritance.CredentialRecord = bool.Parse(xmlnode.Attributes["InheritCredentialRecord"]?.Value ?? "False");
connectionInfo.CredentialRecordId = Guid.Parse(xmlnode.Attributes["CredentialId"]?.Value ?? "");
connectionInfo.CredentialRecordId = Guid.Parse(xmlnode.Attributes["CredentialId"]?.Value ?? "").Maybe();
}
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ private void SerializeConnectionInfo(ConnectionInfo con)

csvLine += con.Name + ";" + GetNodePath(con) + ";" + con.Description + ";" + con.Icon + ";" + con.Panel + ";";

if (con.CredentialRecordId.HasValue)
if (con.CredentialRecordId.Any())
{
var credentialRecord =
_credentialRepositoryList.GetCredentialRecord(con.CredentialRecordId.Value);
_credentialRepositoryList.GetCredentialRecord(con.CredentialRecordId.Single());

if (_saveFilter.SaveUsername)
csvLine += credentialRecord?.Username + ";";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void ApplyCredentialMapping(IDictionary<Guid, ICredentialRecord> map, IEn
Guid id;
Guid.TryParse(connectionInfo.ConstantID, out id);
if (map.ContainsKey(id))
connectionInfo.CredentialRecordId = map[id].Id;
connectionInfo.CredentialRecordId = map[id].Id.Maybe();
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions mRemoteV1/Connection/AbstractConnectionRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using mRemoteNG.Connection.Protocol.ICA;
using mRemoteNG.Connection.Protocol.RDP;
using mRemoteNG.Connection.Protocol.VNC;
using mRemoteNG.Credential;
using mRemoteNG.Tools;
using mRemoteNG.UI.Controls;
// ReSharper disable ArrangeAccessorOwnerBody
Expand All @@ -24,7 +23,7 @@ public abstract class AbstractConnectionRecord : INotifyPropertyChanged
private string _panel = "";

private string _hostname = "";
private Guid? _credentialRecordId;
private Maybe<Guid> _credentialRecordId = new Maybe<Guid>();

private ProtocolType _protocol;
private string _extApp = "";
Expand Down Expand Up @@ -137,7 +136,7 @@ public virtual string Hostname
LocalizedAttributes.LocalizedDescription(nameof(Language.strPropertyDescriptionCredential))]
[Editor(typeof(CredentialRecordListAdaptor), typeof(UITypeEditor))]
[TypeConverter(typeof(ExpandableObjectConverter))]
public virtual Guid? CredentialRecordId
public virtual Maybe<Guid> CredentialRecordId
{
get { return GetPropertyValue(nameof(CredentialRecordId), _credentialRecordId); }
set { SetField(ref _credentialRecordId, value, nameof(CredentialRecordId)); }
Expand Down
5 changes: 3 additions & 2 deletions mRemoteV1/Connection/Protocol/ICA/IcaProtocol.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Security;
using AxWFICALib;
using System.Windows.Forms;
Expand Down Expand Up @@ -123,9 +124,9 @@ private void SetCredentials()
var pass = new SecureString();
var dom = "";

if (_info.CredentialRecordId.HasValue)
if (_info.CredentialRecordId.Any())
{
var credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(_info.CredentialRecordId.Value);
var credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(_info.CredentialRecordId.Single());
user = credentialRecord?.Username ?? "";
pass = credentialRecord?.Password ?? "".ConvertToSecureString();
dom = credentialRecord?.Domain ?? "";
Expand Down
5 changes: 3 additions & 2 deletions mRemoteV1/Connection/Protocol/PuttyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using mRemoteNG.Credential;
Expand Down Expand Up @@ -75,9 +76,9 @@ public override bool Connect()
var password = "";
var credentialRecord = default(ICredentialRecord);

if (InterfaceControl.Info.CredentialRecordId.HasValue)
if (InterfaceControl.Info.CredentialRecordId.Any())
{
credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(InterfaceControl.Info.CredentialRecordId.Value);
credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(InterfaceControl.Info.CredentialRecordId.Single());
}

if (!string.IsNullOrEmpty(credentialRecord?.Username))
Expand Down
9 changes: 5 additions & 4 deletions mRemoteV1/Connection/Protocol/RDP/RdpProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;
using System.Linq;
using System.Security;
using mRemoteNG.Messages;
using mRemoteNG.App;
Expand Down Expand Up @@ -346,9 +347,9 @@ private void SetRdGateway()
{
if (_connectionInfo.RDGatewayUseConnectionCredentials == RDGatewayUseConnectionCredentials.Yes)
{
if (!_connectionInfo.CredentialRecordId.HasValue) return;
if (!_connectionInfo.CredentialRecordId.Any()) return;
var credentialRecord =
Runtime.CredentialProviderCatalog.GetCredentialRecord(_connectionInfo.CredentialRecordId.Value);
Runtime.CredentialProviderCatalog.GetCredentialRecord(_connectionInfo.CredentialRecordId.Single());
_rdpClient.TransportSettings2.GatewayUsername = credentialRecord?.Username;
_rdpClient.TransportSettings2.GatewayPassword = credentialRecord?.Password.ConvertToUnsecureString();
_rdpClient.TransportSettings2.GatewayDomain = credentialRecord?.Domain;
Expand Down Expand Up @@ -424,9 +425,9 @@ private void SetCredentials()
var password = new SecureString();
var domain = "";

if (_connectionInfo.CredentialRecordId.HasValue)
if (_connectionInfo.CredentialRecordId.Any())
{
var credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(_connectionInfo.CredentialRecordId.Value);
var credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(_connectionInfo.CredentialRecordId.Single());
userName = credentialRecord?.Username ?? "";
password = credentialRecord?.Password ?? new SecureString();
domain = credentialRecord?.Domain ?? "";
Expand Down
9 changes: 5 additions & 4 deletions mRemoteV1/Connection/Protocol/VNC/Connection.Protocol.VNC.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using mRemoteNG.App;
using System.ComponentModel;
using System.Linq;
using mRemoteNG.Security;
using mRemoteNG.Tools;
using mRemoteNG.UI.Forms;
Expand Down Expand Up @@ -165,9 +166,9 @@ private void SetEventHandlers()
_VNC.ConnectComplete += VNCEvent_Connected;
_VNC.ConnectionLost += VNCEvent_Disconnected;
FrmMain.ClipboardChanged += VNCEvent_ClipboardChanged;
if (!Info.CredentialRecordId.HasValue)
if (!Info.CredentialRecordId.Any())
return;
var credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(Info.CredentialRecordId.Value);
var credentialRecord = Runtime.CredentialProviderCatalog.GetCredentialRecord(Info.CredentialRecordId.Single());
if (((int)Force & (int)ConnectionInfo.Force.NoCredentials) != (int)ConnectionInfo.Force.NoCredentials
&& credentialRecord?.Password?.Length > 0)
{
Expand Down Expand Up @@ -202,8 +203,8 @@ private void VNCEvent_ClipboardChanged()

private string VNCEvent_Authenticate()
{
return Info.CredentialRecordId.HasValue
? Runtime.CredentialProviderCatalog.GetCredentialRecord(Info.CredentialRecordId.Value).Password.ConvertToUnsecureString()
return Info.CredentialRecordId.Any()
? Runtime.CredentialProviderCatalog.GetCredentialRecord(Info.CredentialRecordId.Single())?.Password?.ConvertToUnsecureString()
: "";
}

Expand Down
11 changes: 11 additions & 0 deletions mRemoteV1/Tools/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

namespace mRemoteNG.Tools
{
public static class Extensions
{
public static Maybe<T> Maybe<T>(this T value)
{
return new Maybe<T>(value);
}
}
}
32 changes: 32 additions & 0 deletions mRemoteV1/Tools/Maybe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections;
using System.Collections.Generic;

namespace mRemoteNG.Tools
{
public class Maybe<T> : IEnumerable<T>
{
private readonly T[] _maybe;

public Maybe()
{
_maybe = new T[0];
}

public Maybe(T value)
{
_maybe = value != null
? new[] {value}
: new T[0];
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public IEnumerator<T> GetEnumerator()
{
return ((IEnumerable<T>)_maybe).GetEnumerator();
}
}
}
2 changes: 2 additions & 0 deletions mRemoteV1/mRemoteV1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,14 @@
<Compile Include="Security\Factories\CryptoProviderFactoryFromXml.cs" />
<Compile Include="Tools\Cmdline\StartupArgumentsInterpreter.cs" />
<Compile Include="Tools\CustomCollections\CollectionUpdatedEventArgs.cs" />
<Compile Include="Tools\Extensions.cs" />
<Compile Include="Tools\ExternalToolArgumentParser.cs" />
<Compile Include="Tools\Cmdline\CmdArgumentsInterpreter.cs" />
<Compile Include="Tools\ConnectionsTreeToMenuItemsConverter.cs" />
<Compile Include="Tools\ExternalToolsService.cs" />
<Compile Include="Tools\ExternalToolsTypeConverter.cs" />
<Compile Include="Tools\CustomCollections\INotifyCollectionUpdated.cs" />
<Compile Include="Tools\Maybe.cs" />
<Compile Include="Tools\MouseClickSimulator.cs" />
<Compile Include="Tools\NotificationAreaIcon.cs" />
<Compile Include="Tools\ScanHost.cs" />
Expand Down

0 comments on commit 792edd9

Please sign in to comment.