Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/25/12 15:29:44 (12 years ago)
Author:
ascheibe
Message:

#1648 added a method for checking if a user is a member of a list of groups

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ClientUserManagement/HeuristicLab.Services.Access/3.3/UserManager.cs

    r7355 r7909  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325using System.Web.Security;
     26using DA = HeuristicLab.Services.Access.DataAccess;
    2427
    2528namespace HeuristicLab.Services.Access {
     
    4043      return Membership.GetUser(userId);
    4144    }
     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    }
    4282  }
    4383}
Note: See TracChangeset for help on using the changeset viewer.