Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore + Glass: Render link, but ignore permissions

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?

like image 677
David Masters Avatar asked Mar 15 '23 19:03

David Masters


1 Answers

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

like image 167
RvanDalen Avatar answered Mar 19 '23 05:03

RvanDalen