Copied to clipboard

Flag this post as spam?

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


  • aits 15 posts 46 karma points
    Jan 20, 2016 @ 04:15
    aits
    0

    Checking if member has accesss on content to create menu

    I am trying to render the menu based on permission from public access..

    @foreach (var primaryNavItem in primaryNavItems)
                        {
                            var isProtected = umbraco.library.IsProtected(primaryNavItem.Id, primaryNavItem.Path.ToString());
                            var loginAcces = isProtected && umbraco.library.HasAccess(primaryNavItem.Id, primaryNavItem.Path.ToString());
    
                            if (!isProtected || loginAcces)
                            {
                              //menu item here
                            }
                        }
    

    But this throws error as below from log

        System.NullReferenceException: Object reference not set to an instance of an object.
       at Umbraco.Core.Services.PublicAccessServiceExtensions.HasAccess(IPublicAccessService publicAccessService, String path, MembershipUser member, RoleProvider roleProvider)
       at Umbraco.Web.UmbracoHelper.MemberHasAccess(String path)
       at Umbraco.Web.UmbracoHelper.MemberHasAccess(Int32 nodeId, String path)
       at CallSite.Target(Closure , CallSite , Type , Object , Object )
       at ASP._Page_Views_MasterLayout_cshtml.Execute() in e:\Demo\Src\MyApp\Views\MasterLayout.cshtml:line 80
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.WebPages.WebPageBase.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer)
       at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
       at System.Web.WebPages.WebPageBase.Write(HelperResult result)
       at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
       at System.Web.WebPages.WebPageBase.PopContext()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
       at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
       at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
       at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
       at System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
       at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
       at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
       at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
    

    Can you please guide me right direction. Umbraco is v7.3.1

    Few more detail: 1. Members are Identity 2 based handled customly 2. Role provider is also custom and replaced umbraco default provider using web.config

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 20, 2016 @ 09:09
    Dennis Aaen
    0

    HI aits,

    Since your are using Umbraco 7, then try have a look at the UmbracoHelper in Razor https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/

    As you can see you have Razor code examples

    .MemberHasAccess(int nodeId, string path)

    Returns a Boolean on whether the currently logged in member has access to the page given its ID and path.

    @if(Umbraco.MemberHasAccess(CurrentPage.Id, CurrentPage.Path)){
        <h1>Welcome!</h1>
    }    
    

    .IsProtected(int pageId, string path)

    Returns a Boolean on whether a page with a given pageId and path has public access restrictions set.

    @foreach (var child in CurrentPage.Children) { 
        <h2>@child.Name</h2>
            @if(Umbraco.IsProtected(child.id, child.Path)){
                <blink>Members only</blink>
            }
    }
    

    Hope this helps,

    /Dennis

  • aits 15 posts 46 karma points
    Jan 20, 2016 @ 10:05
    aits
    0

    Hi Dennis,

    Thanks, for your direction. But I have already tried this too. Even I got the same exception, so I tried with lower level methods but still no luck.

  • Bijesh Tank 192 posts 420 karma points
    Jan 20, 2016 @ 10:21
    Bijesh Tank
    0

    Hi Dennis,

    Quick question, would this be the preferred method instead?

    Umbraco.MemberHasAccess(page.Path)
    

    If I try to specify page.Id then Visual Studio complains that this is obsolete:

    Method 'Umbraco.Web.UmbracoHelper.MemberHasAccess' is obsolete: Use the MemberHasAccess method that only specifies path

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 20, 2016 @ 12:37
    Dennis Aaen
    0

    Hi Bijesh,

    You need to do something like this.

    @Umbraco.MemberHasAccess(page.Id, page.Path)
    

    Hope this helps,

    /Dennis

  • Bijesh Tank 192 posts 420 karma points
    Jan 20, 2016 @ 12:44
    Bijesh Tank
    0

    That's what I mean, when i use @Umbraco.MemberHasAccess(page.Id, page.Path) I get a message that it's obsolete.

    If I use just Umbraco.MemberHasAccess(page.Path) it seems fine and works ok.

    I'm wondering if the documentation needs updating?

    I'm using 7.3.3.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 20, 2016 @ 12:46
    Dennis Aaen
    0

    Okay that for this information.

    /Dennis

  • aits 15 posts 46 karma points
    Jan 20, 2016 @ 12:21
    aits
    0

    Hi Dennis,

    One more thing, is there a way to get the roles name that is associated with a content, SO that I could check wiht current login user as I have role in session like

    @foreach (var primaryNavItem in primaryNavItems)
                    {
                        var isProtected = umbraco.library.IsProtected(primaryNavItem.Id, primaryNavItem.Path.ToString());
                      var rolesAllowed=primaryNavItem.SomeMethod();
    
                        if (!isProtected || rolesAllowed.Contians(Session["myRole"])
                        {
                          //menu item here
                        }
                    }
    

    Is there any way to accomplish, I really don't want to manipulate the umbraco table to check the permission for performance issue. It looks like the problem with my custom membership. So I am thinking of this approach.

    Any help could be great.

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft