private static bool Enable10(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="perms"></param>
 /// <param name="formName"></param>
 /// <param name="moduleName"></param>
 /// <param name="actionName"></param>
 /// <returns></returns>
 public static bool ExceptControl(this PermissionCollection perms, string formName, string moduleName, string actionName = "")
 {
     try
     {
         foreach (Permission per in perms)
         {
             if (per.IsExcept)
             {
                 if (per.TheModule != null)
                 {
                     if (per.TheAction != null)
                     {
                         if (per.TheModule.FormName.Equals(formName, StringComparison.InvariantCultureIgnoreCase) && per.TheModule.ControlName.Equals(moduleName, StringComparison.InvariantCultureIgnoreCase) && per.TheModule.FormName.Equals(per.TheAction.FormName, StringComparison.InvariantCultureIgnoreCase) && per.TheAction.ControlName.Equals(actionName, StringComparison.InvariantCultureIgnoreCase))
                         {
                             return(true);
                         }
                     }
                     else
                     {
                         if (per.TheModule.FormName.Equals(formName, StringComparison.InvariantCultureIgnoreCase) && per.TheModule.ControlName.Equals(actionName, StringComparison.InvariantCultureIgnoreCase))
                         {
                             return(true);
                         }
                     }
                 }
             }
         }
     }
     catch { }
     return(false);
 }
        /// <summary>
        /// 用于子窗体的排除权限
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        internal static bool DisableForUser2(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.DisableForUser2(user);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemple #4
0
        /// <summary>
        /// 获取当前用户拥有的所有权限
        /// </summary>
        public static PermissionCollection GetAllPermissions(this User user, bool Obsolete)
        {
            PermissionCollection ps = new PermissionCollection();

            foreach (Role r in user.Roles())
            {
                if (r.Flag)
                {
                    foreach (Permission p in r.Permissions)
                    {
                        if (!ps.ContainsPermission(p))
                        {
                            ps.Add(p);
                        }
                    }
                }
            }
            foreach (Department dep in user.Departments)
            {
                //Department dep = DepartmentLogic.GetInstance().GetDepartment(d);
                foreach (Role r in dep.Roles())
                {
                    if (r.Flag)
                    {
                        foreach (Permission p in r.Permissions)
                        {
                            if (!ps.ContainsPermission(p))
                            {
                                ps.Add(p);
                            }
                        }
                    }
                }
            }
            foreach (UserGroup ugg in user.Usergroups)
            {
                //UserGroup ugg = UserGroupLogic.GetInstance().GetUserGroup(ug);
                foreach (Role r in ugg.Roles())
                {
                    if (r.Flag)
                    {
                        foreach (Permission p in r.Permissions())
                        {
                            if (!ps.ContainsPermission(p))
                            {
                                ps.Add(p);
                            }
                        }
                    }
                }
            }
            return(ps);
        }
        private static bool Disable10(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);
        }
        private static bool Enable20(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.EnableChildrenForUser2(user))
                            {
                                nofinded--;
                            }
                        }
                    }
                    if (nofinded == menuStrip.Items.Count)
                    {
                        menuStrip.Enabled = false; return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #7
0
 /// <summary>
 /// 包含权限的判断
 /// </summary>
 /// <param name="perms"></param>
 /// <param name="perm"></param>
 /// <returns></returns>
 public static bool ContainsPermission(this PermissionCollection perms, Permission perm)
 {
     foreach (Permission per in perms)
     {
         try
         {
             //if (per.PermissionValue.Equals(perm.PermissionValue, StringComparison.InvariantCultureIgnoreCase))
             if (per.ID == perm.ID)
             {
                 return(true);
             }
         }
         catch { }
     }
     return(false);
 }
Exemple #8
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="id"></param>
 /// <param name="name"></param>
 /// <param name="permissions"></param>
 public Role(int id, string name, PermissionCollection permissions)
 {
     this.id = id;
     if (name != null && name.Trim() != "")
     {
         this.name = name;
     }
     if (permissions == null)
     {
         this.permissions = new PermissionCollection();
     }
     else
     {
         this.permissions = permissions;
     }
 }
Exemple #9
0
        public static string GetPermissionsStr(PermissionCollection perms)
        {
            StringBuilder sb = new StringBuilder();

            foreach (Permission perm in perms)
            {
                if (sb.Length == 0)
                {
                    sb.Append(perm.ID.ToString());
                }
                else
                {
                    sb.Append("," + perm.ID.ToString());
                }
            }
            return(sb.ToString());
        }
Exemple #10
0
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (comboBox1.SelectedIndex > -1)
     {
         User user = comboBox1.SelectedItem as User;
         if (user != null)
         {
             listBox1.Items.Clear();
             PermissionCollection ps = user.GetAllPermissionsByUser();
             foreach (Permission per in ps)
             {
                 listBox1.Items.Add(per);
             }
             label5.Text = "查看权限(" + ps.Count + ")";
         }
     }
 }
        private static bool Disable20(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.DisableForUser2(user);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemple #12
0
        public static PermissionCollection GetPermissions(string pers, PermissionLogic pl = null)
        {
            PermissionCollection perms = new PermissionCollection();

            string[] ids = pers.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            if (pl == null)
            {
                pl = PermissionLogic.GetInstance();
            }
            foreach (string id in ids)
            {
                int I;
                if (int.TryParse(id, out I))
                {
                    Permission perm = pl.GetPermission(I);
                    perms.Add(perm);
                }
            }
            return(perms);
        }
        /// <summary>
        /// 用于主窗体的包含权限
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        internal static bool EnableChildrenForUser2(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.EnableChildrenForUser2(user))
                                    {
                                        nofinded--;
                                    }
                                }
                            }
                            if (nofinded == items.Count)
                            {
                                dropmenu.Enabled = false; return(false);
                            }
                            else
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemple #14
0
        public static PermissionCollection Permissions(this Role thisRole)
        {
            PermissionCollection ps = Common.GetPermissions(Common.GetPermissionsStr(thisRole.Permissions));

            return(ps);
        }
