Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClientUserManagement/HeuristicLab.Services.Access/3.3/AccessService.cs @ 7475

Last change on this file since 7475 was 7436, checked in by ascheibe, 13 years ago

#1648 started working on a class which should store all user information on the client side

File size: 29.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.ServiceModel;
26using System.ServiceModel.Channels;
27using System.Web.Security;
28using HeuristicLab.GeoIP;
29using DA = HeuristicLab.Services.Access.DataAccess;
30using DT = HeuristicLab.Services.Access.DataTransfer;
31
32namespace HeuristicLab.Services.Access {
33  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
34  public class AccessService : IAccessService {
35    private IUserManager userManager;
36    private IUserManager UserManager {
37      get {
38        if (userManager == null) userManager = new UserManager();
39        return userManager;
40      }
41    }
42
43    private IRoleVerifier roleVerifier;
44    private IRoleVerifier RoleVerifier {
45      get {
46        if (roleVerifier == null) roleVerifier = new RoleVerifier();
47        return roleVerifier;
48      }
49    }
50
51    #region Client Members
52    public bool ClientExists(Guid id) {
53      if (id != Guid.Empty) {
54        using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
55          return (context.Resources.Where(x => x.Id == id).Count() != 0);
56        }
57      }
58      return false;
59    }
60
61    public DT.Client GetClient(Guid id) {
62      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
63        var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
64                    where c.Id == id
65                    select c;
66
67        return Convert.ToDto(query.FirstOrDefault());
68      }
69    }
70
71    public IEnumerable<DT.Client> GetClients(IEnumerable<Guid> ids) {
72      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
73        var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
74                    where ids.Contains(c.Id)
75                    select Convert.ToDto(c);
76        return query.ToList();
77      }
78    }
79
80    public IEnumerable<DT.Client> GetAllClients() {
81      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
82        var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
83                    select Convert.ToDto(c);
84        return query.ToList();
85      }
86    }
87
88    public Guid AddClient(DT.Client client) {
89      string country = string.Empty;
90
91      OperationContext opContext = OperationContext.Current;
92
93      if (opContext != null) {
94        MessageProperties properties = opContext.IncomingMessageProperties;
95        RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
96        string ipAdr = endpoint.Address;
97        country = GeoIPLookupService.Instance.GetCountryName(ipAdr);
98      }
99
100      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
101        DA.Client entity = Convert.ToEntity(client);
102
103        if (country != string.Empty) {
104          var query = from c in context.GetTable<DA.Country>()
105                      where c.Name == country
106                      select c;
107
108          if (query.Count() > 0) {
109            entity.CountryId = query.First().Id;
110          }
111        }
112
113        context.Resources.InsertOnSubmit(entity);
114        context.SubmitChanges();
115        return entity.Id;
116      }
117    }
118
119    public void UpdateClient(DT.Client client) {
120      AddClient(client);
121    }
122
123    public void DeleteClient(DT.Client client) {
124      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
125        //load client because we could get a detached object
126        var query = from c in context.GetTable<DA.Resource>().OfType<DA.Client>()
127                    where c.Id == client.Id
128                    select c;
129        if (query.Count() > 0) {
130
131          //delete affiliation first
132          var queryMapping = context.ResourceResourceGroups.Where(x => x.ResourceId == client.Id);
133          if (queryMapping.Count() > 0) {
134            context.ResourceResourceGroups.DeleteAllOnSubmit(queryMapping);
135          }
136
137          context.Resources.DeleteOnSubmit(query.First());
138          context.SubmitChanges();
139        }
140      }
141    }
142    #endregion
143
144    #region ClientGroup
145    public IEnumerable<DT.ClientGroup> GetAllClientGroups() {
146      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
147        var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
148                    select Convert.ToDto(c);
149        return query.ToList();
150      }
151    }
152
153    public IEnumerable<DT.ClientGroup> GetClientGroups(IEnumerable<Guid> ids) {
154      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
155        var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
156                    where ids.Contains(c.Id)
157                    select Convert.ToDto(c);
158        return query.ToList();
159      }
160    }
161
162    public Guid AddClientGroup(DT.ClientGroup group) {
163      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
164        var entity = Convert.ToEntity(group);
165        context.Resources.InsertOnSubmit(entity);
166        context.SubmitChanges();
167        return entity.Id;
168      }
169    }
170
171    public void UpdateClientGroup(DT.ClientGroup group) {
172      AddClientGroup(group);
173    }
174
175    public void DeleteClientGroup(DT.ClientGroup clientGroup) {
176      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
177        //load clientGroup because we could get a detached object
178        var query = from c in context.GetTable<DA.Resource>().OfType<DA.ClientGroup>()
179                    where c.Id == clientGroup.Id
180                    select c;
181        if (query.Count() > 0) {
182          context.Resources.DeleteOnSubmit(query.First());
183          context.SubmitChanges();
184        }
185      }
186    }
187
188    public void AddResourceToGroup(DT.Resource resource, DT.ClientGroup group) {
189      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
190        DA.ResourceResourceGroup rrg = new DA.ResourceResourceGroup() {
191          ResourceId = resource.Id,
192          ResourceGroupId = group.Id
193        };
194
195        context.ResourceResourceGroups.InsertOnSubmit(rrg);
196        context.SubmitChanges();
197      }
198    }
199
200    public void RemoveResourceFromGroup(DT.Resource resource, DT.ClientGroup group) {
201      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
202        var query = context.ResourceResourceGroups.Where(x => x.ResourceId == resource.Id && x.ResourceGroupId == group.Id);
203        if (query.Count() > 0) {
204          context.ResourceResourceGroups.DeleteOnSubmit(query.First());
205          context.SubmitChanges();
206        }
207      }
208    }
209    #endregion
210
211    #region ClientGroupMapping
212    public IEnumerable<DT.ClientGroupMapping> GetClientGroupMapping() {
213      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
214        var query = from c in context.GetTable<DA.ResourceResourceGroup>()
215                    select Convert.ToDto(c);
216        return query.ToList();
217      }
218    }
219    #endregion
220
221    #region Resource
222    public IEnumerable<DT.Resource> GetResources() {
223      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
224        var query = from r in context.Resources
225                    select Convert.ToDto(r);
226        return query.ToList();
227      }
228    }
229    #endregion
230
231    #region ClientLog
232    public DT.ClientLog GetLastClientLog(Guid clientId) {
233      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
234        var query = from r in context.ClientLogs
235                    where r.ResourceId == clientId
236                    select r;
237        return Convert.ToDto(query.OrderBy(x => x.Timestamp).LastOrDefault());
238      }
239    }
240
241    public IEnumerable<DT.ClientLog> GetClientLogs(Guid clientId) {
242      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
243        var query = from r in context.ClientLogs
244                    where r.ResourceId == clientId
245                    select Convert.ToDto(r);
246        return query.ToList();
247      }
248    }
249
250    public IEnumerable<DT.ClientLog> GetClientLogsSince(DateTime startDate) {
251      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
252        var query = from r in context.ClientLogs
253                    where r.Timestamp >= startDate
254                    select Convert.ToDto(r);
255        return query.ToList();
256      }
257    }
258
259    public void AddClientLog(DT.ClientLog log) {
260      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
261        context.ClientLogs.InsertOnSubmit(Convert.ToEntity(log));
262        context.SubmitChanges();
263      }
264    }
265
266    public void DeleteClientLog(DT.ClientLog log) {
267      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
268        context.ClientLogs.DeleteOnSubmit(Convert.ToEntity(log));
269        context.SubmitChanges();
270      }
271    }
272    #endregion
273
274    #region User
275    private DT.User BuildUserDto(Guid userId) {
276      DA.aspnet_User aspUser = null;
277      DA.aspnet_Membership aspMembership = null;
278      DA.User accessUser = null;
279
280      using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
281        var userQuery = from u in context.aspnet_Users
282                        where u.UserId == userId
283                        select u;
284        if (userQuery.Count() == 1) {
285          aspUser = userQuery.First();
286        }
287
288        var memQuery = from u in context.aspnet_Memberships
289                       where u.UserId == userId
290                       select u;
291        if (memQuery.Count() == 1) {
292          aspMembership = memQuery.First();
293        }
294      }
295
296      if (aspUser == null || aspMembership == null) {
297        using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
298          var query = from u in context.UserGroupBases.OfType<DA.User>()
299                      where u.Id == userId
300                      select u;
301          if (query.Count() == 1) {
302            accessUser = query.First();
303          } else {
304            //if the user is not in the access db add it (this makes it easy to upgrade with an existing asp.net authentication db)
305            DA.User user = new DA.User();
306            user.Id = userId;
307            user.FullName = "Not set";
308            context.UserGroupBases.InsertOnSubmit(user);
309            context.SubmitChanges();
310            accessUser = user;
311          }
312        }
313      }
314
315      if (aspUser == null || aspMembership == null || accessUser == null) {
316        throw new Exception("User with id " + userId + " not found.");
317      } else {
318        return Convert.ToDto(accessUser, aspUser, aspMembership);
319      }
320    }
321
322    private DT.LightweightUser BuildLightweightUserDto(Guid userId) {
323      DA.aspnet_User aspUser = null;
324      DA.User accessUser = null;
325
326      using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
327        var userQuery = from u in context.aspnet_Users
328                        where u.UserId == userId
329                        select u;
330        if (userQuery.Count() == 1) {
331          aspUser = userQuery.First();
332        }
333      }
334
335      if (aspUser != null) {
336        using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
337          var query = from u in context.UserGroupBases.OfType<DA.User>()
338                      where u.Id == userId
339                      select u;
340          if (query.Count() == 1) {
341            accessUser = query.First();
342          } else {
343            //if the user is not in the access db add it (this makes it easy to upgrade with an existing asp.net authentication db)
344            DA.User user = new DA.User();
345            user.Id = userId;
346            user.FullName = "Not set";
347            context.UserGroupBases.InsertOnSubmit(user);
348            context.SubmitChanges();
349            accessUser = user;
350          }
351        }
352      }
353
354      if (aspUser == null || accessUser == null) {
355        throw new Exception("User with id " + userId + " not found.");
356      } else {
357        return Convert.ToDto(accessUser, aspUser);
358      }
359    }
360
361    public DT.LightweightUser Login() {
362      Guid userId = UserManager.CurrentUserId;
363      return BuildLightweightUserDto(userId);
364    }
365
366    public IEnumerable<DT.UserGroup> GetGroupsOfCurrentUser() {
367      Guid userId = UserManager.CurrentUserId;
368
369      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
370        //TODO: this has to be done recursive, so check if a group is in another
371        //group because then the user is also in this group...
372        var query = from g in context.UserGroupUserGroups
373                    from ug in context.UserGroupBases.OfType<DA.UserGroup>()
374                    where g.UserGroupId == userId && g.UserGroupUserGroupId == ug.Id
375                    select Convert.ToDto(ug);
376        return query.ToList();
377      }
378    }
379
380    public IEnumerable<DT.Role> GetRolesOfCurrentUser() {
381      Guid userId = UserManager.CurrentUserId;
382
383      using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
384        var query = from ur in context.aspnet_UsersInRoles
385                    from r in context.aspnet_Roles
386                    where ur.UserId == userId && ur.RoleId == r.RoleId
387                    select Convert.ToDto(r);
388        return query.ToList();
389      }
390    }
391
392
393    public IEnumerable<DT.LightweightUser> GetAllLightweightUsers() {
394      List<Guid> accessUserGuids = null;
395
396      using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
397        var query = from u in context.aspnet_Users
398                    select u.UserId;
399        accessUserGuids = query.ToList();
400      }
401      return accessUserGuids.Select(x => BuildLightweightUserDto(x));
402    }
403
404    public IEnumerable<DT.User> GetAllUsers() {
405      List<Guid> accessUserGuids = null;
406
407      using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
408        var query = from u in context.aspnet_Users
409                    select u.UserId;
410        accessUserGuids = query.ToList();
411      }
412
413      return accessUserGuids.Select(x => BuildUserDto(x));
414    }
415
416    public IEnumerable<DT.User> GetUsers(IEnumerable<Guid> ids) {
417      List<Guid> accessUserGuids = null;
418
419      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
420        var query = from u in context.UserGroupBases.OfType<DA.User>()
421                    where ids.Contains(u.Id)
422                    select u.Id;
423        accessUserGuids = query.ToList();
424      }
425
426      if (accessUserGuids.Count() != ids.Count()) {
427        throw new Exception("Couldn't find one or more users for the given user ids.");
428      }
429
430      return accessUserGuids.Select(x => BuildUserDto(x));
431    }
432
433    public IEnumerable<DT.LightweightUser> GetLightweightUsers(IEnumerable<Guid> ids) {
434      List<Guid> accessUserGuids = null;
435
436      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
437        var query = from u in context.UserGroupBases.OfType<DA.User>()
438                    where ids.Contains(u.Id)
439                    select u.Id;
440        accessUserGuids = query.ToList();
441      }
442
443      if (accessUserGuids.Count() != ids.Count()) {
444        throw new Exception("Couldn't find one or more users for the given user ids.");
445      }
446
447      return accessUserGuids.Select(x => BuildLightweightUserDto(x));
448    }
449
450    public DT.User AddUser(DT.User user) {
451      DA.User accessUser;
452      DA.aspnet_User aspUser;
453      DA.aspnet_Membership aspMembership;
454      bool userExistsInASP;
455
456      Convert.ToEntity(user, out accessUser, out aspUser, out aspMembership, out userExistsInASP);
457
458      if (userExistsInASP) {
459        if (accessUser.Id == null || accessUser.Id == Guid.Empty) {
460          accessUser.Id = aspMembership.UserId;
461        }
462        using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
463          context.UserGroupBases.InsertOnSubmit(accessUser);
464          context.SubmitChanges();
465        }
466        MembershipUser membershipUser = Membership.GetUser((object)accessUser.Id);
467        if (membershipUser != null) {
468          membershipUser.Email = aspMembership.Email;
469          membershipUser.IsApproved = aspMembership.IsApproved;
470          membershipUser.Comment = aspMembership.Comment;
471          Membership.UpdateUser(membershipUser);
472        }
473      } else {
474        MembershipUser membershipUser = Membership.CreateUser(aspUser.UserName, aspUser.UserName, aspMembership.Email);
475        membershipUser.IsApproved = aspMembership.IsApproved;
476        membershipUser.Comment = aspMembership.Comment;
477        Membership.UpdateUser(membershipUser);
478
479        Guid userId = (Guid)membershipUser.ProviderUserKey;
480        accessUser.Id = userId;
481
482        using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
483          context.UserGroupBases.InsertOnSubmit(accessUser);
484          context.SubmitChanges();
485        }
486      }
487
488      using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
489        var newAspUser = context.aspnet_Users.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
490        var newAspMembership = context.aspnet_Memberships.Where(x => x.UserId == accessUser.Id).FirstOrDefault();
491        return Convert.ToDto(accessUser, newAspUser, newAspMembership);
492      }
493    }
494
495    public void DeleteUser(DT.User user) {
496      if (user.Id != null && user.Id != Guid.Empty) {
497        //delete asp.net user
498        Membership.DeleteUser(user.UserName);
499        using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
500          var query = context.UserGroupBases.OfType<DA.User>().Where(x => x.Id == user.Id);
501          if (query.Count() > 0) {
502
503            //delete affiliation first
504            var queryMapping = context.UserGroupUserGroups.Where(x => x.UserGroupId == user.Id);
505            if (queryMapping.Count() > 0) {
506              context.UserGroupUserGroups.DeleteAllOnSubmit(queryMapping);
507            }
508
509            //delete user from access db
510            context.UserGroupBases.DeleteOnSubmit(query.First());
511            context.SubmitChanges();
512          }
513        }
514      }
515    }
516
517    public void UpdateUser(DT.User user) {
518      AddUser(user);
519    }
520
521    public void AddUserToRole(DT.Role role, DT.User user) {
522      //TODO: usernames and rolenames have to be unique!
523      MembershipUser msUser = Membership.GetUser((object)user.Id);
524      if (msUser != null) {
525        Roles.AddUserToRole(msUser.UserName, role.Name);
526      }
527    }
528
529    public void RemoveUserFromRole(DT.Role role, DT.User user) {
530      MembershipUser msUser = Membership.GetUser((object)user.Id);
531      if (msUser != null) {
532        Roles.RemoveUserFromRole(msUser.UserName, role.Name);
533      }
534    }
535
536    public bool ResetPassword(DT.User user, string oldPassword, string newPassword) {
537      MembershipUser msUser = Membership.GetUser((object)user.Id);
538      if (msUser != null) {
539        return msUser.ChangePassword(oldPassword, newPassword);
540      }
541      return false;
542    }
543    #endregion
544
545    #region UserGroup
546    public IEnumerable<DT.UserGroup> GetAllUserGroups() {
547      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
548        var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
549                    select Convert.ToDto(u);
550        return query.ToList();
551      }
552    }
553
554    public IEnumerable<DT.UserGroup> GetUserGroupsOfUser(Guid userId) {
555
556      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
557        var groupIds = from g in context.UserGroupUserGroups
558                       where g.UserGroupId == userId
559                       select g.UserGroupUserGroupId;
560
561        var query = from g in context.UserGroupBases.OfType<DA.UserGroup>()
562                    where groupIds.Contains(g.Id)
563                    select Convert.ToDto(g);
564
565        return query.ToList();
566      }
567    }
568
569    public IEnumerable<DT.UserGroup> GetUserGroups(IEnumerable<Guid> ids) {
570      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
571        var query = from u in context.UserGroupBases.OfType<DA.UserGroup>()
572                    where ids.Contains(u.Id)
573                    select Convert.ToDto(u);
574        return query.ToList();
575      }
576    }
577
578    public Guid AddUserGroup(DT.UserGroup group) {
579      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
580        //because id is not automatically set because of user, we have to do it here manually for group
581        group.Id = Guid.NewGuid();
582        context.UserGroupBases.InsertOnSubmit(Convert.ToEntity(group));
583        context.SubmitChanges();
584        return group.Id;
585      }
586    }
587
588    public void UpdateUserGroup(DT.UserGroup group) {
589      AddUserGroup(group);
590    }
591
592    public void DeleteUserGroup(DT.UserGroup group) {
593      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
594        context.UserGroupBases.DeleteOnSubmit(Convert.ToEntity(group));
595        context.SubmitChanges();
596      }
597    }
598
599    public void AddUserGroupBaseToGroup(DT.UserGroupBase resource, DT.UserGroup group) {
600      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
601        DA.UserGroupUserGroup ugug = new DA.UserGroupUserGroup();
602        ugug.UserGroupId = resource.Id;
603        ugug.UserGroupUserGroupId = group.Id;
604        context.UserGroupUserGroups.InsertOnSubmit(ugug);
605        context.SubmitChanges();
606      }
607    }
608
609    public void RemoveUserGroupBaseFromGroup(DT.UserGroupBase resource, DT.UserGroup userGroup) {
610      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
611        var query = from u in context.UserGroupUserGroups
612                    where u.UserGroupId == resource.Id && u.UserGroupUserGroupId == userGroup.Id
613                    select u;
614
615        if (query.Count() == 1) {
616          context.UserGroupUserGroups.DeleteOnSubmit(query.First());
617          context.SubmitChanges();
618        }
619      }
620    }
621
622    public IEnumerable<DT.UserGroupBase> GetUsersAndGroups() {
623      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
624        var query = from u in context.UserGroupBases
625                    select Convert.ToDto(u);
626        return query.ToList();
627      }
628    }
629
630    public IEnumerable<DT.UserGroupMapping> GetUserGroupMapping() {
631      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
632        var query = from u in context.UserGroupUserGroups
633                    select Convert.ToDto(u);
634        return query.ToList();
635      }
636    }
637    #endregion
638
639    #region UserGroupBase
640    public IEnumerable<DT.UserGroupBase> GetAllLeightweightUsersAndGroups() {
641      List<DA.UserGroupBase> dbUserGroupsBases = new List<DA.UserGroupBase>();
642      List<DT.UserGroupBase> result = new List<DT.UserGroupBase>();
643
644      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
645        var query = from u in context.UserGroupBases
646                    select u;
647        dbUserGroupsBases = query.ToList();
648      }
649
650      foreach (var ugb in dbUserGroupsBases) {
651        if (ugb.GetType() == typeof(DA.User)) {
652          var user = BuildLightweightUserDto(ugb.Id);
653          result.Add(user);
654        } else if (ugb.GetType() == typeof(DA.UserGroup)) {
655          var group = Convert.ToDto(ugb as DA.UserGroup);
656          result.Add(group);
657        }
658      }
659      return result;
660    }
661
662    public IEnumerable<DT.UserGroupBase> GetLeightweightUsersAndGroups(IEnumerable<Guid> ids) {
663      List<DA.UserGroupBase> dbUserGroupsBases = new List<DA.UserGroupBase>();
664      List<DT.UserGroupBase> result = new List<DT.UserGroupBase>();
665
666      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
667        var query = from u in context.UserGroupBases
668                    where ids.Contains(u.Id)
669                    select u;
670        dbUserGroupsBases = query.ToList();
671      }
672
673      foreach (var ugb in dbUserGroupsBases) {
674        if (ugb.GetType() == typeof(DA.User)) {
675          var user = BuildLightweightUserDto(ugb.Id);
676          result.Add(user);
677        } else if (ugb.GetType() == typeof(DA.UserGroup)) {
678          var group = Convert.ToDto(ugb as DA.UserGroup);
679          result.Add(group);
680        }
681      }
682      return result;
683    }
684    #endregion
685
686    #region Roles
687    public IEnumerable<DT.Role> GetRoles() {
688      using (DA.ASPNETAuthenticationDataContext context = new DA.ASPNETAuthenticationDataContext()) {
689        var query = from u in context.aspnet_Roles
690                    select Convert.ToDto(u);
691        return query.ToList();
692      }
693    }
694
695    public DT.Role AddRole(DT.Role role) {
696      Roles.CreateRole(role.Name);
697      return role;
698    }
699
700    public void DeleteRole(DT.Role role) {
701      Roles.DeleteRole(role.Name);
702    }
703
704    public IEnumerable<DT.Role> GetUserRoles(DT.User user) {
705      var roles = Roles.GetRolesForUser(user.UserName);
706      return roles.Select(x => new DT.Role() { Name = x });
707    }
708
709    public void AddRoleToGroup(DT.UserGroup userGroup, DT.Role role) {
710      Guid[] userIds;
711      string[] aspUsers;
712
713      using (DA.ClientManagementDataContext accessContext = new DA.ClientManagementDataContext()) {
714        userIds = (from u in accessContext.UserGroupUserGroups
715                   where u.UserGroupUserGroupId == userGroup.Id
716                   select u.UserGroupId).ToArray();
717      }
718
719      using (DA.ASPNETAuthenticationDataContext aspContext = new DA.ASPNETAuthenticationDataContext()) {
720        aspUsers = (from u in aspContext.aspnet_Users
721                    where userIds.Contains(u.UserId)
722                    select u.UserName).ToArray();
723      }
724
725      Roles.AddUsersToRole(aspUsers, role.Name);
726
727    }
728
729    public void RemoveRoleFromGroup(DT.UserGroup userGroup, DT.Role role) {
730      Guid[] userIds;
731      string[] aspUsers;
732
733      using (DA.ClientManagementDataContext accessContext = new DA.ClientManagementDataContext()) {
734        userIds = (from u in accessContext.UserGroupUserGroups
735                   where u.UserGroupUserGroupId == userGroup.Id
736                   select u.UserGroupId).ToArray();
737      }
738
739      using (DA.ASPNETAuthenticationDataContext aspContext = new DA.ASPNETAuthenticationDataContext()) {
740        aspUsers = (from u in aspContext.aspnet_Users
741                    where userIds.Contains(u.UserId)
742                    select u.UserName).ToArray();
743      }
744
745      Roles.RemoveUsersFromRole(aspUsers.ToArray(), role.Name);
746    }
747    #endregion
748
749    #region Error Reporting
750    public void ReportError(DT.ClientError error) {
751      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
752        context.ClientErrors.InsertOnSubmit(Convert.ToEntity(error));
753        context.SubmitChanges();
754      }
755    }
756
757    public IEnumerable<DT.ClientError> GetClientErrors() {
758      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
759        var query = from c in context.ClientErrors
760                    select Convert.ToDto(c);
761        return query.ToList();
762      }
763    }
764
765    public IEnumerable<DT.ClientError> GetLastClientErrors(DateTime startDate) {
766      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
767        var query = from c in context.ClientErrors
768                    where c.Timestamp >= startDate
769                    select Convert.ToDto(c);
770        return query.ToList();
771      }
772    }
773
774    public void DeleteError(DT.ClientError error) {
775      using (DA.ClientManagementDataContext context = new DA.ClientManagementDataContext()) {
776        var query = context.ClientErrors.Where(x => x.Id == error.Id);
777        if (query.Count() > 0) {
778          context.ClientErrors.DeleteOnSubmit(query.First());
779          context.SubmitChanges();
780        }
781      }
782    }
783    #endregion
784  }
785}
Note: See TracBrowser for help on using the repository browser.