Example #1
0
/// <summary>
/// Get dictionary of all groups that specified user belongs to keyed by internal group name
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>

        public static Dictionary <string, UserGroup> GetMemberGroups(string userName)
        {
            Dictionary <string, UserGroup> dict = new Dictionary <string, UserGroup>();

            for (int gi = 0; gi < UserGroups.Items.Count; gi++)
            {
                UserGroup group = UserGroups.Items[gi];
                if (group == null || group.ACL == null || group.ACL.Items == null)
                {
                    continue;
                }

                for (int ii = 0; ii < group.ACL.Items.Count; ii++)
                {
                    AclItem item = group.ACL.Items[ii];
                    if (Lex.Eq(item.AssignedTo, userName))
                    {
                        dict[group.InternalName] = group;
                        break;
                    }
                }
            }

            return(dict);
        }
Example #2
0
/// <summary>
/// Deserialize assuming the initial AccessControlList element has be read in already
/// </summary>
/// <param name="tr"></param>
/// <returns></returns>

        public static AccessControlList Deserialize(
            XmlTextReader tr)
        {
            string txt;
            int    i1 = 0;

            AccessControlList acl = new AccessControlList();

            while (true)             // loop on list of permissions
            {
                tr.Read();           // move to next permission
                tr.MoveToContent();

                if (tr.NodeType == XmlNodeType.EndElement)
                {
                    break;                                                        // end AccessControlList tag
                }
                else if (Lex.Ne(tr.Name, "Item"))
                {
                    throw new Exception("Unexpected element: " + tr.Name);
                }

                AclItem p = new AclItem();
                acl.Items.Add(p);

                XmlUtil.GetBoolAttribute(tr, "GroupItem", ref p.IsGroup);
                XmlUtil.GetStringAttribute(tr, "AssignedTo", ref p.AssignedTo);
                if (XmlUtil.GetIntAttribute(tr, "Permissions", ref i1))
                {
                    p.Permissions = (PermissionEnum)i1;
                }

                if (!tr.IsEmptyElement)
                {
                    tr.Read(); tr.MoveToContent();
                    if (tr.NodeType != XmlNodeType.EndElement)
                    {
                        throw new Exception("Expected EndElement");
                    }
                }
            }

            return(acl);
        }
Example #3
0
/// <summary>
/// Add a public read-only access item to the ACL
/// </summary>

        public bool AddPublicReadItem()
        {
            foreach (AclItem item0 in Items)
            {
                if (Lex.Eq(item0.AssignedTo, "Public"))
                {
                    return(false);
                }
            }

            AclItem item = new AclItem();

            item             = new AclItem();
            item.IsGroup     = true;
            item.AssignedTo  = "Public";
            item.Permissions = PermissionEnum.Read;
            Items.Add(item);
            return(true);
        }
Example #4
0
/// <summary>
/// Add read access for user
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>

        public bool AddReadUserItem(string userName)
        {
            foreach (AclItem item0 in Items)
            {
                if (Lex.Eq(item0.AssignedTo, userName))
                {
                    item0.Permissions |= PermissionEnum.Read;
                    return(false);
                }
            }

            AclItem item = new AclItem();

            item.IsUser      = true;
            item.AssignedTo  = userName;
            item.Permissions = PermissionEnum.Read;
            Items.Add(item);

            return(true);
        }
Example #5
0
/// <summary>
/// Sort the ACL first by group names then by individual names
/// </summary>

        public void Sort()
        {
            int    i1, i2;
            string v1, v2;

            for (i1 = 2; i1 < Items.Count; i1++)
            {
                AclItem item1 = Items[i1];
                if (Items[i1].IsGroup)
                {
                    v1 = "G";
                }
                else
                {
                    v1 = "U";
                }
                v1 += Items[i1].GetExternalName();
                for (i2 = i1 - 1; i2 >= 0; i2--)
                {
                    if (Items[i2].IsGroup)
                    {
                        v2 = "G";
                    }
                    else
                    {
                        v2 = "U";
                    }
                    v2 += Items[i2].GetExternalName();

                    if (Lex.Le(v2, v1))
                    {
                        break;
                    }

                    Items[i2 + 1] = Items[i2];
                }

                Items[i2 + 1] = item1;
            }
        }
Example #6
0
/// <summary>
/// Remove a user item from the acl
/// </summary>
/// <param name="groupName"></param>

        public void RemoveGroupItem(string groupName)
        {
            if (Lex.IsNullOrEmpty(groupName))
            {
                return;
            }

            int ii = 0;

            while (ii < Items.Count)
            {
                AclItem item = Items[ii];
                if (item.IsGroup && Lex.Eq(item.AssignedTo, groupName))
                {
                    Items.Remove(item);
                    return;
                }
                else
                {
                    ii++;
                }
            }
        }
Example #7
0
/// <summary>
/// Remove a user item from the acl
/// </summary>
/// <param name="userName"></param>

        public void RemoveUserItem(string userName)
        {
            if (Lex.IsNullOrEmpty(userName))
            {
                return;
            }

            int ii = 0;

            while (ii < Items.Count)
            {
                AclItem item = Items[ii];
                if (item.IsUser && Lex.Eq(item.AssignedTo, userName))
                {
                    Items.Remove(item);
                    return;
                }
                else
                {
                    ii++;
                }
            }
        }