forked from UniStuttgart-VISUS/Visus.LdapAuthentication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchResultEntryExtensions.cs
89 lines (81 loc) · 3.82 KB
/
SearchResultEntryExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// <copyright file="SearchResultEntryExtensions.cs" company="Visualisierungsinstitut der Universität Stuttgart">
// Copyright © 2022 -2024 Visualisierungsinstitut der Universität Stuttgart.
// Licensed under the MIT licence. See LICENCE file for details.
// </copyright>
// <author>Christoph Müller</author>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices.Protocols;
using System.Runtime.CompilerServices;
namespace Visus.DirectoryAuthentication {
/// <summary>
/// Extension methods for <see cref="SearchResultEntry"/>.
/// </summary>
public static class SearchResultEntryExtensions {
/// <summary>
/// Assigns LDAP attributes to the given target object.
/// </summary>
/// <param name="that">The entry holding the properties to assign.
/// </param>
/// <param name="target">The target object to assign the attributes to.
/// </param>
/// <param name="schema">The LDAP schema determining the names of the
/// attributes we search..</param>
/// <exception cref="ArgumentNullException">If <paramref name="that"/>
/// is <c>null</c>, or <paramref name="target"/> is <c>null</c>.
/// </exception>
public static void AssignTo(this SearchResultEntry that, object target,
string schema) {
_ = that ?? throw new ArgumentNullException(nameof(that));
_ = target ?? throw new ArgumentNullException(nameof(target));
var props = LdapAttributeAttribute.GetLdapProperties(
target.GetType(), schema);
foreach (var p in props.Keys) {
try {
var a = props[p];
var v = a.GetValue(that);
p.SetValue(target, v);
} catch (KeyNotFoundException) {
Debug.WriteLine($"LDAP attribute \"{p.Name}\" not found, "
+ "ignoring it while assigning properties.");
continue;
}
}
}
/// <summary>
/// Assigns LDAP attributes to the given target object.
/// </summary>
/// <param name="that">The entry holding the properties to assign.
/// </param>
/// <param name="target">The target object to assign the attributes to.
/// </param>
/// <param name="options">The LDAP options determining the schema that
/// is used while searching for the LDAP attributes.</param>
/// <exception cref="ArgumentNullException">If <paramref name="that"/>
/// is <c>null</c>, or <paramref name="target"/> is <c>null</c>.
/// </exception>
public static void AssignTo(this SearchResultEntry that, object target,
ILdapOptions options)
=> that.AssignTo(target, options?.Schema);
/// <summary>
/// Gets the attribute with the specified name from
/// <paramref name="that"/>.
/// </summary>
/// <remarks>
/// This is a convenience accessor for the
/// <see cref="SearchResultEntry.Attributes"/> property, which reduces
/// the changes required to port from Novell LDAP.
/// </remarks>
/// <param name="that">The entry to retrieve the attribute for.</param>
/// <param name="attribute">The name of the attribute to be retrived.
/// </param>
/// <returns>The <see cref="DirectoryAttribute"/> with the specified
/// name.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static DirectoryAttribute GetAttribute(
this SearchResultEntry that, string attribute) {
return that?.Attributes[attribute];
}
}
}