Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        private XElement Save(UserDocument user)
        {
            XElement xe = new XElement("UserDocument");
            XElement re = new XElement("Users");

            xe.Add(re);
            foreach (var vv in user.Users)
            {
                re.Add(Save(vv.Value));
            }

            re = new XElement("Groups");
            foreach (var vv in user.Groups.Values)
            {
                XElement grp = new XElement("Group");
                grp.SetAttributeValue("Name", vv.Name);
                grp.SetAttributeValue("FullName", vv.FullName);
                if (vv.Parent != null)
                {
                    grp.SetAttributeValue("Parent", vv.Parent.FullName);
                }
                if (vv.Permissions != null)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var vvv in vv.Permissions)
                    {
                        sb.Append(vvv + ",");
                    }
                    sb.Length = sb.Length > 0 ? sb.Length - 1 : sb.Length;
                    re.SetAttributeValue("Permissions", sb.ToString());
                }
            }

            return(xe);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public UserDocument LoadUsers(XElement element)
        {
            UserDocument re = new UserDocument();

            if (element.Element("Users") != null)
            {
                re.Users = new Dictionary <string, UserItem>();
                foreach (var vv in element.Element("Users").Elements())
                {
                    var user = LoadUserItem(vv);
                    if (re.Users.ContainsKey(user.Name))
                    {
                        re.Users.Add(user.Name, user);
                    }
                }
            }

            if (element.Element("Groups") != null)
            {
                Dictionary <string, UserGroup> groups  = new Dictionary <string, UserGroup>();
                Dictionary <UserGroup, string> parents = new Dictionary <UserGroup, string>();
                foreach (var vv in element.Element("Groups").Elements())
                {
                    string sname    = vv.Attribute("Name").Value;
                    string fullName = vv.Attribute("FullName").Value;
                    string parent   = "";
                    if (vv.Attribute("Parent") != null)
                    {
                        parent = vv.Attribute("Parent").Value;
                    }
                    UserGroup grp = new UserGroup()
                    {
                        Name = sname
                    };
                    if (vv.Attribute("Permissions") != null)
                    {
                        grp.Permissions = vv.Attribute("Permissions").Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    }
                    groups.Add(fullName, grp);
                    parents.Add(grp, parent);
                }

                foreach (var vv in parents)
                {
                    if (!string.IsNullOrEmpty(vv.Value))
                    {
                        if (groups.ContainsKey(vv.Value))
                        {
                            vv.Key.Parent = groups[vv.Value];
                        }
                    }
                }
            }

            return(re);
        }