public WebsiteAccessPermissionProvider(
     Entity website,
     IContentMapProvider contentMapProvider)
     : base(contentMapProvider)
 {
     _website = website;
 }
Esempio n. 2
0
        public FetchXmlIndexDocumentFactory(ICrmEntityIndex index, FetchXml fetchXml, string titleAttributeLogicalName, FetchXmlLocaleConfig localeConfig)
        {
            if (index == null)
            {
                throw new ArgumentNullException("index");
            }

            if (fetchXml == null)
            {
                throw new ArgumentNullException("fetchXml");
            }

            if (titleAttributeLogicalName == null)
            {
                throw new ArgumentNullException("titleAttributeLogicalName");
            }

            if (localeConfig == null)
            {
                throw new ArgumentNullException("localeConfig");
            }

            _index = index;
            _contentMapProvider        = AdxstudioCrmConfigurationManager.CreateContentMapProvider();
            _fetchXml                  = fetchXml;
            _titleAttributeLogicalName = titleAttributeLogicalName;
            _localeConfig              = localeConfig;

            _dataContext = _index.DataContext;
        }
        /// <summary>
        /// Checks if the site maker has a url defined.
        /// </summary>
        /// <param name="contentMapProvider">
        /// The content map provider.
        /// </param>
        /// <param name="siteMakerName">
        /// The site maker name.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> if the site marker has a url defined.
        /// </returns>
        public static bool IsSiteMakerUrlDefined(IContentMapProvider contentMapProvider, string siteMakerName)
        {
            if (string.IsNullOrEmpty(siteMakerName))
            {
                return(false);
            }

            return(contentMapProvider.Using(
                       contentMap =>
            {
                IDictionary <EntityReference, EntityNode> lookup;
                if (!contentMap.TryGetValue("adx_sitemarker", out lookup))
                {
                    return false;
                }

                var siteMarker = lookup.Values.Cast <SiteMarkerNode>().FirstOrDefault(x => x.Name == siteMakerName);

                if (siteMarker == null)
                {
                    return false;
                }

                var webpage = siteMarker.WebPage;
                if (webpage == null)
                {
                    return false;
                }

                return IsWebPageUrlDefined(webpage);
            }));
        }
 /// <summary>
 /// Gets guids of root localized web pages for the given web page.
 /// </summary>
 /// <param name="contentMapProvider">
 /// The content map provider.
 /// </param>
 /// <param name="entityId">
 /// The webpage Id.
 /// </param>
 /// <returns>
 /// The <see cref="IEnumerable"/> of the web page GUIDs for descendant web pages.
 /// </returns>
 public static IEnumerable <Guid> GetDescendantRootWebpagesForWebpage(IContentMapProvider contentMapProvider, Guid entityId)
 {
     return(contentMapProvider.Using(contentMap => SelectAllDescendantWebpagesWithPredicates(entityId, contentMap, new List <Predicate <WebPageNode> >()
     {
         IsRootWebPage
     })));
 }
Esempio n. 5
0
 public ContentMapUncachedProvider(IContentMapProvider contentMapProvider)
     : base(
         new WebPageAccessControlSecurityProvider(contentMapProvider),
         new PublishedDatesAccessProvider(contentMapProvider),
         new PublishingStateAccessProvider(contentMapProvider))
 {
     _contentMapProvider = contentMapProvider;
 }
 public WebsiteAccessPermissionProvider(
     Entity website,
     CacheSupportingCrmEntitySecurityProvider webPageAccessControlSecurityProvider,
     IContentMapProvider contentMapProvider)
     : base(contentMapProvider)
 {
     _webPageAccessControlSecurityProvider = webPageAccessControlSecurityProvider as WebPageAccessControlSecurityProvider;
     _website = website;
 }
Esempio n. 7
0
        public IdentityErrorDescriber(IContentMapProvider contentMapProvider, ContextLanguageInfo language)
        {
            if (contentMapProvider == null)
            {
                throw new ArgumentNullException("contentMapProvider");
            }

            this.ContentMapProvider = contentMapProvider;
            this.Language           = language;
        }
        public ContentMapFileSystem(IContentMapProvider contentMapProvider, IContentMapEntityUrlProvider contentMapUrlProvider, IDataAdapterDependencies dependencies) : base(dependencies)
        {
            if (contentMapProvider == null)
            {
                throw new ArgumentNullException("contentMapProvider");
            }
            if (contentMapUrlProvider == null)
            {
                throw new ArgumentNullException("contentMapUrlProvider");
            }

            ContentMapProvider    = contentMapProvider;
            ContentMapUrlProvider = contentMapUrlProvider;
        }
