Free cookie consent management tool by TermsFeed Policy Generator

source: branches/UserManagement/HeuristicLab.Services.Authentication/AuthenticationService.cs @ 4962

Last change on this file since 4962 was 4962, checked in by mjesner, 13 years ago

#1196

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