4

I have the following:

<a href="@Model.Link.Url">

Where by the Link is a glass Link property:

        public virtual Link Link { get; set; }

The problem is that the destination page has restricted access to logged in users only. It seems that glass/sitecore is smart enough to realise this and won't render the Url. Instead it renders the current url, rather than "currenturl/edit".

The problem with this is that when an unauthenticated user clicks the link I want it to take them to the login page, where once logged in it will redirect them to the restricted page they originally wanted. But because the correct url isn't being rendered, this process is not invoked.

How do I get it to render the Url regardless of permissions?

David Masters
  • 8,069
  • 2
  • 44
  • 75

1 Answers1

7

Yes Sitecore does that by default. I actually ran into this same situation a while ago: https://sdn.sitecore.net/SDN5/Forum/ShowPost.aspx?PostID=62876

I remember I ended up overriding the the SitecoreFieldLinkMapper and changed this:

if (linkField.TargetItem == null) link.Url = string.Empty;
else link.Url = LinkManager.GetItemUrl(linkField.TargetItem, urlOptions);

to this:

Item targetItem = null;
using (new SecurityDisabler())
{
    targetItem = linkField.TargetItem;
}

if (targetItem == null) link.Url = string.Empty;
else link.Url = LinkManager.GetItemUrl(targetItem, urlOptions);

Now at that time I was "fortunate" that the architect had included Glass's entire source into the solution. So I only had to change the original mapper. But if I had to do it again I would extend the original mapper and register it at startup. You can find an example right here: http://glass.lu/Mapper/Sc/Tutorials/Tutorial19

RvanDalen
  • 1,155
  • 5
  • 10
  • 4
    Thanks. I guess the root problem is that is that sitecore's GetItem method returns null if the current user doesn't have permission to that item, which to me is stupid - a security exception would be better. This has tripped me up on another issue. It also seems stupid for GetItemUrl to return a *different* url than the one requested depending on permissions; what good is a *different* url? Seeing as Sitecore will deny access when the user requests the page - the link manager should just provide the correct link so the login process can be invoked. Anyway, rant over. – David Masters Sep 12 '15 at 09:32