Include restricted content with excerpt include macro
Asked by Geert Graat:

Hi,

We have a space with pages that are each restricted to a group of users. A user can have access to more than one page of that space. On the space main page, I want to list the contents of the pages that the user has access to. I use the excerpt macro on the page and the excerpt include macro on the main page.

So I have pages **A** , **B** and **C** and the user has access to **A and B** , **not C**.

Now on the main page I want to show **contents of A** and **contents of B** to the user.

This works great, except for the contents of page C which the user cannot access. Instead of simply not showing the content, the macro outputs "page does not exist", which obviously is not what I want.

What is interesting is that if I just want to show the pages (not the content) that a user can access on the main page, I can use the children macro and that will work fine, e.g. show only the pages that a user can access and simply leave out the ones the user cannot access.

Is there a way to achieve the same behavior with the excerpt-include macro as with the children macro, or should I use a different method?

Thanks,

Geert


Answers:
author: Geert Graat
body: So I found this: <https://jira.atlassian.com/browse/CONF-5436>, where a workaround is given by hiding the error message using CSS. A bit ugly, but it works. You have to use the page include rather than the excerpt include, as the excerpt errors are not associated with any styling, but the page include errors come in a nice div with class error. You can probably achieve the same for the excerpt by adding styling to the page, but that is even more hacking so I use the page include for now.

I actually hope there is a better solution, but in any case there is a workaround.


author: Arjen Breur
body: This *user macro* "wraps" the default *Insert Page* macro and only shows it if the current user has permission to view that page. It does not show anything if the user doesn't.  

```
## Macro title: Include Restricted Page
## Date created: 01/11/2018
## Macro has a body: N

## @param space:title=Space|type=spacekey|desc=Space where the page excerpt is found
## @param page:title=Page|type=confluence-content|required=true|desc=Page where the excerpt is found
## Check for valid space key, otherwise use current
#set($spacekey= $paramspace)
#if (!$spacekey)
 #set ($spacekey=$space.key)
#end
#set($m1 = "{include:")
#set($m2 = "}")
#set($macro = "${m1}${spacekey}:${parampage}${m2}")

#if ($permissionHelper.canViewPage($action.remoteUser, $spacekey, $parampage))
 $action.getHelper().renderConfluenceMacro($macro)
#end
```

comments: author: Shaun Grose
body: Hi [@Arjen Breur](/t5/user/viewprofilepage/user-id/504887) , thanks for that macro, it was a life saver.

I used your macro and modified it to use the 'Include Excerpt' macro instead. There was some weirdness that meant the space key was getting included in the page title, so I also added a check to ignore the space key if it had been prepended to the page name.

Here's my update for anyone else looking some something similar:

```
## Macro title: Include Restricted Excerpt
## Date created: 01/11/2018 by Arjen Breur
## Date modified: 28/05/2019 by Shaun Grose
## Macro has a body: N

## @Param space:title=Space|type=spacekey|desc=Space where the page excerpt is found
## @Param page:title=Page|type=confluence-content|required=true|desc=Page where the excerpt is found
## Check for valid space key, otherwise use current
#set($spacekey= $paramspace)
#if (!$spacekey)
#set ($spacekey=$space.key)
#end
#set ($pageId = $content.getId())

#set($m1 = "{excerpt-include:")
#set($m2 = "|nopanel=true}")
#if ($parampage.contains($spacekey))
#set($macro = "${m1}${parampage}${m2}")
#else
#set($macro = "${m1}${spacekey}:${parampage}${m2}")
#end

#if ($permissionHelper.canViewPage($action.remoteUser, $spacekey, $parampage))
$action.getHelper().renderConfluenceMacro($macro)
#elseif ($permissionHelper.canViewPage($action.remoteUser, $pageId))
$action.getHelper().renderConfluenceMacro($macro)
#end
```

Cheers!


author: Shaun Grose
body: Hi [@Arjen Breur](/t5/user/viewprofilepage/user-id/504887) , thanks for that macro, it was a life saver.

I used your macro and modified it to use the 'Include Excerpt' macro instead. There was some weirdness that meant the space key was getting included in the page title, so I also added a check to ignore the space key if it had been prepended to the page name.

Here's my update for anyone else looking some something similar:

```
## Macro title: Include Restricted Page
## Date created: 01/11/2018 by Arjen Breuer
## From Page: https://community.atlassian.com/t5/Confluence-questions/Include-restricted-content-with-excerpt-include-macro/qaq-p/277766
## Date modified: 30/05/2019 by Shaun Grose
## Macro has a body: N

## @param space:title=Space|type=spacekey|desc=Space where the page excerpt is found
## @param page:title=Page|type=confluence-content|required=true|desc=Page where the excerpt is found
## Check for valid space key, otherwise use current
#set($spacekey= $paramspace)
#if (!$spacekey)
#set ($spacekey=$space.key)
#end
#set($len1 = $space.key.length()+1)
#set($len2 = $parampage.length())
#set($pagename = $parampage.substring($len1,$len2))
#set($m1 = "{excerpt-include:")
#set($m2 = "|nopanel=true}")
#if ($parampage.contains($spacekey))
#set($macro = "${m1}${parampage}${m2}") 
#else
#set($macro = "${m1}${spacekey}:${parampage}${m2}")
#end

#if ($permissionHelper.canViewPage($action.remoteUser, $spacekey, $parampage) || $permissionHelper.canViewPage($action.remoteUser, $spacekey, $pagename) )
$action.getHelper().renderConfluenceMacro($macro)
#end
```

Cheers

(Edit: Realised I was referring to the wrong page ID in the last statement. Made an update to check the correct page.)


Tags: confluence-administration