Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClientUserManagement/HeuristicLab.Clients.Access.Administration/3.3/Views/RefreshableRoleSelectionListView.cs @ 7876

Last change on this file since 7876 was 7876, checked in by ascheibe, 13 years ago

#1648 implemented assigning of roles to users

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
26
27namespace HeuristicLab.Clients.Access.Administration {
28  public partial class RefreshableRoleSelectionListView : RefreshableView {
29    public RefreshableRoleSelectionListView() {
30      InitializeComponent();
31    }
32
33    public List<Role> CurrentRoles = new List<Role>();
34    public User CurrentUser { get; set; }
35
36    protected override void RefreshData() {
37      if (CurrentUser != null) {
38        Content.ExecuteActionAsync(new Action(delegate {
39          if (Content.Roles == null) {
40            Content.RefreshRoles();
41          }
42          CurrentRoles = AccessAdministrationClient.CallAccessService<List<Role>>(s => s.GetUserRoles(CurrentUser));
43        }), PluginInfrastructure.ErrorHandling.ShowErrorDialog);
44      }
45    }
46
47    protected override void Content_Refreshing(object sender, EventArgs e) {
48      if (InvokeRequired) {
49        Invoke(new EventHandler(Content_Refreshing), sender, e);
50      } else {
51        base.Content_Refreshing(sender, e);
52        roleSelectionListView.Enabled = false;
53      }
54    }
55
56    protected override void Content_Refreshed(object sender, EventArgs e) {
57      if (InvokeRequired) {
58        Invoke(new EventHandler(Content_Refreshed), sender, e);
59      } else {
60        base.Content_Refreshed(sender, e);
61        roleSelectionListView.Enabled = true;
62
63        roleSelectionListView.Content = new ItemList<Role>(Content.Roles);
64        foreach (var role in CurrentRoles) {
65          foreach (ListViewItem lstRole in roleSelectionListView.ItemsListView.Items) {
66            if (((Role)lstRole.Tag).Equals(role)) {
67              lstRole.Checked = true;
68            }
69          }
70        }
71      }
72    }
73
74    protected override void DeregisterContentEvents() {
75      base.DeregisterContentEvents();
76    }
77
78    private void storeButton_Click(object sender, EventArgs e) {
79      List<Role> addedRoles = new List<Role>();
80      List<Role> deletedRoles = new List<Role>();
81
82      foreach (ListViewItem lstRole in roleSelectionListView.ItemsListView.Items) {
83        if (!CurrentRoles.Contains(((Role)lstRole.Tag)) && lstRole.Checked) {
84          addedRoles.Add((Role)lstRole.Tag);
85          CurrentRoles.Add((Role)lstRole.Tag);
86        }
87
88        if (CurrentRoles.Contains(((Role)lstRole.Tag)) && !lstRole.Checked) {
89          deletedRoles.Add((Role)lstRole.Tag);
90          CurrentRoles.Remove((Role)lstRole.Tag);
91        }
92      }
93
94      Content.ExecuteActionAsync(new Action(delegate {
95        foreach (var role in addedRoles) {
96          AccessAdministrationClient.CallAccessService(s => s.AddUserToRole(role, CurrentUser));
97        }
98
99        foreach (var role in deletedRoles) {
100          AccessAdministrationClient.CallAccessService(s => s.RemoveUserFromRole(role, CurrentUser));
101        }
102      }), PluginInfrastructure.ErrorHandling.ShowErrorDialog);
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.