1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.ServiceModel;
|
---|
5 | using HeuristicLab.Services.Authentication.DataTransfer;
|
---|
6 | using HeuristicLab.Services.Authentication.DataAccess;
|
---|
7 | using System.Data.Linq;
|
---|
8 | using System.Diagnostics;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Services.Authentication
|
---|
11 | {
|
---|
12 | [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
|
---|
13 | public class AuthenticationService : IAuthenticationService
|
---|
14 | {
|
---|
15 |
|
---|
16 | #region User
|
---|
17 |
|
---|
18 |
|
---|
19 | public DataTransfer.User GetUser(Guid id)
|
---|
20 | {
|
---|
21 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
22 | {
|
---|
23 | var user = db.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault();
|
---|
24 | var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault();
|
---|
25 |
|
---|
26 |
|
---|
27 | return Convert.ToDataTransfer(user, membership);
|
---|
28 |
|
---|
29 |
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | public IEnumerable<DataTransfer.User> GetAllUsers() {
|
---|
34 | List<DataTransfer.User> userList = new List<DataTransfer.User>();
|
---|
35 | using (UserManagementDataContext db = new UserManagementDataContext()) {
|
---|
36 | var users = db.aspnet_Users.ToList<aspnet_User>();
|
---|
37 | foreach (aspnet_User user in users) {
|
---|
38 | var membership = db.aspnet_Memberships.Where(x => x.UserId == user.UserId).FirstOrDefault();
|
---|
39 | userList.Add(Convert.ToDataTransfer(user, membership));
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | return userList;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public IEnumerable<DataTransfer.User> GetUsers(Guid applicationId)
|
---|
47 | {
|
---|
48 | List<DataTransfer.User> userList = new List<DataTransfer.User>();
|
---|
49 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
50 | {
|
---|
51 | var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList<aspnet_User>();
|
---|
52 |
|
---|
53 | foreach (aspnet_User user in users)
|
---|
54 | {
|
---|
55 | var membership = db.aspnet_Memberships.Where(x => x.UserId == user.UserId).FirstOrDefault();
|
---|
56 |
|
---|
57 | userList.Add(Convert.ToDataTransfer(user, membership));
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | }
|
---|
64 | return userList;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public Guid AddUser(User user)
|
---|
68 | {
|
---|
69 | if (user != null)
|
---|
70 | {
|
---|
71 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
72 | {
|
---|
73 |
|
---|
74 | aspnet_User eUser = new aspnet_User();
|
---|
75 | aspnet_Membership eMembership = new aspnet_Membership();
|
---|
76 |
|
---|
77 | Convert.ToEntity(user, eUser, eMembership);
|
---|
78 | try
|
---|
79 | {
|
---|
80 | db.aspnet_Users.InsertOnSubmit(eUser);
|
---|
81 | db.aspnet_Memberships.InsertOnSubmit(eMembership);
|
---|
82 | db.SubmitChanges();
|
---|
83 | }
|
---|
84 | catch (Exception ex)
|
---|
85 | {
|
---|
86 | return Guid.Empty;
|
---|
87 | }
|
---|
88 |
|
---|
89 | return user.Id;
|
---|
90 |
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | }
|
---|
95 | return Guid.Empty;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public bool DeleteUser(Guid id)
|
---|
99 | {
|
---|
100 |
|
---|
101 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
102 | {
|
---|
103 | var user = db.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault();
|
---|
104 | var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault();
|
---|
105 | var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == id).ToList<aspnet_UsersInRole>();
|
---|
106 | try
|
---|
107 | {
|
---|
108 | db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles);
|
---|
109 | db.aspnet_Memberships.DeleteOnSubmit(membership);
|
---|
110 | db.aspnet_Users.DeleteOnSubmit(user);
|
---|
111 | db.SubmitChanges();
|
---|
112 | }
|
---|
113 | catch (Exception ex)
|
---|
114 | {
|
---|
115 | return false;
|
---|
116 | }
|
---|
117 | return true;
|
---|
118 |
|
---|
119 | }
|
---|
120 | return false;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | public bool UpdateUser(User user)
|
---|
125 | {
|
---|
126 | if (user != null)
|
---|
127 | {
|
---|
128 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
129 | {
|
---|
130 |
|
---|
131 | var eUser = db.aspnet_Users.Where(x => x.UserId == user.Id).FirstOrDefault();
|
---|
132 | var eMembership = db.aspnet_Memberships.Where(x => x.UserId == user.Id).FirstOrDefault();
|
---|
133 | Convert.ToEntity(user, eUser, eMembership);
|
---|
134 | try
|
---|
135 | {
|
---|
136 |
|
---|
137 | db.SubmitChanges();
|
---|
138 | }
|
---|
139 | catch (Exception ex)
|
---|
140 | {
|
---|
141 | return false;
|
---|
142 | }
|
---|
143 |
|
---|
144 | return true;
|
---|
145 |
|
---|
146 | }
|
---|
147 |
|
---|
148 | }
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | public bool AddUserToRole(Guid roleId, Guid userId) {
|
---|
154 | using (UserManagementDataContext db = new UserManagementDataContext()) {
|
---|
155 | try {
|
---|
156 | aspnet_UsersInRole r = new aspnet_UsersInRole();
|
---|
157 | r.RoleId = roleId;
|
---|
158 | r.UserId = userId;
|
---|
159 | db.aspnet_UsersInRoles.InsertOnSubmit(r);
|
---|
160 | db.SubmitChanges();
|
---|
161 | return true;
|
---|
162 | }
|
---|
163 | catch (Exception ex) {
|
---|
164 | Debug.WriteLine(ex.InnerException);
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | public IEnumerable<Guid> GetUsersInRole(Guid roleId) {
|
---|
171 | List<Guid> userList = new List<Guid>();
|
---|
172 | using (UserManagementDataContext db = new UserManagementDataContext()) {
|
---|
173 | try {
|
---|
174 | var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId).ToList<aspnet_UsersInRole>();
|
---|
175 |
|
---|
176 | foreach (aspnet_UsersInRole u in users) {
|
---|
177 | userList.Add(GetUser(u.UserId).Id);
|
---|
178 | }
|
---|
179 |
|
---|
180 | }
|
---|
181 | catch (Exception ex) {
|
---|
182 | Debug.WriteLine(ex.InnerException);
|
---|
183 | return new List<Guid>();
|
---|
184 |
|
---|
185 | }
|
---|
186 |
|
---|
187 | }
|
---|
188 |
|
---|
189 | return userList;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /*
|
---|
193 | public IEnumerable<User> GetUsersInRole(Guid roleId) {
|
---|
194 | List<User> userList = new List<User>();
|
---|
195 | using (UserManagementDataContext db = new UserManagementDataContext()) {
|
---|
196 | try {
|
---|
197 | var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId).ToList<aspnet_UsersInRole>();
|
---|
198 |
|
---|
199 | foreach (aspnet_UsersInRole u in users) {
|
---|
200 | userList.Add(GetUser(u.UserId));
|
---|
201 |
|
---|
202 | }
|
---|
203 |
|
---|
204 | }
|
---|
205 | catch (Exception ex) {
|
---|
206 | Debug.WriteLine(ex.InnerException);
|
---|
207 | return new List<User>();
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | return userList;
|
---|
212 | }
|
---|
213 | */
|
---|
214 |
|
---|
215 | public bool IsUserInRole(Guid userId, Guid roleId) {
|
---|
216 | bool isInRole = false;
|
---|
217 | using (UserManagementDataContext db = new UserManagementDataContext()) {
|
---|
218 | var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).ToList<aspnet_UsersInRole>();
|
---|
219 | foreach (aspnet_UsersInRole u in users) {
|
---|
220 | isInRole = true;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | return isInRole;
|
---|
224 |
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | public bool RemoveUserFromRole(Guid roleId, Guid userId) {
|
---|
229 | using (UserManagementDataContext db = new UserManagementDataContext()) {
|
---|
230 | try {
|
---|
231 | var role = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).FirstOrDefault();
|
---|
232 |
|
---|
233 | db.aspnet_UsersInRoles.DeleteOnSubmit(role);
|
---|
234 | db.SubmitChanges();
|
---|
235 | return true;
|
---|
236 |
|
---|
237 | }
|
---|
238 | catch (Exception ex) {
|
---|
239 | Debug.WriteLine(ex.InnerException);
|
---|
240 | return false;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 |
|
---|
248 |
|
---|
249 | #endregion
|
---|
250 |
|
---|
251 |
|
---|
252 |
|
---|
253 | #region Role
|
---|
254 |
|
---|
255 | public Role GetRole(Guid id)
|
---|
256 | {
|
---|
257 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
258 | {
|
---|
259 | var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault();
|
---|
260 | return Convert.ToDataTransfer(role);
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | public IEnumerable<Role> GetAllRoles() {
|
---|
265 | List<DataTransfer.Role> roleList = new List<DataTransfer.Role>();
|
---|
266 | using (UserManagementDataContext db = new UserManagementDataContext()) {
|
---|
267 | var roles = db.aspnet_Roles.ToList<aspnet_Role>();
|
---|
268 | foreach (aspnet_Role role in roles) {
|
---|
269 | roleList.Add(Convert.ToDataTransfer(role));
|
---|
270 | }
|
---|
271 | }
|
---|
272 | return roleList;
|
---|
273 | }
|
---|
274 |
|
---|
275 | public IEnumerable<Role> GetRoles(Guid applicationId)
|
---|
276 | {
|
---|
277 | List<DataTransfer.Role> roleList = new List<DataTransfer.Role>();
|
---|
278 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
279 | {
|
---|
280 | var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Role>();
|
---|
281 |
|
---|
282 | foreach (aspnet_Role role in roles)
|
---|
283 | {
|
---|
284 | roleList.Add(Convert.ToDataTransfer(role));
|
---|
285 | }
|
---|
286 |
|
---|
287 | }
|
---|
288 | return roleList;
|
---|
289 | }
|
---|
290 |
|
---|
291 | public Guid AddRole(Role role)
|
---|
292 | {
|
---|
293 |
|
---|
294 | if (role != null)
|
---|
295 | {
|
---|
296 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
297 | {
|
---|
298 |
|
---|
299 | aspnet_Role eRole = new aspnet_Role();
|
---|
300 |
|
---|
301 | Convert.ToEntity(role, eRole);
|
---|
302 | try
|
---|
303 | {
|
---|
304 | db.aspnet_Roles.InsertOnSubmit(eRole);
|
---|
305 | db.SubmitChanges();
|
---|
306 | }
|
---|
307 | catch (Exception ex)
|
---|
308 | {
|
---|
309 | return Guid.Empty;
|
---|
310 | }
|
---|
311 |
|
---|
312 | return role.Id;
|
---|
313 | }
|
---|
314 |
|
---|
315 | }
|
---|
316 | return Guid.Empty;
|
---|
317 | }
|
---|
318 |
|
---|
319 | public bool DeleteRole(Guid id)
|
---|
320 | {
|
---|
321 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
322 | {
|
---|
323 | var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault();
|
---|
324 |
|
---|
325 |
|
---|
326 | try
|
---|
327 | {
|
---|
328 | db.aspnet_Roles.DeleteOnSubmit(role);
|
---|
329 | db.SubmitChanges();
|
---|
330 | }
|
---|
331 | catch (Exception ex)
|
---|
332 | {
|
---|
333 | return false;
|
---|
334 | }
|
---|
335 | return true;
|
---|
336 |
|
---|
337 |
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | public IEnumerable<Guid> GetRolesForUser(Guid userId) {
|
---|
342 | List<Guid> roleList = new List<Guid>();
|
---|
343 | using (UserManagementDataContext db = new UserManagementDataContext()) {
|
---|
344 | try {
|
---|
345 | var roles = db.aspnet_UsersInRoles.Where(x => x.UserId == userId).ToList<aspnet_UsersInRole>();
|
---|
346 | foreach (aspnet_UsersInRole r in roles) {
|
---|
347 | roleList.Add(GetRole(r.RoleId).Id);
|
---|
348 | }
|
---|
349 | }
|
---|
350 | catch (Exception ex) {
|
---|
351 | Debug.WriteLine(ex.InnerException);
|
---|
352 | return new List<Guid>();
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | return roleList;
|
---|
357 |
|
---|
358 | }
|
---|
359 |
|
---|
360 | /*
|
---|
361 | public IEnumerable<Role> GetRolesForUser(Guid userId)
|
---|
362 | {
|
---|
363 | List<Role> roleList = new List<Role>();
|
---|
364 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
365 | {
|
---|
366 | try
|
---|
367 | {
|
---|
368 | var roles = db.aspnet_UsersInRoles.Where(x => x.UserId == userId).ToList<aspnet_UsersINRole>();
|
---|
369 |
|
---|
370 | foreach (aspnet_UsersInRole r in roles)
|
---|
371 | {
|
---|
372 | roleList.Add(GetRole(r.RoleId));
|
---|
373 |
|
---|
374 | }
|
---|
375 |
|
---|
376 | }
|
---|
377 | catch (Exception ex)
|
---|
378 | {
|
---|
379 | Debug.WriteLine(ex.InnerException);
|
---|
380 | return new List<Role>();
|
---|
381 | }
|
---|
382 | }
|
---|
383 |
|
---|
384 | return roleList;
|
---|
385 | }
|
---|
386 | */
|
---|
387 |
|
---|
388 |
|
---|
389 |
|
---|
390 | public bool UpdateRole(Role role)
|
---|
391 | {
|
---|
392 | if (role != null)
|
---|
393 | {
|
---|
394 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
395 | {
|
---|
396 |
|
---|
397 | var eRole = db.aspnet_Roles.Where(x => x.RoleId == role.Id).FirstOrDefault();
|
---|
398 |
|
---|
399 | Convert.ToEntity(role, eRole);
|
---|
400 | try
|
---|
401 | {
|
---|
402 |
|
---|
403 | db.SubmitChanges();
|
---|
404 | }
|
---|
405 | catch (Exception ex)
|
---|
406 | {
|
---|
407 | return false;
|
---|
408 | }
|
---|
409 |
|
---|
410 | return true;
|
---|
411 |
|
---|
412 | }
|
---|
413 |
|
---|
414 | }
|
---|
415 | return false;
|
---|
416 | }
|
---|
417 |
|
---|
418 |
|
---|
419 |
|
---|
420 | #endregion
|
---|
421 |
|
---|
422 |
|
---|
423 |
|
---|
424 | #region Application
|
---|
425 |
|
---|
426 | public Guid AddApplication(Application application)
|
---|
427 | {
|
---|
428 | if (application != null)
|
---|
429 | {
|
---|
430 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
431 | {
|
---|
432 |
|
---|
433 | aspnet_Application eApplication = new aspnet_Application();
|
---|
434 |
|
---|
435 | Convert.ToEntity(application, eApplication);
|
---|
436 | try
|
---|
437 | {
|
---|
438 | db.aspnet_Applications.InsertOnSubmit(eApplication);
|
---|
439 | db.SubmitChanges();
|
---|
440 | }
|
---|
441 | catch (Exception ex)
|
---|
442 | {
|
---|
443 | return Guid.Empty;
|
---|
444 | }
|
---|
445 |
|
---|
446 | return application.Id;
|
---|
447 |
|
---|
448 | }
|
---|
449 |
|
---|
450 | }
|
---|
451 | return Guid.Empty;
|
---|
452 | }
|
---|
453 |
|
---|
454 | public bool UpdateApplication(Application application)
|
---|
455 | {
|
---|
456 | if (application != null)
|
---|
457 | {
|
---|
458 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
459 | {
|
---|
460 |
|
---|
461 | var eApplication = db.aspnet_Applications.Where(x => x.ApplicationId == application.Id).FirstOrDefault();
|
---|
462 |
|
---|
463 | Convert.ToEntity(application, eApplication);
|
---|
464 | try
|
---|
465 | {
|
---|
466 |
|
---|
467 | db.SubmitChanges();
|
---|
468 | }
|
---|
469 | catch (Exception ex)
|
---|
470 | {
|
---|
471 | return false;
|
---|
472 | }
|
---|
473 |
|
---|
474 | return true;
|
---|
475 |
|
---|
476 | }
|
---|
477 |
|
---|
478 | }
|
---|
479 | return false;
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | public bool DeleteApplication(Guid applicationId)
|
---|
484 | {
|
---|
485 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
486 | {
|
---|
487 | var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList<aspnet_User>();
|
---|
488 | var memberships = db.aspnet_Memberships.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Membership>();
|
---|
489 |
|
---|
490 | var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Role>();
|
---|
491 | var application = db.aspnet_Applications.Where(x => x.ApplicationId == applicationId).FirstOrDefault();
|
---|
492 |
|
---|
493 | try
|
---|
494 | {
|
---|
495 | foreach (aspnet_User u in users)
|
---|
496 | {
|
---|
497 | var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == u.UserId).ToList<aspnet_UsersInRole>();
|
---|
498 | db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles);
|
---|
499 | }
|
---|
500 |
|
---|
501 | db.aspnet_Memberships.DeleteAllOnSubmit(memberships);
|
---|
502 | db.aspnet_Users.DeleteAllOnSubmit(users);
|
---|
503 | db.aspnet_Roles.DeleteAllOnSubmit(roles);
|
---|
504 | db.aspnet_Applications.DeleteOnSubmit(application);
|
---|
505 | db.SubmitChanges();
|
---|
506 | }
|
---|
507 | catch (Exception ex)
|
---|
508 | {
|
---|
509 | return false;
|
---|
510 | }
|
---|
511 | return true;
|
---|
512 |
|
---|
513 |
|
---|
514 | }
|
---|
515 | return false;
|
---|
516 | }
|
---|
517 |
|
---|
518 | public Application GetApplication(Guid id)
|
---|
519 | {
|
---|
520 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
521 | {
|
---|
522 | var application = db.aspnet_Applications.Where(x => x.ApplicationId == id).FirstOrDefault();
|
---|
523 | return Convert.ToDataTransfer(application);
|
---|
524 | }
|
---|
525 | }
|
---|
526 |
|
---|
527 | public IEnumerable<DataTransfer.Application> GetApplications()
|
---|
528 | {
|
---|
529 | List<DataTransfer.Application> applicationList = new List<DataTransfer.Application>();
|
---|
530 | using (UserManagementDataContext db = new UserManagementDataContext())
|
---|
531 | {
|
---|
532 | var apps = db.aspnet_Applications.ToList<aspnet_Application>();
|
---|
533 |
|
---|
534 | foreach (aspnet_Application app in apps)
|
---|
535 | {
|
---|
536 | applicationList.Add(Convert.ToDataTransfer(app));
|
---|
537 | }
|
---|
538 |
|
---|
539 | }
|
---|
540 | return applicationList;
|
---|
541 | }
|
---|
542 |
|
---|
543 | #endregion
|
---|
544 | }
|
---|
545 |
|
---|
546 | }
|
---|
547 |
|
---|