Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectPermissionsView.cs @ 16184

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

#2839 fixed bug appeared when clicking on user

File size: 12.9 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    }
120
121    private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
122      var checkedPermission = (UserGroupBase)e.Node.Tag;
123      if (e.Node.Parent == null
124        || newInheritedPermissions.Contains(checkedPermission)
125        || checkedPermission.Id == Guid.Empty
126        || Content.OwnerUserId == checkedPermission.Id)
127          e.Cancel = true;
128    }
129
130    private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
131      var checkedPermission = (UserGroupBase)e.Node.Tag;
132      if (e.Node.Checked)
133        newAssignedPermissions.Add(checkedPermission);
134      else
135        newAssignedPermissions.Remove(checkedPermission);
136
137      UpdateNewPermissionList();
138    }
139    #endregion
140
141    #region Helpers
142    private void UpdatePermissionList() {
143      AccessClient.Instance.Refresh();
144
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.ElementAt(i).Id != Content.OwnerUserId) {
173          newAssignedPermissions.Remove(newAssignedPermissions.ElementAt(i));
174        }
175      }
176    }
177
178    private void UpdateInheritedPermissions() {
179      inheritedPermissions.Clear();
180      newInheritedPermissions.Clear();
181      foreach(var item in assignedPermissions) {
182        if(userGroupDescendants.ContainsKey(item.Id)) {
183          foreach(var descendant in userGroupDescendants[item.Id]) {
184            if(!assignedPermissions.Contains(descendant)) {
185              inheritedPermissions.Add(descendant);
186              newInheritedPermissions.Add(descendant);
187            }
188          }
189        }
190      }
191    }
192
193    private void UpdateNewInheritedPermissions() {
194      newInheritedPermissions.Clear();
195      foreach(var item in newAssignedPermissions) {
196        if(userGroupDescendants.ContainsKey(item.Id)) {
197          foreach(var descendant in userGroupDescendants[item.Id]) {
198            if(!newAssignedPermissions.Contains(descendant))
199              newInheritedPermissions.Add(descendant);
200          }
201        }
202      }
203    }
204
205    private void UpdateUserGroupGenealogy() {
206      userGroupAncestors.Clear();
207      userGroupDescendants.Clear();
208
209      var usersAndGroups = AccessClient.Instance.UsersAndGroups;
210      foreach (var ug in usersAndGroups) {
211        userGroupAncestors.Add(ug.Id, new HashSet<UserGroupBase>());
212        userGroupDescendants.Add(ug.Id, new HashSet<UserGroupBase>());
213      }
214
215      var userGroupTree = HiveServiceLocator.Instance.CallHiveService(s => s.GetUserGroupTree());
216      foreach(var branch in userGroupTree) {
217        var parent = usersAndGroups.Where(x => x.Id == branch.Key).SingleOrDefault();
218        if(parent != null) {
219          var userGroupsToAdd = usersAndGroups.Where(x => userGroupTree[parent.Id].Contains(x.Id));
220          foreach (var node in userGroupsToAdd) {
221            userGroupDescendants[parent.Id].Add(node);
222            userGroupAncestors[node.Id].Add(parent);
223          }
224        }
225      }
226    }
227
228    private static IEnumerable<UserGroupBase> GetGrantedPermissionsForProject(Guid projectId) {
229      if (projectId == Guid.Empty) return Enumerable.Empty<UserGroupBase>();
230      var projectPermissions = HiveServiceLocator.Instance.CallHiveService(s => s.GetProjectPermissions(projectId));
231      var userIds = new HashSet<Guid>(projectPermissions.Select(x => x.GrantedUserId));
232      return AccessClient.Instance.UsersAndGroups.Where(x => userIds.Contains(x.Id));
233    }
234
235    private static void SetGrantedProjectPermissions(Guid projectId, IEnumerable<Guid> userIds, bool reassign, bool cascading, bool reassignCascading) {
236      if (projectId == null || userIds == null) return;
237      HiveServiceLocator.Instance.CallHiveService(s => {
238        s.SaveProjectPermissions(projectId, userIds.ToList(), reassign, cascading, reassignCascading);
239      });
240    }
241
242    private UserGroupBase BuildPermissionsList(IEnumerable<UserGroupBase> usersAndGroups) {
243      addedPermissions = newAssignedPermissions.Except(assignedPermissions);
244      removedPermissions = assignedPermissions.Except(newAssignedPermissions);
245      addedIncludes = newInheritedPermissions.Except(inheritedPermissions);
246      removedIncludes = inheritedPermissions.Except(newInheritedPermissions);
247
248      treeView.Nodes.Clear();
249      if (!usersAndGroups.Any()) return null;
250
251      treeView.BeforeCheck -= treeView_BeforeCheck;
252      treeView.AfterCheck -= treeView_AfterCheck;
253
254      var userGroups = new HashSet<UserGroup>(usersAndGroups.OfType<UserGroup>());
255      var users = new HashSet<LightweightUser>(usersAndGroups.OfType<LightweightUser>());
256      UserGroupBase first = null;
257
258      var groupsNode = new TreeNode("Groups") { ForeColor = SystemColors.GrayText };
259      groupsNode.ImageIndex = groupsNode.SelectedImageIndex = userGroupImageIndex;
260
261      foreach (var group in userGroups.OrderBy(x => x.Name)) {
262        var node = new TreeNode(group.Name) { Tag = group };
263        node.ImageIndex = userGroupImageIndex;
264        node.SelectedImageIndex = node.ImageIndex;
265        BuildNode(group, node);
266        groupsNode.Nodes.Add(node);
267        if (first == null) first = group;
268      }
269
270      var usersNode = new TreeNode("Users") { ForeColor = SystemColors.GrayText };
271      usersNode.ImageIndex = usersNode.SelectedImageIndex = userImageIndex;
272
273      foreach (var user in users.OrderBy(x => x.ToString())) {
274        var node = new TreeNode(user.ToString()) { Tag = user };
275        node.ImageIndex = userImageIndex;
276        node.SelectedImageIndex = node.ImageIndex;
277        BuildNode(user, node);
278        usersNode.Nodes.Add(node);
279        if (first == null) first = user;
280      }
281
282      treeView.Nodes.Add(groupsNode);
283      treeView.Nodes.Add(usersNode);
284      treeView.BeforeCheck += treeView_BeforeCheck;
285      treeView.AfterCheck += treeView_AfterCheck;
286      treeView.ExpandAll();
287
288      return first;
289    }
290
291    private void BuildNode(UserGroupBase ug, TreeNode node) {
292
293      if (newAssignedPermissions.Contains(ug)) {
294        node.Checked = true;
295      } else if (newInheritedPermissions.Contains(ug)) {
296        node.Checked = true;
297        node.ForeColor = SystemColors.GrayText;
298      }
299
300      if (inheritedPermissions.Contains(ug) && newInheritedPermissions.Contains(ug)) {
301        node.Text += " [included]";
302      } else if (addedIncludes.Contains(ug)) {
303        node.BackColor = addedIncludeColor;
304        node.ForeColor = SystemColors.GrayText;
305        node.Text += " [added include]";
306      } else if (removedIncludes.Contains(ug)) {
307        node.BackColor = removedIncludeColor;
308        node.ForeColor = SystemColors.GrayText;
309        node.Text += " [removed include]";
310      }
311
312      if (addedPermissions.Contains(ug)) {
313        node.BackColor = addedAssignmentColor;
314        node.ForeColor = SystemColors.ControlText;
315        node.Text += " [added assignment]";
316      } else if (removedPermissions.Contains(ug)) {
317        node.BackColor = removedAssignmentColor;
318        node.ForeColor = SystemColors.ControlText;
319        node.Text += " [removed assignment]";
320      }
321
322      if(Content != null && ug != null && ug.Id != Guid.Empty
323        && Content.OwnerUserId == ug.Id) {
324        node.ForeColor = projectOwnerColor;
325      }
326
327    }
328
329    #endregion
330  }
331}
Note: See TracBrowser for help on using the repository browser.