Can We Restrict Comments for Confluence 4.2?
Asked by tousifishaikh:

Hi Team,

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

thanks.


Answers:
author: GuilhermeA
body: 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 &amp;&amp; !$action.userWatchingPage &amp;&amp; !$action.userWatchingSpace)#set ($commentTextareaTabIndex = 200)#if ($page.type == "blogpost")&lt;div class="hidden parameters"&gt;    ## 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.    &lt;input type="hidden" id="pageTitle" value="/$page.datePath/$page.title"&gt;&lt;/div&gt;#end#macro (displayCommentActions $commentId $commentActionSection $webInterfaceContext)  #set ($items=$webInterfaceManager.getDisplayableItems("system.comment.action/$commentActionSection", $webInterfaceContext))  #if ($items)    &lt;ul class="comment-actions-$commentActionSection"&gt;        #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            &lt;li class="$className"&gt;            &lt;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               &gt;&lt;span&gt;$item.label.getDisplayableLabel($req, $webInterfaceContext)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;        #end    &lt;/ul&gt;  #end#end#macro (displayCommentForm $formname $actionParams $pageId $rte)    &lt;p class="comment-user-logo"&gt;        #logoBlock("~$remoteUser.name")    &lt;/p&gt;    &lt;div class="comment-header"&gt;        &lt;h4 class="author"&gt;$action.getText("comment.author.byline", ["#usernameLink($remoteUser.name)"])&lt;/h4&gt;    &lt;/div&gt;    &lt;div class="comment-body"&gt;        #parse ("/includes/alert-anonymous.vm")        &lt;form name="$formname" method="post" action="$req.contextPath/pages/doaddcomment.action?${actionParams}"&gt;            #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                &lt;div id="comments-text-editor"&gt;                    &lt;span class="comment-actions"&gt;                        &lt;ul&gt;                            &lt;li&gt;                            &lt;a href="$req.contextPath$generalUtil.customGetPageUrl($page)showComments=true&amp;amp;showCommentArea=true#addcomment" id="add-comment-rte" accesskey="$action.getTextStrict("add.comment.accesskey")" class="toolbar-trigger"&gt;$action.getText('comment.load.rte')&lt;/a&gt;                            &lt;/li&gt;                        &lt;/ul&gt;                    &lt;/span&gt;                    &lt;textarea name="textContent" placeholder="$action.getText('comment.add.placeholder')"&gt;&lt;/textarea&gt;                    &lt;div class="text-editor-chrome aui-toolbar"&gt;                        &lt;ul class="toolbar-group"&gt;                            &lt;li class="toolbar-item"&gt;                                &lt;button class="toolbar-trigger" id="text-comment-save" name="confirm" value="$action.getText('save.name')" type="submit"&gt;$action.getText('save.name')&lt;/button&gt;                            &lt;/li&gt;                        &lt;/ul&gt;                    &lt;/div&gt;                &lt;/div&gt;            #end        &lt;/form&gt;    &lt;/div&gt;#end#macro (commentThread $comment)    #set ($page = $comment.owner)    &lt;li id="comment-thread-$comment.id" class="comment-thread"&gt;        #if ($action.editComment &amp;&amp; $action.comment &amp;&amp; $action.comment.id == $comment.id &amp;&amp; $permissionHelper.canEdit($remoteUser, $comment ))            #putMetadata('show-comments', true)            #requireResourcesForContext("editor")            #requireResourcesForContext("comment-editor")            &lt;div class="comment edit" id="comment-$comment.id"&gt;                &lt;p class="comment-user-logo"&gt;                    #logoBlock("~$comment.creatorName")                &lt;/p&gt;                &lt;p class="date" title="$dateFormatter.formatDateTime($comment.lastModificationDate)"&gt;                    $action.formatFriendlyDate($comment.lastModificationDate)                &lt;/p&gt;                &lt;div class="comment-header"&gt;                    &lt;h4 class="author"&gt;$action.getText("comment.author.byline", ["#usernameLink($comment.creatorName)"])&lt;/h4&gt;                &lt;/div&gt;                &lt;div class="comment-body"&gt;                    &lt;form name="editcommentform" method="post" action="$req.contextPath/pages/doeditcomment.action?pageId=$page.id&amp;amp;commentId=$comment.id"&gt;                        #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                    &lt;/form&gt;            &lt;/div&gt;        #else            #if ($comment.id == $focusedCommentId)                #putMetadata('show-comments', true)            #end            &lt;div class="comment #if ($comment.id == $focusedCommentId) focused #end " id="comment-$comment.id"&gt;                &lt;p class="comment-user-logo"&gt;                    #logoBlock("~$comment.creatorName")                &lt;/p&gt;                &lt;p class="date" title="$dateFormatter.formatDateTime($comment.lastModificationDate)"&gt;                    $action.formatFriendlyDate($comment.lastModificationDate)                &lt;/p&gt;                &lt;div class="comment-header"&gt;                    &lt;h4 class="author"&gt;$action.getText("comment.author.byline", ["#usernameLink ($comment.creatorName)"])#if ($page.type == "blogpost" &amp;&amp; $comment.creatorName == $page.creatorName) &lt;span class="author-lozenge"&gt;$i18n.getText('comment.author.lozenge')&lt;/span&gt;#end&lt;/h4&gt;                &lt;/div&gt;                &lt;div class="comment-body"&gt;                    &lt;div class="comment-content wiki-content"&gt;                        $action.getXHtmlComments().get($comment)                    &lt;/div&gt;                    &lt;div class="comment-actions"&gt;                        #set ($webInterfaceContext = $action.getWebInterfaceContext($comment))                        #displayCommentActions($comment.id "secondary" $webInterfaceContext)                        #displayCommentActions($comment.id "primary" $webInterfaceContext)                    &lt;/div&gt;                &lt;/div&gt;            &lt;/div&gt;            #trackbackRdf ($trackbackUtils.getContentIdentifier($comment) $page.title $trackbackUtils.getPingUrl($comment))            #if ( $action.threadComments &amp;&amp; $action.replyToComment == $comment.id &amp;&amp; $permissionHelper.canComment($action.remoteUser, $comment.owner) &amp;&amp; $action.isPrintableVersion()==false )                &lt;ol class="comment-threads"&gt;                    &lt;li class="comment-thread"&gt;                        &lt;div class="comment reply"&gt;                            #displayCommentForm("threadedcommentform", "pageId=${page.id}&amp;parentId=${comment.id}", ${page.id}, true)                        &lt;/div&gt;                    &lt;/li&gt;                &lt;/ol&gt;            #end        #end        #if ($threadComments)            #if ($comment.children.size() &gt; 0)                &lt;ol class="comment-threads"&gt;                #foreach ($childComment in $comment.children)                    #commentThread($childComment)                #end                &lt;/ol&gt;            #end        #end    &lt;/li&gt;#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())&lt;div id="comments-section" class="pageSection group"&gt;    #if ($page.comments.size() &gt; 0 || $action.showCommentArea)        &lt;div class="section-header #if (!$action.commentsShowing) collapsed #end"&gt;            &lt;h2 id="comments-section-title" class="section-title"&gt;                &lt;a href="#" class="comments-show-hide #if($action.commentsShowing)comments-showing#end" name="comments"&gt;                    #if ($page.comments.size() == 1)                        $action.getText("comment.singular")                    #else                        $action.getText("comment.plural", [$page.comments.size()])                    #end                &lt;/a&gt;            &lt;/h2&gt;            &lt;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"&gt;                $i18n.getText("comments.show.hide")            &lt;/a&gt;        &lt;/div&gt;        #end    #* Show expanded comments. If there are none, then show nothing at all (no heading, zilch!) *#    #if ($page.comments.size() &gt; 0)        #if ($threadComments)            #set ($topLevelComments = $page.topLevelComments)        #else            #set ($topLevelComments = $page.comments)        #end        &lt;ol class="comment-threads top-level #if (!$action.commentsShowing) hidden #end" id="page-comments"&gt;        #foreach ($comment in $topLevelComments)            #commentThread($comment)        #end        &lt;/ol&gt;    #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)    &lt;div class="bottom-comment-panels comment-panels"&gt;        #foreach ($panel in $bottomCommentPanels)        $panel.getHtml($action.context)        #end       &lt;/div&gt;    #end    #if (!$action.isPrintableVersion() &amp;&amp; $permissionHelper.canComment($remoteUser, $page))        #if ($action.showCommentArea)            &lt;div id="addcomment" class="comment add"&gt;            #displayCommentForm("inlinecommentform", "pageId=${page.id}", ${page.id}, true)            &lt;/div&gt;        #elseif($darkFeatures.isFeatureEnabled('text.comment.editor'))            &lt;div id="addcomment" class="comment add comment-text"&gt;            #displayCommentForm("textcommentform", "pageId=${page.id}", ${page.id}, false)            &lt;/div&gt;        #else                    &lt;div id="comments-actions" class="aui-toolbar noprint"&gt;                &lt;p class="toolbar-group"&gt;                    &lt;span class="toolbar-item"&gt;&lt;a href="$req.contextPath$generalUtil.customGetPageUrl($page)showComments=true&amp;amp;showCommentArea=true#addcomment" id="add-comment-rte" accesskey="$action.getTextStrict("add.comment.accesskey")" class="toolbar-trigger"&gt;$action.getText('action.add.comment')&lt;/a&gt;&lt;/span&gt;                &lt;/p&gt;            &lt;/div&gt;        #end    #end#else##Add a custom Message if you want, otherwise leave this blank#end&lt;/div&gt;#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


author: tousifs
body: may be you need to do the customization.