1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
#region
 
using System;
using System.Data;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.DataAccess;
using CSFrameworkV5.Interfaces;
using CSFrameworkV5.Models;
using CSFrameworkV5.WebRef.SystemModule;
 
#endregion
 
namespace CSFrameworkV5.Business.BLL_Permission
{
    /// <summary>
    ///     角色的权限视图管理
    /// </summary>
    public class RoleActionsView
    {
        protected DataTable _Actions;
        protected string _CurrentRoleID; //当前角色编号
        private bool _IsCreatedTree; //是否创建树结构
        protected DataTable _MenuData;
        protected DataTable _ModuleData;
        protected DataTable _MyFormTagName;
        protected bool _OnInitTree;
        protected bool _OnViewMode;
 
        protected IBridge_Permission _PermissionBridge;
        protected DataTable _RoleActionData;
        protected DataTable _RoleData;
 
        protected TreeView _Tree;
 
        /// <summary>
        ///     构造器
        /// </summary>
        /// <param name="tree">树视图</param>
        /// <param name="menuData">菜单数据</param>
        public RoleActionsView(TreeView tree)
        {
            _OnViewMode = false;
            _Tree = tree;
            _PermissionBridge = CreateBridge();
        }
 
        /// <summary>
        ///     功能点的自定义名称数据
        /// </summary>
        public DataTable ActionCustomNameData
        {
            get => _MyFormTagName;
            set => _MyFormTagName = value;
        }
 
        /// <summary>
        ///     所有功能点数据
        /// </summary>
        public DataTable ActionsData
        {
            get => _Actions;
            set => _Actions = value;
        }
 
        /// <summary>
        ///     当前操作的角色编号
        /// </summary>
        public string CurrentRoleID
        {
            get => _CurrentRoleID;
            set => _CurrentRoleID = value;
        }
 
        /// <summary>
        ///     是否创建树结构
        /// </summary>
        public bool IsCreatedTree
        {
            get => _IsCreatedTree;
            set => _IsCreatedTree = value;
        }
 
        /// <summary>
        ///     所有菜单数据
        /// </summary>
        public DataTable MenuData
        {
            get => _MenuData;
            set => _MenuData = value;
        }
 
        /// <summary>
        ///     模块数据
        /// </summary>
        public DataTable ModuleData
        {
            get => _ModuleData;
            set => _ModuleData = value;
        }
 
        /// <summary>
        ///     处于初始化树状态
        /// </summary>
        public bool OnInitTree => _OnInitTree;
 
        /// <summary>
        ///     处于查看模式
        /// </summary>
        public bool OnViewMode
        {
            get => _OnViewMode;
            set => _OnViewMode = value;
        }
 
        /// <summary>
        ///     角色的权限数据
        /// </summary>
        public DataTable RoleActionData => _RoleActionData;
 
        /// <summary>
        ///     角色数据
        /// </summary>
        public DataTable RoleData
        {
            get => _RoleData;
            set => _RoleData = value;
        }
 
        /// <summary>
        ///     取消操作
        /// </summary>
        public void CancelOperation()
        {
            _MenuData.RejectChanges();
            _ModuleData.RejectChanges();
            _Actions.RejectChanges();
        }
 
        private IBridge_Permission CreateBridge()
        {
            if (BridgeFactory.IsADODirect)
                return new dalPermission(Loginer.CurrentUser);
 
            if (BridgeFactory.IsWCFBridge) return new WCF_Permission();
 
            throw new CustomException(BridgeFactory.UNKNOW_BRIDGE_TYPE);
        }
 
