Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ivan Ponomarenko 20 posts 105 karma points
    Oct 07, 2016 @ 08:11
    Ivan Ponomarenko
    1

    Secure searching - role based protection

    Hi community,

    I need implement role based protection for search results. I do it as in this blog-post http://thecogworks.co.uk/blog/2013/03/15/examiness-hints-and-tips-from-the-trenches-part-9-secure-searching but in my case I catch exception when call method umbraco.library.IsProtected(int DocumentId, string Path) where says:

    Parameter name: umbracoContext at Umbraco.Web.UmbracoHelper..ctor(UmbracoContext umbracoContext) at umbraco.library.IsProtected(Int32 DocumentId, String Path) at WebSite.Extensions.ExamineEvents.InjectGroups(IndexingNodeDataEventArgs e) in D:\projects\WebSite\Extensions\ExamineEvents.cs:line 34 2016-10-06 17:18:56,938 [P10464/D2/T8] ERROR WebSite.Extensions.ExamineEvents - Value cannot be null.

    There is my full code:

    public class ExamineEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ExamineManager.Instance.IndexProviderCollection["globalIndexer"].GatheringNodeData += ExamineEvents_GatheringNodeData;
        }
    
        private void ExamineEvents_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
        {
            InjectGroups(e);
        }
    
        private void InjectGroups(IndexingNodeDataEventArgs e)
        {
            try
            {
                var node = new Node(e.NodeId);
                if (umbraco.library.IsProtected(node.Id, node.Path))
                {
                    var groups = Access.GetAccessingMembershipRoles(node.Id, node.Path);
                    var groupsAccess = new StringBuilder();
                    foreach (var group in groups)
                    {
                        groupsAccess.Append(group);
                        groupsAccess.Append(" ");
                    }
                    e.Fields.Add("GroupAccess", groupsAccess.ToString().Trim());
                    e.Fields.Add("IsPublic", "false");
                }
                else
                {
                    e.Fields.Add("GroupAccess", "0");
                    e.Fields.Add("IsPublic", "true");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(GetType(), ex.Message, ex);
            }
        }
    }
    

    Any ideas how to resolve it? (Umbraco version 7.4.3)

    Regards, Ivan.

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Oct 07, 2016 @ 08:13
    Alex Skrypnyk
    101

    Hi Ivan,

    Interesting issue, did you try to use services?

    I think you have to use PublicAccessService, read more here -

    https://our.umbraco.org/apidocs/csharp/api/Umbraco.Core.Services.PublicAccessService.html

    Thanks,

    Alex

  • Ivan Ponomarenko 20 posts 105 karma points
    Oct 07, 2016 @ 08:41
    Ivan Ponomarenko
    0

    Hi Alex,

    Thanks for the reply.

    I did not know about this service. Now I replaced umbraco.library.IsProtected(node.Id, node.Path) method on ApplicationContext.Current.Services.PublicAccessService.IsProtected(node.Path)

    And it works, many thanks :)

    Regards, Ivan.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Oct 07, 2016 @ 08:50
    Ismail Mayat
    1

    Ivan,

    No need to new up new node you can get what you need from e.Fields will be quicker.

  • Ivan Ponomarenko 20 posts 105 karma points
    Oct 07, 2016 @ 09:02
    Ivan Ponomarenko
    2

    Yeah, now I do not create instance of Node, thanks.

    enter image description here

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Oct 07, 2016 @ 08:23
    Ismail Mayat
    1

    Ivan,

    I suspect the new Node is giving you null. The parameters that you need are in the e.Fields collection so you could them from there and therefore no need to new up new node? So

    e.Fields["path"] you already have node id e.NodeId

    Also as pointed out by Alex umbraco.library is old and deprecated.

    Regards

    Ismail

  • Ivan Ponomarenko 20 posts 105 karma points
    Oct 07, 2016 @ 08:55
    Ivan Ponomarenko
    1

    Hi Ismail,

    Filed "path" does not exist in Fields property, I was checked it.

    I think, on event ApplicationStarted - umbraco cant create instance of UmbracoHelper because Umbraco.Core.ApplicationContext.Current is null at this moment.

    Regards, Ivan.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Oct 07, 2016 @ 09:02
    Ismail Mayat
    1

    Ivan,

    I have done

    e.Fields["path"]
    

    On all my search projects, works fine.

    Regards

    Ismail

  • Ivan Ponomarenko 20 posts 105 karma points
    Oct 07, 2016 @ 09:12
    Ivan Ponomarenko
    0

    Ismail, it is very strange, I do not have "path" in e.Fields. See attached screenshot above.

    Regards, Ivan.

Please Sign in or register to post replies

Write your reply to:

Draft