Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectPermissionsView.cs @ 17712

Last change on this file since 17712 was 15978, checked in by jzenisek, 6 years ago

#2839: applied several fixes:

  • show full project-path in project/resource selector
  • handle lost of project-ownership by not withdrawing permissions
  • update automatically after hand-down save
  • lock jobs for which statistics/deletion is pending
  • lock the disabled checkbox in ProjectResourcesView...
File size: 13.1 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    private readonly Color projectOwnerColor = Color.DarkRed;
56
57    public new Project Content {
58      get { return (Project)base.Content; }
59      set { base.Content = value; }
60    }
61
62    public ProjectPermissionsView() {
63      InitializeComponent();
64
65      treeView.ImageList.Images.Add(VSImageLibrary.User);
66      treeView.ImageList.Images.Add(VSImageLibrary.UserAccounts);
67    }
68
69    #region Overrides
70    protected override void OnContentChanged() {
71      base.OnContentChanged();
72      if (Content == null) {
73        assignedPermissions.Clear();
74        inheritedPermissions.Clear();
75        newAssignedPermissions.Clear();
76        newInheritedPermissions.Clear();
77        treeView.Nodes.Clear();
78        detailsViewHost.Content = null;
79      } else {
80        UpdatePermissionList();
81        detailsViewHost.ActiveView.Locked = true;
82      }
83    }
84
85    protected override void SetEnabledStateOfControls() {
86      base.SetEnabledStateOfControls();
87      bool enabled = Content != null && !Locked && !ReadOnly;
88
89      inheritButton.Enabled = enabled;
90      saveButton.Enabled = enabled;
91      treeView.Enabled = enabled;
92    }
93    #endregion
94
95    #region Event Handlers
96    private void ProjectPermissionsView_Load(object sender, EventArgs e) {
97
98    }
99
100    private void refreshButton_Click(object sender, EventArgs e) {
101      UpdatePermissionList();
102    }
103
104    private async void inheritButton_Click(object sender, EventArgs e) {
105      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
106        action: () => SetGrantedProjectPermissions(Content.Id, newAssignedPermissions.Select(x => x.Id), false, true, false));
107      UpdatePermissionList();
108    }
109
110    private async void saveButton_Click(object sender, EventArgs e) {
111      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
112        action: () => SetGrantedProjectPermissions(Content.Id, newAssignedPermissions.Select(x => x.Id), false, false, false));
113      UpdatePermissionList();
114    }
115
116    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
117      var selectedPermission = (UserGroupBase)e.Node.Tag;
118      detailsViewHost.Content = selectedPermission;
119      if (selectedPermission is LightweightUser)
120        detailsViewHost.ViewType = typeof(Access.Views.RefreshableLightweightUserInformationView);
121    }
122
123    private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
124      var checkedPermission = (UserGroupBase)e.Node.Tag;
125      if (e.Node.Parent == null
126        || newInheritedPermissions.Contains(checkedPermission)
127        || checkedPermission.Id == Guid.Empty
128        || Content.OwnerUserId == checkedPermission.Id)
129          e.Cancel = true;
130    }
131
132    private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
133      var checkedPermission = (UserGroupBase)e.Node.Tag;
134      if (e.Node.Checked)
135        newAssignedPermissions.Add(checkedPermission);
136      else
137        newAssignedPermissions.Remove(checkedPermission);
138
139      UpdateNewPermissionList();
140    }
141    #endregion
142
143    #region Helpers
144    private void UpdatePermissionList() {
145      AccessClient.Instance.Refresh();
146
147      UpdateUserGroupGenealogy();
148      UpdateAssignedPermissions();
149      UpdateInheritedPermissions();
150      var top = BuildPermissionsList(AccessClient.Instance.UsersAndGroups);
151      detailsViewHost.Content = top;
152    }
153
154    private void UpdateNewPermissionList() {
155      UpdateNewAssignedPermissions();
156      UpdateNewInheritedPermissions();
157      var top = BuildPermissionsList(AccessClient.Instance.UsersAndGroups);
158      detailsViewHost.Content = top;
159    }
160
161    private void UpdateAssignedPermissions() {
162      assignedPermissions.Clear();
163      newAssignedPermissions.Clear();
164      var grantedPermissions = GetGrantedPermissionsForProject(Content.Id);
165      foreach (var r in grantedPermissions) {
166        assignedPermissions.Add(r);
167        newAssignedPermissions.Add(r);
168      }
169    }
170
171    private void UpdateNewAssignedPermissions() {
172      for(int i = newAssignedPermissions.Count-1; i >= 0; i--) {
173        if(newAssignedPermissions.Intersect(userGroupAncestors[newAssignedPermissions.ElementAt(i).Id]).Any()
174          && newAssignedPermissions.ElementAt(i).Id != Content.OwnerUserId) {
175          newAssignedPermissions.Remove(newAssignedPermissions.ElementAt(i));
176        }
177      }
178    }
179
180    private void UpdateInheritedPermissions() {
181      inheritedPermissions.Clear();
182      newInheritedPermissions.Clear();
183      foreach(var item in assignedPermissions) {
184        if(userGroupDescendants.ContainsKey(item.Id)) {
185          foreach(var descendant in userGroupDescendants[item.Id]) {
186            if(!assignedPermissions.Contains(descendant)) {
187              inheritedPermissions.Add(descendant);
188              newInheritedPermissions.Add(descendant);
189            }
190          }
191        }
192      }
193    }
194
195    private void UpdateNewInheritedPermissions() {
196      newInheritedPermissions.Clear();
197      foreach(var item in newAssignedPermissions) {
198        if(userGroupDescendants.ContainsKey(item.Id)) {
199          foreach(var descendant in userGroupDescendants[item.Id]) {
200            if(!newAssignedPermissions.Contains(descendant))
201              newInheritedPermissions.Add(descendant);
202          }
203        }
204      }
205    }
206
207    private void UpdateUserGroupGenealogy() {
208      userGroupAncestors.Clear();
209      userGroupDescendants.Clear();
210
211      var usersAndGroups = AccessClient.Instance.UsersAndGroups;
212      foreach (var ug in usersAndGroups) {
213        userGroupAncestors.Add(ug.Id, new HashSet<UserGroupBase>());
214        userGroupDescendants.Add(ug.Id, new HashSet<UserGroupBase>());
215      }
216
217      var userGroupTree = HiveServiceLocator.Instance.CallHiveService(s => s.GetUserGroupTree());
218      foreach(var branch in userGroupTree) {
219        var parent = usersAndGroups.Where(x => x.Id == branch.Key).SingleOrDefault();
220        if(parent != null) {
221          var userGroupsToAdd = usersAndGroups.Where(x => userGroupTree[parent.Id].Contains(x.Id));
222          foreach (var node in userGroupsToAdd) {
223            userGroupDescendants[parent.Id].Add(node);
224            userGroupAncestors[node.Id].Add(parent);
225          }
226        }
227      }
228    }
229
230    private static IEnumerable<UserGroupBase> GetGrantedPermissionsForProject(Guid projectId) {
231      if (projectId == Guid.Empty) return Enumerable.Empty<UserGroupBase>();
232      var projectPermissions = HiveServiceLocator.Instance.CallHiveService(s => s.GetProjectPermissions(projectId));
233      var userIds = new HashSet<Guid>(projectPermissions.Select(x => x.GrantedUserId));
234      return AccessClient.Instance.UsersAndGroups.Where(x => userIds.Contains(x.Id));
235    }
236
237    private static void SetGrantedProjectPermissions(Guid projectId, IEnumerable<Guid> userIds, bool reassign, bool cascading, bool reassignCascading) {
238      if (projectId == null || userIds == null) return;
239      HiveServiceLocator.Instance.CallHiveService(s => {
240        s.SaveProjectPermissions(projectId, userIds.ToList(), reassign, cascading, reassignCascading);
241      });
242    }
243
244    private UserGroupBase BuildPermissionsList(IEnumerable<UserGroupBase> usersAndGroups) {
245      addedPermissions = newAssignedPermissions.Except(assignedPermissions);
246      removedPermissions = assignedPermissions.Except(newAssignedPermissions);
247      addedIncludes = newInheritedPermissions.Except(inheritedPermissions);
248      removedIncludes = inheritedPermissions.Except(newInheritedPermissions);
249
250      treeView.Nodes.Clear();
251      if (!usersAndGroups.Any()) return null;
252
253      treeView.BeforeCheck -= treeView_BeforeCheck;
254      treeView.AfterCheck -= treeView_AfterCheck;
255
256      var userGroups = new HashSet<UserGroup>(usersAndGroups.OfType<UserGroup>());
257      var users = new HashSet<LightweightUser>(usersAndGroups.OfType<LightweightUser>());
258      UserGroupBase first = null;
259
260      var groupsNode = new TreeNode("Groups") { ForeColor = SystemColors.GrayText };
261      groupsNode.ImageIndex = groupsNode.SelectedImageIndex = userGroupImageIndex;
262
263      foreach (var group in userGroups.OrderBy(x => x.Name)) {
264        var node = new TreeNode(group.Name) { Tag = group };
265        node.ImageIndex = userGroupImageIndex;
266        node.SelectedImageIndex = node.ImageIndex;
267        BuildNode(group, node);
268        groupsNode.Nodes.Add(node);
269        if (first == null) first = group;
270      }
271
272      var usersNode = new TreeNode("Users") { ForeColor = SystemColors.GrayText };
273      usersNode.ImageIndex = usersNode.SelectedImageIndex = userImageIndex;
274
275      foreach (var user in users.OrderBy(x => x.ToString())) {
276        var node = new TreeNode(user.ToString()) { Tag = user };
277        node.ImageIndex = userImageIndex;
278        node.SelectedImageIndex = node.ImageIndex;
279        BuildNode(user, node);
280        usersNode.Nodes.Add(node);
281        if (first == null) first = user;
282      }
283
284      treeView.Nodes.Add(groupsNode);
285      treeView.Nodes.Add(usersNode);
286      treeView.BeforeCheck += treeView_BeforeCheck;
287      treeView.AfterCheck += treeView_AfterCheck;
288      treeView.ExpandAll();
289
290      return first;
291    }
292
293    private void BuildNode(UserGroupBase ug, TreeNode node) {
294
295      if (newAssignedPermissions.Contains(ug)) {
296        node.Checked = true;
297      } else if (newInheritedPermissions.Contains(ug)) {
298        node.Checked = true;
299        node.ForeColor = SystemColors.GrayText;
300      }
301
302      if (inheritedPermissions.Contains(ug) && newInheritedPermissions.Contains(ug)) {
303        node.Text += " [included]";
304      } else if (addedIncludes.Contains(ug)) {
305        node.BackColor = addedIncludeColor;
306        node.ForeColor = SystemColors.GrayText;
307        node.Text += " [added include]";
308      } else if (removedIncludes.Contains(ug)) {
309        node.BackColor = removedIncludeColor;
310        node.ForeColor = SystemColors.GrayText;
311        node.Text += " [removed include]";
312      }
313
314      if (addedPermissions.Contains(ug)) {
315        node.BackColor = addedAssignmentColor;
316        node.ForeColor = SystemColors.ControlText;
317        node.Text += " [added assignment]";
318      } else if (removedPermissions.Contains(ug)) {
319        node.BackColor = removedAssignmentColor;
320        node.ForeColor = SystemColors.ControlText;
321        node.Text += " [removed assignment]";
322      }
323
324      if(Content != null && ug != null && ug.Id != Guid.Empty
325        && Content.OwnerUserId == ug.Id) {
326        node.ForeColor = projectOwnerColor;
327      }
328
329    }
330
331    #endregion
332  }
333}
Note: See TracBrowser for help on using the repository browser.