Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2522_RefactorPluginInfrastructure/HeuristicLab.Clients.Access.Administration/3.3/Views/RefreshableRoleSelectionListView.cs

Last change on this file was 15973, checked in by gkronber, 6 years ago

#2522: merged trunk changes from r13402:15972 to branch resolving conflicts where necessary

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Windows.Forms;
25using HeuristicLab.Core;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Clients.Access.Administration {
29  public partial class RefreshableRoleSelectionListView : RefreshableView {
30    public RefreshableRoleSelectionListView() {
31      InitializeComponent();
32    }
33
34    public List<Role> CurrentRoles = new List<Role>();
35
36    private User currentUser;
37    public User CurrentUser {
38      get { return currentUser; }
39      set {
40        currentUser = value;
41        roleSelectionListView.Content = null;
42        storeButton.Enabled = false;
43      }
44    }
45
46    protected override void RefreshData() {
47      if (CurrentUser != null) {
48        Content.ExecuteActionAsync(new Action(delegate {
49          if (Content.Roles == null) {
50            Content.RefreshRoles();
51          }
52          CurrentRoles = AccessAdministrationClient.CallAccessService<List<Role>>(s => s.GetUserRoles(CurrentUser));
53        }), (ex) => MainFormManager.MainForm.ShowError(ex.Message, ex));
54      }
55    }
56
57    protected override void Content_Refreshing(object sender, EventArgs e) {
58      if (InvokeRequired) {
59        Invoke(new EventHandler(Content_Refreshing), sender, e);
60      } else {
61        base.Content_Refreshing(sender, e);
62        storeButton.Enabled = false;
63        roleSelectionListView.Enabled = false;
64      }
65    }
66
67    protected override void Content_Refreshed(object sender, EventArgs e) {
68      if (InvokeRequired) {
69        Invoke(new EventHandler(Content_Refreshed), sender, e);
70      } else {
71        base.Content_Refreshed(sender, e);
72        if (Content.Roles != null) {
73          roleSelectionListView.Enabled = true;
74          storeButton.Enabled = true;
75
76          roleSelectionListView.Content = new ItemList<Role>(Content.Roles);
77          foreach (var role in CurrentRoles) {
78            foreach (ListViewItem lstRole in roleSelectionListView.ItemsListView.Items) {
79              if (((Role)lstRole.Tag).Equals(role)) {
80                lstRole.Checked = true;
81              }
82            }
83          }
84        }
85      }
86    }
87
88    private void storeButton_Click(object sender, EventArgs e) {
89      List<Role> addedRoles = new List<Role>();
90      List<Role> deletedRoles = new List<Role>();
91
92      foreach (ListViewItem lstRole in roleSelectionListView.ItemsListView.Items) {
93        if (!CurrentRoles.Contains(((Role)lstRole.Tag)) && lstRole.Checked) {
94          addedRoles.Add((Role)lstRole.Tag);
95          CurrentRoles.Add((Role)lstRole.Tag);
96        }
97
98        if (CurrentRoles.Contains(((Role)lstRole.Tag)) && !lstRole.Checked) {
99          deletedRoles.Add((Role)lstRole.Tag);
100          CurrentRoles.Remove((Role)lstRole.Tag);
101        }
102      }
103
104      Content.ExecuteActionAsync(new Action(delegate {
105        foreach (var role in addedRoles) {
106          AccessAdministrationClient.CallAccessService(s => s.AddUserToRole(role, CurrentUser));
107        }
108
109        foreach (var role in deletedRoles) {
110          AccessAdministrationClient.CallAccessService(s => s.RemoveUserFromRole(role, CurrentUser));
111        }
112      }), (ex) => MainFormManager.MainForm.ShowError(ex.Message, ex));
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.