        /// <summary>
        ///     由菜单的数据创建树结点
        /// </summary>
        /// <param name="row">菜单的数据</param>
        /// <param name="tree">树视图</param>
        /// <param name="parentNode">父级结点</param>
        /// <returns></returns>
        protected TreeNode DataRow2NodeHandler(DataRow row, TreeView tree,
            TreeNode parentNode)
        {
            var node = new TreeNode();
            node.Text = ConvertEx.ToString(row[tb_MyMenu.MenuCaption]); //显示菜单标题
            if (parentNode != null)
                parentNode.Nodes.Add(node);
            else
                tree.Nodes.Add(node);
 
            return node;
        }
 
        /// <summary>
        ///     根据父级菜单名获取所有子级的资料行.
        /// </summary>
        /// <param name="parentValue">父级菜单名</param>
        /// <returns></returns>
        protected DataRow[] GetRows(string parentMenuName)
        {
            string filter;
 
            if (string.IsNullOrEmpty(parentMenuName))
                filter = string.Format("({0} IS NULL) OR ({1} ='')",
                    tb_MyMenu.ParentMenuName,
                    tb_MyMenu.ParentMenuName);
            else
                filter = string.Format("{0}='{1}'", tb_MyMenu.ParentMenuName,
                    parentMenuName);
 
            return _MenuData.Select(filter);
        }
 
        //_RoleData, txtRoleID.Text, "RoleID", _UpdateType)
        public static bool IsExistsInCache(DataTable cache, string keyValue,
            string keyName, UpdateType updateType)
        {
            if (UpdateType.Add == updateType)
            {
                var rows = cache.Select(keyName + "='" + keyValue + "'");
                return rows.Length > 0;
            }
 
            //修改状态是禁止修改主键的
            if (UpdateType.Modify == updateType) return false;
 
            return false;
        }
 
        /// <summary>
        ///     提交菜单管理功能的数据
        /// </summary>
        /// <returns></returns>
        public bool PostChanges()
        {
            var success = false;
 
            var dtMenu = MenuData.GetChanges();
            var dtActionCustomName = ActionCustomNameData.GetChanges();
            if (dtMenu != null)
                success =
                    new bllMenu().Update(dtMenu,
                        dtActionCustomName); //提交菜单和自定义功能点数据
 
            if (success)
            {
                MenuData.AcceptChanges();
                ActionCustomNameData.AcceptChanges();
            }
 
            //提交模块字典数据
            var dtModule = ModuleData.GetChanges();
            if (dtModule != null)
                success = new bllSystemModule().Update(dtModule);
 
            if (success) ModuleData.AcceptChanges();
 
            //提交功能点数据
            var dtActions = ActionsData.GetChanges();
            if (dtActions != null) success = new bllActions().Update(dtActions);
 
            if (success) ActionsData.AcceptChanges();
 
            return success;
        }
 
        /// <summary>
        ///     重新获取角色依赖的数据
        /// </summary>
        public void RefreshRoleRelatedData()
        {
            _MyFormTagName = _PermissionBridge.GetFormTagName();
        }
 
        #region BeforeCheck/AfterCheck
 
        private void OnTreeAuthorityBeforeCheck(object sender,
            TreeViewCancelEventArgs e)
        {
            //查看模式支持勾选.
            if (_OnViewMode) e.Cancel = true;
        }
 
