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

Last change on this file since 15567 was 15567, checked in by jzenisek, 5 years ago

#2839 worked on views for project and resource administration

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