forked from UniStuttgart-VISUS/Visus.LdapAuthentication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LdapAttributeAttribute.cs
235 lines (209 loc) · 9.91 KB
/
LdapAttributeAttribute.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// <copyright file="LdapAttributeAttribute.cs" company="Visualisierungsinstitut der Universität Stuttgart">
// Copyright © 2021 - 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.DirectoryServices.Protocols;
using System.Linq;
using System.Reflection;
namespace Visus.DirectoryAuthentication {
/// <summary>
/// Annotates properties of a <see cref="LdapUserBase"/> to enable the class
/// retrieving the property values automatically from a
/// <see cref="System.DirectoryServices.Protocols.SearchResultEntry"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Property,
AllowMultiple = true,
Inherited = false)]
public sealed class LdapAttributeAttribute : Attribute {
#region Public class methods
/// <summary>
/// Gets, if any, the <see cref="LdapAttributeAttribute"/> for the given
/// property and schema.
/// </summary>
/// <param name="property">The <see cref="PropertyInfo"/> of the
/// property to retrieve the attribute for.</param>
/// <param name="schema">The LDAP schema to retrieve the attribute for.
/// </param>
/// <returns>The attribute for the given schema or <c>null</c> if no such
/// attribute was found.</returns>
/// <exception cref="ArgumentNullException">If
/// <paramref name="property"/> is <c>null</c>.</exception>
public static LdapAttributeAttribute GetLdapAttribute(
PropertyInfo property, string schema) {
_ = property ?? throw new ArgumentNullException(nameof(property));
var retval = (from a in property.GetCustomAttributes<LdapAttributeAttribute>()
where a.Schema == schema
select a).FirstOrDefault();
return retval;
}
/// <summary>
/// Gets, if any, the <see cref="LdapAttributeAttribute"/> for the given
/// property of the given type.
/// </summary>
/// <param name="type">The type to retrieve the attribute for.</param>
/// <param name="property">The name of the property to retrieve the
/// attribute of. It is safe to specify the name of a property that does
/// not exist or is not a string property. In this case, the method will
/// return <c>null</c>.</param>
/// <param name="schema">The LDAP schema to retrieve the attribute for.
/// </param>
/// <returns>The attribute for the given schema or <c>null</c> if no such
/// attribute was found.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="type"/>
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">If
/// <paramref name="property"/> is <c>null</c>.</exception>
public static LdapAttributeAttribute GetLdapAttribute(Type type,
string property, string schema) {
_ = type ?? throw new ArgumentNullException(nameof(type));
_ = property ?? throw new ArgumentNullException(nameof(property));
var prop = type.GetProperty(property);
if ((prop != null) && (prop.PropertyType == typeof(string))) {
return GetLdapAttribute(prop, schema);
} else {
return null;
}
}
/// <summary>
/// Gets, if any, the <see cref="LdapAttributeAttribute"/> for the given
/// property of the given type.
/// </summary>
/// <typeparam name="TType">The type to retrieve the attribute for.
/// </typeparam>
/// <param name="property">The name of the property to retrieve the
/// attribute of. It is safe to specify the name of a property that does
/// not exist or is not a string property. In this case, the method will
/// return <c>null</c>.</param>
/// <param name="schema">The LDAP schema to retrieve the attribute for.
/// </param>
/// <returns>The attribute for the given schema or <c>null</c> if no such
/// attribute was found.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="type"/>
/// is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">If
/// <paramref name="property"/> is <c>null</c>.</exception>
public static LdapAttributeAttribute GetLdapAttribute<TType>(
string property, string schema) {
return GetLdapAttribute(typeof(TType), property, schema);
}
/// <summary>
/// Gets all annotated and string-convertible properties of
/// <paramref name="type"/> that support the given schema.
/// </summary>
/// <param name="type">The type to retrieve the attributes for.</param>
/// <param name="schema">The LDAP schema to retrieve the attribute for.
/// </param>
/// <returns>The properties and their attributes.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="type"/>
/// is <c>null</c>.</exception>
public static Dictionary<PropertyInfo, LdapAttributeAttribute>
GetLdapProperties(Type type, string schema) {
_ = type ?? throw new ArgumentNullException(nameof(type));
var retval = new Dictionary<PropertyInfo, LdapAttributeAttribute>();
var props = from p in type.GetProperties()
let a = p.GetCustomAttributes<LdapAttributeAttribute>()
.Where(a => a.Schema == schema)
.FirstOrDefault()
where (a != null)
//&& ((p.PropertyType == typeof(string))
//|| (p.PropertyType == typeof(bool)))
select new {
Property = p,
Attribute = a
};
var patchSetter = typeof(LdapUserBase).IsAssignableFrom(type);
foreach (var p in props) {
if (patchSetter && (p.Property?.SetMethod?.IsPublic != true)) {
var pp = typeof(LdapUserBase).GetProperty(p.Property.Name);
retval[pp ?? p.Property] = p.Attribute;
} else {
retval[p.Property] = p.Attribute;
}
}
return retval;
}
/// <summary>
/// Gets all annotated and string-convertible properties of
/// <typeparamref name="TType"/> that support the given schema.
/// </summary>
/// <typeparam name="TType">The type to retrieve the attributes for.
/// </typeparam>
/// <param name="schema">he LDAP schema to retrieve the attribute for.
/// </param>
/// <returns>The properties and their attributes.</returns>
public static Dictionary<PropertyInfo, LdapAttributeAttribute>
GetLdapProperties<TType>(string schema) {
return GetLdapProperties(typeof(TType), schema);
}
#endregion
#region Public constructors
/// <summary>
/// Initialises a new instance.
/// </summary>
/// <param name="schema">The name of the schema the mapping is valid
/// for.</param>
/// <param name="name">The name of the LDAP attribute to lookup for the
/// annotated property.</param>
public LdapAttributeAttribute(string schema, string name) {
this.Name = name
?? throw new ArgumentNullException(nameof(name));
this.Schema = schema
?? throw new ArgumentNullException(nameof(schema));
}
#endregion
#region Public properties
/// <summary>
/// Getr or sets the type of an optional converter transforming the
/// attribute into a usable form.
/// </summary>
public Type Converter { get; set; }
/// <summary>
/// Gets the name of the LDAP attribute storing the value of the
/// property.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the name of the LDAP schema the mapping is valid for.
/// </summary>
public string Schema { get; }
#endregion
#region Public methods
/// <summary>
/// Instantiates the converter if any.
/// </summary>
/// <returns>A <see cref="ILdapAttributeConverter"/> or <c>null</c> if
/// no converter was annotated.</returns>
public ILdapAttributeConverter GetConverter() {
if (this.Converter != null) {
return Activator.CreateInstance(this.Converter)
as ILdapAttributeConverter;
} else {
return null;
}
}
/// <summary>
/// Gets the value described by the attribute from the given entry.
/// </summary>
/// <param name="entry">The entry to get the attribute from.</param>
/// <param name="parameter">An optional converter parameter.</param>
/// <returns>The value of the attribute, normally as
/// <see cref="string"/>.
/// </returns>
/// <exception cref="ArgumentNullException">If <paramref name="entry"/>
/// is <c>null</c>.</exception>
/// <exception cref="System.Collections.Generic.KeyNotFoundException">
/// If the designated attribute does not exist for the entry or if it
/// has not been loaded.</exception>
public object GetValue(SearchResultEntry entry,
object parameter = null) {
_ = entry ?? throw new ArgumentNullException(nameof(entry));
var attribute = entry.Attributes[this.Name];
var retval = attribute.GetValue(this, parameter);
return retval;
}
#endregion
}
}