Free cookie consent management tool by TermsFeed Policy Generator

source: branches/UserManagement/HeuristicLab.Services.Authentication.ServiceClients/AuthenticationClient.cs @ 5313

Last change on this file since 5313 was 5257, checked in by mjesner, 14 years ago

#1196

File size: 10.0 KB
Line 
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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25using HeuristicLab.Services.Authentication;
26using System.ServiceModel.Security;
27using System.ServiceModel;
28using HeuristicLab.Core;
29using HeuristicLab.Collections;
30
31namespace 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());
118          }
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 {
149        try {
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 {
168        try {
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  }
353}
Note: See TracBrowser for help on using the repository browser.