Skip to:
Content
Pages
Categories
Search
Top
Bottom

Open groups: Allow all members full rights in every group without joining


  • mikeboltonca
    Participant

    @mikeboltonca

    Howdy,
    I use BuddyPress as part of a company intranet. Our BuddyPress Groups are used as a quick way to find who works in a particular department or location.

    For our purposes, it makes sense for any staff member to be able to use any groups’ activity stream and bbPress forums, without having to join that group. The only exceptions would be for hidden groups, such as our Management group; they should still be invisible to non-members.

    I’ve done a lot of research into different ways to tackle this problem, but I haven’t found anything viable yet.

    Can anyone recommend an approach to explore?

    Thanks a lot for your ideas, folks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Perhaps, upon user registration you should auto-join them to all public groups. And modify templates in your child theme to hide JOIN / LEAVE buttons for public groups.


    mikeboltonca
    Participant

    @mikeboltonca

    Hi Slava,
    Thanks for sharing your idea!

    Unfortunately, this approach won’t work for two reasons:

    • Since this is a corporate intranet, the user base is pre-existing and linked to our Active Directory staff list, so there are no “new user sign-ups” to trigger a solution
    • The groups are used to see who’s in a particular department or location; if everyone is a member of every group, the group’s member list won’t be meaningful.

    I really appreciate your attempt!

    In your case – makes sense. All members joining all public groups won’t work.

    So, you need a more “hacky” solution.
    Try to dive into bp_group_is_member() function. It has a filter with the same name.

    You can try to do something like this (not tested):

    add_action( 'bp_group_is_member', function( $is_member, $group ) {
    	if ( bp_get_group_is_public( $group ) ) {
    		return true;
    	}
    	
    	return $is_member;
    } );

    Put it into bp-custom.php.


    mikeboltonca
    Participant

    @mikeboltonca

    Hi Slava,

    I tested that code and got no visible effect, but I think we’re in the right territory here.
    I explored both of these functions:

    bp_group_is_member()
    groups_is_user_member()

    It helped me realize that the two specific problems I wanted to solve were as follows:
    – Users can’t create topics in a BuddyPress group’s forum unless they join the group
    – Users can’t reply to topic in a BuddyPress group’s forum unless they join the group

    So I started on the page template itself (form-topic.php).
    Before displaying the “Create a topic” form, the template does a check to see if the user is allowed to create topics:
    if ( bbp_current_user_can_access_create_topic_form() ) : ?
    This was coming back as false, so I checked through each possible path through the function.

    Here’s the whole function for quick reference:

    function bbp_current_user_can_access_create_topic_form() {
    
    	// Users need to earn access
    	$retval = false;
    
    	// Always allow keymasters
    	if ( bbp_is_user_keymaster() ) {
    		$retval = true;
    
    	// Looking at a single forum & forum is open
    	} elseif ( ( bbp_is_single_forum() || is_page() || is_single() ) && bbp_is_forum_open() ) {
    		$retval = bbp_current_user_can_publish_topics();
    
    	// User can edit this topic
    	} elseif ( bbp_is_topic_edit() ) {
    		$retval = current_user_can( 'edit_topic', bbp_get_topic_id() );
    	}
    
    	// Allow access to be filtered
    	return (bool) apply_filters( 'bbp_current_user_can_access_create_topic_form', (bool) $retval );
    }

    Every single path through that function was returning true.
    If I removed the filter and replaced it with a hard-coded true as follows…
    return (bool) true;
    …then the user would have access to the group’s forum without joining the group.

    That told me that something in BuddyPress was filtering the result of this function. It was checking whether the user was a member of the group, and if not, was forcing bbp_current_user_can_access_create_topic_form() to return 0.

    I couldn’t find the portion of BuddyPress that was filtering the result, unfortunately.

    I have a working solution now, but it’s not elegant. I created a plugin with two filters (one for creating topics, one for replying to topics). The content of those filters is identical to the original bbPress functions. Since the plugin is loaded last, it basically overrides whatever BuddyPress says and goes with the original functions instead.

    Here it is:

    /**************************************************
    Part 1: Allow users to create topics
    Allow anyone to create a topic in any forum, even if that forum is inside a group the user hasn't joined.
    If the group is hidden (e.g. "Management"), they still won't have access because that check is done first.
    ***************************************************/
    // define the bbp_current_user_can_access_create_topic_form callback
    function filter_bbp_current_user_can_access_create_topic_form( $retval ) {
    	// Users need to earn access
    	$retval = false;
    
    	// Always allow keymasters
    	if ( bbp_is_user_keymaster() ) {
    		$retval = true;
    
    	// Looking at a single forum & forum is open
    	} elseif ( ( bbp_is_single_forum() || is_page() || is_single() ) && bbp_is_forum_open() ) {
    		$retval = bbp_current_user_can_publish_topics();
    
    	// User can edit this topic
    	} elseif ( bbp_is_topic_edit() ) {
    		$retval = current_user_can( 'edit_topic', bbp_get_topic_id() );
    	}
    return (bool) $retval;
    };
    
    // add the filter
    add_filter( 'bbp_current_user_can_access_create_topic_form', 'filter_bbp_current_user_can_access_create_topic_form', 999, 1 );
    
    /**************************************************
    Part 2: Reply to comments
    Allow anyone to reply to a comment in any forum, even if that forum is inside a group the user hasn't joined.
    If the group is hidden (e.g. "Management"), they still won't have access because that check is done first.
    ***************************************************/
    function filter_bbp_current_user_can_access_create_reply_form( $retval ) {
    
    	// Users need to earn access
    	$retval = false;
    
    	// Always allow keymasters
    	if ( bbp_is_user_keymaster() ) {
    		$retval = true;
    
    	// Looking at a single topic, topic is open, and forum is open
    	} elseif ( ( bbp_is_single_topic() || is_page() || is_single() ) && bbp_is_topic_open() && bbp_is_forum_open() ) {
    		$retval = bbp_current_user_can_publish_replies();
    
    	// User can edit this topic
    	} elseif ( bbp_is_reply_edit() ) {
    		$retval = current_user_can( 'edit_reply', bbp_get_reply_id() );
    	}
    
    return (bool) $retval;
    };
    // add the filter
    add_filter( 'bbp_current_user_can_access_create_reply_form', 'filter_bbp_current_user_can_access_create_reply_form', 999, 1 );

    If I can find where BuddyPress filters bbp_current_user_can_access_create_topic_form() and bbp_current_user_can_access_create_reply_form(), I’ll write a more elegant plugin.

    Hi Slava

    What an awesome function! Just what I was looking for 🙂

    It still works on my BuddyPress setup, but I just wanna know if you have made the function even more elegant or if you have solved the issue in another way.

    Thanks
    Torben


    mikeboltonca
    Participant

    @mikeboltonca

    Hey Torben,

    I’m glad to hear this is helpful for you!

    Sorry I don’t have any refinements to give you–I moved on to another company a couple of years ago.

    Best of luck with your project!

    Mike

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar