using dk.nsi.seal.Model.Constants; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Xml.Linq; namespace dk.nsi.seal.Model { public class BasicPrivileges { public class Constraint { public string Name { get; private set; } public string Value { get; private set; } public Constraint(string name, string value) { this.Name = name; this.Value = value; } public override string ToString() { return "{name=" + Name + ", value=" + Value + "}"; } } public static BasicPrivileges Decode(String base64Encoded) { byte[] decoded = System.Convert.FromBase64String(base64Encoded); String decodedString = System.Text.Encoding.UTF8.GetString(decoded); XDocument privilegesXml = XDocument.Parse(decodedString); return new BasicPrivileges(privilegesXml.Root); } public Dictionary> Privileges => privileges; public Dictionary> Constraints => constraints; private readonly Dictionary> privileges = new Dictionary>(); private readonly Dictionary> constraints = new Dictionary>(); public BasicPrivileges(XElement root) { /* root=PrivilegeList */ /* PrivilegeGroup (0..*) */ var x = root.Elements(); var privileGroups = root.Elements(BasicPrivilegesTags.PrivilegeGroup); foreach (XElement group in privileGroups) { /* PrivilegeGroup Scope='?' */ XAttribute scopeAtt = group.Attribute(BasicPrivilegesAttributes.Scope); /* Privilege (0..*) */ CreatePrivileges(group, scopeAtt); /* Constraint (0..*) */ CreateConstrants(group, scopeAtt); } } private void CreatePrivileges(XElement group, XAttribute scopeAtt) { var privilegesElm = group.Elements(BasicPrivilegesTags.Privilege); IList privilegeValues = new List(); foreach (XElement valueElm in privilegesElm) { privilegeValues.Add(valueElm.Value); } privileges.Add(scopeAtt.Value, new ReadOnlyCollection(privilegeValues)); } private void CreateConstrants(XElement group, XAttribute scopeAtt) { var constraintElms = group.Elements(XName.Get("Constraint")); IList constraintValues = new List(); foreach (XElement constraintElm in constraintElms) { var name = constraintElm.Attribute(BasicPrivilegesAttributes.Name); string value = constraintElm.Value; constraintValues.Add(new Constraint(name.Value, value)); } constraints.Add(scopeAtt.Value, new ReadOnlyCollection(constraintValues)); } public Dictionary>.KeyCollection getScopes() { return privileges.Keys; } public IList getPrivileges(string scope) { return privileges[scope]; } public IList getConstraints(string scope) { return constraints[scope]; } } }