[5257] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 | using System;
|
---|
[4962] | 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Services.Authentication;
|
---|
| 26 | using System.ServiceModel.Security;
|
---|
| 27 | using System.ServiceModel;
|
---|
[5257] | 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Collections;
|
---|
[4962] | 30 |
|
---|
[5257] | 31 | namespace HeuristicLab.Services.Authentication {
|
---|
| 32 | public class AuthenticationClient {
|
---|
[4962] | 33 |
|
---|
[5257] | 34 | private static AuthenticationClient instance;
|
---|
| 35 | public static AuthenticationClient Instance {
|
---|
| 36 | get {
|
---|
| 37 | if (instance == null) instance = new AuthenticationClient();
|
---|
| 38 | return instance;
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
[4962] | 41 |
|
---|
[5257] | 42 | #region Properties
|
---|
[4962] | 43 |
|
---|
[5257] | 44 | private ItemCollection<User> users;
|
---|
| 45 | public ItemCollection<User> Users {
|
---|
| 46 | get { return users; }
|
---|
| 47 | }
|
---|
| 48 | private ItemCollection<Application> applications;
|
---|
| 49 | public ItemCollection<Application> Applications {
|
---|
| 50 | get { return applications; }
|
---|
| 51 | }
|
---|
[4962] | 52 |
|
---|
[5257] | 53 | private ItemCollection<Role> roles;
|
---|
| 54 | public ItemCollection<Role> Roles {
|
---|
| 55 | get { return roles; }
|
---|
| 56 | }
|
---|
[4962] | 57 |
|
---|
[5257] | 58 | #endregion
|
---|
[4962] | 59 |
|
---|
| 60 |
|
---|
[5257] | 61 | private AuthenticationClient() {
|
---|
| 62 | applications = new ItemCollection<Application>();
|
---|
| 63 | applications.ItemsRemoved += new CollectionItemsChangedEventHandler<Application>(applications_ItemsRemoved);
|
---|
[4962] | 64 |
|
---|
[5257] | 65 | users = new ItemCollection<User>();
|
---|
| 66 | users.ItemsRemoved += new CollectionItemsChangedEventHandler<User>(users_ItemsRemoved);
|
---|
[4962] | 67 |
|
---|
[5257] | 68 | roles = new ItemCollection<Role>();
|
---|
| 69 | roles.ItemsRemoved += new CollectionItemsChangedEventHandler<Role>(roles_ItemsRemoved);
|
---|
| 70 | }
|
---|
[4962] | 71 |
|
---|
| 72 |
|
---|
[5257] | 73 | #region Store
|
---|
[4962] | 74 |
|
---|
[5257] | 75 | public bool Store(AuthenticationItem item) {
|
---|
| 76 | try {
|
---|
| 77 | if (item.Id == Guid.Empty) {
|
---|
| 78 | if (item is Role)
|
---|
| 79 | item.Id = CallService<Guid>(s => s.AddRole((Role)item));
|
---|
| 80 | else if (item is User)
|
---|
| 81 | item.Id = CallService<Guid>(s => s.AddUser((User)item));
|
---|
| 82 | else if (item is Application)
|
---|
| 83 | item.Id = CallService<Guid>(s => s.AddApplication((Application)item));
|
---|
| 84 | } else {
|
---|
| 85 | if (item is Role)
|
---|
| 86 | CallService(s => s.UpdateRole((Role)item));
|
---|
| 87 | else if (item is User)
|
---|
| 88 | CallService(s => s.UpdateUser((User)item));
|
---|
| 89 | else if (item is Application)
|
---|
| 90 | CallService(s => s.UpdateApplication((Application)item));
|
---|
[4962] | 91 | }
|
---|
| 92 |
|
---|
[5257] | 93 | return true;
|
---|
| 94 | }
|
---|
| 95 | catch (Exception ex) {
|
---|
| 96 | //ErrorHandling.ShowErrorDialog("Store failed.", ex);
|
---|
| 97 | return false;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[4962] | 100 |
|
---|
[5257] | 101 | #endregion
|
---|
[4962] | 102 |
|
---|
[5257] | 103 | #region Refresh
|
---|
[4962] | 104 |
|
---|
[5257] | 105 | public void Refresh(Guid applicationId) {
|
---|
| 106 | OnRefreshing();
|
---|
[4962] | 107 |
|
---|
[5257] | 108 | users.Clear();
|
---|
| 109 | roles.Clear();
|
---|
| 110 | applications.Clear();
|
---|
[4962] | 111 |
|
---|
[5257] | 112 | var call = new Func<Exception>(delegate() {
|
---|
| 113 | try {
|
---|
| 114 | applications.AddRange(CallService<Application[]>(s => s.GetApplications()).OrderBy(x => x.Name).ToArray());
|
---|
| 115 | if (!applicationId.Equals(Guid.Empty)) {
|
---|
| 116 | users.AddRange(CallService<User[]>(s => s.GetUsersForApplication(applicationId)).OrderBy(x => x.Name).ToArray());
|
---|
| 117 | roles.AddRange(CallService<Role[]>(s => s.GetRolesForApplication(applicationId)).OrderBy(x => x.Name).ToArray());
|
---|
[4995] | 118 | }
|
---|
[5257] | 119 | return null;
|
---|
[4995] | 120 | }
|
---|
[5257] | 121 | catch (Exception ex) {
|
---|
| 122 | return ex;
|
---|
[4995] | 123 | }
|
---|
[5257] | 124 | });
|
---|
| 125 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
| 126 | Exception ex = call.EndInvoke(result);
|
---|
| 127 | if (ex != null) {
|
---|
| 128 | //ErrorHandling.ShowErrorDialog("Refresh failed.", ex);
|
---|
[4995] | 129 | }
|
---|
[5257] | 130 | OnRefreshed();
|
---|
| 131 | }, null);
|
---|
| 132 | }
|
---|
[4995] | 133 |
|
---|
| 134 |
|
---|
[5257] | 135 | #endregion
|
---|
[4995] | 136 |
|
---|
[5257] | 137 | #region Helpers
|
---|
[4962] | 138 |
|
---|
[5257] | 139 | private void CallService(Action<IAuthenticationService> call) {
|
---|
| 140 | AuthenticationServiceClient client = new AuthenticationServiceClient();
|
---|
| 141 | client.ClientCredentials.UserName.UserName = "Alice";
|
---|
| 142 | client.ClientCredentials.UserName.Password = "YouWillNeverKnow";
|
---|
| 143 | client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
|
---|
| 144 | //AuthenticationServiceClient client = ClientFactory.Create<AuthenticationClient, IAuthenticationService>();
|
---|
| 145 | try {
|
---|
| 146 | call(client);
|
---|
| 147 | }
|
---|
| 148 | finally {
|
---|
[4995] | 149 | try {
|
---|
[5257] | 150 | client.Close();
|
---|
[4995] | 151 | }
|
---|
[5257] | 152 | catch (Exception) {
|
---|
| 153 | client.Abort();
|
---|
[4995] | 154 | }
|
---|
| 155 | }
|
---|
[5257] | 156 | }
|
---|
[4995] | 157 |
|
---|
[5257] | 158 | private T CallService<T>(Func<IAuthenticationService, T> call) {
|
---|
| 159 | AuthenticationServiceClient client = new AuthenticationServiceClient();
|
---|
| 160 | client.ClientCredentials.UserName.UserName = "Alice";
|
---|
| 161 | client.ClientCredentials.UserName.Password = "YouWillNeverKnow";
|
---|
| 162 | client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
|
---|
| 163 | //AuthenticationServiceClient client = ClientFactory.Create<AuthenticationClient, IAuthenticationService>();
|
---|
| 164 | try {
|
---|
| 165 | return call(client);
|
---|
[4995] | 166 | }
|
---|
[5257] | 167 | finally {
|
---|
[4995] | 168 | try {
|
---|
[5257] | 169 | client.Close();
|
---|
[4995] | 170 | }
|
---|
[5257] | 171 | catch (Exception) {
|
---|
| 172 | client.Abort();
|
---|
[4995] | 173 | }
|
---|
| 174 | }
|
---|
[5257] | 175 | }
|
---|
[4995] | 176 |
|
---|
[5257] | 177 | #endregion
|
---|
[4995] | 178 |
|
---|
[5257] | 179 | #region Application methods
|
---|
| 180 |
|
---|
| 181 | public Application GetApplication(Guid applicationId) {
|
---|
| 182 | try {
|
---|
| 183 | return CallService<Application>(s => s.GetApplication(applicationId));
|
---|
[4995] | 184 | }
|
---|
[5257] | 185 | catch (Exception ex) {
|
---|
| 186 | //ErrorHandling.ShowErrorDialog("Refresh problem data failed.", ex);
|
---|
| 187 | return null;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
[4995] | 190 |
|
---|
[5257] | 191 | public IEnumerable<Application> GetApplications() {
|
---|
| 192 | try {
|
---|
| 193 | return CallService<IEnumerable<Application>>(s => s.GetApplications());
|
---|
[4995] | 194 | }
|
---|
[5257] | 195 | catch (Exception ex) {
|
---|
| 196 | // Todo Errorhandling
|
---|
| 197 | return null;
|
---|
[4995] | 198 |
|
---|
[5257] | 199 | }
|
---|
| 200 | }
|
---|
[4995] | 201 |
|
---|
[5257] | 202 |
|
---|
| 203 |
|
---|
| 204 | #endregion
|
---|
| 205 |
|
---|
| 206 | #region Role Methods
|
---|
| 207 |
|
---|
| 208 | public Role GetRole(Guid Id) {
|
---|
| 209 | try {
|
---|
| 210 | return CallService<Role>(s => s.GetRole(Id));
|
---|
[4995] | 211 | }
|
---|
[5257] | 212 | catch (Exception ex) {
|
---|
| 213 | // todo Errorhandling
|
---|
| 214 | return null;
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
[4995] | 217 |
|
---|
[5257] | 218 | public IEnumerable<Guid> GetRolesForUser(Guid userId) {
|
---|
| 219 | try {
|
---|
| 220 | return CallService<IEnumerable<Guid>>(s => s.GetRolesForUser(userId));
|
---|
| 221 | }
|
---|
| 222 | catch (Exception ex) {
|
---|
| 223 | // Todo Errorhandling
|
---|
| 224 | return null;
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
[4995] | 227 |
|
---|
| 228 |
|
---|
[5257] | 229 | #endregion
|
---|
[4995] | 230 |
|
---|
[5257] | 231 | #region User methods
|
---|
| 232 |
|
---|
| 233 | public User GetUser(Guid Id) {
|
---|
| 234 | try {
|
---|
| 235 | return CallService<User>(s => s.GetUser(Id));
|
---|
[4995] | 236 | }
|
---|
[5257] | 237 | catch (Exception ex) {
|
---|
| 238 | // Todo Errorhandling
|
---|
| 239 | return null;
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
[4995] | 242 |
|
---|
[5257] | 243 | public IEnumerable<User> GetUsers() {
|
---|
| 244 | try {
|
---|
| 245 | return CallService<IEnumerable<User>>(s => s.GetUsers());
|
---|
[4995] | 246 | }
|
---|
[5257] | 247 | catch (Exception ex) {
|
---|
| 248 | // Todo Errorhandling
|
---|
| 249 | return null;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
[4995] | 252 |
|
---|
[5257] | 253 |
|
---|
| 254 | public bool IsUserInRole(Guid userId, Guid roleId) {
|
---|
| 255 | try {
|
---|
| 256 | return CallService<bool>(s => s.IsUserInRole(userId, roleId));
|
---|
[4995] | 257 | }
|
---|
[5257] | 258 | catch (Exception ex) {
|
---|
| 259 | // Todo Errorhandling
|
---|
| 260 | return false;
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
[4995] | 263 |
|
---|
[5257] | 264 |
|
---|
| 265 | public void AddUserToRole(Guid roleId, Guid userId) {
|
---|
| 266 | try {
|
---|
| 267 | CallService(s => s.AddUserToRole(roleId, userId));
|
---|
[4995] | 268 | }
|
---|
[5257] | 269 | catch (Exception ex) {
|
---|
| 270 | // Todo Errorhandling
|
---|
[4995] | 271 |
|
---|
| 272 | }
|
---|
[5257] | 273 | }
|
---|
[4995] | 274 |
|
---|
[5257] | 275 | public void RemoveUserFromRole(Guid roleId, Guid userId) {
|
---|
| 276 | try {
|
---|
| 277 | CallService(s => s.RemoveUserFromRole(roleId, userId));
|
---|
| 278 | }
|
---|
| 279 | catch (Exception ex) {
|
---|
| 280 | // Todo Errorhandling
|
---|
[4995] | 281 |
|
---|
| 282 | }
|
---|
[5257] | 283 | }
|
---|
[4995] | 284 |
|
---|
| 285 |
|
---|
[5257] | 286 | public IEnumerable<Guid> GetUsersInRole(Guid roleId) {
|
---|
| 287 | try {
|
---|
| 288 | return CallService<IEnumerable<Guid>>(s => s.GetUsersInRole(roleId));
|
---|
[4995] | 289 | }
|
---|
[5257] | 290 | catch (Exception ex) {
|
---|
| 291 | // Todo Errorhandling
|
---|
| 292 | return null;
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
[4995] | 295 |
|
---|
[5257] | 296 | public User ResetPassword(string applicationName, string userName, string password) {
|
---|
| 297 | try {
|
---|
| 298 | return CallService<User>(s => s.ResetPassword(applicationName, userName, password));
|
---|
| 299 | }
|
---|
| 300 | catch (Exception ex) {
|
---|
| 301 | // Todo Errorhandling
|
---|
| 302 | return null;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
[4995] | 305 |
|
---|
[5257] | 306 | #endregion
|
---|
[4995] | 307 |
|
---|
[5257] | 308 | #region Events
|
---|
| 309 | public event EventHandler Refreshing;
|
---|
| 310 | private void OnRefreshing() {
|
---|
| 311 | EventHandler handler = Refreshing;
|
---|
| 312 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 313 | }
|
---|
| 314 | public event EventHandler Refreshed;
|
---|
| 315 | private void OnRefreshed() {
|
---|
| 316 | EventHandler handler = Refreshed;
|
---|
| 317 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 318 | }
|
---|
[4995] | 319 |
|
---|
| 320 |
|
---|
[5257] | 321 | void roles_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Role> e) {
|
---|
| 322 | try {
|
---|
| 323 | foreach (Role r in e.Items)
|
---|
| 324 | CallService(s => s.DeleteRole(r.Id));
|
---|
| 325 | }
|
---|
| 326 | catch (Exception ex) {
|
---|
| 327 | // ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
[4995] | 330 |
|
---|
[5257] | 331 | void users_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<User> e) {
|
---|
| 332 | try {
|
---|
| 333 | foreach (User u in e.Items)
|
---|
| 334 | CallService(s => s.DeleteUser(u.Id));
|
---|
| 335 | }
|
---|
| 336 | catch (Exception ex) {
|
---|
| 337 | // ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
[4962] | 340 |
|
---|
[5257] | 341 | void applications_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Application> e) {
|
---|
| 342 | try {
|
---|
| 343 | foreach (Application a in e.Items)
|
---|
| 344 | CallService(s => s.DeleteApplication(a.Id));
|
---|
| 345 | }
|
---|
| 346 | catch (Exception ex) {
|
---|
| 347 | // ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
[4962] | 350 |
|
---|
[5257] | 351 | #endregion
|
---|
| 352 | }
|
---|
[4962] | 353 | }
|
---|