Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2839: updated behavior of permission assignment for project owners (client and server side)

File size: 12.8 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      UpdateUserGroupGenealogy();
146      UpdateAssignedPermissions();
147      UpdateInheritedPermissions();
148      var top = BuildPermissionsList(AccessClient.Instance.UsersAndGroups);
149      detailsViewHost.Content = top;
150    }
151
152    private void UpdateNewPermissionList() {
153      UpdateNewAssignedPermissions();
154      UpdateNewInheritedPermissions();
155      var top = BuildPermissionsList(AccessClient.Instance.UsersAndGroups);
156      detailsViewHost.Content = top;
157    }
158
159    private void UpdateAssignedPermissions() {
160      assignedPermissions.Clear();
161      newAssignedPermissions.Clear();
162      var grantedPermissions = GetGrantedPermissionsForProject(Content.Id);
163      foreach (var r in grantedPermissions) {
164        assignedPermissions.Add(r);
165        newAssignedPermissions.Add(r);
166      }
167    }
168
169    private void UpdateNewAssignedPermissions() {
170      for(int i = newAssignedPermissions.Count-1; i >= 0; i--) {
171        if(newAssignedPermissions.Intersect(userGroupAncestors[newAssignedPermissions.ElementAt(i).Id]).Any()) {
172          newAssignedPermissions.Remove(newAssignedPermissions.ElementAt(i));
173        }
174      }
175    }
176
177    private void UpdateInheritedPermissions() {
178      inheritedPermissions.Clear();
179      newInheritedPermissions.Clear();
180      foreach(var item in assignedPermissions) {
181        if(userGroupDescendants.ContainsKey(item.Id)) {
182          foreach(var descendant in userGroupDescendants[item.Id]) {
183            inheritedPermissions.Add(descendant);
184            newInheritedPermissions.Add(descendant);
185          }
186        }
187      }
188    }
189
190    private void UpdateNewInheritedPermissions() {
191      newInheritedPermissions.Clear();
192      foreach(var item in newAssignedPermissions) {
193        if(userGroupDescendants.ContainsKey(item.Id)) {
194          foreach(var descendant in userGroupDescendants[item.Id]) {
195            newInheritedPermissions.Add(descendant);
196          }
197        }
198      }
199    }
200
201    private void UpdateUserGroupGenealogy() {
202      userGroupAncestors.Clear();
203      userGroupDescendants.Clear();
204
205      var usersAndGroups = AccessClient.Instance.UsersAndGroups;
206      foreach (var ug in usersAndGroups) {
207        userGroupAncestors.Add(ug.Id, new HashSet<UserGroupBase>());
208        userGroupDescendants.Add(ug.Id, new HashSet<UserGroupBase>());
209      }
210
211      var userGroupTree = HiveServiceLocator.Instance.CallHiveService(s => s.GetUserGroupTree());
212      foreach(var branch in userGroupTree) {
213        var parent = usersAndGroups.Where(x => x.Id == branch.Key).SingleOrDefault();
214        if(parent != null) {
215          var userGroupsToAdd = usersAndGroups.Where(x => userGroupTree[parent.Id].Contains(x.Id));
216          foreach (var node in userGroupsToAdd) {
217            userGroupDescendants[parent.Id].Add(node);
218            userGroupAncestors[node.Id].Add(parent);
219          }
220        }
221      }
222    }
223
224    private static IEnumerable<UserGroupBase> GetGrantedPermissionsForProject(Guid projectId) {
225      if (projectId == Guid.Empty) return Enumerable.Empty<UserGroupBase>();
226      var projectPermissions = HiveServiceLocator.Instance.CallHiveService(s => s.GetProjectPermissions(projectId));
227      var userIds = new HashSet<Guid>(projectPermissions.Select(x => x.GrantedUserId));
228      return AccessClient.Instance.UsersAndGroups.Where(x => userIds.Contains(x.Id));
229    }
230
231    private static void SetGrantedProjectPermissions(Guid projectId, IEnumerable<Guid> userIds, bool reassign, bool cascading, bool reassignCascading) {
232      if (projectId == null || userIds == null) return;
233      HiveServiceLocator.Instance.CallHiveService(s => {
234        s.SaveProjectPermissions(projectId, userIds.ToList(), reassign, cascading, reassignCascading);
235      });
236    }
237
238    private UserGroupBase BuildPermissionsList(IEnumerable<UserGroupBase> usersAndGroups) {
239      addedPermissions = newAssignedPermissions.Except(assignedPermissions);
240      removedPermissions = assignedPermissions.Except(newAssignedPermissions);
241      addedIncludes = newInheritedPermissions.Except(inheritedPermissions);
242      removedIncludes = inheritedPermissions.Except(newInheritedPermissions);
243
244      treeView.Nodes.Clear();
245      if (!usersAndGroups.Any()) return null;
246
247      treeView.BeforeCheck -= treeView_BeforeCheck;
248      treeView.AfterCheck -= treeView_AfterCheck;
249
250      var userGroups = new HashSet<UserGroup>(usersAndGroups.OfType<UserGroup>());
251      var users = new HashSet<LightweightUser>(usersAndGroups.OfType<LightweightUser>());
252      UserGroupBase first = null;
253
254      var groupsNode = new TreeNode("Groups") { ForeColor = SystemColors.GrayText };
255      groupsNode.ImageIndex = groupsNode.SelectedImageIndex = userGroupImageIndex;
256
257      foreach (var group in userGroups.OrderBy(x => x.Name)) {
258        var node = new TreeNode(group.Name) { Tag = group };
259        node.ImageIndex = userGroupImageIndex;
260        node.SelectedImageIndex = node.ImageIndex;
261        BuildNode(group, node);
262        groupsNode.Nodes.Add(node);
263        if (first == null) first = group;
264      }
265
266      var usersNode = new TreeNode("Users") { ForeColor = SystemColors.GrayText };
267      usersNode.ImageIndex = usersNode.SelectedImageIndex = userImageIndex;
268
269      foreach (var user in users.OrderBy(x => x.ToString())) {
270        var node = new TreeNode(user.ToString()) { Tag = user };
271        node.ImageIndex = userImageIndex;
272        node.SelectedImageIndex = node.ImageIndex;
273        BuildNode(user, node);
274        usersNode.Nodes.Add(node);
275        if (first == null) first = user;
276      }
277
278      treeView.Nodes.Add(groupsNode);
279      treeView.Nodes.Add(usersNode);
280      treeView.BeforeCheck += treeView_BeforeCheck;
281      treeView.AfterCheck += treeView_AfterCheck;
282      treeView.ExpandAll();
283
284      return first;
285    }
286
287    private void BuildNode(UserGroupBase ug, TreeNode node) {
288
289      if (newAssignedPermissions.Contains(ug)) {
290        node.Checked = true;
291      } else if (newInheritedPermissions.Contains(ug)) {
292        node.Checked = true;
293        node.ForeColor = SystemColors.GrayText;
294      }
295
296      if (inheritedPermissions.Contains(ug) && newInheritedPermissions.Contains(ug)) {
297        node.Text += " [included]";
298      } else if (addedIncludes.Contains(ug)) {
299        node.BackColor = addedIncludeColor;
300        node.ForeColor = SystemColors.GrayText;
301        node.Text += " [added include]";
302      } else if (removedIncludes.Contains(ug)) {
303        node.BackColor = removedIncludeColor;
304        node.ForeColor = SystemColors.GrayText;
305        node.Text += " [removed include]";
306      }
307
308      if (addedPermissions.Contains(ug)) {
309        node.BackColor = addedAssignmentColor;
310        node.ForeColor = SystemColors.ControlText;
311        node.Text += " [added assignment]";
312      } else if (removedPermissions.Contains(ug)) {
313        node.BackColor = removedAssignmentColor;
314        node.ForeColor = SystemColors.ControlText;
315        node.Text += " [removed assignment]";
316      }
317
318      if(Content != null && ug != null && ug.Id != Guid.Empty
319        && Content.OwnerUserId == ug.Id) {
320        node.ForeColor = projectOwnerColor;
321      }
322
323    }
324
325    #endregion
326  }
327}
Note: See TracBrowser for help on using the repository browser.