Ejemplo n.º 1
0
        /// <summary>
        /// Search for content
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="searchDescriptor"></param>
        /// <param name="includeNeutralLanguage"></param>
        /// <param name="rootReferences"></param>
        /// <param name="typeFilter"></param>
        /// <param name="principleReadFilter"></param>
        /// <returns></returns>
        public virtual ISearchResponse <IContent> SearchContent <T>(
            Func <SearchDescriptor <T>, SearchDescriptor <T> > searchDescriptor = null,
            bool includeNeutralLanguage = false,
            IEnumerable <ContentReference> rootReferences = null,
            IEnumerable <Type> typeFilter  = null,
            IPrincipal principleReadFilter = null) where T : class, IContent
        {
            var resolvedDescriptor = searchDescriptor == null ? new SearchDescriptor <T>() : searchDescriptor.Invoke(new SearchDescriptor <T>());

            typeFilter         = typeFilter ?? typeof(T).GetSearchTypesFor(VulcanFieldConstants.AbstractFilter);
            resolvedDescriptor = resolvedDescriptor.Type(string.Join(",", typeFilter.Select(t => t.FullName)))
                                 .ConcreteTypeSelector((d, docType) => typeof(VulcanContentHit));

            var indexName = IndexName;

            if (!Language.Equals(CultureInfo.InvariantCulture) && includeNeutralLanguage)
            {
                indexName += "," + VulcanHelper.GetAliasName(VulcanHandler.Index, CultureInfo.InvariantCulture, IndexAlias);
            }

            resolvedDescriptor = resolvedDescriptor.Index(indexName);
            var validRootReferences = rootReferences?.Where(x => !ContentReference.IsNullOrEmpty(x)).ToList();
            var filters             = new List <QueryContainer>();

            if (validRootReferences?.Count > 0)
            {
                var scopeDescriptor = new QueryContainerDescriptor <T>().
                                      Terms(t => t.Field(VulcanFieldConstants.Ancestors).Terms(validRootReferences.Select(x => x.ToReferenceWithoutVersion().ToString())));

                filters.Add(scopeDescriptor);
            }

            if (principleReadFilter != null)
            {
                var permissionDescriptor = new QueryContainerDescriptor <T>().
                                           Terms(t => t.Field(VulcanFieldConstants.ReadPermission).Terms(principleReadFilter.GetRoles()));

                filters.Add(permissionDescriptor);
            }

            if (filters.Count > 0)
            {
                var descriptor = resolvedDescriptor;
                Func <SearchDescriptor <T>, ISearchRequest> selector = ts => descriptor;
                var container = selector.Invoke(new SearchDescriptor <T>());

                if (container.Query != null)
                {
                    filters.Insert(0, container.Query);
                }

                resolvedDescriptor = resolvedDescriptor.Query(q => q.Bool(b => b.Must(filters.ToArray())));
            }

            var response = Search <T, IContent>(resolvedDescriptor);

            return(response);
        }
 public static IPermissions GetPermissionsForSection(this IPrincipal user, string sectionName)
 {
     return(new SitePermissions(sectionName, user.GetRoles()));
 }