啊鑫
2024-07-09 0552fcc8cb73fc3021e2915129f55a42ed3f20e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
///*************************************************************************/
///*
///* 文件名    :SystemAuthentication.cs                                
///* 程序说明  : 系统授权全局公共类
///* 原创作者  :孙中吕 
///* 
///* Copyright 2006-2021 C/S框架网 www.csframework.com
///*
///**************************************************************************/
 
using System;
using System.Data;
using System.Windows.Forms;
 
namespace CSFrameworkV5.Core.SystemSecurity
{
    /// <summary>
    /// 系统授权全局公共类
    /// </summary>
    public class SystemAuthentication
    {
        /// <summary>
        /// 当前用户的权限数据
        /// </summary>
        private static DataTable _UserAuthorities = null;
 
        /// <summary>
        /// 当前用户的权限数据
        /// </summary>
        public static DataTable UserAuthorities
        {
            get => _UserAuthorities;
            set => _UserAuthorities = value;
        }
 
        /// <summary>
        /// 系统登录策略
        /// </summary>
        private static ILoginAuthorization _Current = null;
 
        /// <summary>
        /// 系统预设的授权模式
        /// </summary>
        public static ILoginAuthorization Current
        {
            get => _Current;
            set => _Current = value;
        }
 
        /// <summary>
        /// 当前登录的用户
        /// </summary>
        public static Loginer User => Loginer.CurrentUser;
 
        /// <summary>
        /// 检查当前用户对指定菜单(窗体)有授权使用
        /// </summary>
        /// <param name="menuName">菜单名</param>
        /// <returns></returns>
        public static bool Authorized(string menuName)
        {
            var rows =
                UserAuthorities.Select(
                    string.Format("MenuName='{0}'", menuName));
            var authority = rows != null && rows.Length > 0 ? rows[0] : null;
            return authority != null;
        }
 
        /// <summary>
        /// 设置窗体的权限
        /// </summary>
        /// <param name="form">支持权限控制的窗体</param>
        /// <param name="menuName">菜单名</param>
        public static void SetFormAuthority(IPurviewControllable form,
            string menuName)
        {
            var rows =
                UserAuthorities.Select(
                    string.Format("MenuName='{0}'", menuName));
 
            var authority = rows != null && rows.Length > 0 ? rows[0] : null;
            if (authority != null)
            {
                var actions = 0;
                int.TryParse(authority["Actions"].ToStringEx(), out actions);
                (form as IPurviewControllable).FormAuthorities = actions;
            }
            else
            {
                (form as IPurviewControllable).FormAuthorities = 0;
            }
        }
 
        /// <summary>
        /// 检查窗体的权限是否拥有参数authorityValue的权限
        /// </summary>
        /// <param name="formAuthorities">窗体的权限</param>
        /// <param name="authorityValue">权限值(功能点的权限值)</param>
        /// <returns></returns>
        public static bool ButtonAuthorized(int formAuthorities,
            int authorityValue)
        {
            if (authorityValue <= 0) return true;
 
            if (Loginer.CurrentUser.IsAdmin()) return true; //超级用户
 
            //逻辑运算(与and运算)
            return (authorityValue & formAuthorities) == authorityValue;
        }
 
        /// <summary>
        /// 设置主菜单的权限。取当前用户拥有的权限,
        /// 枚举所有菜单,没有权限的菜单隐藏。
        /// </summary>
        /// <param name="menuStrip"></param>
        public static void SetMenuAuthority(MenuStrip menuStrip)
        {
            foreach (ToolStripItem item in menuStrip.Items)
            {
                if (item.Tag != null && item.Tag.ToStringEx() == "IsSystemMenu")
                    continue;
 
                if (item is ToolStripMenuItem)
                {
                    var menu = item as ToolStripMenuItem;
                    if (menu.DropDown.Items.Count > 0)
                        SetMenuItemSecurity(menu);
                }
            }
        }
 
        /// <summary>
        /// 递归设置菜单的权限
        /// </summary>
        /// <param name="parent">父级菜单</param>
        private static void SetMenuItemSecurity(ToolStripMenuItem parent)
        {
            if (!Loginer.CurrentUser.IsAdmin() && UserAuthorities == null)
                throw new Exception("User does't login!");
 
            //The parent menuitem object will disabled if all child menuitem is disabled.
            var hasShow = false;
 
            foreach (ToolStripItem sub in parent.DropDown.Items)
                if (sub is ToolStripMenuItem)
                {
                    var rows =
                        UserAuthorities.Select(string.Format("MenuName='{0}' ",
                            sub.Name));
                    var menu = sub as ToolStripMenuItem;
                    //窗体
                    if (menu.DropDown.Items.Count == 0)
                    {
                        sub.Enabled = Loginer.CurrentUser.IsAdmin() ||
                                      (rows != null && rows.Length > 0);
                        sub.Visible = sub.Enabled; //No right,No Menu
                        if (sub.Enabled) hasShow = true;
                    }
 
                    if (menu.DropDown.Items.Count > 0)
                        SetMenuItemSecurityChild(menu, ref hasShow);
                }
 
            //Process the parent menuitem
            parent.Enabled = hasShow;
            parent.Visible = hasShow;
        }
 
        /// <summary>
        /// 递归设置菜单的权限
        /// </summary>
        /// <param name="parent">父级菜单</param>
        /// /// <param name="hideParentIfAllChildInvisible">如果子菜单都被隐藏,是否要显示父级菜单</param>
        private static void SetMenuItemSecurityChild(ToolStripMenuItem parent,
            ref bool hideParentIfAllChildInvisible)
        {
            if (!Loginer.CurrentUser.IsAdmin() && UserAuthorities == null)
                throw new Exception("User does't login!");
 
            //The parent menuitem object will disabled if all child menuitem is disabled.
            var hasShow = false;
 
            foreach (ToolStripItem sub in parent.DropDown.Items)
                if (sub is ToolStripMenuItem)
                {
                    var rows =
                        UserAuthorities.Select(string.Format("MenuName='{0}' ",
                            sub.Name));
                    var menu = sub as ToolStripMenuItem;
                    if (menu.DropDown.Items.Count == 0)
                    {
                        sub.Enabled = Loginer.CurrentUser.IsAdmin() ||
                                      (rows != null && rows.Length > 0);
                        sub.Visible = sub.Enabled; //No right,No Menu
                        if (sub.Enabled) hasShow = true;
                    }
 
                    if (menu.DropDown.Items.Count > 0)
                        SetMenuItemSecurityChild(menu, ref hasShow);
                }
 
            //Process the parent menuitem
            parent.Enabled = hasShow;
            parent.Visible = hasShow;
 
            if (!hideParentIfAllChildInvisible)
                hideParentIfAllChildInvisible = hasShow;
        }
    }
}