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 = CallService<Application[]>(s => s.GetApplications()).OrderBy(x => x.Name);
|
---|
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 | #endregion
|
---|
172 |
|
---|
173 |
|
---|
174 | //public static void Main() {
|
---|
175 | // AuthenticationServiceClient auth = new AuthenticationServiceClient();
|
---|
176 |
|
---|
177 | // auth.ClientCredentials.UserName.UserName = "Alice";
|
---|
178 | // auth.ClientCredentials.UserName.Password = "YouWillKnow";
|
---|
179 | // auth.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
|
---|
180 |
|
---|
181 | // Application[] apps = auth.GetApplications();
|
---|
182 | // foreach (Application app in apps) {
|
---|
183 | // Console.WriteLine(app.Name);
|
---|
184 | // }
|
---|
185 | // Console.ReadLine();
|
---|
186 | //}
|
---|
187 | }
|
---|
188 | }
|
---|