        private void OnTreeAuthorityAfterCheck(object sender,
            TreeViewEventArgs e)
        {
            //枚举结点
            if (e.Action == TreeViewAction.ByKeyboard ||
                e.Action == TreeViewAction.ByMouse)
            {
                if (e.Node.Checked)
                {
                    TreeNodeSelectedChild(e.Node, true);
                    TreeNodeParentSelected(e.Node, true);
                }
                else if (e.Node.Checked == false)
                {
                    TreeNodeSelectedChild(e.Node, false);
                    //当所有子结点去掉勾选时,父结点也去掉勾选时
                    //TreeNodeChildsUnSelected(e.Node);
                }
            }
 
            //勾选功能结点
            if (e.Node.Tag is ActionNodeTag)
            {
                var action = e.Node.Tag as ActionNodeTag; //功能结点的Tag对象
                var tag = e.Node.Parent.Tag as RoleActionNodeTag; //数据窗体结点Tag对象
 
                //初始化角色的权限资料行
                if (tag.RoleActionData == null)
                    tag.RoleActionData =
                        CreateRoleActionData(_CurrentRoleID, tag.MenuName, 0);
 
                //汇总数据窗体结点的所有打勾的功能点的值
                var actionsValue = GetActionsValue(e.Node.Parent);
 
                //设置权限值
                tag.RoleActionData[tb_MyRoleActions.Actions] = actionsValue;
            }
 
            //勾选数据窗体结点
            if (e.Node.Tag is RoleActionNodeTag)
            {
                var tag = e.Node.Tag as RoleActionNodeTag;
                if (e.Node.Checked) //打勾
                {
                    if (tag.RoleActionData == null) //新增角色的权限
                        tag.RoleActionData =
                            CreateRoleActionData(_CurrentRoleID, tag.MenuName,
                                0);
 
                    if (tag.Deleted) tag.Deleted = false;
                }
                else //取消勾选
                {
                    if (tag.RoleActionData != null)
                    {
                        if (tag.RoleActionData.RowState == DataRowState.Added)
                            tag.RoleActionData.Table.Rows.Remove(
                                tag.RoleActionData); //删除"新增状态"的记录
                        else
                            tag.Deleted = true; //给记录打上删除标记
                    }
                }
            }
        }
 
        /// <summary>
        ///     获取角色及角色的权限已修改的数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetChanges()
        {
            foreach (TreeNode node in _Tree.Nodes) BuildNodeData(node);
 
            var changes = _RoleActionData.GetChanges();
            return changes;
        }
 
        /// <summary>
        ///     将结点(取消勾选)绑定的DataRow设为删除
        /// </summary>
        /// <param name="node"></param>
        private void BuildNodeData(TreeNode node)
        {
            if (node == null || node.Tag == null) return;
 
            //用于测试
            if (node.Text == "动态交叉报表")
            {
            }
 
            //功能点节点(如:增、删、改)
            if (node.Tag is ActionNodeTag)
            {
                var tag = node.Parent.Tag as RoleActionNodeTag; //数据窗体结点Tag对象
 
                //勾选了功能点,初始化角色的权限资料行
                if (tag.RoleActionData == null && node.Parent.Checked)
                    tag.RoleActionData =
                        CreateRoleActionData(_CurrentRoleID, tag.MenuName, 0);
 
                if (tag.RoleActionData != null)
                {
                    //汇总数据窗体结点的所有打勾的功能点的值
                    var actionsValue = GetActionsValue(node.Parent);
 
                    if (actionsValue > 0 &&
                        tag.RoleActionData.RowState !=
                        DataRowState.Deleted) //更新权限值                    
                        tag.RoleActionData[tb_MyRoleActions.Actions] =
                            actionsValue;
                    //else
                    //    tag.RoleActionData.Delete();//删除
                }
            }
 
            //窗体结点
            if (node.Tag is RoleActionNodeTag)
            {
                var tag = node.Tag as RoleActionNodeTag;
                if (node.Checked) //打勾
                {
                    if (tag.RoleActionData == null) //新增角色的权限
                        tag.RoleActionData =
                            CreateRoleActionData(_CurrentRoleID, tag.MenuName,
                                0);
                }
                else //取消勾选
                {
                    if (tag.RoleActionData != null) //删除
                    {
                        if (tag.RoleActionData.RowState == DataRowState.Added)
                            tag.RoleActionData.Table.Rows.Remove(
                                tag.RoleActionData); //删除"新增状态"的记录
                        else
                            tag.RoleActionData.Delete(); //删除记录
                    }
                }
            }
 
            //若当前节点有子节点,递归处理
            if (node.Nodes.Count > 0)
                foreach (TreeNode n in node.Nodes)
                    BuildNodeData(n);
        }
 
