-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modification.cs
165 lines (143 loc) · 5.86 KB
/
Modification.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
using System;
using System.DirectoryServices.Protocols;
using System.Globalization;
namespace DirLink
{
/// <summary>
/// Helper class for generating <see cref="DirectoryAttributeModification"/> arrays.
/// </summary>
public static class Modification
{
/// <summary>
/// Deletes all values for the specified attribute.
/// </summary>
public static DirectoryAttributeModification Clear(string attributeName)
{
return new DirectoryAttributeModification { Name = attributeName, Operation = DirectoryAttributeOperation.Delete };
}
/// <summary>
/// Sets a single integer value for the specified attribute.
/// </summary>
/// <remarks>
/// All existing values will be removed (if any).
/// </remarks>
public static DirectoryAttributeModification Set(string attributeName, int value)
{
return Set(attributeName, value.ToString(CultureInfo.InvariantCulture));
}
/// <summary>
/// Sets a single long integer value for the specified attribute.
/// </summary>
/// <remarks>
/// All existing values will be removed (if any).
/// </remarks>
public static DirectoryAttributeModification Set(string attributeName, long value)
{
return Set(attributeName, value.ToString(CultureInfo.InvariantCulture));
}
/// <summary>
/// Sets a single boolean value for the specified attribute.
/// </summary>
/// <remarks>
/// All existing values will be removed (if any).
/// </remarks>
public static DirectoryAttributeModification Set(string attributeName, bool value)
{
return Set(attributeName, value ? "TRUE" : "FALSE");
}
/// <summary>
/// Sets a single GUID value for the specified attribute.
/// </summary>
/// <remarks>
/// All existing values will be removed (if any).
/// </remarks>
public static DirectoryAttributeModification Set(string attributeName, Guid value)
{
return Set(attributeName, value.ToByteArray());
}
/// <summary>
/// Sets a single date and time value for the specified attribute.
/// </summary>
/// <param name="asFileTime">Specify <c>true</c> to store the value as file time long integer instead of a real date and time.</param>
/// <remarks>
/// All existing values will be removed (if any).
/// </remarks>
public static DirectoryAttributeModification Set(string attributeName, DateTimeOffset value, bool asFileTime = false)
{
if (asFileTime)
{
return Set(attributeName, value.ToFileTime());
}
else
{
return Set(attributeName, value.ToString("yyyyMMddHHmmmss.fK", CultureInfo.InvariantCulture));
}
}
/// <summary>
/// Sets a single blob value for the specified attribute.
/// </summary>
/// <remarks>
/// All existing values will be removed (if any).
/// </remarks>
public static DirectoryAttributeModification Set(string attributeName, byte[] value)
{
var dam = new DirectoryAttributeModification { Name = attributeName, Operation = DirectoryAttributeOperation.Replace };
dam.Clear();
dam.Add(value);
return dam;
}
/// <summary>
/// Sets a single string value for the specified attribute.
/// </summary>
/// <remarks>
/// All existing values will be removed (if any).
/// </remarks>
public static DirectoryAttributeModification Set(string attributeName, string value)
{
var dam = new DirectoryAttributeModification { Name = attributeName, Operation = DirectoryAttributeOperation.Replace };
dam.Clear();
dam.Add(value);
return dam;
}
/// <summary>
/// Sets multiple string values for the specified attribute.
/// </summary>
/// <remarks>
/// All existing values will be removed (if any).
/// </remarks>
public static DirectoryAttributeModification Set(string attributeName, string[] value)
{
var dam = new DirectoryAttributeModification { Name = attributeName, Operation = DirectoryAttributeOperation.Replace };
dam.Clear();
foreach (var v in value)
{
dam.Add(v);
}
return dam;
}
/// <summary>
/// Adds a single string value for the specified attribute.
/// </summary>
/// <remarks>
/// All existing values will be kept (if any).
/// </remarks>
public static DirectoryAttributeModification Add(string attributeName, string value)
{
var dam = new DirectoryAttributeModification { Name = attributeName, Operation = DirectoryAttributeOperation.Add };
dam.Add(value);
return dam;
}
/// <summary>
/// Removes a single string value from the specified attribute.
/// </summary>
/// <remarks>
/// All other existing values will be kept (if any).
/// </remarks>
public static DirectoryAttributeModification Remove(string attributeName, string value)
{
var dam = new DirectoryAttributeModification { Name = attributeName, Operation = DirectoryAttributeOperation.Delete };
dam.Add(value);
return dam;
}
}
}