Exemple #15
0
        /// <summary>
        /// 获取当前用户拥有的所有权限
        /// </summary>
        public static PermissionCollection GetAllPermissionsByUser(this User user)
        {
            PermissionCollection ps = new PermissionCollection();

            foreach (Role role in user.Roles)
            {
                //Role role = RoleLogic.GetInstance().GetRole(r);
                if (role.Flag)
                {
                    //PermissionLogic pl = PermissionLogic.GetInstance();
                    foreach (Permission per in role.Permissions)
                    {
                        //Permission per = pl.GetPermission(p);
                        if (!ps.ContainsPermission(per))
                        {
                            ps.Add(per);
                        }
                    }
                }
            }
            foreach (Department dep in user.Departments)
            {
                //Department dep = DepartmentLogic.GetInstance().GetDepartment(d);
                foreach (Role role in dep.Roles)
                {
                    //Role role = RoleLogic.GetInstance().GetRole(r);
                    if (role.Flag)
                    {
                        //PermissionLogic pl = PermissionLogic.GetInstance();
                        foreach (Permission per in role.Permissions)
                        {
                            //Permission per = pl.GetPermission(p);
                            if (!ps.ContainsPermission(per))
                            {
                                ps.Add(per);
                            }
                        }
                    }
                }
            }
            foreach (UserGroup ugg in user.Usergroups)
            {
                //UserGroup ugg = UserGroupLogic.GetInstance().GetUserGroup(ug);
                foreach (Role role in ugg.Roles)
                {
                    //Role role = RoleLogic.GetInstance().GetRole(r);
                    if (role.Flag)
                    {
                        //PermissionLogic pl = PermissionLogic.GetInstance();
                        foreach (Permission per in role.Permissions)
                        {
                            //Permission per = pl.GetPermission(p);
                            if (!ps.ContainsPermission(per))
                            {
                                ps.Add(per);
                            }
                        }
                    }
                }
            }
            return(ps);
        }