//----------------------------------------------------------------------- // // Copyright (C) 2011 Microsoft Corporation // //----------------------------------------------------------------------- namespace Microsoft.Samples.Management.OData.RoleBasedPlugins { using System; using System.Collections.Generic; using System.IO; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; /// /// Keeps Configuration for the RbacSystem /// It reads the RacSystem configuration for configuratin file and creates RbacConfiguration /// [Serializable] [XmlRoot("RbacConfiguration")] public class XmlConfiguration { /// /// Initializes a new instance of the XmlConfiguration class /// public XmlConfiguration() { this.Users = new List(); this.Groups = new List(); } /// Gets collection of groups [XmlArray("Groups")] [XmlArrayItem("Group", typeof(XmlGroup))] public List Groups { get; private set; } /// Gets collection of users [XmlArray("Users")] [XmlArrayItem("User", typeof(XmlUser))] public List Users { get; private set; } /// /// Creates RbacConfiguration from Rbac configuration file /// /// full path to the config file /// RbacConfiguration created from the configuration file public static XmlConfiguration Create(string configPath) { string configData = File.ReadAllText(configPath); try { XmlReader xsd = XmlReader.Create(new StringReader(Resources.rbac)); XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; settings.IgnoreProcessingInstructions = true; settings.IgnoreWhitespace = true; settings.XmlResolver = null; settings.ValidationType = ValidationType.Schema; settings.ValidationEventHandler += delegate(object sender, ValidationEventArgs args) { throw new ArgumentException("Rbac configuration file is incorrect", args.Exception); }; XmlSerializer serializer = new XmlSerializer(typeof(XmlConfiguration)); using (XmlReader reader = XmlReader.Create(new StringReader(configData), settings)) { return serializer.Deserialize(reader) as XmlConfiguration; } } catch (XmlException) { throw; } } } /// /// Represents Group in the RbacConfiguration /// [Serializable] public class XmlGroup { /// /// Initializes a new instance of the XmlGroup class /// public XmlGroup() { this.Cmdlets = new List(); this.Scripts = new List(); this.Modules = new List(); } /// Gets or sets name of the group [XmlAttribute("Name")] public string Name { get; set; } /// Gets or sets user name of the user in which context commands are executed for this group [XmlAttribute("UserName")] public string UserName { get; set; } /// Gets or sets password of the user in which context commands are executed for this group [XmlAttribute("Password")] public string Password { get; set; } /// Gets or sets domain of the user in which context commands are executed for this group [XmlAttribute("DomainName")] public string DomainName { get; set; } /// Gets or sets a value indicating whether to map incoming user to the user context returned from custom authorization [XmlAttribute("MapIncomingUser")] public bool MapIncomingUser { get; set; } /// Gets collection of cmdlets in the group [XmlArray("Cmdlets")] [XmlArrayItem("Cmdlet", typeof(string))] public List Cmdlets { get; private set; } /// Gets collection of cmdlets in the group [XmlArray("Scripts")] [XmlArrayItem("Script", typeof(string))] public List Scripts { get; private set; } /// Gets collection of cmdlets in the group [XmlArray("Modules")] [XmlArrayItem("Module", typeof(string))] public List Modules { get; private set; } } /// /// Represents User in the RbacConfiguration /// [Serializable] public class XmlUser { /// Gets or sets name of the user [XmlAttribute("Name")] public string Name { get; set; } /// Gets or sets authentication type used for the user [XmlAttribute("AuthenticationType")] public string AuthenticationType { get; set; } /// Gets or sets domain name of the user. If this is null/empty, domain is local machine [XmlAttribute("DomainName")] public string DomainName { get; set; } /// Gets or sets group in which the user has membership [XmlAttribute("GroupName")] public string GroupName { get; set; } /// Gets or sets quota for the user [XmlElement("Quota", typeof(XmlQuota))] public XmlQuota Quota { get; set; } } /// /// Represents quota for a user /// [Serializable] public class XmlQuota { /// Gets or sets maximum concurrent requests [XmlAttribute("MaxConcurrentRequests")] public int MaxConcurrentRequests { get; set; } /// Gets or sets maximum requests per time slot [XmlAttribute("MaxRequestsPerTimeslot")] public int MaxRequestsPerTimeslot { get; set; } /// Gets or sets time slot [XmlAttribute("Timeslot")] public int Timeslot { get; set; } } }