17 using System.Collections.Generic;
18 using System.ComponentModel;
37 public sealed
class AcGroups : List<AcPrincipal>
39 #region Class variables
40 private bool _includeMembersList;
41 private bool _includeDeactivated;
42 [NonSerialized]
private readonly
object _locker =
new object();
43 [NonSerialized]
private int _counter;
46 #region object construction:
91 public AcGroups(
bool includeMembersList =
false,
bool includeDeactivated =
false)
93 _includeMembersList = includeMembersList;
94 _includeDeactivated = includeDeactivated;
107 public async Task<bool>
initAsync(IProgress<int> progress = null)
113 .ConfigureAwait(
false);
114 if (r != null && r.
RetVal == 0)
116 XElement xml = XElement.Parse(r.
CmdResult);
117 IEnumerable<XElement> query =
from element in xml.Elements(
"Element") select element;
118 List<Task<bool>> tasks = null;
119 if (_includeMembersList)
121 int num = query.Count();
122 tasks =
new List<Task<bool>>(num);
124 Func<Task<bool>,
bool> cf = t =>
127 if (res && progress != null) progress.Report(Interlocked.Increment(ref _counter));
131 foreach (XElement e
in query)
134 group.
Name = (string)e.Attribute(
"Name");
135 group.
ID = (int)e.Attribute(
"Number");
138 lock (_locker) { Add(group); }
139 if (_includeMembersList)
146 if (!_includeMembersList)
150 bool[] arr = await Task.WhenAll(tasks).ConfigureAwait(
false);
151 ret = (arr != null && arr.All(n => n ==
true));
158 AcDebug.
Log($
"AcUtilsException caught and logged in AcGroups.initAsync{Environment.NewLine}{ecx.Message}");
161 catch (Exception ecx)
163 AcDebug.
Log($
"Exception caught and logged in AcGroups.initAsync{Environment.NewLine}{ecx.Message}");
193 if (r != null && r.
RetVal == 0)
195 SortedSet<string> members =
new SortedSet<string>();
196 XElement xml = XElement.Parse(r.
CmdResult);
197 foreach (XElement e
in xml.Elements(
"Element"))
199 string name = (string)e.Attribute(
"User");
215 AcDebug.
Log($
"AcUtilsException caught and logged in AcGroups.initMembersListAsync{Environment.NewLine}{ecx.Message}");
218 catch (Exception ecx)
220 AcDebug.
Log($
"Exception caught and logged in AcGroups.initMembersListAsync{Environment.NewLine}{ecx.Message}");
245 string command = String.Format(
@"ismember ""{0}"" ""{1}""", user, group);
248 using (Process process =
new Process())
250 process.StartInfo.FileName =
"accurev";
251 process.StartInfo.Arguments = command;
252 process.StartInfo.UseShellExecute =
false;
253 process.StartInfo.CreateNoWindow =
true;
254 process.StartInfo.RedirectStandardInput =
true;
255 process.StartInfo.RedirectStandardOutput =
true;
256 process.StartInfo.RedirectStandardError =
true;
257 process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
258 process.StartInfo.StandardErrorEncoding = Encoding.UTF8;
261 process.BeginErrorReadLine();
262 string result = process.StandardOutput.ReadToEnd();
263 process.WaitForExit();
264 if (process.ExitCode == 0)
266 if (result.Length > 0 && result[0] ==
'1')
271 string err = String.Format(
"AccuRev program return: {0}{1}{2}", process.ExitCode, Environment.NewLine,
"accurev " + command);
277 catch (Win32Exception ecx)
279 string msg = String.Format(
"Win32Exception caught and logged in AcGroups.isMember{0}{1}{0}accurev {2}{0}errorcode: {3}{0}native errorcode: {4}{0}{5}{0}{6}{0}{7}",
280 Environment.NewLine, ecx.Message, command, ecx.ErrorCode.ToString(), ecx.NativeErrorCode.ToString(), ecx.StackTrace, ecx.Source, ecx.GetBaseException().Message);
284 catch (InvalidOperationException ecx)
286 string msg = String.Format(
"InvalidOperationException caught and logged in AcGroups.isMember{0}{1}{0}accurev {2}",
287 Environment.NewLine, ecx.Message, command);
301 return this.SingleOrDefault(n => n.Name == name);
317 SortedSet<string> members = prncpl.
Members;
319 list = String.Join(
", ", members);
AcGroups(bool includeMembersList=false, bool includeDeactivated=false)
A container class for AcPrincipal objects that define AccuRev groups. Elements contain AccuRev group ...
int ID
AccuRev principal ID number for the user or group.
async Task< bool > initAsync(IProgress< int > progress=null)
Populate this container with AcPrincipal objects as per constructor parameters.
A container class for AcPrincipal objects that define AccuRev groups.
AccuRev program return value and command result.
AcPrincipal getPrincipal(string name)
Retrieves the AcPrincipal object for AccuRev group name.
PrinStatus
Whether the principal is active or inactive in AccuRev.
Contains the AccuRev principal attributes name, ID and status (active or inactive) for users and grou...
async Task< bool > initMembersListAsync(string group)
Optionally called during list construction to initialize the list of principals (users and groups) th...
string getMembers(string group)
Returns the list of members in group as a formatted string. List optionally initialized by AcGroups c...
string CmdResult
The command result (usually XML) emitted by AccuRev.
int RetVal
The AccuRev program return value for the command, otherwise minus one (-1) on error.
static void Log(string message, bool formatting=true)
Write the message text to STDOUT, to weekly log files located in %LOCALAPPDATA%\AcTools\Logs, and to trigger.log in the AccuRev server's ..storage\site_slice\logs folder in the case of triggers.
static void errorDataHandler(object sendingProcess, DataReceivedEventArgs errLine)
Centralized error handler.
string Name
AccuRev principal name for the user or group.
Exception thrown when an AccuRev command fails. The AccuRev program return value is zero (0) on succe...
AccuRev command processing.
static async Task< AcResult > runAsync(string command, ICmdValidate validator=null)
Run the AccuRev command asynchronously with non-blocking I/O.
Use to log and display error and general purpose text messages, and to save the XML param data sent b...
SortedSet< string > Members
The list of groups a user has membership in, or the list of principals (users and groups) in a group...
static bool isMember(string user, string group)
Determines if user is a member of group by way of direct or indirect (implicit) membership, e.g. Mary is implicitly a member of groupA because she's a member of groupB which is a member of groupA.
PrinStatus Status
Whether the principal is active or inactive in AccuRev.