//----------------------------------------------------------------------- // // Copyright (C) 2011 Microsoft Corporation // //----------------------------------------------------------------------- namespace Microsoft.Samples.Management.OData.RoleBasedPlugins { using System; using System.Management.Automation.Remoting; using System.Security.Principal; /// /// Class Represents a user in RBAC /// internal class RbacUser { /// /// Initializes a new instance of the RbacUser class. /// /// Name of the user /// User quota value public RbacUser(RbacUserInfo userInfo, XmlQuota quota) { this.UserInfo = userInfo; this.Quota = new RbacQuota(quota); } /// Gets user information public RbacUserInfo UserInfo { get; private set; } /// Gets or sets list of groups the user is member of public RbacGroup Group { get; set; } /// Gets quota limits for the user public RbacQuota Quota { get; private set; } /// /// Gets the membershipId of the user /// /// Membership Id of the user public string GetMembershipId() { return this.Group.GetMembershipId(); } /// /// RBAC system user information /// public class RbacUserInfo : IEquatable { /// /// Initializes a new instance of the RbacUserInfo class. /// /// Name of the user /// Authentication type used /// Domain name of the user. If the domain name is empty, localmachine name is used as domain public RbacUserInfo( string name, string authenticationType, string domainName) { if (string.IsNullOrEmpty(domainName)) { domainName = Environment.MachineName; } this.Name = domainName + "\\" + name; this.AuthenticationType = authenticationType; } /// /// Initializes a new instance of the RbacUserInfo class. /// /// User identity public RbacUserInfo(IIdentity identity) : this(identity, null) { } /// /// Initializes a new instance of the RbacUserInfo class. /// /// User identity /// Http client certificate public RbacUserInfo(IIdentity identity, PSCertificateDetails clientCert) { if (identity == null) { throw new ArgumentNullException("identity"); } this.WindowsIdentity = identity as WindowsIdentity; this.Name = identity.Name; this.AuthenticationType = identity.AuthenticationType; this.CertificateDetails = clientCert; } /// Gets name of the user public string Name { get; private set; } /// Gets authentication type public string AuthenticationType { get; private set; } /// Gets windows identity for the user public WindowsIdentity WindowsIdentity { get; private set; } /// /// Gets details of the (optional) client certificate /// public PSCertificateDetails CertificateDetails { get; private set; } /// /// compare two PSCredentialDetails for equivalence. /// /// one set of details /// the other set of details /// true if they refer to the same certificate public static bool AreSame(PSCertificateDetails first, PSCertificateDetails second) { if (first == null && second == null) { return true; } if (first == null || second == null) { return false; } if (string.Equals(first.IssuerName, second.IssuerName, StringComparison.OrdinalIgnoreCase) == false || string.Equals(first.Subject, second.Subject, StringComparison.OrdinalIgnoreCase) == false || string.Equals(first.IssuerThumbprint, second.IssuerThumbprint, StringComparison.OrdinalIgnoreCase) == false) { return false; } return true; } /// /// Indicates whether the current object is equal to another object of the same type. /// /// Other RbacUserInfo instance /// True if the current object is equal to the other parameter; otherwise, False. public bool Equals(RbacUserInfo other) { if (other == null) { return false; } if (string.Equals(this.Name, other.Name, StringComparison.OrdinalIgnoreCase) == false || string.Equals(this.AuthenticationType, other.AuthenticationType, StringComparison.OrdinalIgnoreCase) == false || AreSame(this.CertificateDetails, other.CertificateDetails) == false) { return false; } return true; } /// /// Indicates whether the current object is equal to another object of the object type. /// /// Other object instance /// true, if both instace are same else false public override bool Equals(object other) { return this.Equals(other as RbacUserInfo); } /// /// Gets hash code for the object /// /// Hash code for the object public override int GetHashCode() { return base.GetHashCode(); } } } }