Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15517 was 15422, checked in by jkarder, 7 years ago

#2839: worked on resources and projects views

File size: 7.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
42    public new Project Content {
43      get { return (Project)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public ProjectPermissionsView() {
48      InitializeComponent();
49
50      treeView.ImageList.Images.Add(VSImageLibrary.User);
51      treeView.ImageList.Images.Add(VSImageLibrary.UserAccounts);
52    }
53
54    #region Overrides
55    protected override void OnContentChanged() {
56      base.OnContentChanged();
57      if (Content == null) {
58        assignedPermissions.Clear();
59        inheritedPermissions.Clear();
60        treeView.Nodes.Clear();
61        detailsViewHost.Content = null;
62      } else {
63        UpdateAssignedPermissions();
64        UpdateInheritedPermissions();
65        var top = BuildPermissionsList(AccessClient.Instance.UsersAndGroups);
66        detailsViewHost.Content = top;
67      }
68    }
69    #endregion
70
71    #region Event Handlers
72    private void ProjectPermissionsView_Load(object sender, EventArgs e) {
73
74    }
75
76    private void refreshButton_Click(object sender, EventArgs e) {
77      UpdateAssignedPermissions();
78      UpdateInheritedPermissions();
79      var top = BuildPermissionsList(AccessClient.Instance.UsersAndGroups);
80      detailsViewHost.Content = top;
81    }
82
83    private void inheritButton_Click(object sender, EventArgs e) {
84      SetGrantedProjectPermissions(Content.Id, Enumerable.Empty<Guid>());
85      UpdateAssignedPermissions();
86      UpdateInheritedPermissions();
87      var top = BuildPermissionsList(AccessClient.Instance.UsersAndGroups);
88      detailsViewHost.Content = top;
89    }
90
91    private async void saveButton_Click(object sender, EventArgs e) {
92      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
93        action: () => SetGrantedProjectPermissions(Content.Id, assignedPermissions.Select(x => x.Id)));
94    }
95
96    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
97      var selectedPermission = (UserGroupBase)e.Node.Tag;
98      detailsViewHost.Content = selectedPermission;
99    }
100
101    private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
102      var checkedPermission = (UserGroupBase)e.Node.Tag;
103      if (e.Node.Parent == null || inheritedPermissions.Contains(checkedPermission) || checkedPermission.Id == Guid.Empty) e.Cancel = true;
104    }
105
106    private void treeView_AfterCheck(object sender, TreeViewEventArgs e) {
107      var checkedPermission = (UserGroupBase)e.Node.Tag;
108      if (e.Node.Checked)
109        assignedPermissions.Add(checkedPermission);
110      else
111        assignedPermissions.Remove(checkedPermission);
112    }
113    #endregion
114
115    #region Helpers
116    private static IEnumerable<UserGroupBase> GetGrantedPermissionsForProject(Guid projectId) {
117      var projectPermissions = HiveServiceLocator.Instance.CallHiveService(s => s.GetProjectPermissions(projectId));
118      var userIds = new HashSet<Guid>(projectPermissions.Select(x => x.GrantedUserId));
119      return AccessClient.Instance.UsersAndGroups.Where(x => userIds.Contains(x.Id));
120    }
121
122    private static void SetGrantedProjectPermissions(Guid projectId, IEnumerable<Guid> userIds) {
123      HiveServiceLocator.Instance.CallHiveService(s => {
124        var currentlyAssignedPermissions = s.GetProjectPermissions(projectId);
125        s.RevokeProjectPermissions(projectId, currentlyAssignedPermissions.Select(x => x.GrantedUserId).ToList());
126        s.GrantProjectPermissions(projectId, userIds.ToList());
127      });
128    }
129
130    private void UpdateAssignedPermissions() {
131      assignedPermissions.Clear();
132      foreach (var r in GetGrantedPermissionsForProject(Content.Id))
133        assignedPermissions.Add(r);
134    }
135
136    private void UpdateInheritedPermissions() {
137      inheritedPermissions.Clear();
138      var parentProject = HiveAdminClient.Instance.Projects.SingleOrDefault(x => x.Id == Content.ParentProjectId);
139      while (parentProject != null) {
140        foreach (var r in GetGrantedPermissionsForProject(parentProject.Id))
141          inheritedPermissions.Add(r);
142        parentProject = HiveAdminClient.Instance.Projects.SingleOrDefault(x => x.Id == parentProject.ParentProjectId);
143      }
144    }
145
146    private UserGroupBase BuildPermissionsList(IEnumerable<UserGroupBase> usersAndGroups) {
147      treeView.Nodes.Clear();
148      if (!usersAndGroups.Any()) return null;
149
150      treeView.BeforeCheck -= treeView_BeforeCheck;
151      treeView.AfterCheck -= treeView_AfterCheck;
152
153      var userGroups = new HashSet<UserGroup>(usersAndGroups.OfType<UserGroup>());
154      var users = new HashSet<LightweightUser>(usersAndGroups.OfType<LightweightUser>());
155      UserGroupBase first = null;
156
157      var groupsNode = new TreeNode("Groups") { ForeColor = SystemColors.GrayText };
158      groupsNode.ImageIndex = groupsNode.SelectedImageIndex = userGroupImageIndex;
159
160      foreach (var group in userGroups.OrderBy(x => x.Name)) {
161        var node = new TreeNode(group.Name) { Tag = group };
162        node.ImageIndex = userGroupImageIndex;
163        node.SelectedImageIndex = node.ImageIndex;
164
165        if (inheritedPermissions.Contains(group)) {
166          node.Checked = true;
167          node.Text += " (Inherited)";
168          node.ForeColor = SystemColors.GrayText;
169        } else if (assignedPermissions.Contains(group))
170          node.Checked = true;
171
172        groupsNode.Nodes.Add(node);
173        if (first == null) first = group;
174      }
175
176      var usersNode = new TreeNode("Users") { ForeColor = SystemColors.GrayText };
177      usersNode.ImageIndex = usersNode.SelectedImageIndex = userImageIndex;
178
179      foreach (var user in users.OrderBy(x => x.ToString())) {
180        var node = new TreeNode(user.ToString()) { Tag = user };
181        node.ImageIndex = userImageIndex;
182        node.SelectedImageIndex = node.ImageIndex;
183
184        if (inheritedPermissions.Contains(user)) {
185          node.Checked = true;
186          node.Text += " (Inherited)";
187          node.ForeColor = SystemColors.GrayText;
188        } else if (assignedPermissions.Contains(user))
189          node.Checked = true;
190
191        usersNode.Nodes.Add(node);
192        if (first == null) first = user;
193      }
194
195      treeView.Nodes.Add(groupsNode);
196      treeView.Nodes.Add(usersNode);
197      treeView.BeforeCheck += treeView_BeforeCheck;
198      treeView.AfterCheck += treeView_AfterCheck;
199      treeView.ExpandAll();
200
201      return first;
202    }
203    #endregion
204  }
205}
Note: See TracBrowser for help on using the repository browser.