Esempio n. 9
0
        /// <summary> Initializes a new instance of the <see cref="ContentMapDataAdapter"/> class. </summary>
        /// <param name="contentMapProvider"> The content map provider. </param>
        /// <param name="language"> The language. </param>
        /// <param name="dependencies"> The dependencies. </param>
        protected ContentMapDataAdapter(IContentMapProvider contentMapProvider, ContextLanguageInfo language, IDataAdapterDependencies dependencies)
        {
            if (contentMapProvider == null)
            {
                throw new ArgumentNullException("contentMapProvider");
            }
            if (dependencies == null)
            {
                throw new ArgumentNullException("dependencies");
            }

            this.ContentMapProvider = contentMapProvider;
            this.Dependencies       = dependencies;
            this.Language           = language;
        }
        /// <summary>
        /// Checks if the web page url defined.
        /// </summary>
        /// <param name="contentMapProvider">
        /// The content map provider.
        /// </param>
        /// <param name="entityId">
        /// The entity id.
        /// </param>
        /// <param name="additionalPartialUrl">
        /// OPTIONAL: additional Partial Url to be added to the webpages url.
        /// </param>
        /// <returns>
        /// If the webpage has a URL defined or not.
        /// </returns>
        public static bool IsWebPageUrlDefined(IContentMapProvider contentMapProvider, Guid entityId, string additionalPartialUrl = null)
        {
            return(contentMapProvider.Using(
                       delegate(ContentMap contentMap)
            {
                EntityNode entity;
                if (!contentMap.TryGetValue(new EntityReference("adx_webpage", entityId), out entity))
                {
                    ADXTrace.Instance.TraceWarning(TraceCategory.Monitoring, string.Format("Web Page url is not defined. EntityId: {0}", entityId));
                    return false;
                }

                var webPage = entity as WebPageNode;

                return IsWebPageUrlDefined(webPage, additionalPartialUrl);
            }));
        }
        /// <summary>
        /// Gets guids of descendant localized web pages for the given web page.
        /// </summary>
        /// <param name="contentMapProvider">
        /// The content map provider.
        /// </param>
        /// <param name="entityId">
        /// The webpage Id.
        /// </param>
        /// <param name="lcid">
        /// The lcid for the page if it's a content page.
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable"/> of the web page GUIDs for descendant web pages.
        /// </returns>
        public static IEnumerable <Guid> GetDescendantLocalizedWebpagesForWebpage(IContentMapProvider contentMapProvider, Guid entityId, int?lcid = null)
        {
            var predicates = new List <Predicate <WebPageNode> >()
            {
                IsContentPage
            };

            if (lcid.HasValue)
            {
                Predicate <WebPageNode> isLocalized = new Predicate <WebPageNode>((WebPageNode webPageNode) =>
                {
                    return(webPageNode.WebPageLanguage.PortalLanguage.Lcid == lcid.Value);
                });
                predicates.Add(isLocalized);
            }

            return(contentMapProvider.Using(contentMap => SelectAllDescendantWebpagesWithPredicates(entityId, contentMap, predicates)));
        }
        /// <summary>
        /// Checks if the forum url is defined.
        /// </summary>
        /// <param name="contentMapProvider">
        /// The content map provider.
        /// </param>
        /// <param name="entityId">
        /// The entity id.
        /// </param>
        /// <returns>
        /// <see cref="bool"/>If the forum has a URL defined or not.
        /// </returns>
        public static bool IsForumUrlDefined(IContentMapProvider contentMapProvider, Guid entityId)
        {
            return(contentMapProvider.Using(
                       contentMap =>
            {
                EntityNode entity;
                if (!contentMap.TryGetValue(new EntityReference("adx_communityforum", entityId), out entity))
                {
                    return false;
                }

                var forum = entity as ForumNode;

                if (forum == null)
                {
                    return false;
                }

                var partialUrl = forum.PartialUrl;

                return IsWebPageUrlDefined(forum.ParentPage, partialUrl);
            }));
        }
Esempio n. 13
0
        /// <summary>
        /// Setup Culture for localization and globalization of the application.
        /// </summary>
        /// <param name="website"></param>
        /// <param name="legacyWebsiteLcid">CrmWebsite.Language value, for backwards compatibility with legacy pre-Multi-Language CRM environments.</param>
        /// <param name="bundles"></param>
        /// <param name="registerAction"></param>
        private static void UseLocalizedBundles(Entity website, int legacyWebsiteLcid, IContentMapProvider contentMapProvider, BundleCollection bundles, Action <BundleCollection, CultureInfo> registerAction)
        {
            // For backward compatibility with pre-Multi-Language CRM environments.
            // At this point in code execution, PortalContext is not available yet, so check for existance of adx_defaultlanguage field to determine if this is legacy environment or not.
            if (!ContextLanguageInfo.IsCrmMultiLanguageEnabledInWebsite(website))
            {
                if (legacyWebsiteLcid != 0)
                {
                    CultureInfo culture = new CultureInfo(legacyWebsiteLcid);
                    CultureInfo.DefaultThreadCurrentCulture   = culture;
                    CultureInfo.DefaultThreadCurrentUICulture = culture;
                    registerAction(bundles, culture);
                }
                return;
            }

            // Note: for environments WITH multi-language, the CurrentCulture will be set by ContextLanguageInfo.BuildContextLanguageInfo(...)

            // Not able to use portal language context so getting language directly
            // (portal context depends on owin context which is accesible only per request, not on startup)

            WebsiteNode websiteNode = null;

            contentMapProvider.Using(contentMap => contentMap.TryGetValue(website, out websiteNode));

            if (websiteNode == null)
            {
                registerAction(bundles, CultureInfo.CurrentCulture);
            }

            foreach (var websiteLanguage in websiteNode.WebsiteLanguages)
            {
                var portalLanguage = websiteLanguage.PortalLanguage;
                var culture        = ContextLanguageInfo.GetCulture(portalLanguage.CrmLanguage ?? 0);

                registerAction(bundles, culture);
            }
        }
 internal ContentMapEntityUrlProvider(IEntityWebsiteProvider websiteProvider, IContentMapProvider contentMapProvider, string portalName = null)
     : base(websiteProvider)
 {
     _contentMapProvider = contentMapProvider;
     PortalName          = portalName;
 }
Esempio n. 15
0
 /// <summary>
 /// Setup Culture for localization and globalization of the application.
 /// </summary>
 /// <param name="app">The app.</param>
 /// <param name="website">The website record.</param>
 public static void ConfigureCulture(this IAppBuilder app, CrmWebsite website, IContentMapProvider contentMapProvider, BundleCollection bundles, Action <BundleCollection, CultureInfo> registerAction)
 {
     UseLocalizedBundles(website.Entity, website.Language, contentMapProvider, bundles, registerAction);
 }
