Wednesday

Allow user to delete his own comments for any commentable asset

The Problem

In the assets where user can comment like blog, journal article and the rest, users should have Delete Discussion Permission for that particular asset to delete comment.

You can test this by commenting on a blog entry. Now give that user Delete Discussion permission for Blog.

Now the problem is, if he is given Delete Discussion permission, he is able to delete any users comment.

What I wanted was like user should be able to delete his own comments.

Solution - Hook to the rescue
  • Commenting comes from liferay:ui discussion. So I created a jsp hook for the same.
  • In the jsp hook, recreate this folder structure html/taglib/ui/discussion/page.jsp under your jsps or whatever folder you have configured for the jsp hook
  • We have to modify page.jsp, so edit the same.Find the below line.
    There are two occurances for the same.

  • <c:if test=\"<%= MBDiscussionPermission.contains(permissionChecker, company.getCompanyId(), scopeGroupId, className, classPK, userId, ActionKeys.DELETE_DISCUSSION)
  • Basically this is the condition that allows user to delete a comment.
  • Now here we want to add an additional condition that if the logged in user has created the comment he should be able to delete the comment.
  • So replace the above line with following for both the occurances of above line.

  • <c:if test=\"<%= MBDiscussionPermission.contains(permissionChecker, company.getCompanyId(), scopeGroupId, className, classPK, userId, ActionKeys.DELETE_DISCUSSION) || (themeDisplay.getUserId() == message.getUserId()) %>\">
  • Deploy the hook.Thats it. Now the user should be able to delete his own comments not anyone else's.

3 comments:

  1. Which Liferay version you were trying this for? I was trying the exact same thing but the result was no effect. Don't know what happened.

    One difference was, in the JSP page, I could only find one occurrence of

    <c:if test=\"<%= MBDiscussionPermission.contains(permissionChecker, company.getCompanyId(), scopeGroupId, className, classPK, userId, ActionKeys.DELETE_DISCUSSION)


    Hence only one replacement as opposed to 2 suggested by you.

    Any help?

    ReplyDelete
  2. As you can see from the label, it was for Liferay 5.2.3 I havent tried it on 6

    ReplyDelete
  3. Very clearly explained, nice info, thanks for sharing...

    ReplyDelete