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