Can We Restrict Comments for Confluence 4.2?

Hi Team,

Can we Restrict Comments in confluence 4.2 for few users based on there groups.

thanks.

2 answers

Hello Peter,

First I would like to apologize but to make it clearer I will post the whole thing here, so you need to copy&paste :)

You can customize each look of Confluence through the UI, so there is no need to mess around with java. You just need a little knowledge in Velocity.

I have used the Groupname Blah! for reference, this will be the name of the group. If you want more then one group, you'll need to tweak it a little, it goes like this:

##Define the groups for the user accessing the page
#set ($groups = $userAccessor.getGroupNames($action.remoteUser))

#requireResource("confluence.web.resources:ajs")
#requireResource("confluence.web.resources:animation")
#set ($useWatch = $remoteUser && !$action.userWatchingPage && !$action.userWatchingSpace)
#set ($commentTextareaTabIndex = 200)

#if ($page.type == "blogpost")
<div class="hidden parameters">
    ## TODO - replace this below with something like comment-page-title, hard to refactor :(( Is it even used? Selenium test check?
    ## Used by macrobrowser smart fields to set the page param for comments.
    <input type="hidden" id="pageTitle" value="/$page.datePath/$page.title">
</div>
#end

#macro (displayCommentActions $commentId $commentActionSection $webInterfaceContext)
  #set ($items=$webInterfaceManager.getDisplayableItems("system.comment.action/$commentActionSection", $webInterfaceContext))
  #if ($items)
    <ul class="comment-actions-$commentActionSection">
        #foreach ($item in $items)
            ## provide first class name for IE6 cause first-child selector doesn't work
            #if ($velocityCount==1) #set ($className="first") #else #set ($className="") #end
            #if ($item.styleClass) #set($className = "$className $item.styleClass") #end
            <li class="$className">
            <a #if ($item.tooltip)title="$item.tooltip.getDisplayableLabel($req, $webInterfaceContext)"#end href="$item.link.getDisplayableUrl($req, $webInterfaceContext)"
               #if ($item.link.id) id="${item.link.id}-$commentId" #end
               ><span>$item.label.getDisplayableLabel($req, $webInterfaceContext)</span></a></li>
        #end
    </ul>
  #end
#end

#macro (displayCommentForm $formname $actionParams $pageId $rte)

    <p class="comment-user-logo">
        #logoBlock("~$remoteUser.name")
    </p>

    <div class="comment-header">
        <h4 class="author">$action.getText("comment.author.byline", ["#usernameLink($remoteUser.name)"])</h4>
    </div>

    <div class="comment-body">
        #parse ("/includes/alert-anonymous.vm")

        <form name="$formname" method="post" action="$req.contextPath/pages/doaddcomment.action?${actionParams}">
            #form_xsrfToken()
            #if ($rte)
                #putMetadata('show-comments', true)
                #requireResourcesForContext("editor")
                #requireResourcesForContext("comment-editor")
                #bodytag ("Component" "name='Content'" "theme='aui'" "template='editor.vm'")
                    #param ("formname" $formname)
                    #param ("spaceKey" "$generalUtil.urlEncode($spaceKey)")
                    #param ("tabindex" $commentTextareaTabIndex)
                    #param ("heartbeat" "true")
                    #param ("contentId" "$pageId")
                    #param ("contentType" "comment")
                    #param ("minEditorHeight" "150")
                    #param ("useWatch" $useWatch)
                    #param ("saveDrafts" "false")
                    #param ("shouldResize" "true")
                    #param ("fullscreen" "false")
                    #param ("requireResources" "true")                    
                    #param ("autoStart" "true")
                #end
            #else
                <div id="comments-text-editor">
                    <span class="comment-actions">
                        <ul>
                            <li>
                            <a href="$req.contextPath$generalUtil.customGetPageUrl($page)showComments=true&showCommentArea=true#addcomment" id="add-comment-rte" accesskey="$action.getTextStrict("add.comment.accesskey")" class="toolbar-trigger">$action.getText('comment.load.rte')</a>
                            </li>
                        </ul>
                    </span>
                    <textarea name="textContent" placeholder="$action.getText('comment.add.placeholder')"></textarea>
                    <div class="text-editor-chrome aui-toolbar">
                        <ul class="toolbar-group">
                            <li class="toolbar-item">
                                <button class="toolbar-trigger" id="text-comment-save" name="confirm" value="$action.getText('save.name')" type="submit">$action.getText('save.name')</button>
                            </li>
                        </ul>
                    </div>
                </div>
            #end
        </form>
    </div>