        /// <summary>
        ///     汇总数据窗体结点的所有打勾的功能点的值
        /// </summary>
        /// <param name="node">数据窗体结点</param>
        /// <returns></returns>
        private int GetActionsValue(TreeNode node)
        {
            var actions = 0;
            foreach (TreeNode n in node.Nodes)
                if (n.Checked && n.Tag != null && n.Tag is ActionNodeTag)
                    actions += (n.Tag as ActionNodeTag).ActionValue;
 
            return actions;
        }
 
        /// <summary>
        ///     新增角色的权限
        /// </summary>
        /// <param name="roleID">角色编号</param>
        /// <param name="menuName">菜单名称</param>
        /// <param name="actions">权限值(功能点汇总)</param>
        /// <returns></returns>
        private DataRow CreateRoleActionData(string roleID, string menuName,
            int actions)
        {
            var row = _RoleActionData.NewRow();
            row[tb_MyRoleActions.DataSetID] = Loginer.CurrentUser.DBID;
            row[tb_MyRoleActions.RoleID] = roleID;
            row[tb_MyRoleActions.MenuName] = menuName;
            row[tb_MyRoleActions.Actions] = actions;
            _RoleActionData.Rows.Add(row);
            return row;
        }
 
        #endregion
 
        #region 递归选择结点父/子结点
 
        /// <summary>
        ///     勾选所有子级结点
        /// </summary>
        /// <param name="thChild">当前结点</param>
        /// <param name="isChecked">是否勾选</param>
        public void TreeNodeSelectedChild(TreeNode thChild, bool isChecked)
        {
            foreach (TreeNode tnChildNext in thChild.Nodes)
            {
                tnChildNext.Checked = isChecked;
                TreeNodeSelectedChild(tnChildNext, isChecked);
            }
        }
 
        /// <summary>
        ///     勾选父级结点
        /// </summary>
        /// <param name="tnChild">当前结点</param>
        /// <param name="isChecked">是否勾选</param>
        private void TreeNodeParentSelected(TreeNode tnChild, bool isChecked)
        {
            if (tnChild.Parent != null)
            {
                tnChild.Parent.Checked = isChecked;
                TreeNodeParentSelected(tnChild.Parent, isChecked);
            }
        }
 
        /// <summary>
        ///     当所有子结点去掉勾选时,父结点也去掉勾选时
        /// </summary>
        /// <param name="tnChild"></param>
        private void TreeNodeChildsUnSelected(TreeNode tnChild)
        {
            if (tnChild.Parent == null) return;
 
            foreach (TreeNode n in tnChild.Parent.Nodes)
                if (n.Checked)
                    return;
 
            tnChild.Parent.Checked = false;
        }
 
        #endregion
 
        #region 递归生成菜单树
 
        /// <summary>
        ///     生成顶级菜单的树结点
        /// </summary>
        public virtual void InitTree(string rootValue)
        {
            try
            {
                _OnInitTree = true; //正在初始化树视图
                _OnViewMode = false; //允许勾选结点
 
                _Tree.BeginUpdate();
                _Tree.AfterCheck -= OnTreeAuthorityAfterCheck;
                _Tree.BeforeCheck -= OnTreeAuthorityBeforeCheck;
 
                _Tree.Nodes.Clear();
 
                //根结点记录 
                var rows = GetRows(rootValue);
                DataRow[] childs;
 
                string pValue; //parentValue; 
                string kValue; //key value 
                TreeNode root;
 
                foreach (var row in rows)
                {
                    pValue = row[tb_MyMenu.ParentMenuName].ToStringEx();
                    kValue = row[tb_MyMenu.MenuName].ToStringEx();
 
                    root = DataRow2NodeHandler(row, _Tree, null); //创建结点
                    root.ImageIndex = 0;
                    root.SelectedImageIndex = 0;
 
                    var tag = new RoleActionNodeTag(); //创建Tag对象
                    tag.MenuData = row;
                    tag.MenuName = kValue;
                    tag.RoleActionData = null;
                    root.Tag = tag;
 
                    childs = GetRows(kValue);
                    if (childs.Length > 0) InitTreeChild(childs, root);
                }
 
                _IsCreatedTree = true;
                _OnInitTree = false;
            }
            finally
            {
                _Tree.BeforeCheck += OnTreeAuthorityBeforeCheck;
                _Tree.AfterCheck += OnTreeAuthorityAfterCheck;
 
                _Tree.EndUpdate();
            }
        }
 
