I want to allow certain nodes to be publicly viewable, but restrict commenting to members of a particular "group" of users. Non-"group" members may be permitted to comment on other nodes.

I put "group" in scares because I don't mind whether I implement this functionality via Organic Groups, roles, taxonomies, content-types or a combination of these features.

However, I cannot see how I can prevent a user from commenting upon a node without either preventing that user from commenting anywhere or preventing anyone from commenting upon that node.

Comments

Daran-1’s picture

Here the official verdict: "comment apis in core are are not flexible enough to achieve this cleanly."

From what I can see, that would apply no matter how you try to implement the functionality.

Here's the 'dirty' way I did this, by hacking comment.modules:

1. Install and enable Organic Groups. (Probably will break if OG isn't enabled.)

2. Add the following two functions to comment.module:

function comment_access_hack_node($node) {
global $user;
if (!user_access('post comments')) return FALSE;
if (count($node->og_groups)==0 ) return TRUE;
$group = array_intersect($node->og_groups, array_keys($user->og_groups));
return (boolean) $group;
}

function comment_access_hack_nid($nid) {
global $user;
if (!user_access('post comments')) return FALSE;
$node = node_load($nid);
if (count($node->og_groups)==0 ) return TRUE;
$group = array_intersect($node->og_groups, array_keys($user->og_groups));
return (boolean) $group;
}

3. In comment.module, there are several calls to "user_access('post comments')". Some of them need to be changed as follows:

In function comment_link, change both calls to: "comment_access_hack_node($node)"
In function comment_reply, change both calls to: "comment_access_hack_node($node)"
In function comment_save, change to: "comment_access_hack_nid($edit['nid'])"
In function comment_links, change both calls to: "comment_access_hack_nid($comment->nid)"
In function comment_render change to: "comment_access_hack_nid($nid)"

Do not change any calls with arguments other than 'post comments'. Do not change the calls in functions comment_menu or comment_admin_settings.

Now a user will only be able to comment on nodes belonging to one or more groups if he is a member of at least one of them, and then, only if he has commenting permission generally, and of course, only if he can see the node in the first place.

PaulPhoto’s picture

I would dearly like to get my hands on this hack.
I modified the comment.module file as you indicated, and came out with no change.

My setup is as follows:
Blogging Community with a Group for each school.
A "Senior Site Administrator" creates a group for each school, and creates a "School Leader" (defined role) to administer his/her group.
The Group's lower-level members are "School Student" roles.

I'm happy to have members of one school see (view) the blog entries and comments of another school, but I'm not happy to have members of one school commenting on another school's blog.
The same goes for the School Leaders.
Ideally I would like to have the option to leave a comment dissapear altogether, when visiting another school's blog.

Any ideas on where to go from here?

-----------------------------------------------------------------------
Mint Chocolate - One thing you can eat after brushing your teeth...

lias’s picture

But I'm still looking for a more current fix if there is one.