Get visitor group by current user

  • Updated

Check the visitor groups that the current visitor is part of IN CODE

public List<VisitorGroup> GetVisitorGroupByCurrentUser()
{
      List<VisitorGroup> filteredVisitorGroup = new List<VisitorGroup>();

      var visitorGroupRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
      var visitorGroupRoleRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRoleRepository>();
      var httpContextAccessor = ServiceLocator.Current.GetInstance<ServiceAccessor<HttpContextBase>>();

      var httpContext = httpContextAccessor();
      var user = httpContext.User;
      var visitorGroups = visitorGroupRepository.List();
      foreach (var visitorGroup in visitorGroups)
      {
          VisitorGroupRole virtualRoleObject;
          if (visitorGroupRoleRepository.TryGetRole(visitorGroup.Name, out virtualRoleObject))
          {
              if (virtualRoleObject.IsMatch(user, httpContext))
              {
                  filteredVisitorGroup.Add(visitorGroup);
              }
          }
      }

      return filteredVisitorGroup;
}