        /// <summary>
        ///     生成子菜单的树结构
        /// </summary>
        protected virtual void InitTreeChild(DataRow[] childs,
            TreeNode parentNode)
        {
            string menuParentName; //当前菜单的父级菜单; 
            string menuName; //当前菜单
            TreeNode root;
 
            foreach (var row in childs)
            {
                menuParentName = row[tb_MyMenu.ParentMenuName].ToStringEx();
                menuName = row[tb_MyMenu.MenuName].ToStringEx();
 
                root = DataRow2NodeHandler(row, _Tree, parentNode); //创建结点
 
                var tag = new RoleActionNodeTag(); //创建Tag对象
                tag.MenuData = row;
                tag.MenuName = menuName;
                tag.RoleActionData = null;
                root.Tag = tag;
 
                var actions = ConvertEx.ToInt(row[tb_MyMenu.Actions]);
                if (actions > 0) //窗体有权限功能
                {
                    root.ImageIndex = 1;
                    root.SelectedImageIndex = 1;
 
                    InitAction(actions, menuName, root); //初始化功能结点
                }
                else
                {
                    root.ImageIndex = 3;
                    root.SelectedImageIndex = 3;
                }
 
                childs = GetRows(menuName);
 
                if (childs.Length > 0) //创建下一级结点
                    InitTreeChild(childs, root);
            }
        }
 
        #endregion
 
        #region InitAction 初始化功能点
 
        /// <summary>
        ///     初始化功能点树结点
        /// </summary>
        /// <param name="node"></param>
        /// <param name="menuItem"></param>
        protected virtual void InitAction(int actions, string menuName,
            TreeNode node)
        {
            var isAdmin = Loginer.CurrentUser.IsAdmin();
 
            var userAuths = actions; //当前用户的权限
 
            foreach (DataRow row in _Actions.Rows) //循环所有功能点.
            {
                var value = ConvertEx.ToInt(row[tb_MyActions.ActionValue]);
                if (value == 0) continue;
 
                //用每个功能点的值与窗体的最大权限"与"运算, 但不能超出当前用户的权限.
                if ((value & actions) == value &&
                    (isAdmin || (value & userAuths) == value))
                {
                    var caption =
                        ConvertEx.ToString(row[tb_MyActions.ActionName]);
                    var tagNameRow =
                        GetCustomTagName(menuName, value); //取按钮的自定义名称
                    if (tagNameRow != null)
                        caption =
                            ConvertEx.ToString(
                                tagNameRow[
                                    tb_MyFormTagName
                                        .TagName]); //功能点名称                    
 
                    var actionNode = new TreeNode(caption, 0, 0);
                    actionNode.ImageIndex = 2;
                    actionNode.SelectedImageIndex = 2;
 
                    var tag = new ActionNodeTag(value);
                    tag.TagMenuName = menuName;
                    tag.TagNameDataRow = tagNameRow;
                    tag.TagNameOld = caption; //按钮标题                    
                    actionNode.Tag = tag;
 
                    node.Nodes.Add(actionNode);
                }
            }
        }
 
        protected DataRow GetCustomTagName(string menuName, int actionValue)
        {
            var rows = _MyFormTagName.Select("MenuName='" + menuName +
                                             "' AND TagValue=" +
                                             actionValue.ToStringEx());
            if (rows.Length > 0) return rows[0];
 
            return null;
        }
 
