Exemple #1
0
        private static bool Enable(Control control, User user)
        {
            Form owner = control.FindForm();

            if (owner != null)
            {
                if (user == null)
                {
                    PermissionForm pf = owner as PermissionForm;
                    if (pf != null)
                    {
                        user = pf.User;
                    }
                }
                if (user != null)
                {
                    control.Enabled = true;
                    int    nofinded         = 0;
                    string formName         = owner.Name;
                    PermissionCollection ps = user.GetAllPermissions(false);
                    foreach (Control c in control.Controls)
                    {
                        string controlName = c.Name;
                        if (ps.ContainsControl(formName, control.Name, controlName))
                        {
                            c.Enabled = true;
                        }
                        else
                        {
                            c.Enabled = false;
                            nofinded++;
                            ToolStrip ms = c as ToolStrip;
                            if (ms != null)
                            {
                                if (ms.EnableChildrenForUser(user))
                                {
                                    nofinded--;
                                }
                            }
                            else
                            {
                                if (c.EnableChildrenForUser(user))
                                {
                                    nofinded--;
                                }
                            }
                        }
                    }
                    if (nofinded == control.Controls.Count)
                    {
                        control.Enabled = false; return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #2
0
        /// <summary>
        /// 用于子窗体的排除权限
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        internal static bool DisableForUser(this ToolStripItem menu, User user = null)
        {
            Form owner = null;

            if (menu.Owner != null)
            {
                if (menu.Owner.Parent != null)
                {
                    owner = menu.Owner.Parent.FindForm();
                }
            }
            if (owner != null)
            {
                if (user == null)
                {
                    PermissionForm pf = owner as PermissionForm;
                    if (pf != null)
                    {
                        user = pf.User;
                    }
                }
                if (user != null)
                {
                    menu.Enabled = true;
                    string formName         = owner.Name;
                    string controlName      = menu.Name;
                    PermissionCollection ps = user.GetAllPermissions(false);
                    if (ps.ExceptControl(formName, controlName))
                    {
                        menu.Enabled = false;
                        return(true);
                    }
                    else
                    {
                        ToolStripDropDownItem dropmenu = menu as ToolStripDropDownItem;
                        if (dropmenu != null)
                        {
                            ToolStripItemCollection items = dropmenu.DropDownItems;
                            if (items != null)
                            {
                                foreach (ToolStripItem c in items)
                                {
                                    controlName = c.Name;
                                    if (ps.ExceptControl(formName, dropmenu.Name, controlName))
                                    {
                                        c.Enabled = false;
                                    }
                                    else
                                    {
                                        c.DisableForUser(user);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemple #3
0
        private static bool Disable(Control control, User user)
        {
            Form owner = control.FindForm();

            if (owner != null)
            {
                if (user == null)
                {
                    PermissionForm pf = owner as PermissionForm;
                    if (pf != null)
                    {
                        user = pf.User;
                    }
                }
                if (user != null)
                {
                    control.Enabled = true;
                    string formName         = owner.Name;
                    string controlName      = control.Name;
                    PermissionCollection ps = user.GetAllPermissions(false);
                    if (ps.ExceptControl(formName, controlName))
                    {
                        control.Enabled = false;
                        return(true);
                    }
                    else
                    {
                        foreach (Control c in control.Controls)
                        {
                            controlName = c.Name;
                            if (ps.ExceptControl(formName, control.Name, controlName))
                            {
                                c.Enabled = false;
                            }
                            else
                            {
                                ToolStrip ms = c as ToolStrip;
                                if (ms != null)
                                {
                                    ms.DisableForUser(user);
                                }
                                else
                                {
                                    c.DisableForUser(user);
                                }
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemple #4
0
        private static bool Enable(ToolStrip menuStrip, User user)
        {
            Form owner = menuStrip.FindForm();

            if (owner != null)
            {
                if (user == null)
                {
                    PermissionForm pf = owner as PermissionForm;
                    if (pf != null)
                    {
                        user = pf.User;
                    }
                }
                if (user != null)
                {
                    menuStrip.Enabled = true;
                    int    nofinded         = 0;
                    string formName         = owner.Name;
                    PermissionCollection ps = user.GetAllPermissions(false);
                    foreach (ToolStripItem c in menuStrip.Items)
                    {
                        string controlName = c.Name;
                        if (ps.ContainsControl(formName, menuStrip.Name, controlName))
                        {
                            c.Enabled = true;
                        }
                        else
                        {
                            c.Enabled = false;
                            nofinded++;
                            if (c.EnableChildrenForUser(user))
                            {
                                nofinded--;
                            }
                        }
                    }
                    if (nofinded == menuStrip.Items.Count)
                    {
                        menuStrip.Enabled = false; return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #5
0
        private static bool Disable(ToolStrip menuStrip, User user)
        {
            Form owner = menuStrip.FindForm();

            if (owner != null)
            {
                if (user == null)
                {
                    PermissionForm pf = owner as PermissionForm;
                    if (pf != null)
                    {
                        user = pf.User;
                    }
                }
                if (user != null)
                {
                    menuStrip.Enabled = true;
                    string formName         = owner.Name;
                    string controlName      = menuStrip.Name;
                    PermissionCollection ps = user.GetAllPermissions(false);
                    if (ps.ExceptControl(formName, controlName))
                    {
                        menuStrip.Enabled = false;
                        return(true);
                    }
                    else
                    {
                        foreach (ToolStripItem c in menuStrip.Items)
                        {
                            controlName = c.Name;
                            if (ps.ExceptControl(formName, menuStrip.Name, controlName))
                            {
                                c.Enabled = false;
                            }
                            else
                            {
                                c.DisableForUser(user);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemple #6
0
        /// <summary>
        /// 用于主窗体的包含权限
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        internal static bool EnableChildrenForUser(this ToolStripItem menu, User user = null)
        {
            Form owner = null;

            if (menu.Owner != null)
            {
                if (menu.Owner.Parent != null)
                {
                    owner = menu.Owner.Parent.FindForm();
                }
            }
            if (owner != null)
            {
                if (user == null)
                {
                    PermissionForm pf = owner as PermissionForm;
                    if (pf != null)
                    {
                        user = pf.User;
                    }
                }
                if (user != null)
                {
                    ToolStripDropDownItem dropmenu = menu as ToolStripDropDownItem;
                    if (dropmenu != null)
                    {
                        ToolStripItemCollection items = dropmenu.DropDownItems;
                        if (items != null)
                        {
                            dropmenu.Enabled = true;
                            int    nofinded         = 0;
                            string formName         = owner.Name;
                            PermissionCollection ps = user.GetAllPermissions(false);
                            foreach (ToolStripItem c in items)
                            {
                                string controlName = c.Name;
                                if (ps.ContainsControl(formName, dropmenu.Name, controlName))
                                {
                                    c.Enabled = true;
                                }
                                else
                                {
                                    c.Enabled = false;
                                    nofinded++;
                                    if (c.EnableChildrenForUser(user))
                                    {
                                        nofinded--;
                                    }
                                }
                            }
                            if (nofinded == items.Count)
                            {
                                dropmenu.Enabled = false; return(false);
                            }
                            else
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }