PageRenderTime 44ms CodeModel.GetById 33ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Providers/XmlProvider/Rights.cs

#
C# | 112 lines | 67 code | 24 blank | 21 comment | 2 complexity | e3a773629b292ba13015267630ae9e1e MD5 | raw file
  1namespace BlogEngine.Core.Providers
  2{
  3    using System;
  4    using System.Collections.Generic;
  5    using System.IO;
  6    using System.Linq;
  7    using System.Text;
  8    using System.Xml;
  9    using System.Security.Permissions;
 10    using System.Web;
 11    using System.Web.Hosting;
 12    using System.Web.Security;
 13
 14    /// <summary>
 15    /// A storage provider for BlogEngine that uses XML files.
 16    ///     <remarks>
 17    /// To build another provider, you can just copy and modify
 18    ///         this one. Then add it to the web.config's BlogEngine section.
 19    ///     </remarks>
 20    /// </summary>
 21    public partial class XmlBlogProvider : BlogProvider
 22    {
 23        /// <summary>
 24        /// Returns a dictionary of right names and the roles associated with them.
 25        /// </summary>
 26        /// <returns></returns>
 27        public override IDictionary<string, IEnumerable<string>> FillRights()
 28        {
 29            var fullyQualifiedPath = string.Format("{0}rights.xml", this.Folder);
 30
 31            //var fullyQualifiedPath =
 32            //    VirtualPathUtility.Combine(
 33            //        VirtualPathUtility.AppendTrailingSlash(HttpRuntime.AppDomainAppVirtualPath), path);
 34
 35            //fullyQualifiedPath = HostingEnvironment.MapPath(fullyQualifiedPath);
 36
 37            var rightsDict = new Dictionary<string, IEnumerable<string>>();
 38
 39            if (File.Exists(fullyQualifiedPath))
 40            {
 41                // Make sure we have permission to read the XML data source and
 42                // throw an exception if we don't
 43                var permission = new FileIOPermission(FileIOPermissionAccess.Write, fullyQualifiedPath);
 44                permission.Demand();
 45
 46                var doc = new XmlDocument();
 47                doc.Load(fullyQualifiedPath);
 48
 49
 50                foreach (XmlNode rightNode in doc.GetElementsByTagName("right"))
 51                {
 52                    var rightName = rightNode.Attributes["name"].Value;
 53                    var roleList = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
 54
 55                    foreach (XmlNode roleNode in rightNode.ChildNodes)
 56                    {
 57                        if (roleNode.Name.Equals("role", StringComparison.OrdinalIgnoreCase))
 58                        {
 59                            var roleName = roleNode.Attributes["name"].Value;
 60                            roleName = roleName.Trim();
 61                            roleList.Add(roleName);
 62                        }
 63                    }
 64
 65                    rightsDict.Add(rightName, roleList);
 66                }
 67
 68            }
 69
 70       
 71            return rightsDict;
 72
 73        }
 74
 75        /// <summary>
 76        /// Saves the current BlogEngine rights.
 77        /// </summary>
 78        /// <param name="rights"></param>
 79        public override void SaveRights(IEnumerable<Right> rights)
 80        {
 81            var fileName = string.Format("{0}rights.xml", this.Folder);
 82
 83            var settings = new XmlWriterSettings { Indent = true };
 84
 85            using (var writer = XmlWriter.Create(fileName, settings))
 86            {
 87                writer.WriteStartDocument(true);
 88                writer.WriteStartElement("rights");
 89
 90                foreach (var right in rights)
 91                {
 92                    writer.WriteStartElement("right");
 93                    writer.WriteAttributeString("name", right.Name);
 94
 95                    foreach (var role in right.Roles)
 96                    {
 97                        writer.WriteStartElement("role");
 98                        writer.WriteAttributeString("name", role);
 99                        writer.WriteEndElement();
100                    }
101                    writer.WriteEndElement();
102                }
103
104
105                writer.WriteEndElement();
106            }
107
108
109        }
110
111    }
112}