        #endregion
 
        #region 显示角色的权限
 
        //显示角色的权限.
        public void ShowRoleActions()
        {
            _OnInitTree = true;
            _RoleActionData = _PermissionBridge.GetRoleAction(_CurrentRoleID);
 
            try
            {
                _Tree.AfterCheck -= OnTreeAuthorityAfterCheck;
 
                //一级结点,模块的主菜单
                foreach (TreeNode node in _Tree.Nodes)
                {
                    var tag = node.Tag as RoleActionNodeTag;
                    var drRoleAction =
                        GetRoleActionDataRow(_CurrentRoleID, tag.MenuName);
                    tag.RoleActionData = drRoleAction; //设置角色的权限
                    tag.Deleted = false;
                    node.Checked = drRoleAction != null;
 
                    if (node.Nodes.Count > 0) ShowRoleActionsChild(node);
                }
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
            finally
            {
                _Tree.AfterCheck += OnTreeAuthorityAfterCheck;
                _OnInitTree = false;
            }
        }
 
        private DataRow GetRoleActionDataRow(string roleID, string menuName)
        {
            var rs = _RoleActionData.Select("RoleID='" + roleID +
                                            "' AND MenuName='" + menuName +
                                            "'");
            return rs.Length > 0 ? rs[0] : null;
        }
 
        private void ShowRoleActionsChild(TreeNode node)
        {
            foreach (TreeNode n in node.Nodes)
            {
                var tag = n.Tag as RoleActionNodeTag;
                var drRoleAction =
                    GetRoleActionDataRow(_CurrentRoleID, tag.MenuName);
                tag.RoleActionData = drRoleAction; //设置角色的权限
                tag.Deleted = false;
                n.Checked = drRoleAction != null;
 
                if (n.Checked) TreeNodeParentSelected(n, true);
 
                var actions =
                    ConvertEx.ToInt(tag.MenuData[tb_MyRoleActions.Actions]);
                if (actions > 0) //当前菜单有功能点(权限值)
                    ShowAction(n, drRoleAction); //显示菜单的功能
                else if (n.Nodes.Count > 0) ShowRoleActionsChild(n);
            }
        }
 
        private void ShowAction(TreeNode node, DataRow roleAction)
        {
            if (node == null) return;
 
            var auth = 0;
            if (roleAction != null)
                auth = int.Parse(roleAction[tb_MyRoleActions.Actions]
                    .ToStringEx()); //角色的权限
 
            foreach (TreeNode n in node.Nodes)
                if (auth != 0)
                {
                    var value = (n.Tag as ActionNodeTag).ActionValue;
                    n.Checked = (value & auth) == value; //逻辑位运算
                }
                else
                {
                    n.Checked = false;
                }
        }
 
        #endregion
    }
 
    /// <summary>
    ///     组的权限视图管理(查看模式)
    /// </summary>
    public class GroupActionsView : RoleActionsView
    {
        private DataTable _GroupActions;
 
        public GroupActionsView(TreeView tree)
            : base(tree)
        {
            _OnViewMode = true;
        }
 
        private DataRow GetGroupExistedMenu(string menuName)
        {
            var rows =
                _GroupActions.Select(tb_MyMenu.MenuName + "='" + menuName +
                                     "'");
            return rows.Length > 0 ? rows[0] : null;
        }
 
