Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.Access/3.3/UserManager.cs @ 10900

Last change on this file since 10900 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Web.Security;
26using DA = HeuristicLab.Services.Access.DataAccess;
27
28namespace HeuristicLab.Services.Access {
29  public class UserManager : IUserManager {
30    public MembershipUser CurrentUser {
31      get { return TryAndRepeat(() => { return Membership.GetUser(); }); }
32    }
33
34    public Guid CurrentUserId {
35      get { return (Guid)CurrentUser.ProviderUserKey; }
36    }
37
38    public MembershipUser GetUserByName(string username) {
39      return Membership.GetUser(username);
40    }
41
42    public MembershipUser GetUserById(Guid userId) {
43      return Membership.GetUser(userId);
44    }
45
46    public bool VerifyUser(Guid userId, List<Guid> allowedUserGroups) {
47      List<DA.UserGroupUserGroup> userGroupBases;
48      List<DA.UserGroup> groups;
49      Dictionary<Guid, Guid> ugMapping = new Dictionary<Guid, Guid>();
50
51      if (allowedUserGroups.Contains(userId)) return true;
52
53      using (DA.AccessServiceDataContext context = new DA.AccessServiceDataContext()) {
54        userGroupBases = context.UserGroupUserGroups.ToList();
55        groups = context.UserGroupBases.OfType<DA.UserGroup>().ToList();
56      }
57
58      foreach (var ugug in userGroupBases) {
59        ugMapping[ugug.UserGroupId] = ugug.UserGroupUserGroupId;
60      }
61
62      foreach (Guid guid in allowedUserGroups) {
63        if (CheckInGroupHierarchy(userId, guid, ugMapping, groups)) return true;
64      }
65      return false;
66    }
67
68    private bool CheckInGroupHierarchy(Guid userId, Guid group, Dictionary<Guid, Guid> ugMapping, List<DA.UserGroup> groups) {
69      //check all subgroups
70      var childs = ugMapping.Where(x => x.Value == group).Select(x => x.Key);
71      var childGroups = childs.Where(x => groups.Where(y => y.Id == x).Count() > 0).ToList();
72      //also check if user is in group
73      childGroups.Add(group);
74
75      foreach (Guid id in childGroups) {
76        if (ugMapping.Where(x => x.Value == id).Select(x => x.Key).Contains(userId)) {
77          return true;
78        }
79      }
80      return false;
81    }
82
83    private static T TryAndRepeat<T>(Func<T> action) {
84      int repetitions = 5;
85      while (true) {
86        try { return action(); }
87        catch (Exception e) {
88          if (repetitions == 0) throw e;
89          repetitions--;
90        }
91      }
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.