source: branches/HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectPermissionsView.cs @ 15580

Last change on this file since 15580 was 15580, checked in by jzenisek, 4 years ago

#2839

  • minor bugfix in HiveService.cs
  • corrected behavior of ProjectPermissionsView
File size: 12.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Clients.Access;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.Clients.Hive.Administrator.Views {
33  [View("ProjectView")]
34  [Content(typeof(Project), IsDefaultView = false)]
35  public partial class ProjectPermissionsView : ItemView {
36    private const int userImageIndex = 0;
37    private const int userGroupImageIndex = 1;
38
39    private readonly HashSet<UserGroupBase> assignedPermissions = new HashSet<UserGroupBase>();
40    private readonly HashSet<UserGroupBase> inheritedPermissions = new HashSet<UserGroupBase>();
41    private readonly HashSet<UserGroupBase> newAssignedPermissions = new HashSet<UserGroupBase>();
42    private readonly HashSet<UserGroupBase> newInheritedPermissions = new HashSet<UserGroupBase>();
43    private readonly Dictionary<Guid, HashSet<UserGroupBase>> userGroupAncestors = new Dictionary<Guid, HashSet<UserGroupBase>>();
44    private readonly Dictionary<Guid, HashSet<UserGroupBase>> userGroupDescendants = new Dictionary<Guid, HashSet<UserGroupBase>>();
45
46    private IEnumerable<UserGroupBase> addedPermissions;
47    private IEnumerable<UserGroupBase> removedPermissions;
48    private IEnumerable<UserGroupBase> addedIncludes;
49    private IEnumerable<UserGroupBase> removedIncludes;
50
51    private readonly Color addedAssignmentColor = Color.FromArgb(255, 87, 191, 193); // #57bfc1
52    private readonly Color removedAssignmentColor = Color.FromArgb(255, 236, 159, 72); // #ec9f48
53    private readonly Color addedIncludeColor = Color.FromArgb(25, 169, 221, 221); // #a9dddd
54    private readonly Color removedIncludeColor = Color.FromArgb(25, 249, 210, 145); // #f9d291
55
56    public new Project Content {
57      get { return (Project)base.Content; }
58      set { base.Content = value; }
59    }
60
61    public ProjectPermissionsView() {
62      InitializeComponent();
63
64      treeView.ImageList.Images.Add(VSImageLibrary.User);
65      treeView.ImageList.Images.Add(VSImageLibrary.UserAccounts);
66    }
67
68    #region Overrides
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        assignedPermissions.Clear();
73        inheritedPermissions.Clear();
74        newAssignedPermissions.Clear();
75        newInheritedPermissions.Clear();
76        treeView.Nodes.Clear();
77        detailsViewHost.Content = null;
78      } else {
79        UpdatePermissionList();
80        detailsViewHost.ActiveView.Locked = true;
81      }
82    }
83    #endregion
84
85    #region Event Handlers
86    private void ProjectPermissionsView_Load(object sender, EventArgs e) {
87
88    }
89
90    private void refreshButton_Click(object sender, EventArgs e) {
91      UpdatePermissionList();
92    }
93
94    private async void inheritButton_Click(object sender, EventArgs e) {
95      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
96        action: () => SetGrantedProjectPermissions(Content.Id, newAssignedPermissions.Select(x => x.Id), false, true, true));
97      UpdatePermissionList();
98    }
99
100    private async void saveButton_Click(object sender, EventArgs e) {
101      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
102        action: () => SetGrantedProjectPermissions(Content.Id, newAssignedPermissions.Select(x => x.Id), false, false, false));
103      UpdatePermissionList();
104    }
105
106    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
107      var selectedPermission = (UserGroupBase)e.Node.Tag;
108      detailsViewHost.Content = selectedPermission;
109    }
110
111    private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
112      var checkedPermission = (UserGroupBase)e.Node.Tag;
113      if (e.Node.Parent == null || newInheritedPermissions.Contains(checkedPermission) || checkedPermission.Id == Guid.Empty) e.Cancel = true;
114    }
115
116    private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
117      var checkedPermission = (UserGroupBase)e.Node.Tag;
118      if (e.Node.Checked)
119        newAssignedPermissions.Add(checkedPermission);
120      else
121        newAssignedPermissions.Remove(checkedPermission);
122
123      UpdateNewPermissionList();
124    }
125    #endregion
126
127    #region Helpers
128    private void UpdatePermissionList() {
129      UpdateUserGroupGenealogy();
130      UpdateAssignedPermissions();
131      UpdateInheritedPermissions();
132      var top = BuildPermissionsList(AccessClient.Instance.UsersAndGroups);
133      detailsViewHost.Content = top;
134    }
135
136    private void UpdateNewPermissionList() {
137      UpdateNewAssignedPermissions();
138      UpdateNewInheritedPermissions();
139      var top = BuildPermissionsList(AccessClient.Instance.UsersAndGroups);
140      detailsViewHost.Content = top;
141    }
142
143    private void UpdateAssignedPermissions() {
144      assignedPermissions.Clear();
145      newAssignedPermissions.Clear();
146      var grantedPermissions = GetGrantedPermissionsForProject(Content.Id);
147      foreach (var r in grantedPermissions) {
148        assignedPermissions.Add(r);
149        newAssignedPermissions.Add(r);
150      }
151    }
152
153    private void UpdateNewAssignedPermissions() {
154      for(int i = newAssignedPermissions.Count-1; i >= 0; i--) {
155        if(newAssignedPermissions.Intersect(userGroupAncestors[newAssignedPermissions.ElementAt(i).Id]).Any()) {
156          newAssignedPermissions.Remove(newAssignedPermissions.ElementAt(i));
157        }
158      }
159    }
160
161    private void UpdateInheritedPermissions() {
162      inheritedPermissions.Clear();
163      newInheritedPermissions.Clear();
164      foreach(var item in assignedPermissions) {
165        if(userGroupDescendants.ContainsKey(item.Id)) {
166          foreach(var descendant in userGroupDescendants[item.Id]) {
167            inheritedPermissions.Add(descendant);
168            newInheritedPermissions.Add(descendant);
169          }
170        }
171      }
172    }
173
174    private void UpdateNewInheritedPermissions() {
175      newInheritedPermissions.Clear();
176      foreach(var item in newAssignedPermissions) {
177        if(userGroupDescendants.ContainsKey(item.Id)) {
178          foreach(var descendant in userGroupDescendants[item.Id]) {
179            newInheritedPermissions.Add(descendant);
180          }
181        }
182      }
183    }
184
185    private void UpdateUserGroupGenealogy() {
186      userGroupAncestors.Clear();
187      userGroupDescendants.Clear();
188
189      var usersAndGroups = AccessClient.Instance.UsersAndGroups;
190      foreach (var ug in usersAndGroups) {
191        userGroupAncestors.Add(ug.Id, new HashSet<UserGroupBase>());
192        userGroupDescendants.Add(ug.Id, new HashSet<UserGroupBase>());
193      }
194
195      var userGroupTree = HiveServiceLocator.Instance.CallHiveService(s => s.GetUserGroupTree());
196      foreach(var branch in userGroupTree) {
197        var parent = usersAndGroups.Where(x => x.Id == branch.Key).SingleOrDefault();
198        if(parent != null) {
199          var userGroupsToAdd = usersAndGroups.Where(x => userGroupTree[parent.Id].Contains(x.Id));
200          foreach (var node in userGroupsToAdd) {
201            userGroupDescendants[parent.Id].Add(node);
202            userGroupAncestors[node.Id].Add(parent);
203          }
204        }
205      }
206    }
207
208    private static IEnumerable<UserGroupBase> GetGrantedPermissionsForProject(Guid projectId) {
209      var projectPermissions = HiveServiceLocator.Instance.CallHiveService(s => s.GetProjectPermissions(projectId));
210      var userIds = new HashSet<Guid>(projectPermissions.Select(x => x.GrantedUserId));
211      return AccessClient.Instance.UsersAndGroups.Where(x => userIds.Contains(x.Id));
212    }
213
214    private static void SetGrantedProjectPermissions(Guid projectId, IEnumerable<Guid> userIds, bool reassign, bool cascading, bool reassignCascading) {
215      if (projectId == null || userIds == null) return;
216      HiveServiceLocator.Instance.CallHiveService(s => {
217        s.SaveProjectPermissions(projectId, userIds.ToList(), reassign, cascading, reassignCascading);
218      });
219    }
220
221    private UserGroupBase BuildPermissionsList(IEnumerable<UserGroupBase> usersAndGroups) {
222      addedPermissions = newAssignedPermissions.Except(assignedPermissions);
223      removedPermissions = assignedPermissions.Except(newAssignedPermissions);
224      addedIncludes = newInheritedPermissions.Except(inheritedPermissions);
225      removedIncludes = inheritedPermissions.Except(newInheritedPermissions);
226
227      treeView.Nodes.Clear();
228      if (!usersAndGroups.Any()) return null;
229
230      treeView.BeforeCheck -= treeView_BeforeCheck;
231      treeView.AfterCheck -= treeView_AfterCheck;
232
233      var userGroups = new HashSet<UserGroup>(usersAndGroups.OfType<UserGroup>());
234      var users = new HashSet<LightweightUser>(usersAndGroups.OfType<LightweightUser>());
235      UserGroupBase first = null;
236
237      var groupsNode = new TreeNode("Groups") { ForeColor = SystemColors.GrayText };
238      groupsNode.ImageIndex = groupsNode.SelectedImageIndex = userGroupImageIndex;
239
240      foreach (var group in userGroups.OrderBy(x => x.Name)) {
241        var node = new TreeNode(group.Name) { Tag = group };
242        node.ImageIndex = userGroupImageIndex;
243        node.SelectedImageIndex = node.ImageIndex;
244        BuildNode(group, node);
245        groupsNode.Nodes.Add(node);
246        if (first == null) first = group;
247      }
248
249      var usersNode = new TreeNode("Users") { ForeColor = SystemColors.GrayText };
250      usersNode.ImageIndex = usersNode.SelectedImageIndex = userImageIndex;
251
252      foreach (var user in users.OrderBy(x => x.ToString())) {
253        var node = new TreeNode(user.ToString()) { Tag = user };
254        node.ImageIndex = userImageIndex;
255        node.SelectedImageIndex = node.ImageIndex;
256        BuildNode(user, node);
257        usersNode.Nodes.Add(node);
258        if (first == null) first = user;
259      }
260
261      treeView.Nodes.Add(groupsNode);
262      treeView.Nodes.Add(usersNode);
263      treeView.BeforeCheck += treeView_BeforeCheck;
264      treeView.AfterCheck += treeView_AfterCheck;
265      treeView.ExpandAll();
266
267      return first;
268    }
269
270    private void BuildNode(UserGroupBase ug, TreeNode node) {
271
272      if (newAssignedPermissions.Contains(ug)) {
273        node.Checked = true;
274      } else if (newInheritedPermissions.Contains(ug)) {
275        node.Checked = true;
276        node.ForeColor = SystemColors.GrayText;
277      }
278
279      if (inheritedPermissions.Contains(ug) && newInheritedPermissions.Contains(ug)) {
280        node.Text += " [included]";
281      } else if (addedIncludes.Contains(ug)) {
282        node.BackColor = addedIncludeColor;
283        node.ForeColor = SystemColors.GrayText;
284        node.Text += " [added include]";
285      } else if (removedIncludes.Contains(ug)) {
286        node.BackColor = removedIncludeColor;
287        node.ForeColor = SystemColors.GrayText;
288        node.Text += " [removed include]";
289      }
290
291      if (addedPermissions.Contains(ug)) {
292        node.BackColor = addedAssignmentColor;
293        node.ForeColor = SystemColors.ControlText;
294        node.Text += " [added assignment]";
295      } else if (removedPermissions.Contains(ug)) {
296        node.BackColor = removedAssignmentColor;
297        node.ForeColor = SystemColors.ControlText;
298        node.Text += " [removed assignment]";
299      }
300
301    }
302
303    #endregion
304  }
305}
Note: See TracBrowser for help on using the repository browser.