1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Services.Authentication;
|
---|
6 | using System.ServiceModel.Security;
|
---|
7 | 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));
|
---|
174 | }
|
---|
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) {
|
---|
219 | 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() {
|
---|
229 | 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 | }
|
---|
377 | }
|
---|