1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Clients.Common;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using System.ServiceModel;
|
---|
27 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
|
---|
28 | using System.Collections.Generic;
|
---|
29 | using System.ServiceModel.Security;
|
---|
30 | using HeuristicLab.Clients.Access;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Clients.Hive.WebJobManager.Services.Imports
|
---|
33 | {
|
---|
34 | /// <summary>
|
---|
35 | /// Controls everything User/Group/Role related
|
---|
36 | /// </summary>
|
---|
37 | [Item("AccessAdministrationClient", "AccessAdministration client.")]
|
---|
38 | public class AccessAdministrationClient : IContent
|
---|
39 | {
|
---|
40 |
|
---|
41 | private WebLoginService weblog;
|
---|
42 | private AccessServiceClient client;
|
---|
43 | public Guid userId { get; set; }
|
---|
44 | #region Properties
|
---|
45 | private ItemList<User> users;
|
---|
46 | public ItemList<User> Users
|
---|
47 | {
|
---|
48 | get
|
---|
49 | {
|
---|
50 | return users;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public ItemList<UserGroup> Groups { get; set; }
|
---|
55 | public ItemList<Role> Roles { get; set; }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | public AccessAdministrationClient(Guid u) {
|
---|
59 | weblog = WebLoginService.Instance;
|
---|
60 | userId = u;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public bool checkAuthorised()
|
---|
64 | {
|
---|
65 | if (client != null)
|
---|
66 | {
|
---|
67 | try
|
---|
68 | {
|
---|
69 | client.GetAllUsers();
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 | catch (Exception e)
|
---|
73 | {
|
---|
74 | if (e is MessageSecurityException || e is InvalidOperationException)
|
---|
75 | {
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | throw;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | return false;
|
---|
82 | }
|
---|
83 |
|
---|
84 | #region Refresh
|
---|
85 | public void RefreshUsers()
|
---|
86 | {
|
---|
87 | users = new ItemList<User>();
|
---|
88 | users.AddRange(CallAccessService<ItemList<User>>(s => new ItemList<User>(s.GetAllUsers())));
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void RefreshUsersAsync(Action<Exception> exceptionCallback)
|
---|
92 | {
|
---|
93 | ExecuteActionAsync(RefreshUsers, exceptionCallback);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void RefreshUserGroups()
|
---|
97 | {
|
---|
98 | Groups = new ItemList<UserGroup>();
|
---|
99 | Groups.AddRange(CallAccessService<ItemList<UserGroup>>(s => new ItemList<UserGroup>(s.GetAllUserGroups())));
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void RefreshUserGroupsAsync(Action<Exception> exceptionCallback)
|
---|
103 | {
|
---|
104 | ExecuteActionAsync(RefreshUserGroups, exceptionCallback);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void RefreshRoles()
|
---|
108 | {
|
---|
109 | Roles = new ItemList<Role>();
|
---|
110 | Roles.AddRange(CallAccessService<ItemList<Role>>(s => new ItemList<Role>(s.GetRoles())));
|
---|
111 | }
|
---|
112 |
|
---|
113 | public void RefreshRolesAsync(Action<Exception> exceptionCallback)
|
---|
114 | {
|
---|
115 | ExecuteActionAsync(RefreshRoles, exceptionCallback);
|
---|
116 | }
|
---|
117 |
|
---|
118 | #endregion
|
---|
119 |
|
---|
120 | #region Store
|
---|
121 | public void StoreUsers()
|
---|
122 | {
|
---|
123 | foreach (User u in users)
|
---|
124 | {
|
---|
125 | if (u.Modified)
|
---|
126 | {
|
---|
127 | if (u.Id == Guid.Empty)
|
---|
128 | {
|
---|
129 | CallAccessService(s => u.Id = s.AddUser(u).Id);
|
---|
130 | }
|
---|
131 | else {
|
---|
132 | CallAccessService(s => s.UpdateUser(u));
|
---|
133 | }
|
---|
134 | u.SetUnmodified();
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public void StoreUsersAsync(Action<Exception> exceptionCallback)
|
---|
140 | {
|
---|
141 | ExecuteActionAsync(StoreUsers, exceptionCallback);
|
---|
142 | }
|
---|
143 |
|
---|
144 | public void StoreUserGroups()
|
---|
145 | {
|
---|
146 | foreach (UserGroup g in Groups)
|
---|
147 | {
|
---|
148 | if (g.Modified)
|
---|
149 | {
|
---|
150 | if (g.Id == Guid.Empty)
|
---|
151 | {
|
---|
152 | CallAccessService(s => g.Id = s.AddUserGroup(g));
|
---|
153 | }
|
---|
154 | else {
|
---|
155 | CallAccessService(s => s.UpdateUserGroup(g));
|
---|
156 | }
|
---|
157 | g.SetUnmodified();
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | public void StoreUserGroupsAsync(Action<Exception> exceptionCallback)
|
---|
163 | {
|
---|
164 | ExecuteActionAsync(StoreUserGroups, exceptionCallback);
|
---|
165 | }
|
---|
166 |
|
---|
167 | public void StoreRoles()
|
---|
168 | {
|
---|
169 | foreach (Role g in Roles)
|
---|
170 | {
|
---|
171 | if (g.Modified)
|
---|
172 | {
|
---|
173 | CallAccessService(s => s.AddRole(g));
|
---|
174 | g.SetUnmodified();
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | public void StoreRolesAsync(Action<Exception> exceptionCallback)
|
---|
180 | {
|
---|
181 | ExecuteActionAsync(StoreRoles, exceptionCallback);
|
---|
182 | }
|
---|
183 | #endregion
|
---|
184 |
|
---|
185 | #region Delete
|
---|
186 | public void DeleteUser(User u)
|
---|
187 | {
|
---|
188 | CallAccessService(s => s.DeleteUser(u));
|
---|
189 | }
|
---|
190 |
|
---|
191 | public void DeleteUserAsync(User u, Action<Exception> exceptionCallback)
|
---|
192 | {
|
---|
193 | Action deleteUserAction = new Action(delegate { DeleteUser(u); });
|
---|
194 | ExecuteActionAsync(deleteUserAction, exceptionCallback);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* BROKEN
|
---|
198 | public void DeleteUserGroup(UserGroup u)
|
---|
199 | {
|
---|
200 | CallAccessService(s => s.DeleteUserGroup(u));
|
---|
201 | }
|
---|
202 |
|
---|
203 | public void DeleteUserGroupAsync(UserGroup u, Action<Exception> exceptionCallback)
|
---|
204 | {
|
---|
205 | Action deleteUserGroupAction = new Action(delegate { DeleteUserGroup(u); });
|
---|
206 | ExecuteActionAsync(deleteUserGroupAction, exceptionCallback);
|
---|
207 | }
|
---|
208 | */
|
---|
209 | public void DeleteRole(Role u)
|
---|
210 | {
|
---|
211 | CallAccessService(s => s.DeleteRole(u));
|
---|
212 | }
|
---|
213 |
|
---|
214 | public void DeleteRoleAsync(Role u, Action<Exception> exceptionCallback)
|
---|
215 | {
|
---|
216 | Action deleteRoleAction = new Action(delegate { DeleteRole(u); });
|
---|
217 | ExecuteActionAsync(deleteRoleAction, exceptionCallback);
|
---|
218 | }
|
---|
219 | #endregion
|
---|
220 |
|
---|
221 | public void ExecuteActionAsync(Action action, Action<Exception> exceptionCallback)
|
---|
222 | {
|
---|
223 | var call = new Func<Exception>(delegate ()
|
---|
224 | {
|
---|
225 | try
|
---|
226 | {
|
---|
227 | OnRefreshing();
|
---|
228 | action();
|
---|
229 | }
|
---|
230 | catch (Exception ex)
|
---|
231 | {
|
---|
232 | return ex;
|
---|
233 | }
|
---|
234 | finally
|
---|
235 | {
|
---|
236 | OnRefreshed();
|
---|
237 | }
|
---|
238 | return null;
|
---|
239 | });
|
---|
240 | call.BeginInvoke(delegate (IAsyncResult result)
|
---|
241 | {
|
---|
242 | Exception ex = call.EndInvoke(result);
|
---|
243 | if (ex != null) exceptionCallback(ex);
|
---|
244 | }, null);
|
---|
245 | }
|
---|
246 |
|
---|
247 | #region Events
|
---|
248 | public event EventHandler Refreshing;
|
---|
249 | private void OnRefreshing()
|
---|
250 | {
|
---|
251 | EventHandler handler = Refreshing;
|
---|
252 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
253 | }
|
---|
254 | public event EventHandler Refreshed;
|
---|
255 | private void OnRefreshed()
|
---|
256 | {
|
---|
257 | EventHandler handler = Refreshed;
|
---|
258 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
259 | }
|
---|
260 | #endregion
|
---|
261 |
|
---|
262 | #region User
|
---|
263 |
|
---|
264 | public string resetPassword(Guid id)
|
---|
265 | {
|
---|
266 | return CallAccessService<string>(s => s.ResetPassword(id));
|
---|
267 | }
|
---|
268 | public Guid addUser(Access.User u)
|
---|
269 | {
|
---|
270 | return CallAccessService(s => s.AddUser(u).Id);
|
---|
271 | }
|
---|
272 |
|
---|
273 | public void updateUser(Access.User u)
|
---|
274 | {
|
---|
275 |
|
---|
276 | CallAccessService(s => s.UpdateUser(u));
|
---|
277 | }
|
---|
278 | public List<Role> getRoles(User u)
|
---|
279 | {
|
---|
280 | return CallAccessService(s => s.GetUserRoles(u));
|
---|
281 | }
|
---|
282 | public void addRoleToUser(User u, Role r)
|
---|
283 | {
|
---|
284 | CallAccessService(s => s.AddUserToRole(r, u));
|
---|
285 | }
|
---|
286 | public void deleteUserRole(User u, Role r)
|
---|
287 | {
|
---|
288 | CallAccessService(s => s.RemoveUserFromRole(r, u));
|
---|
289 | }
|
---|
290 | #endregion
|
---|
291 |
|
---|
292 | #region Group
|
---|
293 | public Guid addGroup(Access.UserGroup g)
|
---|
294 | {
|
---|
295 | return CallAccessService(s => s.AddUserGroup(g));
|
---|
296 | }
|
---|
297 | public void updateGroup(UserGroup g)
|
---|
298 | {
|
---|
299 | CallAccessService(s => s.UpdateUserGroup(g));
|
---|
300 | }
|
---|
301 | public List<Access.UserGroup> getSubscribedGroups(Guid id)
|
---|
302 | {
|
---|
303 | var maps = CallAccessService(s => s.GetUserGroupMapping());
|
---|
304 | maps = maps.FindAll(x => x.Child == id);
|
---|
305 |
|
---|
306 | RefreshUserGroups();
|
---|
307 | RefreshUsers();
|
---|
308 |
|
---|
309 | List<Access.UserGroup> members = new List<Access.UserGroup>();
|
---|
310 | foreach (var map in maps)
|
---|
311 | {
|
---|
312 | UserGroup g = Groups.Find(x => x.Id == map.Parent);
|
---|
313 | if (g != null)
|
---|
314 | {
|
---|
315 | members.Add( g);
|
---|
316 | }
|
---|
317 | }
|
---|
318 | return members;
|
---|
319 | }
|
---|
320 | public List<Access.UserGroupBase> getGroupMembers(Guid id)
|
---|
321 | {
|
---|
322 | var maps = CallAccessService(s => s.GetUserGroupMapping());
|
---|
323 | maps = maps.FindAll(x => x.Parent == id);
|
---|
324 |
|
---|
325 | RefreshUserGroups();
|
---|
326 | RefreshUsers();
|
---|
327 |
|
---|
328 | List<Access.UserGroupBase> members = new List<Access.UserGroupBase>();
|
---|
329 | foreach (var map in maps)
|
---|
330 | {
|
---|
331 | User t = users.Find(x => x.Id == map.Child);
|
---|
332 | if (t == null)
|
---|
333 | {
|
---|
334 | UserGroup g = Groups.Find(x => x.Id == map.Child);
|
---|
335 | if (g != null)
|
---|
336 | {
|
---|
337 | members.Insert(0,g);//Groups always in the front
|
---|
338 | }
|
---|
339 | }
|
---|
340 | else
|
---|
341 | {
|
---|
342 | members.Add(t);//members in the back
|
---|
343 | }
|
---|
344 | }
|
---|
345 | return members;
|
---|
346 | }
|
---|
347 |
|
---|
348 | public void deleteUserGroup(UserGroup group)
|
---|
349 | {
|
---|
350 | deleteAllMembers(group);
|
---|
351 | CallAccessService(s => s.DeleteUserGroup(group));
|
---|
352 | }
|
---|
353 | private void deleteAllMembers(UserGroup group)
|
---|
354 | {
|
---|
355 | var list = getGroupMembers(group.Id);
|
---|
356 | foreach (var member in list)
|
---|
357 | {//Empties all member connections
|
---|
358 | CallAccessService(s => s.RemoveUserGroupBaseFromGroup(member, group));
|
---|
359 | }
|
---|
360 | }
|
---|
361 | public void addMember(UserGroupBase member, UserGroup group)
|
---|
362 | {
|
---|
363 | CallAccessService(s => s.AddUserGroupBaseToGroup(member, group));
|
---|
364 | }
|
---|
365 | public void deleteMember(UserGroupBase member,UserGroup group)
|
---|
366 | {
|
---|
367 | CallAccessService(s => s.RemoveUserGroupBaseFromGroup(member, group));
|
---|
368 | }
|
---|
369 | #endregion
|
---|
370 |
|
---|
371 | #region Roles
|
---|
372 | public Role addRole(Access.Role r)
|
---|
373 | {
|
---|
374 | return CallAccessService(s => s.AddRole(r));
|
---|
375 | }
|
---|
376 | public void deleteRole(string name)
|
---|
377 | {
|
---|
378 | Role r = Roles.Find(x => x.Name == name);
|
---|
379 | CallAccessService(s => s.DeleteRole(r));
|
---|
380 | }
|
---|
381 |
|
---|
382 | public List<UserGroupBase> getEnrolled(Role r)
|
---|
383 | {
|
---|
384 | var enroll = new List<UserGroupBase>();
|
---|
385 | RefreshUsers();
|
---|
386 | foreach(var us in users)
|
---|
387 | {
|
---|
388 | var temp = CallAccessService(s => s.GetUserRoles(us));
|
---|
389 | if (temp.Contains(r))
|
---|
390 | enroll.Add(us);
|
---|
391 | }
|
---|
392 |
|
---|
393 | return enroll;
|
---|
394 | }
|
---|
395 | #endregion
|
---|
396 |
|
---|
397 |
|
---|
398 | #region Helpers
|
---|
399 | private void getAccessClient()
|
---|
400 | {
|
---|
401 | //build config by code
|
---|
402 | WSHttpBinding binding = new WSHttpBinding();
|
---|
403 | binding.Name = "WSHttpBinding_IAccessService";
|
---|
404 | binding.CloseTimeout = TimeSpan.FromMinutes(1);
|
---|
405 | binding.OpenTimeout = TimeSpan.FromMinutes(1);
|
---|
406 | binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
|
---|
407 | binding.SendTimeout = TimeSpan.FromMinutes(1);
|
---|
408 | binding.BypassProxyOnLocal = false;
|
---|
409 |
|
---|
410 | binding.TransactionFlow = false;
|
---|
411 |
|
---|
412 | binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
|
---|
413 | binding.MaxBufferPoolSize = 524288;
|
---|
414 | binding.MaxReceivedMessageSize = 65536;
|
---|
415 | binding.MessageEncoding = WSMessageEncoding.Text;
|
---|
416 |
|
---|
417 | binding.TextEncoding = System.Text.Encoding.UTF8;
|
---|
418 | binding.UseDefaultWebProxy = true;
|
---|
419 | binding.AllowCookies = false;
|
---|
420 |
|
---|
421 | binding.ReaderQuotas.MaxDepth = 32;
|
---|
422 | binding.ReaderQuotas.MaxStringContentLength = 8192;
|
---|
423 | binding.ReaderQuotas.MaxArrayLength = 16384;
|
---|
424 | binding.ReaderQuotas.MaxBytesPerRead = 4096;
|
---|
425 | binding.ReaderQuotas.MaxNameTableCharCount = 16384;
|
---|
426 |
|
---|
427 | binding.ReliableSession.Ordered = true;
|
---|
428 | binding.ReliableSession.InactivityTimeout = TimeSpan.Parse("00:10:00");
|
---|
429 | binding.ReliableSession.Enabled = false;
|
---|
430 |
|
---|
431 |
|
---|
432 | binding.Security.Mode = SecurityMode.Message;
|
---|
433 | binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
|
---|
434 | binding.Security.Message.NegotiateServiceCredential = true;
|
---|
435 | binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
|
---|
436 |
|
---|
437 | binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
|
---|
438 | binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
|
---|
439 | binding.Security.Transport.Realm = "";
|
---|
440 |
|
---|
441 | var end = new EndpointAddress("http://services.heuristiclab.com/AccessService-3.3/AccessService.svc");
|
---|
442 | client = new AccessServiceClient(binding, end);
|
---|
443 | client.ClientCredentials.UserName.UserName = weblog.getLoginViewModel(userId).loginName;
|
---|
444 | //PASSWORD CANNOT BE ENCRYPTED
|
---|
445 | client.ClientCredentials.UserName.Password = weblog.getServiceLocator(userId).Password;
|
---|
446 | client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
|
---|
447 | }
|
---|
448 | public void CallAccessService(Action<IAccessService> call)
|
---|
449 | {
|
---|
450 | getAccessClient();
|
---|
451 | try
|
---|
452 | {
|
---|
453 | call(client);
|
---|
454 | }
|
---|
455 | finally
|
---|
456 | {
|
---|
457 | try
|
---|
458 | {
|
---|
459 | client.Close();
|
---|
460 | }
|
---|
461 | catch (Exception)
|
---|
462 | {
|
---|
463 | client.Abort();
|
---|
464 | }
|
---|
465 | }
|
---|
466 | }
|
---|
467 | public T CallAccessService<T>(Func<IAccessService, T> call)
|
---|
468 | {
|
---|
469 | getAccessClient();
|
---|
470 | try
|
---|
471 | {
|
---|
472 | return call(client);
|
---|
473 | }
|
---|
474 | finally
|
---|
475 | {
|
---|
476 | try
|
---|
477 | {
|
---|
478 | client.Close();
|
---|
479 | }
|
---|
480 | catch (Exception)
|
---|
481 | {
|
---|
482 | client.Abort();
|
---|
483 | }
|
---|
484 | }
|
---|
485 | }
|
---|
486 | #endregion
|
---|
487 |
|
---|
488 | internal bool CheckLogin()
|
---|
489 | {
|
---|
490 | try
|
---|
491 | {
|
---|
492 | this.RefreshUsers();
|
---|
493 | return true;
|
---|
494 | }
|
---|
495 | catch (SecurityAccessDeniedException e)
|
---|
496 | {
|
---|
497 | return false;
|
---|
498 | }
|
---|
499 | catch (MessageSecurityException e)
|
---|
500 | {
|
---|
501 | return false;
|
---|
502 | }
|
---|
503 | }
|
---|
504 | }
|
---|
505 | }
|
---|