#end

#macro (commentThread $comment)
    #set ($page = $comment.owner)
    <li id="comment-thread-$comment.id" class="comment-thread">
        #if ($action.editComment && $action.comment && $action.comment.id == $comment.id && $permissionHelper.canEdit($remoteUser, $comment ))
            #putMetadata('show-comments', true)
            #requireResourcesForContext("editor")
            #requireResourcesForContext("comment-editor")
            <div class="comment edit" id="comment-$comment.id">
                <p class="comment-user-logo">
                    #logoBlock("~$comment.creatorName")
                </p>
                <p class="date" title="$dateFormatter.formatDateTime($comment.lastModificationDate)">
                    $action.formatFriendlyDate($comment.lastModificationDate)
                </p>
                <div class="comment-header">
                    <h4 class="author">$action.getText("comment.author.byline", ["#usernameLink($comment.creatorName)"])</h4>
                </div>

                <div class="comment-body">
                    <form name="editcommentform" method="post" action="$req.contextPath/pages/doeditcomment.action?pageId=$page.id&commentId=$comment.id">
                        #form_xsrfToken()
                        #bodytag ("Component" "name='Content'" "theme='aui'" "template='editor.vm'")
                            #param ("formname" "editcommentform")
                            #param ("spaceKey" "$generalUtil.urlEncode($spaceKey)")
                            #param ("tabindex" $commentTextareaTabIndex)
                            #param ("heartbeat" "true")
                            #param ("wysiwygContent" "$action.getCommentAsXHtmlForWysiwyg()")
                            #param ("contentId" "$comment.id")
                            #param ("contentType" "comment")
                            #param ("minEditorHeight" "150")
							#param ("useWatch" $useWatch)
							#param ("saveDrafts" "false")
							#param ("shouldResize" "true")
							#param ("fullscreen" "false")
							#param ("requireResources" "true")
							#param ("autoStart" "true")
                        #end
                    </form>
            </div>

        #else
            #if ($comment.id == $focusedCommentId)
                #putMetadata('show-comments', true)
            #end
            <div class="comment #if ($comment.id == $focusedCommentId) focused #end " id="comment-$comment.id">
                <p class="comment-user-logo">
                    #logoBlock("~$comment.creatorName")
                </p>
                <p class="date" title="$dateFormatter.formatDateTime($comment.lastModificationDate)">
                    $action.formatFriendlyDate($comment.lastModificationDate)
                </p>
                <div class="comment-header">
                    <h4 class="author">$action.getText("comment.author.byline", ["#usernameLink ($comment.creatorName)"])#if ($page.type == "blogpost" && $comment.creatorName == $page.creatorName) <span class="author-lozenge">$i18n.getText('comment.author.lozenge')</span>#end</h4>
                </div>
                <div class="comment-body">
                    <div class="comment-content wiki-content">
                        $action.getXHtmlComments().get($comment)
                    </div>
                    <div class="comment-actions">
                        #set ($webInterfaceContext = $action.getWebInterfaceContext($comment))
                        #displayCommentActions($comment.id "secondary" $webInterfaceContext)
                        #displayCommentActions($comment.id "primary" $webInterfaceContext)
                    </div>
                </div>
            </div>

            #trackbackRdf ($trackbackUtils.getContentIdentifier($comment) $page.title $trackbackUtils.getPingUrl($comment))

            #if ( $action.threadComments && $action.replyToComment == $comment.id && $permissionHelper.canComment($action.remoteUser, $comment.owner) && $action.isPrintableVersion()==false )
                <ol class="comment-threads">
                    <li class="comment-thread">
                        <div class="comment reply">
                            #displayCommentForm("threadedcommentform", "pageId=${page.id}&parentId=${comment.id}", ${page.id}, true)
                        </div>
                    </li>
                </ol>
            #end
        #end

        #if ($threadComments)
            #if ($comment.children.size() > 0)
                <ol class="comment-threads">
                #foreach ($childComment in $comment.children)
                    #commentThread($childComment)
                #end
                </ol>
            #end
        #end
    </li>
