/******************************************************************************* Copyright (c) 2011, Perforce Software, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ /******************************************************************************* * Name : User.cs * * Author : dbb * * Description : Class used to abstract a form specification in Perforce. * ******************************************************************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Perforce.P4 { /// /// Defines the user type. /// [Flags] public enum UserType { /// /// Standard: a normal user (licensed) /// Standard = 0x000, /// /// Service: a service user (not licensed) /// Service = 0x001, /// /// Operator: an operator user (not licensed) /// Operator = 0x002 } /// /// A user on a Perforce Server, represented by a P4 User spec. /// /// ///
p4 help user ///
///
user -- Create or edit a user specification ///
///
p4 user [-f] [name] ///
p4 user -d [-f] name ///
p4 user -o [name] ///
p4 user -i [-f] ///
///
Create a new user specification or edit an existing user specification. ///
The specification form is put into a temporary file and the editor ///
(configured by the environment variable $P4EDITOR) is invoked. ///
///
Normally, a user specification is created automatically the first ///
time that the user issues any command that updates the depot. The ///
'p4 user' command is typically used to edit the user's subscription ///
list for change review. ///
///
The user specification form contains the following fields: ///
///
User: The user name (read-only). ///
///
Email: The user's email address (Default: user@client). ///
///
Update: The date the specification was last modified (read-only). ///
///
Access: The date that the user last issued a client command. ///
///
FullName: The user's real name. ///
///
JobView: Selects jobs that are displayed when the user creates ///
a changelist. These jobs can be closed automatically ///
when the user submits the changelist. For a description ///
of jobview syntax, see 'p4 help jobview' ///
///
Reviews: The subscription list for change review. There is no ///
limit on the number of lines that this field can contain. ///
You can include the following wildcards: ///
///
... matches any characters including / ///
* matches any character except / ///
///
Password: The user's password. See 'p4 help passwd'. ///
///
Type: Must be 'service', operator, or 'standard'. Default is ///
'standard'. Once set, the user type cannot be changed. ///
///
The -d flag deletes the specified user (unless the user has files ///
open). ///
///
The -o flag writes the user specification to the standard output. ///
The user's editor is not invoked. ///
///
The -i flag reads a user specification from the standard input. ///
The user's editor is not invoked. ///
///
The -f flag forces the creation, update or deletion of the specified ///
user, and enables you to change the Last Modified date. By default, ///
users can only delete or modify their own user specifications. The ///
-f flag requires 'super' access, which is granted by 'p4 protect'. ///
///
///
public class User { public User() { } public User(string id, string fullname, string password, string emailaddress, DateTime updated, DateTime accessed, string jobview, List reviews, UserType type, FormSpec spec ) { Id = id; FullName = fullname; Password = password; EmailAddress = emailaddress; Updated = updated; Accessed = accessed; JobView = jobview; Reviews = reviews; Type = type; Spec = spec; } private bool _initialized; private FormBase _baseForm; #region properties /// /// The user's user name. /// public string Id { get; set; } /// /// The user's real name. /// public string FullName { get; set; } /// /// If set, user must have matching $P4PASSWD on client. /// public string Password { get; set; } /// /// The user's email address; for email review. /// public string EmailAddress { get; set; } /// /// The date this specification was last modified. /// public DateTime Updated { get; set; } /// /// The date this user was last active. Read only. /// public DateTime Accessed { get; set; } /// /// Selects jobs for inclusion during changelist creation. /// public string JobView { get; set; } /// /// Listing of depot files to be reviewed by user. /// public IList Reviews { get; set; } private StringEnum _type; /// /// Either 'service', 'operator', or 'standard'. /// Default: 'standard'. Read only. /// public UserType Type { get {return _type;} set {_type= value;} } /// /// The specification for the user form /// public FormSpec Spec { get; set; } #endregion /// /// Read the fields from the tagged output of a user command /// /// Tagged output from the 'user' command public void FromUserCmdTaggedOutput(TaggedObject objectInfo, string offset, bool dst_mismatch) { _initialized = true; _baseForm = new FormBase(); _baseForm.SetValues(objectInfo); if (objectInfo.ContainsKey("User")) Id = objectInfo["User"]; if (objectInfo.ContainsKey("Email")) EmailAddress = objectInfo["Email"]; if (objectInfo.ContainsKey("Update")) { DateTime d; if (DateTime.TryParse(objectInfo["Update"] as string, out d)) { Updated = d; } else { long unixTime = 0; if (Int64.TryParse(objectInfo["Update"], out unixTime)) { DateTime UTC = FormBase.ConvertUnixTime(unixTime); DateTime GMT = new DateTime(UTC.Year, UTC.Month, UTC.Day, UTC.Hour, UTC.Minute, UTC.Second, DateTimeKind.Unspecified); Updated = FormBase.ConvertFromUTC(GMT, offset, dst_mismatch); } } } if (objectInfo.ContainsKey("Access")) { DateTime d; if (DateTime.TryParse(objectInfo["Access"] as string, out d)) { Updated = d; } else { long unixTime = 0; if (Int64.TryParse(objectInfo["Access"], out unixTime)) { DateTime UTC = FormBase.ConvertUnixTime(unixTime); DateTime GMT = new DateTime(UTC.Year, UTC.Month, UTC.Day, UTC.Hour, UTC.Minute, UTC.Second, DateTimeKind.Unspecified); Accessed = FormBase.ConvertFromUTC(GMT, offset, dst_mismatch); } } } if (objectInfo.ContainsKey("FullName")) FullName = objectInfo["FullName"]; if (objectInfo.ContainsKey("JobView")) JobView = objectInfo["JobView"]; String key = "Reviews0"; int idx = 1; if (objectInfo.ContainsKey(key)) { Reviews = new StringList(); while (objectInfo.ContainsKey(key)) { Reviews.Add(objectInfo[key]); key = String.Format("Reviews{0}", ++idx); } } if (objectInfo.ContainsKey("Password")) Password = objectInfo["Password"]; if (objectInfo.ContainsKey("Type")) _type = objectInfo["Type"]; } /// /// Parse the fields from a user specification /// /// Text of the user specification in server format /// public bool Parse(String spec) { _baseForm = new FormBase(); _baseForm.Parse(spec); // parse the values into the underlying dictionary if (_baseForm.ContainsKey("User")) { Id = _baseForm["User"] as string; } if (_baseForm.ContainsKey("Email")) { EmailAddress = _baseForm["Email"] as string; } if (_baseForm.ContainsKey("Update")) { DateTime v = DateTime.MinValue; DateTime.TryParse(_baseForm["Update"] as string, out v); Updated = v; } if (_baseForm.ContainsKey("Access")) { DateTime v = DateTime.MinValue; DateTime.TryParse(_baseForm["Access"] as string, out v); Accessed = v; } if (_baseForm.ContainsKey("FullName")) FullName = _baseForm["FullName"] as string; if (_baseForm.ContainsKey("JobView")) JobView = _baseForm["JobView"] as string; if ((_baseForm.ContainsKey("Reviews")) && (_baseForm["Reviews"] is IList)) Reviews = _baseForm["Reviews"] as IList; if (_baseForm.ContainsKey("Password")) Password = _baseForm["Password"] as string; if (_baseForm.ContainsKey("Type")) _type = _baseForm["Type"] as string; else Type = UserType.Standard; return true; } /// /// Format of a user specification used to save a user to the server /// private static String UserSpecFormat = "User:\t{0}\r\n" + "\r\n" + "Email:\t{1}\r\n" + "\r\n" + "Update:\t{2}\r\n" + "\r\n" + "Access:\t{3}\r\n" + "\r\n" + "FullName:\t{4}\r\n" + "\r\n" + "JobView:\t{5}\r\n" + "\r\n" + "Reviews:\t\r\n{6}" + "\r\n" + "Password:\t{7}\r\n"; /// /// Convert to specification in server format /// /// override public String ToString() { String reviewsView = String.Empty; if (Reviews != null) { for (int idx = 0; idx < Reviews.Count; idx++) { reviewsView += String.Format("\t{0}\r\n", Reviews[idx]); } } String value = String.Format(UserSpecFormat, Id, EmailAddress, FormBase.FormatDateTime(Updated), FormBase.FormatDateTime(Accessed), FullName, JobView, reviewsView, Password); return value; } } }