Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2877_HiveImprovements/HeuristicLab.Clients.Access.Administration/3.3/AccessAdministrationClient.cs @ 15632

Last change on this file since 15632 was 15632, checked in by swagner, 7 years ago

#2877: Improved User Administration GUI

  • sorted users, groups and roles by name
  • made password textbox in password reset dialog read-only
File size: 6.8 KB
RevLine 
[8042]1#region License Information
2/* HeuristicLab
[15583]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8042]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;
[15632]23using System.Linq;
[8042]24using HeuristicLab.Clients.Common;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.Clients.Access.Administration {
29  [Item("AccessAdministrationClient", "AccessAdministration client.")]
30  public class AccessAdministrationClient : IContent {
31    private static AccessAdministrationClient instance;
32    public static AccessAdministrationClient Instance {
33      get {
34        if (instance == null) instance = new AccessAdministrationClient();
35        return instance;
36      }
37    }
38
39    #region Properties
40    private ItemList<User> users;
41    public ItemList<User> Users {
42      get {
43        return users;
44      }
45    }
46
47    public ItemList<UserGroup> Groups { get; set; }
48    public ItemList<Role> Roles { get; set; }
49    #endregion
50
51    private AccessAdministrationClient() { }
52
53    #region Refresh
54    public void RefreshUsers() {
55      users = new ItemList<User>();
[15632]56      users.AddRange(CallAccessService<ItemList<User>>(s => new ItemList<User>(s.GetAllUsers().OrderBy(x => x.UserName))));
[8042]57    }
58
59    public void RefreshUsersAsync(Action<Exception> exceptionCallback) {
60      ExecuteActionAsync(RefreshUsers, exceptionCallback);
61    }
62
63    public void RefreshUserGroups() {
64      Groups = new ItemList<UserGroup>();
[15632]65      Groups.AddRange(CallAccessService<ItemList<UserGroup>>(s => new ItemList<UserGroup>(s.GetAllUserGroups().OrderBy(x => x.Name))));
[8042]66    }
67
68    public void RefreshUserGroupsAsync(Action<Exception> exceptionCallback) {
69      ExecuteActionAsync(RefreshUserGroups, exceptionCallback);
70    }
71
72    public void RefreshRoles() {
73      Roles = new ItemList<Role>();
[15632]74      Roles.AddRange(CallAccessService<ItemList<Role>>(s => new ItemList<Role>(s.GetRoles().OrderBy(x => x.Name))));
[8042]75    }
76
77    public void RefreshRolesAsync(Action<Exception> exceptionCallback) {
78      ExecuteActionAsync(RefreshRoles, exceptionCallback);
79    }
80
81    #endregion
82
83    #region Store
84    public void StoreUsers() {
85      foreach (User u in users) {
86        if (u.Modified) {
87          if (u.Id == Guid.Empty) {
88            CallAccessService(s => u.Id = s.AddUser(u).Id);
89          } else {
90            CallAccessService(s => s.UpdateUser(u));
91          }
92          u.SetUnmodified();
93        }
94      }
95    }
96
97    public void StoreUsersAsync(Action<Exception> exceptionCallback) {
98      ExecuteActionAsync(StoreUsers, exceptionCallback);
99    }
100
101    public void StoreUserGroups() {
102      foreach (UserGroup g in Groups) {
103        if (g.Modified) {
104          if (g.Id == Guid.Empty) {
105            CallAccessService(s => g.Id = s.AddUserGroup(g));
106          } else {
107            CallAccessService(s => s.UpdateUserGroup(g));
108          }
109          g.SetUnmodified();
110        }
111      }
112    }
113
114    public void StoreUserGroupsAsync(Action<Exception> exceptionCallback) {
115      ExecuteActionAsync(StoreUserGroups, exceptionCallback);
116    }
117
118    public void StoreRoles() {
119      foreach (Role g in Roles) {
120        if (g.Modified) {
121          CallAccessService(s => s.AddRole(g));
122          g.SetUnmodified();
123        }
124      }
125    }
126
127    public void StoreRolesAsync(Action<Exception> exceptionCallback) {
128      ExecuteActionAsync(StoreRoles, exceptionCallback);
129    }
130    #endregion
131
132    #region Delete
133    public void DeleteUser(User u) {
134      CallAccessService(s => s.DeleteUser(u));
135    }
136
137    public void DeleteUserAsync(User u, Action<Exception> exceptionCallback) {
138      Action deleteUserAction = new Action(delegate { DeleteUser(u); });
139      ExecuteActionAsync(deleteUserAction, exceptionCallback);
140    }
141
142    public void DeleteUserGroup(UserGroup u) {
143      CallAccessService(s => s.DeleteUserGroup(u));
144    }
145
146    public void DeleteUserGroupAsync(UserGroup u, Action<Exception> exceptionCallback) {
147      Action deleteUserGroupAction = new Action(delegate { DeleteUserGroup(u); });
148      ExecuteActionAsync(deleteUserGroupAction, exceptionCallback);
149    }
150
151    public void DeleteRole(Role u) {
152      CallAccessService(s => s.DeleteRole(u));
153    }
154
155    public void DeleteRoleAsync(Role u, Action<Exception> exceptionCallback) {
156      Action deleteRoleAction = new Action(delegate { DeleteRole(u); });
157      ExecuteActionAsync(deleteRoleAction, exceptionCallback);
158    }
159    #endregion
160
161    public void ExecuteActionAsync(Action action, Action<Exception> exceptionCallback) {
162      var call = new Func<Exception>(delegate() {
163        try {
164          OnRefreshing();
165          action();
166        }
167        catch (Exception ex) {
168          return ex;
169        }
170        finally {
171          OnRefreshed();
172        }
173        return null;
174      });
175      call.BeginInvoke(delegate(IAsyncResult result) {
176        Exception ex = call.EndInvoke(result);
177        if (ex != null) exceptionCallback(ex);
178      }, null);
179    }
180
181    #region Events
182    public event EventHandler Refreshing;
183    private void OnRefreshing() {
184      EventHandler handler = Refreshing;
185      if (handler != null) handler(this, EventArgs.Empty);
186    }
187    public event EventHandler Refreshed;
188    private void OnRefreshed() {
189      EventHandler handler = Refreshed;
190      if (handler != null) handler(this, EventArgs.Empty);
191    }
192    #endregion
193
194    #region Helpers
195    public static void CallAccessService(Action<IAccessService> call) {
196      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
197      try {
198        call(client);
199      }
200      finally {
201        try {
202          client.Close();
203        }
204        catch (Exception) {
205          client.Abort();
206        }
207      }
208    }
209    public static T CallAccessService<T>(Func<IAccessService, T> call) {
210      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
211      try {
212        return call(client);
213      }
214      finally {
215        try {
216          client.Close();
217        }
218        catch (Exception) {
219          client.Abort();
220        }
221      }
222    }
223    #endregion
224  }
225}
Note: See TracBrowser for help on using the repository browser.