#end

#* make sure you pass in an AbstractPage as a decorator parameter when you call this inline decorator *#
#set ($page = $params.get("page"))

## NOTE: While we still have both statically and dynamically rendered comments in Confluence these templates are duplicated in comments.soy.
##       If you are changing one you will probably also need to change the other.

#if (!$action.isPrintableVersion())
<div id="comments-section" class="pageSection group">
    #if ($page.comments.size() > 0 || $action.showCommentArea)
        <div class="section-header #if (!$action.commentsShowing) collapsed #end">
            <h2 id="comments-section-title" class="section-title">
                <a href="#" class="comments-show-hide #if($action.commentsShowing)comments-showing#end" name="comments">
                    #if ($page.comments.size() == 1)
                        $action.getText("comment.singular")
                    #else
                        $action.getText("comment.plural", [$page.comments.size()])
                    #end
                </a>
            </h2>
            <a title="$i18n.getText('comments.show.hide')" href="#" 
               class="comments-show-hide icon #if($action.commentsShowing)comments-showing icon-section-opened#else icon-section-closed#end">
                $i18n.getText("comments.show.hide")
            </a>
        </div>    
    #end

    #* Show expanded comments. If there are none, then show nothing at all (no heading, zilch!) *#
    #if ($page.comments.size() > 0)
        #if ($threadComments)
            #set ($topLevelComments = $page.topLevelComments)
        #else
            #set ($topLevelComments = $page.comments)
        #end
        <ol class="comment-threads top-level #if (!$action.commentsShowing) hidden #end" id="page-comments">
        #foreach ($comment in $topLevelComments)
            #commentThread($comment)
        #end
        </ol>
    #end

#######################
## CHANGE GROUPNAME BELOW##
#######################
##Hide the Option to add comments if the user does not belong to a Group
#if ($groups.contains("blah!"))

    #set ($bottomCommentPanels=$webInterfaceManager.getDisplayableWebPanels("atl.comments.view.bottom", $action.context))
    #if ($bottomCommentPanels)
    <div class="bottom-comment-panels comment-panels">
        #foreach ($panel in $bottomCommentPanels)
        $panel.getHtml($action.context)
        #end   
    </div>
    #end

    #if (!$action.isPrintableVersion() && $permissionHelper.canComment($remoteUser, $page))
        #if ($action.showCommentArea)
            <div id="addcomment" class="comment add">
            #displayCommentForm("inlinecommentform", "pageId=${page.id}", ${page.id}, true)
            </div>
        #elseif($darkFeatures.isFeatureEnabled('text.comment.editor'))
            <div id="addcomment" class="comment add comment-text">
            #displayCommentForm("textcommentform", "pageId=${page.id}", ${page.id}, false)
            </div>
        #else        
            <div id="comments-actions" class="aui-toolbar noprint">
                <p class="toolbar-group">
                    <span class="toolbar-item"><a href="$req.contextPath$generalUtil.customGetPageUrl($page)showComments=true&showCommentArea=true#addcomment" id="add-comment-rte" accesskey="$action.getTextStrict("add.comment.accesskey")" class="toolbar-trigger">$action.getText('action.add.comment')</a></span>
                </p>
            </div>
        #end
    #end
#else
##Add a custom Message if you want, otherwise leave this blank
#end
</div>
#end

Just follow the massive # signs and change the groupname Blah! to a group you would like to use.

This will do exactly as you want, if he is not member of the group he will not be able to comment. If he is, then the comment will be available.

There is an easier option under "Browse > Space Admin > Permissions" Then you can add or remove the "add comment" permission for the specific groups.

Cheers!

Guilherme

may be you need to do the customization.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

364 views 20 10
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you