This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
AcPrincipal.cs
190 lines (174 loc) · 6.9 KB
/
AcPrincipal.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
/*! \file
Copyright (C) 2016-2018 Verizon. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace AcUtils
{
#region enums
/*! \ingroup acenum */
///@{
/// <summary>
/// Whether the principal is active or inactive in AccuRev.
/// </summary>
public enum PrinStatus
{
/*! \var Unknown
The principal status is \e unknown. */
Unknown,
/*! \var Inactive
The principal is \e inactive. */
Inactive,
/*! \var Active
The principal is \e active. */
Active
};
///@}
#endregion
/// <summary>
/// Contains the AccuRev principal attributes \e name, \e ID and \e status (active or inactive)
/// for users and groups. Additionally, lists of group [members](@ref AcUtils#AcGroups#initMembersListAsync)
/// and user group [memberships](@ref AcUtils#AcUser#initGroupsListAsync) are initialized optionally
/// as per [AcGroups](@ref AcUtils#AcGroups#AcGroups) and [AcUsers](@ref AcUtils#AcUsers#AcUsers)
/// constructor parameters \e includeMembersList and \e includeGroupsList respectively.
/// </summary>
[Serializable]
[DebuggerDisplay("{Name} ({ID}) {Status}")]
public sealed class AcPrincipal : IEquatable<AcPrincipal>, IComparable<AcPrincipal>, IComparable
{
#region Class variables
private int _id;
private string _name;
private PrinStatus _status = PrinStatus.Unknown;
private SortedSet<string> _members;
#endregion
#region Constructors
/// <summary>
/// Constructor used during AcUsers and AcGroups list construction. It is called internally and not by user code.
/// </summary>
internal AcPrincipal() { }
#endregion
#region Equality comparison
/*! \name Equality comparison */
/**@{*/
/// <summary>
/// IEquatable implementation to determine the equality of instances of type AcPrincipal.
/// Uses the AccuRev principal ID number to compare instances.
/// </summary>
/// <param name="other">The AcPrincipal object being compared to \e this instance.</param>
/// <returns>\e true if AcPrincipal \e other is the same, \e false otherwise.</returns>
public bool Equals(AcPrincipal other)
{
if (ReferenceEquals(other, null)) return false;
if (ReferenceEquals(this, other)) return true;
return ID == other.ID;
}
/// <summary>
/// Overridden to determine equality.
/// </summary>
/// <returns>Return value of generic [Equals(AcPrincipal)](@ref AcPrincipal#Equals) version.</returns>
public override bool Equals(object other)
{
if (ReferenceEquals(other, null)) return false;
if (ReferenceEquals(this, other)) return true;
if (GetType() != other.GetType()) return false;
return Equals(other as AcPrincipal);
}
/// <summary>
/// Override appropriate for type AcPrincipal.
/// </summary>
/// <returns>AccuRev principal ID number since it's immutable and unique across both users and groups.</returns>
public override int GetHashCode()
{
return ID;
}
/**@}*/
#endregion
#region Order comparison
/*! \name Order comparison */
/**@{*/
/// <summary>
/// Generic IComparable implementation (default) for comparing AcPrincipal objects to sort by AccuRev principal name.
/// </summary>
/// <param name="other">An AcPrincipal object to compare with this instance.</param>
/// <returns>Value indicating the relative order of the AcPrincipal objects being compared.</returns>
public int CompareTo(AcPrincipal other)
{
int result;
if (AcPrincipal.ReferenceEquals(this, other))
result = 0;
else
result = String.Compare(Name, other.Name);
return result;
}
/// <summary>
/// Pre-generic interface implementation for code using reflection.
/// </summary>
/// <param name="other">An AcPrincipal object to compare with this instance.</param>
/// <returns>Return value of generic [CompareTo(AcPrincipal)](@ref AcPrincipal#CompareTo) version.</returns>
/// <exception cref="ArgumentException">thrown if argument is not an AcPrincipal object.</exception>
int IComparable.CompareTo(object other)
{
if (!(other is AcPrincipal))
throw new ArgumentException("Argument is not an AcPrincipal", "other");
AcPrincipal o = (AcPrincipal)other;
return this.CompareTo(o);
}
/**@}*/
#endregion
/// <summary>
/// AccuRev principal ID number for the user or group.
/// </summary>
public int ID
{
get { return _id; }
internal set { _id = value; }
}
/// <summary>
/// AccuRev principal name for the user or group.
/// </summary>
public string Name
{
get { return _name ?? String.Empty; }
internal set { _name = value; }
}
/// <summary>
/// Whether the principal is active or inactive in AccuRev.
/// </summary>
public PrinStatus Status
{
get { return _status; }
internal set { _status = value; }
}
/// <summary>
/// The list of groups a user has membership in, or the list of principals (users and groups)
/// in a group. Both initialized optionally as per [AcGroups](@ref AcUtils#AcGroups#AcGroups)
/// and [AcUsers](@ref AcUtils#AcUsers#AcUsers) constructor parameters \e includeMembersList
/// and \e includeGroupsList respectively.
/// </summary>
/*! \accunote_ We use a set object as a workaround for <tt>show -fx -u \<user\> groups</tt> where
the same group names are repeated multiple times. AccuRev defect 28001. */
public SortedSet<string> Members
{
get { return _members; }
internal set { _members = value; }
}
/// <summary>
/// Returns the AccuRev principal name.
/// </summary>
public override string ToString()
{
return Name;
}
}
}