Haut de page

How to hide certain contents of a Confluence page from or show them to users


Posted by
Cyrille MARTIN

July 2, 2019

Visibility is an app developed by ServiceRocket and available on the Atlassian Marketplace. It allows one to display or hide sections of the contents of a Confluence page from a user according to different criteria, such as rights, group membership, page ownership, and even display mode and web browser properties.

The Visibility plugin has changed a lot since its first release. Many macros have disappeared in order to refocus on the essentials with “show-if” and “hide-if”. As their names indicate, these macros allow one to show or hide the contents of a page if certain conditions are satisfied.

The fact that it is free and has many use cases has made this app popular and widely used in implementations. You can decide to display parts of a page based on a team’s group membership, or simply leave instructions invisible in reading mode. Hidden information remains accessible via the “view source” action, so it is not a safe solution for hiding sensitive information.

But since January 2019, and version 5.0, Visibility has become a paid app. So if you are a user, be aware that when you reach the end of your license (or its renewal date), your app will not work unless you buy it. Fortunately it’s still affordable at $150 for 250 users, $1,000 for 10,000 users, and $1,500 for unlimited users.

If you prefer an alternative to this app, be aware that much of the functionality provided by Visibility can be natively covered with user macros.

The implementation achieved by these user macros does not entirely cover the scope offered by the Visibility app.

Here are the features included:

  • Named users
  • Group membership
  • Rights for a page or space
  • The type of display—Be careful, as only the PDF export will be available with the “printable” option
  • All conditions / one condition

A user macro is a macro that has been declared and defined by the Confluence administrator, and that can process content or display additional content. Creating a user macro requires some skill in development and knowing how to read the Java API.

Here we reveal how to replace Visibility with a native solution.

  1. First of all, you will have to disable the Visibility app, since it will be replaced by user macros
  2. You will have to modify the CSS to be able to choose to hide certain parts of the Confluence page during PDF export
  3. Finally, you will have to create 2 user macros

Let’s get to the heart of the action now.

Creating the “Hide-if” user macro

MODIFYING THE CSS

Adding CSS to the PDF Style of your instance

.NoDisplay {  
  display:none !important;  
}

 

CREATING THE USER MACRO

Follow the instructions for creating a new user macro using the following settings:

Name of the macro: hide-if

Visibility: Visible to all users in the macro browser

Title of the macro: Hide-If

Description: Hide content when conditions are met

Categories: Confluence Content

Macro body processing: Displayed

Template

## Macro title: hide-if
## Macro has a body: Y 
## Body processing: render
##
## Developed by: Cyrille Martin [VALIANTYS]
## Date created: dd/mm/yyyy
## Installed by: My Name

## @param match:title=Match Using|type=enum|enumValues=all,any|required=false|desc=If set to any, only one of the specified restrictions needs to match.|default=any
## @param user:title=Users|type=username|required=false|desc=The list of users to match.|multiple=true
## @param group:title=User groups|type=string|required=false|desc=The list of user groups to match.
## @param spacePermission:title=Space Permissions|type=string|required=false|desc=This property allows you to define whether the content inside this macro is hidden to a certain user based on his space permissions. This can be done by either of the following:</br>
Using @self to specify current space, followed by ":", then followed by permission type e.g. @self:view
These are the permission types:</br>
<ul>
<li><strong>view</strong> - (default) The user can view the space.</li>
<li><strong>edit</strong> - The user can edit pages.</li>
<li><strong>admin</strong> - The user can administer the space.</li>
<li><strong>news</strong> - The user can post a blog entry</li></ul>
## @param display:title=Display Types|type=string|required=false|desc=This property allows the user to hide content only in a document that has been exported to PDF and not on the Confluence page. </br>The (list of) display modes:</br><strong>default</strong> - content is hidden in both Confluence and the exported PDF document</br><strong>printable</strong> - content is hidden only in the exported PDF document

#set( $hidecontentuser = false)
#set( $hidecontentgroup = false)
#set( $getPermission = false)
#set( $username = "$req.getRemoteUser()" )

## Check User
#if( $!paramuser)
  #foreach( $userlisted in $paramuser.split(",") )
    #if( $userlisted == $username)
      #set( $hidecontentuser = true)
    #end 
  #end
#end

## Check Groups
#if( $!paramgroup)
  #foreach( $grouplist in $userAccessor.getGroupNamesForUserName($username))
    #foreach( $grouplisted in $paramgroup.split(","))
      #if( $grouplist == $grouplisted)
        #set( $hidecontentgroup = true)
      #end 
    #end
  #end
#end

## Check Permission
#if( $!paramspacePermission)
  #set ($colonIndex = $paramspacePermission.indexOf(":"))
  #if ($colonIndex == -1)
    #set ($pagePermission = $paramspacePermission)
  #else
    #set ($spaceKey = $paramspacePermission.substring(0, $colonIndex))
    #set ($pageActionIndex = $colonIndex + 1 )
    #set ($pagePermission = $paramspacePermission.substring($pageActionIndex))
  #end
  #if( $pagePermission == "edit")
    #set( $getPermission = $permissionHelper.canEdit($userAccessor.getUserByName($username),$content) )
  #elseif( $pagePermission ==  "view")
    #set( $getPermission = $permissionHelper.canViewPage($userAccessor.getUserByName($username),$content))
  #elseif( $pagePermission ==  "news")
    #set( $getPermission = $permissionHelper.canCreateBlogPost($userAccessor.getUserByName($username),$space))
  #elseif( $pagePermission ==  "admin")
    #set( $getPermission = $permissionHelper.canAdminister($userAccessor.getUserByName($username),$space))
  #end
