1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Windows.Forms;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 |
|
---|
27 | namespace 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 |
|
---|
35 | private User currentUser;
|
---|
36 | public User CurrentUser {
|
---|
37 | get { return currentUser; }
|
---|
38 | set {
|
---|
39 | currentUser = value;
|
---|
40 | roleSelectionListView.Content = null;
|
---|
41 | storeButton.Enabled = false;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected override void RefreshData() {
|
---|
46 | if (CurrentUser != null) {
|
---|
47 | Content.ExecuteActionAsync(new Action(delegate {
|
---|
48 | if (Content.Roles == null) {
|
---|
49 | Content.RefreshRoles();
|
---|
50 | }
|
---|
51 | CurrentRoles = AccessAdministrationClient.CallAccessService<List<Role>>(s => s.GetUserRoles(CurrentUser));
|
---|
52 | }), PluginInfrastructure.ErrorHandling.ShowErrorDialog);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected override void Content_Refreshing(object sender, EventArgs e) {
|
---|
57 | if (InvokeRequired) {
|
---|
58 | Invoke(new EventHandler(Content_Refreshing), sender, e);
|
---|
59 | } else {
|
---|
60 | base.Content_Refreshing(sender, e);
|
---|
61 | storeButton.Enabled = false;
|
---|
62 | roleSelectionListView.Enabled = false;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected override void Content_Refreshed(object sender, EventArgs e) {
|
---|
67 | if (InvokeRequired) {
|
---|
68 | Invoke(new EventHandler(Content_Refreshed), sender, e);
|
---|
69 | } else {
|
---|
70 | base.Content_Refreshed(sender, e);
|
---|
71 | if (Content.Roles != null) {
|
---|
72 | roleSelectionListView.Enabled = true;
|
---|
73 | storeButton.Enabled = true;
|
---|
74 |
|
---|
75 | roleSelectionListView.Content = new ItemList<Role>(Content.Roles);
|
---|
76 | foreach (var role in CurrentRoles) {
|
---|
77 | foreach (ListViewItem lstRole in roleSelectionListView.ItemsListView.Items) {
|
---|
78 | if (((Role)lstRole.Tag).Equals(role)) {
|
---|
79 | lstRole.Checked = true;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void storeButton_Click(object sender, EventArgs e) {
|
---|
88 | List<Role> addedRoles = new List<Role>();
|
---|
89 | List<Role> deletedRoles = new List<Role>();
|
---|
90 |
|
---|
91 | foreach (ListViewItem lstRole in roleSelectionListView.ItemsListView.Items) {
|
---|
92 | if (!CurrentRoles.Contains(((Role)lstRole.Tag)) && lstRole.Checked) {
|
---|
93 | addedRoles.Add((Role)lstRole.Tag);
|
---|
94 | CurrentRoles.Add((Role)lstRole.Tag);
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (CurrentRoles.Contains(((Role)lstRole.Tag)) && !lstRole.Checked) {
|
---|
98 | deletedRoles.Add((Role)lstRole.Tag);
|
---|
99 | CurrentRoles.Remove((Role)lstRole.Tag);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | Content.ExecuteActionAsync(new Action(delegate {
|
---|
104 | foreach (var role in addedRoles) {
|
---|
105 | AccessAdministrationClient.CallAccessService(s => s.AddUserToRole(role, CurrentUser));
|
---|
106 | }
|
---|
107 |
|
---|
108 | foreach (var role in deletedRoles) {
|
---|
109 | AccessAdministrationClient.CallAccessService(s => s.RemoveUserFromRole(role, CurrentUser));
|
---|
110 | }
|
---|
111 | }), PluginInfrastructure.ErrorHandling.ShowErrorDialog);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|