Require users to log in to access a specific topic page

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 năm cách đây
Hi,

We need a topic page that requires users to log in before they can access it.
So we created a topic page and set the "Customer roles" to "Registered" so only logged in users can access that page. I first disabled "Ignore ACl rules (sitewide)" to make this work via Admin > Settings > Catalog Settings


However, when logged out and you try to access that topic page, you're automatically redirected to the homepage.
Is this by design? I was expecting to be instead redirected to the login page which then has the returnurl set as the topic page. Then when you log in, you get redirected to the topic page.

By the way, we're using nopCommerce 3.9.
5 năm cách đây
Hello,

here is the code you should modify with v3.90

1) below highlighted code you should modify in Nop.web/TopicController.cs


 [NopHttpsRequirement(SslRequirement.No)]
        public virtual ActionResult TopicDetails(int topicId)
        {
            var model = _topicModelFactory.PrepareTopicModelById(topicId);

            //custom code by sk5202
            var withoutacl = _topicModelFactory.PrepareCustomTopicModelById(topicId, false);
            if(model == null && withoutacl != null)
            {
                if(!string.IsNullOrEmpty(withoutacl.SeName))
                    return RedirectToRoute("Login", new { returnUrl = Url.RouteUrl("Topic", new { SeName = withoutacl.SeName }) });
            }

                
            if (model == null)
                return RedirectToRoute("HomePage");

            //display "edit" (manage) link
            if (_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && _permissionService.Authorize(StandardPermissionProvider.ManageTopics))
                DisplayEditLink(Url.Action("Edit", "Topic", new { id = model.Id, area = "Admin" }));

            //template
            var templateViewPath = _topicModelFactory.PrepareTemplateViewPath(model.TopicTemplateId);
            return View(templateViewPath, model);
        }


2) below method you should put at Nop.web/Factories/TopicModelFactories.cs



/// <summary>
        /// Get the topic model by topic identifier
        /// </summary>
        /// <param name="topicId">Topic identifier</param>
        /// <param name="isAcl">check acl Permission</param>
        /// <returns>Topic model</returns>
        public virtual TopicModel PrepareCustomTopicModelById(int topicId,bool isAcl = true)
        {
          
                var topic = _topicService.GetTopicById(topicId);
                if (topic == null)
                    return null;
                if (!topic.Published)
                    return null;
                //Store mapping
                if (!_storeMappingService.Authorize(topic))
                    return null;
                //ACL (access control list)

            if(isAcl)
            {
                if (!_aclService.Authorize(topic))
                    return null;
            }
              
                var topicPage =  PrepareTopicModel(topic);
          

            return topicPage;
        }



3). below Interface you should put at now.web/Factories/ITopicModelFactories.cs


 /// <summary>
        /// Get the topic model by topic identifier
        /// </summary>
        /// <param name="topicId">Topic identifier</param>
        /// <param name="isAcl">check acl Permission</param>
        /// <returns>Topic model</returns>
        TopicModel PrepareCustomTopicModelById(int topicId, bool isAcl = true);




hope it would solved your problem.
5 năm cách đây
Thank you for the reply sk5202.

So is it by design that it was made that way?
5 năm cách đây
Hello,

Yes you have to display that topic page link at anywhere at that time you should use above logic.

After,when click than it's working fine with my code.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.