#end


## Display AREA
#if( $parammatch == "any")
  #if($hidecontentuser || $hidecontentgroup || $getPermission) 
    #if ($!paramdisplay != "printable")
    #else
      
<div class="NoDisplay">
        $body
      </div>
    #end
  #else  
    $body
  #end
#else
  #if( (($!paramuser && $hidecontentuser) || !($!paramuser || $hidecontentuser)) && (( $!paramgroup && $hidecontentgroup) || !( $!paramgroup || $hidecontentgroup)) && (($!paramspacePermission && $getPermission) || !($!paramspacePermission || $getPermission)) )
    #if ($!paramdisplay != "printable")
    #else
      
<div class="NoDisplay">
        $body
      </div>  
    #end
  #else  
    $body
  #end
#end

That’s it—your users can now hide parts of a Confluence page according to the conditions that they choose.

Creating the “Show-if” user macro

MODIFYING THE CSS

Adding CSS to the PDF Style of your instance

.Display {  
  display:none !important;  
}

 

CREATING THE USER MACRO

Follow the instructions for creating a new user macro using the following settings:

Name of the macro: Show-if

Visibility: Visible to all users in the macro browser

Title of the macro: Show-if

Description: Show content when conditions are met

Categories: Confluence Content

Macro body processing: Displayed

Template

## Macro title: show-if
## Macro has a body: Y 
## Body processing: render
##
## Developed by: Cyrille Martin [VALIANTYS]
## Date created: dd/mm/yyyy
## Installed by: My Name

## @param match:title=Match Using|type=enum|enumValues=all,any|required=false|desc=If set to any, only one of the specified restrictions needs to match.|default=any
## @param user:title=Users|type=username|required=false|desc=The list of users to match.|multiple=true
## @param group:title=User groups|type=string|required=false|desc=The list of user groups to match.
## @param spacePermission:title=Space Permissions|type=string|required=false|desc=This property allows you to define whether the content inside this macro is shown to a certain user based on his space permissions. This can be done by either of the following:</br>
Using @self to specify current space, followed by ":", then followed by permission type e.g. @self:view
These are the permission types:</br>
<ul>
<li><strong>view</strong> - (default) The user can view the space.</li>
<li><strong>edit</strong> - The user can edit pages.</li>
<li><strong>admin</strong> - The user can administer the space.</li>
<li><strong>news</strong> - The user can post a blog entry</li></ul>
## @param display:title=Display Types|type=string|required=false|desc=This property allows the user to show content only in a document that has been exported to PDF and not on the Confluence page. </br>The (list of) display modes:</br><strong>default</strong> - content is displayed in both Confluence and the exported PDF document</br><strong>printable</strong> - content is displayed only in the exported PDF document

#set( $showcontentuser = false)
#set( $showcontentgroup = false)
#set( $getPermission = false)
#set( $username = "$req.getRemoteUser()" )

## Check User
#if( $!paramuser)
  #foreach( $userlisted in $paramuser.split(",") )
    #if( $userlisted == $username)
      #set( $showcontentuser = true)
    #end 
  #end
#end

## Check Groups
#if( $!paramgroup)
  #foreach( $grouplist in $userAccessor.getGroupNamesForUserName($username))
    #foreach( $grouplisted in $paramgroup.split(","))
      #if( $grouplist == $grouplisted)
        #set( $showcontentgroup = true)
      #end 
    #end
  #end
#end

## Check Permission
#if( $!paramspacePermission)
  #set ($colonIndex = $paramspacePermission.indexOf(":"))
  #if ($colonIndex == -1)
    #set ($pagePermission = $paramspacePermission)
  #else
    #set ($spaceKey = $paramspacePermission.substring(0, $colonIndex))
    #set ($pageActionIndex = $colonIndex + 1 )
    #set ($pagePermission = $paramspacePermission.substring($pageActionIndex))
  #end
  #if( $pagePermission == "edit")
    #set( $getPermission = $permissionHelper.canEdit($userAccessor.getUserByName($username),$content) )
  #elseif( $pagePermission ==  "view")
    #set( $getPermission = $permissionHelper.canViewPage($userAccessor.getUserByName($username),$content))
  #elseif( $pagePermission ==  "news")
    #set( $getPermission = $permissionHelper.canCreateBlogPost($userAccessor.getUserByName($username),$space))
  #elseif( $pagePermission ==  "admin")
    #set( $getPermission = $permissionHelper.canAdminister($userAccessor.getUserByName($username),$space))
  #end
#end

## Set the style for the show-if
<style>
 #if( $!paramdisplay == "printable")
  .Display {
   display:none;
  }
 #else
  .Display {
   display:block;
  }
 #end
</style>
## Display AREA

<div class="Display">
#if( $parammatch == "any")
  #if( $showcontentuser || $showcontentgroup || $getPermission )
    $body
  #end
#else
  #if((($!paramuser && $showcontentuser) || !($!paramuser || $showcontentuser)) && (( $!paramgroup && $showcontentgroup) || !($!paramgroup || $showcontentgroup)) && (($!paramspacePermission && $getPermission) || !($!paramspacePermission || $getPermission)))
    $body
  #end
#end
</div>

You will now be able to show content only if certain conditions are met.

The choice is yours

As you may have understood, depending on your situation and your internal resources, you can either buy the Visibility app or create a workaround by making two user macros that will provide you with essentially the same functionalites.

If you have any questions, don’t hesitate to contact our Atlassian Certified Consultants.