-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into release/8.0.0
- Loading branch information
Showing
25 changed files
with
485 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
using System; | ||
using JetBrains.Annotations; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Qwiq.Identity | ||
{ | ||
/// <summary> | ||
/// Defines a method that converts the value of the implementing reference or value type to another reference or value type. | ||
/// </summary> | ||
public interface IIdentityValueConverter | ||
public interface IIdentityValueConverter<T, U> | ||
{ | ||
/// <summary> | ||
/// Converts the specified <paramref name="value"/> to an <see cref="object"/>. | ||
/// </summary> | ||
/// <param name="value">The value to convert.</param> | ||
/// <returns>An <see cref="object"/> instance whose value is equivalent to the value of <paramref name="value"/>.</returns> | ||
[ContractAnnotation("null => null; notnull => notnull")] | ||
object Map([CanBeNull] object value); | ||
U Map([CanBeNull] T value); | ||
|
||
IReadOnlyDictionary<T,U> Map(IEnumerable<T> values); | ||
|
||
[Obsolete("This method is depreciated and will be removed in a future version.")] | ||
object Map(object value); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,10 @@ namespace Microsoft.Qwiq.Identity | |
/// <summary> | ||
/// Converts a <see cref="string"/> representing an alias to a user principal name. | ||
/// </summary> | ||
/// <seealso cref="IIdentityValueConverter" /> | ||
/// <seealso cref="IIdentityManagementService"/> | ||
public class IdentityAliasValueConverter : IIdentityValueConverter | ||
public class IdentityAliasValueConverter : IdentityValueConverterBase | ||
{ | ||
private static readonly IReadOnlyDictionary<string, object> Empty = new Dictionary<string, object>(); | ||
private readonly string[] _domains; | ||
|
||
private readonly IIdentityManagementService _identityManagementService; | ||
|
@@ -53,29 +53,33 @@ public IdentityAliasValueConverter( | |
_domains = domains; | ||
} | ||
|
||
/// <summary> | ||
/// Converts the specified <paramref name="value" /> to an <see cref="object" />. | ||
/// </summary> | ||
/// <param name="value">The value.</param> | ||
/// <returns>An <see cref="object" /> instance whose value is equivilent to the value of <paramref name="value" />.</returns> | ||
/// <example>"danj" becomes "[email protected]"</example> | ||
[ContractAnnotation("null => null; notnull => notnull")] | ||
public object Map([CanBeNull] object value) | ||
public override IReadOnlyDictionary<string, object> Map(IEnumerable<string> values) | ||
{ | ||
if (value is string stringValue) return GetIdentityNames(stringValue).Single(); | ||
|
||
if (value is IEnumerable<string> stringArray) return GetIdentityNames(stringArray.ToArray()); | ||
if (values == null) return Empty; | ||
var r = GetIdentityForAliases(values.ToList(), _tenantId, _domains); | ||
var retval = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); | ||
foreach (var result in r) | ||
{ | ||
var v = result.Value as ITeamFoundationIdentity; | ||
retval.Add(result.Key, v?.GetIdentityName() ?? result.Key); | ||
} | ||
|
||
return value; | ||
return retval; | ||
} | ||
|
||
private string[] GetIdentityNames(params string[] aliases) | ||
[Obsolete("This method is depreciated and will be removed in a future version.")] | ||
public override object Map(object value) | ||
{ | ||
var identities = GetIdentityForAliases(aliases.ToList(), _tenantId, _domains); | ||
return identities.Select(i => i.Value.GetIdentityName() ?? i.Key).ToArray(); | ||
if (value is string stringValue) return Map(stringValue); | ||
if (value is IEnumerable<string> stringArray) | ||
{ | ||
return Map(stringArray).Select(s => (string)s.Value).ToArray(); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private IDictionary<string, ITeamFoundationIdentity> GetIdentityForAliases( | ||
private Dictionary<string, object> GetIdentityForAliases( | ||
ICollection<string> logonNames, | ||
string tenantId, | ||
params string[] domains) | ||
|
@@ -96,7 +100,7 @@ private IDictionary<string, ITeamFoundationIdentity> GetIdentityForAliases( | |
return identities; | ||
} | ||
|
||
private IDictionary<string, ICollection<IIdentityDescriptor>> CreatePossibleIdentityDescriptors( | ||
private Dictionary<string, ICollection<IIdentityDescriptor>> CreatePossibleIdentityDescriptors( | ||
IEnumerable<string> aliases, | ||
string[] domains, | ||
string tenantId) | ||
|
@@ -119,14 +123,14 @@ private IDictionary<string, ICollection<IIdentityDescriptor>> CreatePossibleIden | |
return descriptors; | ||
} | ||
|
||
private IDictionary<string, ITeamFoundationIdentity> GetIdentitiesForAliases( | ||
private Dictionary<string, object> GetIdentitiesForAliases( | ||
IDictionary<string, ICollection<IIdentityDescriptor>> aliasDescriptors) | ||
{ | ||
var descriptors = aliasDescriptors.SelectMany(ad => ad.Value).ToList(); | ||
var descriptorToAliasLookup = aliasDescriptors | ||
.SelectMany(ad => ad.Value.Select(d => new KeyValuePair<string, string>(d.ToString(), ad.Key))) | ||
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, StringComparer.OrdinalIgnoreCase); | ||
var validIdentities = new Dictionary<string, ITeamFoundationIdentity>(StringComparer.OrdinalIgnoreCase); | ||
var validIdentities = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); | ||
var lookupResults = _identityManagementService.ReadIdentities(descriptors); | ||
foreach (var identity in lookupResults.Where(id => id != null)) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using JetBrains.Annotations; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Qwiq.Identity | ||
{ | ||
public class IdentityFieldValueConverter : IdentityValueConverterBase | ||
{ | ||
private static readonly IReadOnlyDictionary<string, object> Empty = new Dictionary<string, object>(); | ||
[NotNull] private readonly IIdentityManagementService _identityManagementService; | ||
|
||
public IdentityFieldValueConverter( | ||
[NotNull] IIdentityManagementService identityManagementService) | ||
{ | ||
_identityManagementService = identityManagementService ?? throw new ArgumentNullException(nameof(identityManagementService)); | ||
} | ||
|
||
|
||
public override IReadOnlyDictionary<string, object> Map(IEnumerable<string> values) | ||
{ | ||
if (values == null) return Empty; | ||
|
||
var identities = _identityManagementService.ReadIdentities(IdentitySearchFactor.DisplayName, values); | ||
var retval = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); | ||
foreach (var identity in identities) | ||
{ | ||
var c = identity.Value?.Count(); | ||
if (c.GetValueOrDefault(0) == 0) | ||
{ | ||
retval.Add(identity.Key, new IdentityFieldValue(identity.Key)); | ||
continue; | ||
} | ||
|
||
if (c > 1) | ||
{ | ||
var m = | ||
$"Multiple identities found matching '{identity.Key}'. Please specify one of the following identities:{string.Join("\r\n- ", identity.Value)}"; | ||
|
||
throw new MultipleIdentitiesFoundException(m); | ||
} | ||
|
||
var v = new IdentityFieldValue(identity.Value.FirstOrDefault()); | ||
retval.Add(identity.Key, v); | ||
} | ||
|
||
return retval; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Qwiq.Identity | ||
{ | ||
public abstract class IdentityValueConverterBase : IIdentityValueConverter<string, object> | ||
{ | ||
public virtual object Map(string value) | ||
{ | ||
if (string.IsNullOrWhiteSpace(value)) return value; | ||
|
||
var r = Map(new[] {value}); | ||
if (r != null) | ||
{ | ||
var kvp = r.FirstOrDefault(); | ||
if (!EqualityComparer<KeyValuePair<string, object>>.Default.Equals(kvp, default(KeyValuePair<string, object>))) | ||
{ | ||
if (kvp.Value != null) | ||
{ | ||
return kvp.Value; | ||
} | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
public abstract IReadOnlyDictionary<string, object> Map(IEnumerable<string> values); | ||
|
||
[Obsolete("This method is depreciated and will be removed in a future version.")] | ||
public virtual object Map(object value) | ||
{ | ||
if (value is string stringValue) return Map(stringValue); | ||
if (value is IEnumerable<string> stringArray) | ||
{ | ||
return Map(stringArray).Select(s=>s.Value).ToArray(); | ||
} | ||
|
||
return value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
|
||
namespace Microsoft.Qwiq.Identity | ||
{ | ||
[Serializable] | ||
public class MultipleIdentitiesFoundException : ApplicationException | ||
{ | ||
public MultipleIdentitiesFoundException(string message) | ||
: base(message) | ||
{ | ||
} | ||
|
||
public MultipleIdentitiesFoundException() : base() | ||
{ | ||
} | ||
|
||
public MultipleIdentitiesFoundException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
|
||
protected MultipleIdentitiesFoundException(SerializationInfo info, StreamingContext context) : base(info, context) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.