        protected override void InitAction(int actions, string menuName,
            TreeNode node)
        {
            var isAdmin = Loginer.CurrentUser.IsAdmin();
 
            foreach (DataRow row in _Actions.Rows) //循环所有功能点.
            {
                var value = ConvertEx.ToInt(row[tb_MyActions.ActionValue]);
                if (value == 0) continue;
 
                //用每个功能点的值与窗体的最大权限"与"运算, 但不能超出当前用户的权限.
                if ((value & actions) == value)
                {
                    var caption =
                        ConvertEx.ToString(row[tb_MyActions.ActionName]);
                    var tagNameRow =
                        GetCustomTagName(menuName, value); //取按钮的自定义名称
                    if (tagNameRow != null)
                        caption =
                            ConvertEx.ToString(
                                tagNameRow[
                                    tb_MyFormTagName
                                        .TagName]); //功能点名称                    
 
                    var actionNode = new TreeNode(caption, 0, 0);
                    actionNode.ImageIndex = 2;
                    actionNode.SelectedImageIndex = 2;
 
                    var tag = new ActionNodeTag(value);
                    tag.TagMenuName = menuName;
                    tag.TagNameDataRow = tagNameRow;
                    tag.TagNameOld = caption; //按钮标题                    
                    actionNode.Tag = tag;
 
                    node.Nodes.Add(actionNode);
                }
            }
        }
 
        public void InitTree(string rootValue, string groupCode)
        {
            //取当前组所有权限
            _GroupActions = new bllPermission().GetGroupActions(groupCode);
 
            InitTree(rootValue);
        }
 
        public override void InitTree(string rootValue)
        {
            if (_GroupActions == null) throw new Exception("无法获取组的权限!");
 
            _OnInitTree = true;
 
            _Tree.Nodes.Clear();
            _Tree.BeginUpdate();
 
            //根结点记录 
            var rows = GetRows(rootValue); //取菜单数据
            DataRow[] childs;
 
            string menuName; //key value 
            TreeNode root;
 
            var isAdmin = Loginer.CurrentUser.IsAdmin();
 
            foreach (var row in rows)
            {
                menuName = row[tb_MyMenu.MenuName].ToStringEx();
 
                //仅加载当前组的角色的权限
                if (GetGroupExistedMenu(menuName) != null)
                {
                    root = DataRow2NodeHandler(row, _Tree, null);
                    root.ImageIndex = 0;
                    root.SelectedImageIndex = 0;
 
                    var tag = new RoleActionNodeTag();
                    tag.MenuData = row;
                    tag.MenuName = menuName;
                    tag.RoleActionData = null;
                    root.Tag = tag;
 
                    childs = GetRows(menuName);
                    if (childs.Length > 0) InitTreeChild(childs, root);
                }
            }
 
            if (_Tree.Nodes.Count == 0) _Tree.Nodes.Add("该组未定义权限!");
 
            _Tree.EndUpdate();
            _OnInitTree = false;
        }
 
        protected override void InitTreeChild(DataRow[] childs,
            TreeNode parentNode)
        {
            string menuParentName; //parentValue; 
            string menuName; //key value 
            TreeNode root;
            var isAdmin = Loginer.CurrentUser.IsAdmin();
            foreach (var row in childs)
            {
                menuParentName = row[tb_MyMenu.ParentMenuName].ToStringEx();
                menuName = row[tb_MyMenu.MenuName].ToStringEx();
 
                var thisMenu = GetGroupExistedMenu(menuName);
 
                if (thisMenu != null)
                {
                    root = DataRow2NodeHandler(row, _Tree, parentNode);
 
                    var tag = new RoleActionNodeTag();
                    tag.MenuData = row;
                    tag.MenuName = menuName;
                    tag.RoleActionData = null;
                    root.Tag = tag;
 
                    var actions = thisMenu == null
                        ? 0
                        : ConvertEx.ToInt(thisMenu[tb_MyMenu.Actions]); //取角色的权限
                    if (actions > 0)
                    {
                        root.ImageIndex = 1;
                        root.SelectedImageIndex = 1;
 
                        InitAction(actions, menuName, root);
                    }
                    else
                    {
                        root.ImageIndex = 3;
                        root.SelectedImageIndex = 3;
                    }
 
                    childs = GetRows(menuName);
 
                    if (childs.Length > 0) InitTreeChild(childs, root);
                }
            }
        }
    }
}