Changeset 5257 for branches/UserManagement/HeuristicLab.Services.Authentication.ServiceClients/AuthenticationClient.cs
- Timestamp:
- 01/08/11 14:39:29 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/UserManagement/HeuristicLab.Services.Authentication.ServiceClients/AuthenticationClient.cs
r4995 r5257 1 using System; 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; 2 22 using System.Collections.Generic; 3 23 using System.Linq; … … 6 26 using System.ServiceModel.Security; 7 27 using System.ServiceModel; 8 9 namespace HeuristicLab.Services.Authentication.ServiceClients 10 { 11 public class AuthenticationClient 12 { 13 14 private static AuthenticationClient instance; 15 public static AuthenticationClient Instance 16 { 17 get 18 { 19 if (instance == null) instance = new AuthenticationClient(); 20 return instance; 21 } 22 } 23 24 #region Properties 25 26 private IEnumerable<User> users; 27 public IEnumerable<User> Users 28 { 29 get { return users; } 30 } 31 private IEnumerable<Application> applications; 32 public IEnumerable<Application> Applications 33 { 34 get { return applications; } 35 } 36 37 private IEnumerable<Role> roles; 38 public IEnumerable<Role> Roles 39 { 40 get { return roles; } 41 } 42 43 #endregion 44 45 46 private AuthenticationClient() 47 { 48 applications = GetApplications(); 49 users = CallService<User[]>(s => s.GetAllUsers()).OrderBy(x => x.Name); 50 roles = CallService<Role[]>(s => s.GetAllRoles()).OrderBy(x => x.Name); 51 } 52 53 54 55 #region Store 56 57 public bool Store(AuthenticationItem item) 58 { 59 try 60 { 61 if (item.Id == Guid.Empty) 62 { 63 if (item is Role) 64 item.Id = CallService<Guid>(s => s.AddRole((Role)item)); 65 else if (item is User) 66 item.Id = CallService<Guid>(s => s.AddUser((User)item)); 67 else if (item is Application) 68 item.Id = CallService<Guid>(s => s.AddApplication((Application)item)); 69 70 } 71 else 72 { 73 if (item is Role) 74 CallService(s => s.UpdateRole((Role)item)); 75 else if (item is User) 76 CallService(s => s.UpdateUser((User)item)); 77 else if (item is Application) 78 CallService(s => s.UpdateApplication((Application)item)); 79 80 } 81 return true; 82 } 83 catch (Exception ex) 84 { 85 //ErrorHandling.ShowErrorDialog("Store failed.", ex); 86 return false; 87 } 88 } 89 90 #endregion 91 92 #region Refresh 93 94 public void Refresh() 95 { 96 97 var call = new Func<Exception>(delegate() 98 { 99 100 Guid applicationId = Guid.Empty; // to be set! 101 102 try 103 { 104 users = CallService<User[]>(s => s.GetUsers(applicationId)).OrderBy(x => x.Name); 105 roles = CallService<Role[]>(s => s.GetRoles(applicationId)).OrderBy(x => x.Name); 106 107 // ... 108 109 return null; 110 } 111 catch (Exception ex) 112 { 113 return ex; 114 } 115 }); 116 call.BeginInvoke(delegate(IAsyncResult result) 117 { 118 Exception ex = call.EndInvoke(result); 119 if (ex != null) 120 { 121 //ErrorHandling.ShowErrorDialog("Refresh failed.", ex); 122 } 123 124 }, null); 125 } 126 127 128 #endregion 129 130 private T CallService<T>(Func<IAuthenticationService, T> call) 131 { 132 AuthenticationServiceClient client = new AuthenticationServiceClient(); 133 client.ClientCredentials.UserName.UserName = "Alice"; 134 client.ClientCredentials.UserName.Password = "YouWillNeverKnow"; 135 client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None; 136 //AuthenticationServiceClient client = ClientFactory.Create<AuthenticationClient, IAuthenticationService>(); 137 try 138 { 139 return call(client); 140 } 141 finally 142 { 143 try 144 { 145 client.Close(); 146 } 147 catch (Exception) 148 { 149 client.Abort(); 150 } 151 } 152 } 153 154 155 #region Application methods 156 157 public Application GetApplication(Guid applicationId) 158 { 159 try 160 { 161 return CallService<Application>(s => s.GetApplication(applicationId)); 162 } 163 catch (Exception ex) 164 { 165 //ErrorHandling.ShowErrorDialog("Refresh problem data failed.", ex); 166 return null; 167 } 168 } 169 170 171 public Guid AddApplication(Application application) { 172 try { 173 return CallService<Guid>(s => s.AddApplication(application)); 28 using HeuristicLab.Core; 29 using HeuristicLab.Collections; 30 31 namespace HeuristicLab.Services.Authentication { 32 public class AuthenticationClient { 33 34 private static AuthenticationClient instance; 35 public static AuthenticationClient Instance { 36 get { 37 if (instance == null) instance = new AuthenticationClient(); 38 return instance; 39 } 40 } 41 42 #region Properties 43 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 } 52 53 private ItemCollection<Role> roles; 54 public ItemCollection<Role> Roles { 55 get { return roles; } 56 } 57 58 #endregion 59 60 61 private AuthenticationClient() { 62 applications = new ItemCollection<Application>(); 63 applications.ItemsRemoved += new CollectionItemsChangedEventHandler<Application>(applications_ItemsRemoved); 64 65 users = new ItemCollection<User>(); 66 users.ItemsRemoved += new CollectionItemsChangedEventHandler<User>(users_ItemsRemoved); 67 68 roles = new ItemCollection<Role>(); 69 roles.ItemsRemoved += new CollectionItemsChangedEventHandler<Role>(roles_ItemsRemoved); 70 } 71 72 73 #region Store 74 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)); 91 } 92 93 return true; 94 } 95 catch (Exception ex) { 96 //ErrorHandling.ShowErrorDialog("Store failed.", ex); 97 return false; 98 } 99 } 100 101 #endregion 102 103 #region Refresh 104 105 public void Refresh(Guid applicationId) { 106 OnRefreshing(); 107 108 users.Clear(); 109 roles.Clear(); 110 applications.Clear(); 111 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()); 174 118 } 175 catch (Exception ex) { 176 // Todo Errorhandling 177 return Guid.Empty; 178 } 179 } 180 181 public bool DeleteApplication(Guid id) { 182 try { 183 return CallService<bool>(s => s.DeleteApplication(id)); 184 } 185 catch (Exception ex) { 186 // Todo Errorhandling 187 return false; 188 } 189 } 190 191 public IEnumerable<Application> GetApplications() { 192 try { 193 return CallService<IEnumerable<Application>>(s => s.GetApplications()); 194 } 195 catch (Exception ex) { 196 // Todo Errorhandling 197 return null; 198 199 } 200 } 201 202 203 public bool UpdateApplication(Application application) { 204 try { 205 return CallService<bool>(s => s.UpdateApplication(application)); 206 } 207 catch (Exception ex) { 208 // Todo Errorhandling 209 return false; 210 } 211 } 212 213 214 #endregion 215 216 #region Role Methods 217 218 public Role GetRole (Guid Id) { 119 return null; 120 } 121 catch (Exception ex) { 122 return ex; 123 } 124 }); 125 call.BeginInvoke(delegate(IAsyncResult result) { 126 Exception ex = call.EndInvoke(result); 127 if (ex != null) { 128 //ErrorHandling.ShowErrorDialog("Refresh failed.", ex); 129 } 130 OnRefreshed(); 131 }, null); 132 } 133 134 135 #endregion 136 137 #region Helpers 138 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 { 219 149 try { 220 return CallService<Role>(s => s.GetRole(Id)); 221 } 222 catch (Exception ex) { 223 // todo Errorhandling 224 return null; 225 } 226 } 227 228 public IEnumerable<Role> GetAllRoles() { 150 client.Close(); 151 } 152 catch (Exception) { 153 client.Abort(); 154 } 155 } 156 } 157 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); 166 } 167 finally { 229 168 try { 230 return CallService<IEnumerable<Role>>(s => s.GetAllRoles()); 231 } 232 catch (Exception ex) { 233 // Todo Errorhandling 234 return null; 235 } 236 } 237 238 public IEnumerable<Role> GetRoles(Guid applicationId) { 239 try { 240 return CallService<IEnumerable<Role>>(s => s.GetRoles(applicationId)); 241 } 242 catch (Exception ex) { 243 // Todo Errorhandling 244 return null; 245 } 246 } 247 248 public Guid AddRole(Role role) { 249 try { 250 return CallService<Guid>(s => s.AddRole(role)); 251 } 252 catch (Exception ex) { 253 // Todo Errorhandling 254 return Guid.Empty; 255 256 } 257 } 258 259 public bool UpdateRole(Role role) { 260 try { 261 return CallService<bool>(s => s.UpdateRole(role)); 262 } 263 catch (Exception ex) { 264 // Todo Errorhandling 265 return false; 266 } 267 } 268 269 270 public bool DeleteRole(Guid Id) { 271 try { 272 return CallService<bool>(s => s.DeleteRole(Id)); 273 } 274 catch (Exception ex) { 275 // Todo Errorhandling 276 return false; 277 } 278 } 279 280 281 #endregion 282 283 #region User methods 284 285 public User GetUser(Guid Id) { 286 try { 287 return CallService<User>(s => s.GetUser(Id)); 288 } 289 catch (Exception ex) { 290 // Todo Errorhandling 291 return null; 292 } 293 } 294 295 public IEnumerable<User> GetAllUsers() { 296 try { 297 return CallService<IEnumerable<User>>(s => s.GetAllUsers()); 298 } 299 catch (Exception ex) { 300 // Todo Errorhandling 301 return null; 302 } 303 } 304 305 public IEnumerable<User> GetUsers(Guid applicationId) { 306 try { 307 return CallService<IEnumerable<User>>(s => s.GetUsers(applicationId)); 308 } 309 catch (Exception ex) { 310 // Todo Errorhandling 311 return null; 312 } 313 } 314 315 public Guid AddUser(User user) { 316 try { 317 return CallService<Guid>(s => s.AddUser(user)); 318 } 319 catch (Exception ex) { 320 // Todo Errorhandling 321 return Guid.Empty; 322 } 323 } 324 325 public bool UpdateUser(User user) { 326 try { 327 return CallService<bool>(s => s.UpdateUser(user)); 328 } 329 catch (Exception ex) { 330 // Todo Errorhandling 331 return false; 332 } 333 } 334 335 336 public bool AddUserToRole(Guid roleId, Guid userId) { 337 try { 338 return CallService<bool>(s => s.AddUserToRole(roleId, userId); 339 } 340 catch (Exception ex) { 341 // Todo Errorhandling 342 return false; 343 } 344 } 345 346 347 public IEnumerable<Guid> GetUsersInRole(Guid roleId) { 348 try { 349 return CallService<IEnumerable<Guid>>(s => s.GetUsersInRole(roleId)); 350 } 351 catch (Exception ex) { 352 // Todo Errorhandling 353 return null; 354 } 355 } 356 357 358 #endregion 359 360 361 362 363 //public static void Main() { 364 // AuthenticationServiceClient auth = new AuthenticationServiceClient(); 365 366 // auth.ClientCredentials.UserName.UserName = "Alice"; 367 // auth.ClientCredentials.UserName.Password = "YouWillKnow"; 368 // auth.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None; 369 370 // Application[] apps = auth.GetApplications(); 371 // foreach (Application app in apps) { 372 // Console.WriteLine(app.Name); 373 // } 374 // Console.ReadLine(); 375 //} 376 } 169 client.Close(); 170 } 171 catch (Exception) { 172 client.Abort(); 173 } 174 } 175 } 176 177 #endregion 178 179 #region Application methods 180 181 public Application GetApplication(Guid applicationId) { 182 try { 183 return CallService<Application>(s => s.GetApplication(applicationId)); 184 } 185 catch (Exception ex) { 186 //ErrorHandling.ShowErrorDialog("Refresh problem data failed.", ex); 187 return null; 188 } 189 } 190 191 public IEnumerable<Application> GetApplications() { 192 try { 193 return CallService<IEnumerable<Application>>(s => s.GetApplications()); 194 } 195 catch (Exception ex) { 196 // Todo Errorhandling 197 return null; 198 199 } 200 } 201 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)); 211 } 212 catch (Exception ex) { 213 // todo Errorhandling 214 return null; 215 } 216 } 217 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 } 227 228 229 #endregion 230 231 #region User methods 232 233 public User GetUser(Guid Id) { 234 try { 235 return CallService<User>(s => s.GetUser(Id)); 236 } 237 catch (Exception ex) { 238 // Todo Errorhandling 239 return null; 240 } 241 } 242 243 public IEnumerable<User> GetUsers() { 244 try { 245 return CallService<IEnumerable<User>>(s => s.GetUsers()); 246 } 247 catch (Exception ex) { 248 // Todo Errorhandling 249 return null; 250 } 251 } 252 253 254 public bool IsUserInRole(Guid userId, Guid roleId) { 255 try { 256 return CallService<bool>(s => s.IsUserInRole(userId, roleId)); 257 } 258 catch (Exception ex) { 259 // Todo Errorhandling 260 return false; 261 } 262 } 263 264 265 public void AddUserToRole(Guid roleId, Guid userId) { 266 try { 267 CallService(s => s.AddUserToRole(roleId, userId)); 268 } 269 catch (Exception ex) { 270 // Todo Errorhandling 271 272 } 273 } 274 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 281 282 } 283 } 284 285 286 public IEnumerable<Guid> GetUsersInRole(Guid roleId) { 287 try { 288 return CallService<IEnumerable<Guid>>(s => s.GetUsersInRole(roleId)); 289 } 290 catch (Exception ex) { 291 // Todo Errorhandling 292 return null; 293 } 294 } 295 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 } 305 306 #endregion 307 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 } 319 320 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 } 330 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 } 340 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 } 350 351 #endregion 352 } 377 353 }
Note: See TracChangeset
for help on using the changeset viewer.