/
Computer.cs
168 lines (137 loc) · 6.46 KB
/
Computer.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace System.DirectoryServices.AccountManagement
{
[DirectoryRdnPrefix("CN")]
public class ComputerPrincipal : AuthenticablePrincipal
{
//
// Public constructors
//
public ComputerPrincipal(PrincipalContext context) : base(context)
{
if (Context.ContextType == ContextType.ApplicationDirectory && this.GetType() == typeof(ComputerPrincipal))
throw new InvalidOperationException(SR.ComputerInvalidForAppDirectoryStore);
this.ContextRaw = context;
this.unpersisted = true;
}
public ComputerPrincipal(PrincipalContext context, string samAccountName, string password, bool enabled) : this(context)
{
if (samAccountName == null || password == null)
throw new ArgumentException(SR.NullArguments);
if (Context.ContextType == ContextType.ApplicationDirectory && this.GetType() == typeof(ComputerPrincipal))
throw new InvalidOperationException(SR.ComputerInvalidForAppDirectoryStore);
if (Context.ContextType != ContextType.ApplicationDirectory)
this.SamAccountName = samAccountName;
this.Name = samAccountName;
this.SetPassword(password);
this.Enabled = enabled;
}
//
// Public properties
//
// ServicePrincipalNames
private PrincipalValueCollection<string> _servicePrincipalNames = new PrincipalValueCollection<string>();
private LoadState _servicePrincipalNamesLoaded = LoadState.NotSet;
public PrincipalValueCollection<string> ServicePrincipalNames
{
get
{
return HandleGet<PrincipalValueCollection<string>>(ref _servicePrincipalNames, PropertyNames.ComputerServicePrincipalNames, ref _servicePrincipalNamesLoaded);
}
}
//
// Public methods
//
public static new PrincipalSearchResult<ComputerPrincipal> FindByLockoutTime(PrincipalContext context, DateTime time, MatchType type)
{
return FindByLockoutTime<ComputerPrincipal>(context, time, type);
}
public static new PrincipalSearchResult<ComputerPrincipal> FindByLogonTime(PrincipalContext context, DateTime time, MatchType type)
{
return FindByLogonTime<ComputerPrincipal>(context, time, type);
}
public static new PrincipalSearchResult<ComputerPrincipal> FindByExpirationTime(PrincipalContext context, DateTime time, MatchType type)
{
return FindByExpirationTime<ComputerPrincipal>(context, time, type);
}
public static new PrincipalSearchResult<ComputerPrincipal> FindByBadPasswordAttempt(PrincipalContext context, DateTime time, MatchType type)
{
return FindByBadPasswordAttempt<ComputerPrincipal>(context, time, type);
}
public static new PrincipalSearchResult<ComputerPrincipal> FindByPasswordSetTime(PrincipalContext context, DateTime time, MatchType type)
{
return FindByPasswordSetTime<ComputerPrincipal>(context, time, type);
}
public static new ComputerPrincipal FindByIdentity(PrincipalContext context, string identityValue)
{
return (ComputerPrincipal)FindByIdentityWithType(context, typeof(ComputerPrincipal), identityValue);
}
public static new ComputerPrincipal FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
return (ComputerPrincipal)FindByIdentityWithType(context, typeof(ComputerPrincipal), identityType, identityValue);
}
//
// Internal "constructor": Used for constructing Computer returned by a query
//
internal static ComputerPrincipal MakeComputer(PrincipalContext ctx)
{
ComputerPrincipal computer = new ComputerPrincipal(ctx);
computer.unpersisted = false;
return computer;
}
//
// Load/Store implementation
//
//
// Loading with query results
//
internal override void LoadValueIntoProperty(string propertyName, object value)
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "Computer", "LoadValueIntoProperty: name=" + propertyName + " value=" + (value == null ? "null" : value.ToString()));
switch (propertyName)
{
case (PropertyNames.ComputerServicePrincipalNames):
_servicePrincipalNames.Load((List<string>)value);
_servicePrincipalNamesLoaded = LoadState.Loaded;
break;
default:
base.LoadValueIntoProperty(propertyName, value);
break;
}
}
//
// Getting changes to persist (or to build a query from a QBE filter)
//
// Given a property name, returns true if that property has changed since it was loaded, false otherwise.
internal override bool GetChangeStatusForProperty(string propertyName)
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "Computer", "GetChangeStatusForProperty: name=" + propertyName);
return propertyName switch
{
PropertyNames.ComputerServicePrincipalNames => _servicePrincipalNames.Changed,
_ => base.GetChangeStatusForProperty(propertyName),
};
}
// Given a property name, returns the current value for the property.
internal override object GetValueForProperty(string propertyName)
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "Computer", "GetValueForProperty: name=" + propertyName);
return propertyName switch
{
PropertyNames.ComputerServicePrincipalNames => _servicePrincipalNames,
_ => base.GetValueForProperty(propertyName),
};
}
// Reset all change-tracking status for all properties on the object to "unchanged".
internal override void ResetAllChangeStatus()
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "Computer", "ResetAllChangeStatus");
_servicePrincipalNames.ResetTracking();
base.ResetAllChangeStatus();
}
}
}