Esempio n. 16
0
        public void UpdateCmsEntityTree(string entityLogicalName, Guid rootEntityId, int?lcid = null)
        {
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Begin updating Cms Entity Tree for logical name: {0}, rootEntityId: {1}", entityLogicalName, rootEntityId));
            var timer = Stopwatch.StartNew();

            if (entityLogicalName == "adx_webpage")
            {
                IContentMapProvider contentMapProvider  = AdxstudioCrmConfigurationManager.CreateContentMapProvider();
                Guid[] descendantLocalizedWebPagesGuids = CmsIndexHelper.GetDescendantLocalizedWebpagesForWebpage(contentMapProvider, rootEntityId, lcid).ToArray();
                Guid[] descendantRootWebPagesGuids      = CmsIndexHelper.GetDescendantRootWebpagesForWebpage(contentMapProvider, rootEntityId).ToArray();

                // -------------------- WEB PAGES ------------------------------
                if (descendantLocalizedWebPagesGuids.Any())
                {
                    var localizedWebPagesUnderTargetWebPageFilter = new Fetch.Filter
                    {
                        Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.Or,
                        Conditions = new List <Fetch.Condition>()
                        {
                            new Fetch.Condition
                            {
                                Attribute = "adx_webpageid",
                                Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.In,
                                Values    = descendantLocalizedWebPagesGuids.Cast <object>().ToList()
                            },
                        }
                    };

                    var webPageIndexers = _index.GetIndexers("adx_webpage", filters: new List <Fetch.Filter> {
                        localizedWebPagesUnderTargetWebPageFilter
                    });

                    UpdateWithIndexers("adx_webpage", webPageIndexers);
                }

                // -------------------- FORUMS ------------------------------
                if (descendantRootWebPagesGuids.Any())
                {
                    var rootWebPagesUnderTargetWebPageFilter = new Fetch.Filter
                    {
                        Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.Or,
                        Conditions = new List <Fetch.Condition>()
                        {
                            new Fetch.Condition
                            {
                                Attribute = "adx_webpageid",
                                Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.In,
                                Values    = descendantRootWebPagesGuids.Cast <object>().ToList()
                            },
                        }
                    };

                    var forumBlogToParentPageLink = new Fetch.Link
                    {
                        Name          = "adx_webpage",
                        FromAttribute = "adx_webpageid",
                        ToAttribute   = "adx_parentpageid",
                        Filters       = new List <Fetch.Filter>()
                        {
                            rootWebPagesUnderTargetWebPageFilter
                        }
                    };

                    Fetch.Link languageFilter = null;

                    if (lcid.HasValue)
                    {
                        languageFilter = new Fetch.Link
                        {
                            Name          = "adx_websitelanguage",
                            FromAttribute = "adx_websitelanguageid",
                            ToAttribute   = "adx_websitelanguageid",
                            Type          = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                            Alias         = "websitelangforupdatefilter",
                            Links         = new List <Fetch.Link>()
                            {
                                new Fetch.Link
                                {
                                    Name          = "adx_portallanguage",
                                    FromAttribute = "adx_portallanguageid",
                                    ToAttribute   = "adx_portallanguageid",
                                    Type          = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                                    Alias         = "portallangforupdatefilter",
                                    Filters       = new List <Fetch.Filter>()
                                    {
                                        new Fetch.Filter
                                        {
                                            Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.And,
                                            Conditions = new List <Fetch.Condition>()
                                            {
                                                new Fetch.Condition
                                                {
                                                    Attribute = "adx_lcid",
                                                    Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,
                                                    Value     = lcid.Value
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        };
                    }

                    var forumBlogLinks = new List <Fetch.Link>()
                    {
                        forumBlogToParentPageLink
                    };
                    if (languageFilter != null)
                    {
                        forumBlogLinks.Add(languageFilter);
                    }

                    var forumIndexers = _index.GetIndexers("adx_communityforum", links: forumBlogLinks);
                    UpdateWithIndexers("adx_communityforum", forumIndexers);

                    var forumThreadForumLinks = new List <Fetch.Link>()
                    {
                        forumBlogToParentPageLink
                    };
                    if (languageFilter != null)
                    {
                        forumThreadForumLinks.Add(languageFilter);
                    }

                    var forumThreadToParentPageLink = new Fetch.Link
                    {
                        Name          = "adx_communityforum",
                        FromAttribute = "adx_communityforumid",
                        ToAttribute   = "adx_forumid",
                        Links         = forumThreadForumLinks
                    };

                    var forumThreadIndexers = _index.GetIndexers("adx_communityforumthread",
                                                                 links: new List <Fetch.Link>()
                    {
                        forumThreadToParentPageLink
                    });
                    UpdateWithIndexers("adx_communityforumthread", forumThreadIndexers);

                    var forumPostToParentPageLink = new Fetch.Link
                    {
                        Name          = "adx_communityforumthread",
                        FromAttribute = "adx_communityforumthreadid",
                        ToAttribute   = "adx_forumthreadid",
                        Alias         = "adx_communityforumpost_communityforumthread",
                        Links         = new List <Fetch.Link>()
                        {
                            forumThreadToParentPageLink
                        }
                    };

                    var forumPostIndexers = _index.GetIndexers("adx_communityforumpost",
                                                               links: new List <Fetch.Link>()
                    {
                        forumPostToParentPageLink
                    });
                    UpdateWithIndexers("adx_communityforumpost", forumPostIndexers);

                    // -------------------- BLOGS ------------------------------
                    var blogIndexers = _index.GetIndexers("adx_blog", links: forumBlogLinks);
                    UpdateWithIndexers("adx_blog", blogIndexers);

                    var blogPostBlogLinks = new List <Fetch.Link>()
                    {
                        forumBlogToParentPageLink
                    };
                    if (languageFilter != null)
                    {
                        blogPostBlogLinks.Add(languageFilter);
                    }

                    var blogPostParentPageLink = new Fetch.Link
                    {
                        Name          = "adx_blog",
                        FromAttribute = "adx_blogid",
                        ToAttribute   = "adx_blogid",
                        Alias         = "adx_blog_blogpost",
                        Links         = blogPostBlogLinks
                    };

                    var blogPostIndexers = _index.GetIndexers("adx_blogpost", links: new List <Fetch.Link> {
                        blogPostParentPageLink
                    });
                    UpdateWithIndexers("adx_blogpost", blogPostIndexers);
                }
            }
            else if (entityLogicalName == "adx_communityforum")
            {
                UpdateEntity("adx_communityforum", rootEntityId);

                var inForumFilterForThread = new Fetch.Filter
                {
                    Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.And,
                    Conditions = new List <Fetch.Condition>()
                    {
                        new Fetch.Condition
                        {
                            Attribute = "adx_forumid",
                            Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,
                            Value     = rootEntityId
                        }
                    }
                };

                var forumThreadIndexers = _index.GetIndexers("adx_communityforumthread", filters: new List <Fetch.Filter> {
                    inForumFilterForThread
                });
                UpdateWithIndexers("adx_communityforumthread", forumThreadIndexers);

                var inForumFilterForPost = new Fetch.Link
                {
                    Name          = "adx_communityforumthread",
                    FromAttribute = "adx_communityforumthreadid",
                    ToAttribute   = "adx_forumthreadid",
                    Alias         = "adx_communityforumpost_communityforumthread",
                    Filters       = new List <Fetch.Filter>()
                    {
                        inForumFilterForThread
                    }
                };

                var forumPostIndexers = _index.GetIndexers("adx_communityforumpost", links: new List <Fetch.Link> {
                    inForumFilterForPost
                });
                UpdateWithIndexers("adx_communityforumpost", forumPostIndexers);
            }
            else if (entityLogicalName == "adx_ideaforum")
            {
                UpdateEntity("adx_ideaforum", rootEntityId);

                var inIdeaForumFilter = new Fetch.Filter
                {
                    Type       = Microsoft.Xrm.Sdk.Query.LogicalOperator.And,
                    Conditions = new List <Fetch.Condition>()
                    {
                        new Fetch.Condition
                        {
                            Attribute = "adx_ideaforumid",
                            Operator  = Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,
                            Value     = rootEntityId
                        }
                    }
                };

                var ideaIndexers = _index.GetIndexers("adx_idea", filters: new List <Fetch.Filter> {
                    inIdeaForumFilter
                });
                UpdateWithIndexers("adx_idea", ideaIndexers);
            }

            timer.Stop();
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Cms Entity Tree updated for logical name: {0}, rootEntityId: {1}, timespan: {2}", entityLogicalName, rootEntityId, timer.ElapsedMilliseconds));
        }
Esempio n. 17
0
        private SearchIndexBuildRequest.SearchIndexInvalidationData GetSearchIndexInvalidationData(OrganizationServiceCachePluginMessage message)
        {
            if (message.Target == null || string.IsNullOrEmpty(message.Target.LogicalName) || message.Target.Id == Guid.Empty)
            {
                return(null);
            }

            var searchIndexInvalidationData = new SearchIndexBuildRequest.SearchIndexInvalidationData();

            IContentMapProvider contentMapProvider = AdxstudioCrmConfigurationManager.CreateContentMapProvider();

            contentMapProvider.Using(contentMap =>
            {
                if (message.Target.LogicalName == "adx_webpage")
                {
                    WebPageNode webPageNode;

                    if (contentMap.TryGetValue(new EntityReference(message.Target.LogicalName, message.Target.Id), out webPageNode))
                    {
                        searchIndexInvalidationData.PartialUrl = webPageNode.PartialUrl;

                        if (webPageNode.Parent != null)
                        {
                            searchIndexInvalidationData.ParentPage = new EntityReference("adx_webpage", webPageNode.Parent.Id);
                        }

                        if (webPageNode.Website != null)
                        {
                            searchIndexInvalidationData.Website = new EntityReference("adx_website", webPageNode.Website.Id);
                        }

                        if (webPageNode.PublishingState != null)
                        {
                            searchIndexInvalidationData.PublishingState = new EntityReference("adx_publishingstate", webPageNode.PublishingState.Id);
                        }
                    }
                }

                if (message.Target.LogicalName == "adx_webpageaccesscontrolrule_webrole")
                {
                    WebPageAccessControlRuleToWebRoleNode webAccessControlToWebRoleNode;

                    if (contentMap.TryGetValue(new EntityReference(message.Target.LogicalName, message.Target.Id), out webAccessControlToWebRoleNode))
                    {
                        WebPageAccessControlRuleNode webAccessControlNode = webAccessControlToWebRoleNode.WebPageAccessControlRule;
                        if (webAccessControlNode != null && webAccessControlNode.WebPage != null)
                        {
                            searchIndexInvalidationData.WebPage = new EntityReference("adx_webpage", webAccessControlNode.WebPage.Id);
                        }
                    }
                }

                if (message.Target.LogicalName == "adx_webpageaccesscontrolrule")
                {
                    WebPageAccessControlRuleNode webAccessControlNode;

                    if (contentMap.TryGetValue(new EntityReference(message.Target.LogicalName, message.Target.Id), out webAccessControlNode))
                    {
                        if (webAccessControlNode.WebPage != null)
                        {
                            searchIndexInvalidationData.WebPage = new EntityReference("adx_webpage", webAccessControlNode.WebPage.Id);
                        }
                    }
                }

                if (message.Target.LogicalName == "adx_communityforumaccesspermission")
                {
                    ForumAccessPermissionNode forumAccessNode;

                    if (contentMap.TryGetValue(new EntityReference(message.Target.LogicalName, message.Target.Id), out forumAccessNode))
                    {
                        if (forumAccessNode.Forum != null)
                        {
                            searchIndexInvalidationData.Forum = new EntityReference("adx_communityforum", forumAccessNode.Forum.Id);
                        }
                    }
                }
            });

            return(searchIndexInvalidationData);
        }
 /// <summary>
 /// Gets the given forums web roles.
 /// </summary>
 /// <param name="contentMapProvider">
 /// The content map provider.
 /// </param>
 /// <param name="forumid">
 /// The forumid.
 /// </param>
 /// <returns>
 /// The <see cref="IEnumerable"/> of web roles associated to the forums.
 /// </returns>
 public static IEnumerable <string> GetForumsWebRoles(IContentMapProvider contentMapProvider, Guid forumid)
 {
     return(contentMapProvider.Using(contentMap => SelectAllForumsWebRoles(forumid, contentMap)));
 }
 /// <summary> Initializes a new instance of the <see cref="ContentMapAccessProvider"/> class. </summary>
 /// <param name="contentMapProvider"> The content map provider. </param>
 protected ContentMapAccessProvider(IContentMapProvider contentMapProvider)
 {
     this.ContentMapProvider = contentMapProvider ?? AdxstudioCrmConfigurationManager.CreateContentMapProvider();
 }
 public ContentMapEntityWebsiteProvider(IContentMapProvider contentMapProvider)
 {
     _contentMapProvider = contentMapProvider;
 }
 public ContentMapPortalRouteHandlerProvider(string portalName, IContentMapProvider provder)
     : base(portalName)
 {
     _contentMapProvider = provder;
 }
 /// <summary>
 /// Gets the given web pages web roles.
 /// </summary>
 /// <param name="contentMapProvider">
 /// The content map provider.
 /// </param>
 /// <param name="entityId">
 /// The webpage Id.
 /// </param>
 /// <returns>
 /// The <see cref="IEnumerable"/> of the associated webroles name for the given Webpage.
 /// </returns>
 public static IEnumerable <string> GetWebPageWebRoles(IContentMapProvider contentMapProvider, Guid entityId)
 {
     return(contentMapProvider.Using(contentMap => SelectAllWebRolesForWebpage(entityId, contentMap)));
 }
 public PublishedDatesAccessProvider(IContentMapProvider contentMapProvider)
     : base(contentMapProvider)
 {
     _contentMapProvider = contentMapProvider;
 }
 public PublishingStateAccessProvider(IContentMapProvider contentMapProvider)
     : base(contentMapProvider)
 {
 }
 /// <summary> Initializes a new instance of the <see cref="BlogSecurityProvider"/> class. </summary>
 /// <param name="contentMapProvider"> The content map provider. </param>
 public BlogSecurityProvider(IContentMapProvider contentMapProvider)
     : base(contentMapProvider)
 {
 }
        private static IEnumerable <Tuple <string, string, int> > ContentStyles(IPortalViewContext portalViewContext, IEnumerable <EntityReference> path, IContentMapProvider contentMapProvider)
        {
            var urlProvider = PortalCrmConfigurationManager.CreateDependencyProvider(portalViewContext.PortalName).GetDependency <IContentMapEntityUrlProvider>();

            if (urlProvider == null)
            {
                return(Enumerable.Empty <Tuple <string, string, int> >());
            }

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Getting content styles using Content Map.");

            var cssWebFileNodes = contentMapProvider.Using(map => path.SelectMany((pathItem, pathOffset) =>
            {
                WebPageNode webPage;

                // Guard against null reference exception on webPage.WebFiles. This might happen if WebPage is a translated content, but for whatever reason has no root web page.
                if (!map.TryGetValue(pathItem, out webPage) || webPage.WebFiles == null)
                {
                    return(Enumerable.Empty <Tuple <int, int, string, string> >());
                }

                return(webPage.WebFiles
                       .Where(e => e.PartialUrl != null && e.PartialUrl.EndsWith(".css"))
                       .Select(e => new Tuple <int, int, string, string>(
                                   pathOffset,
                                   e.DisplayOrder.GetValueOrDefault(0),
                                   urlProvider.GetUrl(map, e),
                                   e.PartialUrl)));
            }));

            return(cssWebFileNodes
                   .Where(e => !string.IsNullOrEmpty(e.Item3))
                   .OrderByDescending(e => e.Item1)
                   .ThenBy(e => e.Item2)
                   .Select(e => new Tuple <string, string, int>(e.Item3, e.Item4, e.Item1))
                   .ToArray());
        }
 public ForumAccessPermissionProvider(IContentMapProvider contentMapProvider)
     : base(contentMapProvider)
 {
 }
 /// <summary> Initializes a new instance of the <see cref="WebPageAccessControlSecurityProvider"/> class. </summary>
 /// <param name="contentMapProvider"> The content map provider. </param>
 public WebPageAccessControlSecurityProvider(IContentMapProvider contentMapProvider)
     : base(contentMapProvider)
 {
 }
        /// <summary>
        /// Process the message from the HTTP POST request and rebuild the search index.
        /// </summary>
        /// <param name="message"></param>
        public static void ProcessMessage(OrganizationServiceCachePluginMessage message, SearchIndexInvalidationData searchIndexInvalidationData = null, OrganizationServiceContext serviceContext = null)
        {
            if (!SearchManager.Enabled)
            {
                ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Search isn't enabled for the current application.");

                return;
            }

            if (message == null)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, "Search Index build failure. Plugin Message is null.");

                return;
            }

            SearchProvider provider;
            var            forceBuild = message.Target != null && message.Target.LogicalName == "adx_website";

            if (!TryGetSearchProvider(out provider))
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, "Search Index build failure. Search Provider could not be found.");

                return;
            }

            if (forceBuild || BuildMessages.Contains(message.MessageName, MessageComparer))
            {
                PerformBuild(provider);

                ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Search Index was successfully built.");

                return;
            }

            IContentMapProvider contentMapProvider = AdxstudioCrmConfigurationManager.CreateContentMapProvider();

            if (UpdateMessages.Contains(message.MessageName, MessageComparer))
            {
                if (message.Target == null || string.IsNullOrEmpty(message.Target.LogicalName))
                {
                    throw new HttpException((int)HttpStatusCode.BadRequest, string.Format("Message {0} requires an EntityName (entity logical name) parameter.", message.MessageName));
                }

                if (message.Target == null || message.Target.Id == Guid.Empty)
                {
                    throw new HttpException((int)HttpStatusCode.BadRequest, string.Format("Message {0} requires an ID (entity ID) parameter.", message.MessageName));
                }

                if (FeatureCheckHelper.IsFeatureEnabled(FeatureNames.CmsEnabledSearching))
                {
                    if (message.Target.LogicalName == "savedquery")
                    {
                        var cachedSavedQuery = SearchMetadataCache.Instance.SearchSavedQueries.FirstOrDefault(x => x.SavedQueryId == message.Target.Id);
                        if (cachedSavedQuery != null)
                        {
                            // SavedQueryUniqueId is timestamp to verify which change was published and need to invalidate Search Index
                            var actualSavedQueryUniqueId = SearchMetadataCache.Instance.GetSavedQueryUniqueId(message.Target.Id);
                            if (cachedSavedQuery.SavedQueryIdUnique != actualSavedQueryUniqueId)
                            {
                                PerformUpdateAsync(provider, updater => updater.UpdateEntitySet(cachedSavedQuery.EntityName));
                                SearchMetadataCache.Instance.CompleteMetadataUpdateForSearchSavedQuery(cachedSavedQuery, actualSavedQueryUniqueId);
                            }
                        }
                    }

                    if (message.Target.LogicalName == "adx_webpage" && searchIndexInvalidationData != null)
                    {
                        // bypass cache and go straight to CRM in case cache hasn't been updated yet
                        var response = serviceContext.Execute(new RetrieveRequest {
                            Target    = new EntityReference(message.Target.LogicalName, message.Target.Id),
                            ColumnSet = new ColumnSet(new string[] {
                                "adx_parentpageid",
                                "adx_websiteid",
                                "adx_publishingstateid",
                                "adx_partialurl"
                            })
                        }) as RetrieveResponse;

                        if (response != null && response.Entity != null)
                        {
                            var updatedWebPage = response.Entity;

                            // If the parent page or website change, we need to invalidate that whole section of the web page hierarchy since the web roles
                            // may change, including both root and content pages if MLP is enabled.
                            if (!EntityReferenceEquals(searchIndexInvalidationData.ParentPage, updatedWebPage.GetAttributeValue <EntityReference>("adx_parentpageid")) ||
                                !EntityReferenceEquals(searchIndexInvalidationData.Website, updatedWebPage.GetAttributeValue <EntityReference>("adx_websiteid")))
                            {
                                PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree("adx_webpage", message.Target.Id));
                            }
                            // If the publishing state or partial URL change, this will effect all the content pages and localized entities underneath them
                            // if MLP is enabled. If MLP is disabled, LCID will equal null, and then just all pages/entities underneath this will reindex.
                            else if (!EntityReferenceEquals(searchIndexInvalidationData.PublishingState, updatedWebPage.GetAttributeValue <EntityReference>("adx_publishingstateid")) ||
                                     searchIndexInvalidationData.PartialUrl != updatedWebPage.GetAttributeValue <string>("adx_partialurl"))
                            {
                                PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree("adx_webpage", message.Target.Id, searchIndexInvalidationData.Lcid));
                            }
                        }
                    }

                    if (message.Target.LogicalName == "adx_webpageaccesscontrolrule_webrole")
                    {
                        contentMapProvider.Using(contentMap =>
                        {
                            WebPageAccessControlRuleToWebRoleNode webAccessControlToWebRoleNode;

                            if (contentMap.TryGetValue(new EntityReference(message.Target.LogicalName, message.Target.Id), out webAccessControlToWebRoleNode))
                            {
                                PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree("adx_webpage", webAccessControlToWebRoleNode.WebPageAccessControlRule.WebPage.Id));
                            }
                        });
                    }

                    if (message.Target.LogicalName == "adx_webpageaccesscontrolrule")
                    {
                        contentMapProvider.Using(contentMap =>
                        {
                            WebPageAccessControlRuleNode webAccessControlNode;

                            if (contentMap.TryGetValue(new EntityReference(message.Target.LogicalName, message.Target.Id), out webAccessControlNode))
                            {
                                PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree("adx_webpage", webAccessControlNode.WebPage.Id));
                            }
                        });
                    }

                    if (message.Target.LogicalName == "adx_communityforumaccesspermission")
                    {
                        contentMapProvider.Using(contentMap =>
                        {
                            ForumAccessPermissionNode forumAccessNode;

                            if (contentMap.TryGetValue(new EntityReference(message.Target.LogicalName, message.Target.Id), out forumAccessNode))
                            {
                                PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree("adx_communityforum", forumAccessNode.Forum.Id));
                            }
                        });
                    }

                    if (message.Target.LogicalName == "connection")
                    {
                        var fetch = new Fetch
                        {
                            Entity = new FetchEntity("connection")
                            {
                                Filters = new[] { new Filter {
                                                      Conditions = new List <Condition> {
                                                          new Condition("connectionid", ConditionOperator.Equal, message.Target.Id)
                                                      }
                                                  } }
                            }
                        };

                        var connectionEntity = ((RetrieveSingleResponse)serviceContext.Execute(fetch.ToRetrieveSingleRequest())).Entity;

                        var record1Id = connectionEntity.GetAttributeValue <EntityReference>("record1id");
                        var record2Id = connectionEntity.GetAttributeValue <EntityReference>("record2id");

                        // new product association to knowledge article could mean new product filtering rules
                        if (record1Id != null && record1Id.LogicalName == "knowledgearticle" &&
                            record2Id != null && record2Id.LogicalName == "product")
                        {
                            PerformUpdate(provider, updater => updater.UpdateEntity("knowledgearticle", record1Id.Id));
                        }
                    }
                    if (message.Target.LogicalName == "adx_contentaccesslevel")
                    {
                        var fetch = GetEntityFetch("adx_knowledgearticlecontentaccesslevel", "knowledgearticleid",
                                                   "adx_contentaccesslevelid", message.Target.Id.ToString());

                        var entities = FetchEntities(serviceContext, fetch);
                        var guids    = entities.Select(e => e.GetAttributeValue <Guid>("knowledgearticleid")).ToList();

                        PerformUpdateAsync(provider, updater => updater.UpdateEntitySet("knowledgearticle", "knowledgearticleid", guids));
                    }
                    if (message.Target.LogicalName == "product")
                    {
                        var fetch = GetEntityFetch("connection", "record2id", "record1id", message.Target.Id.ToString());

                        var entities = FetchEntities(serviceContext, fetch);
                        var guids    = entities.Select(e => e.GetAttributeValue <EntityReference>("record2id")).Select(g => g.Id).Distinct().ToList();

                        PerformUpdateAsync(provider, updater => updater.UpdateEntitySet("knowledgearticle", "knowledgearticleid", guids));
                    }
                    if (message.Target.LogicalName == "annotation")
                    {
                        var annotationFetch = new Fetch
                        {
                            Entity = new FetchEntity("annotation", new[] { "objectid" })
                            {
                                Filters = new[]
                                {
                                    new Filter
                                    {
                                        Conditions = new List <Condition>
                                        {
                                            new Condition("annotationid", ConditionOperator.Equal, message.Target.Id),
                                        }
                                    }
                                }
                            }
                        };


                        var response =
                            (RetrieveSingleResponse)serviceContext.Execute(annotationFetch.ToRetrieveSingleRequest());

                        if (response.Entity == null)
                        {
                            ADXTrace.Instance.TraceError(TraceCategory.Application, $"Retrieve of annotation entity failed for annotationId : {message.Target.Id}");
                            throw new TransientNullReferenceException($"Retrieve of annotation entity failed for annotationId : {message.Target.Id}");
                        }

                        var knowledgeArticle = response.Entity?.GetAttributeValue <EntityReference>("objectid");
                        if (knowledgeArticle == null)
                        {
                            ADXTrace.Instance.TraceError(TraceCategory.Application, $"Could not find objectId in the retrieved annotation with annotationId : {message.Target.Id}");
                            throw new TransientNullReferenceException($"Could not find objectId in the retrieved annotation with annotationId : {message.Target.Id}");
                        }

                        if (knowledgeArticle.Id != Guid.Empty && knowledgeArticle.LogicalName == "knowledgearticle")
                        {
                            //Updating Knowledge Article related to this Annotation
                            PerformUpdate(provider, updater => updater.UpdateEntity("knowledgearticle", knowledgeArticle.Id));
                        }
                    }
                    //Re-indexing annotations and related knowledge articles if NotesFilter gets changes
                    if (message.Target.LogicalName == "adx_sitesetting" && message.Target.Name == "KnowledgeManagement/NotesFilter")
                    {
                        var notes             = GetAllNotes(serviceContext);
                        var knowledgeArticles = notes.Select(n => n.GetAttributeValue <EntityReference>("objectid"))
                                                .Distinct().Select(ka => ka.Id).Distinct()
                                                .ToList();

                        PerformUpdateAsync(provider, updater => updater.UpdateEntitySet("annotation", "annotationid", notes.Select(n => n.Id).Distinct().ToList()));
                        PerformUpdateAsync(provider, updater => updater.UpdateEntitySet("knowledgearticle", "knowledgearticleid", knowledgeArticles));

                        return;
                    }
                    if (message.Target.LogicalName == "adx_sitesetting" && message.Target.Name == "KnowledgeManagement/DisplayNotes")
                    {
                        PerformUpdateAsync(provider, updater => updater.DeleteEntitySet("annotation"));

                        var notes             = GetAllNotes(serviceContext);
                        var knowledgeArticles = notes.Select(n => n.GetAttributeValue <EntityReference>("objectid"))
                                                .Distinct().Select(ka => ka.Id).Distinct()
                                                .ToList();

                        PerformUpdateAsync(provider, updater => updater.UpdateEntitySet("annotation", "annotationid", notes.Select(n => n.Id).Distinct().ToList()));
                        PerformUpdateAsync(provider, updater => updater.UpdateEntitySet("knowledgearticle", "knowledgearticleid", knowledgeArticles));
                        return;
                    }
                }

                PerformUpdate(provider, updater => updater.UpdateEntity(message.Target.LogicalName, message.Target.Id));

                ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Search Index was successfully updated. ({0}, {1}:{2})", message.MessageName, EntityNamePrivacy.GetEntityName(message.Target.LogicalName), message.Target.Id));

                return;
            }

            if (FeatureCheckHelper.IsFeatureEnabled(FeatureNames.CmsEnabledSearching))
            {
                if (AssociateDisassociateMessages.Contains(message.MessageName, MessageComparer))
                {
                    if (message.Target == null || string.IsNullOrEmpty(message.Target.LogicalName))
                    {
                        throw new HttpException((int)HttpStatusCode.BadRequest, string.Format("Message {0} requires an EntityName (entity logical name) parameter.", message.MessageName));
                    }

                    if (message.Target == null || message.Target.Id == Guid.Empty)
                    {
                        throw new HttpException((int)HttpStatusCode.BadRequest, string.Format("Message {0} requires an ID (entity ID) parameter.", message.MessageName));
                    }

                    if (message.RelatedEntities == null)
                    {
                        throw new HttpException((int)HttpStatusCode.BadRequest, string.Format("Message {0} requires an EntityName (entity logical name) parameter.", message.MessageName));
                    }

                    if ((message.Target.LogicalName == "adx_webpage" && HasRelatedEntityType(message, "adx_webpageaccesscontrolrule")) ||
                        (message.Target.LogicalName == "adx_communityforum" && HasRelatedEntityType(message, "adx_communityforumaccesspermission")) ||
                        (message.Target.LogicalName == "adx_ideaforum" && HasRelatedEntityType(message, "adx_webrole")))
                    {
                        PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree(message.Target.LogicalName, message.Target.Id));
                    }

                    if (message.Target.LogicalName == "adx_webpageaccesscontrolrule" &&
                        (HasRelatedEntityType(message, "adx_webrole") || HasRelatedEntityType(message, "adx_publishingstate")))
                    {
                        contentMapProvider.Using(contentMap =>
                        {
                            WebPageAccessControlRuleNode webAccessControlNode;

                            if (contentMap.TryGetValue(new EntityReference(message.Target.LogicalName, message.Target.Id), out webAccessControlNode))
                            {
                                PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree("adx_webpage", webAccessControlNode.WebPage.Id));
                            }
                        });
                    }

                    if (message.Target.LogicalName == "adx_communityforumaccesspermission" && HasRelatedEntityType(message, "adx_webrole"))
                    {
                        contentMapProvider.Using(contentMap =>
                        {
                            ForumAccessPermissionNode forumAccessNode;

                            if (contentMap.TryGetValue(new EntityReference(message.Target.LogicalName, message.Target.Id), out forumAccessNode))
                            {
                                PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree("adx_communityforum", forumAccessNode.Forum.Id));
                            }
                        });
                    }

                    if (message.Target.LogicalName == "adx_contentaccesslevel" && HasRelatedEntityType(message, "knowledgearticle"))
                    {
                        foreach (var entityReference in message.RelatedEntities)
                        {
                            if (entityReference.LogicalName == "knowledgearticle")
                            {
                                PerformUpdate(provider, updater => updater.UpdateEntity(entityReference.LogicalName, entityReference.Id));
                            }
                        }
                    }

                    //Perform update for disassociate messages from WebNotification Plugin
                    if (message.Target.LogicalName == "knowledgearticle" && (HasRelatedEntityType(message, "adx_contentaccesslevel")))
                    {
                        PerformUpdate(provider, updater => updater.UpdateEntity(message.Target.LogicalName, message.Target.Id));
                    }

                    ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Search Index was successfully updated. ({0}, {1}:{2})", message.MessageName, EntityNamePrivacy.GetEntityName(message.Target.LogicalName), message.Target.Id));

                    return;
                }
            }

            if (DeleteMessages.Contains(message.MessageName, MessageComparer))
            {
                if (message.Target == null || string.IsNullOrEmpty(message.Target.LogicalName))
                {
                    throw new HttpException((int)HttpStatusCode.BadRequest, string.Format("Message {0} requires an EntityName (entity logical name) parameter.", message.MessageName));
                }

                if (message.Target == null || message.Target.Id == Guid.Empty)
                {
                    throw new HttpException((int)HttpStatusCode.BadRequest, string.Format("Message {0} requires an ID (entity ID) parameter.", message.MessageName));
                }

                if (FeatureCheckHelper.IsFeatureEnabled(FeatureNames.CmsEnabledSearching))
                {
                    if (message.Target.LogicalName == "adx_webpageaccesscontrolrule" && searchIndexInvalidationData.WebPage != null)
                    {
                        PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree(searchIndexInvalidationData.WebPage.LogicalName, searchIndexInvalidationData.WebPage.Id));
                    }

                    if (message.Target.LogicalName == "adx_webpageaccesscontrolrule_webrole" && searchIndexInvalidationData.WebPage != null)
                    {
                        PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree(searchIndexInvalidationData.WebPage.LogicalName, searchIndexInvalidationData.WebPage.Id));
                    }

                    if (message.Target.LogicalName == "adx_communityforumaccesspermission" && searchIndexInvalidationData.Forum != null)
                    {
                        PerformUpdateAsync(provider, updater => updater.UpdateCmsEntityTree(searchIndexInvalidationData.Forum.LogicalName, searchIndexInvalidationData.Forum.Id));
                    }

                    if (message.Target.LogicalName == "connection")
                    {
                        // To update Knowledge Article that was related to this connection(Product) we need to retrieve KAid from index
                        var relatedEntityList = GetRelatedEntities("connectionid", message.Target.Id, 1);

                        if (!relatedEntityList.Any())
                        {
                            return;
                        }

                        // Taking first here since there can only be one Knowledge Article related to connection
                        var entity = relatedEntityList.First();
                        if (entity.LogicalName != null && entity.LogicalName.Equals("knowledgearticle"))
                        {
                            PerformUpdate(provider, updater => updater.UpdateEntity("knowledgearticle", entity.Id));
                        }
                        return;
                    }
                    if (message.Target.LogicalName == "product" || message.Target.LogicalName == "adx_contentaccesslevel")
                    {
                        IEnumerable <EntityReference> relatedKnowledgeArticles = new List <EntityReference>();
                        var indexedFieldName = "adx_contentaccesslevel";

                        if (message.Target.LogicalName == "product")
                        {
                            indexedFieldName = FixedFacetsConfiguration.ProductFieldFacetName;
                        }

                        relatedKnowledgeArticles = GetRelatedEntities(indexedFieldName, message.Target.Id, 10000);
                        if (!relatedKnowledgeArticles.Any())
                        {
                            return;
                        }

                        var knowledgeArticlesIds =
                            relatedKnowledgeArticles.Where(r => r.LogicalName.Equals("knowledgearticle")).Select(i => i.Id).ToList();

                        PerformUpdateAsync(provider, updater => updater.UpdateEntitySet("knowledgearticle", "knowledgearticleid", knowledgeArticlesIds));
                    }
                    if (message.Target.LogicalName == "annotation")
                    {
                        var relatedKnowledgeArticles = GetRelatedEntities("annotationid", message.Target.Id, 10);
                        var knowledgeArticleId       = relatedKnowledgeArticles.Where(a => a.LogicalName == "knowledgearticle").Select(ka => ka.Id).FirstOrDefault();

                        //Updating Knowledge Article related to this Annotation
                        PerformUpdate(provider, updater => updater.UpdateEntity("knowledgearticle", knowledgeArticleId));
                    }
                }

                PerformUpdate(provider, updater => updater.DeleteEntity(message.Target.LogicalName, message.Target.Id));

                ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Search Index was successfully updated. ({0}, {1}:{2})", message.MessageName, EntityNamePrivacy.GetEntityName(message.Target.LogicalName), message.Target.Id));

                return;
            }

            var supportedMessages = DeleteMessages.Union(BuildMessages.Union(UpdateMessages, MessageComparer), MessageComparer);

            ADXTrace.Instance.TraceError(TraceCategory.Application, string.Format(@"Search Index Build Failed. Message ""{0}"" is not supported. Valid messages are {1}.", message.MessageName, string.Join(", ", supportedMessages.ToArray())));
        }
 /// <summary>
 /// Gets idea forum web roles.
 /// </summary>
 /// <param name="contentMapProvider">
 /// The content map provider.
 /// </param>
 /// <param name="ideaForumId">
 /// The idea forum id.
 /// </param>
 /// <returns>
 /// The <see cref="IEnumerable"/> of web roles associated to the Idea Forum.
 /// </returns>
 public static IEnumerable <string> GetIdeaForumWebRoles(IContentMapProvider contentMapProvider, Guid ideaForumId)
 {
     return(contentMapProvider.Using(contentMap => SelectAllIdeaForumWebRoles(ideaForumId, contentMap)));
 }