BuddyDev

Search

Restrict joining of certain buddypress groups to specific member types

  • Participant
    Level: Initiated
    Posts: 19
    adam gibbs on #33745

    This is very similar to what I am trying to achieve. I too have the member type pro and have tried using pp capabilities with it but it hasn’t done what I hope to achieve – it hasn’t done anything basically.

    Here is what I was hoping to accomplish, I hope I haven’t explained it in an over complicated way, I have made a diagram (see image) to help explain.

    diagram > https://imgur.com/a/AyVzZsP

    I have 2 types of user, fans and artists. There are more ‘kinds’ of artists than fans. Let’s call them:

    Member Type A (fan)
    Member Type B (artist kind 1)
    Member Type C (artist kind 2)
    Member Type D (artist kind 3)
    Member Type E (artist kind 4)
    etc

    I have mapped all above to the same named user roles:

    Member Type A –> User Role A
    Member Type B –> User Role B
    Member Type C –> User Role C
    Member Type D –> User Role D
    Member Type E –> User Role E
    etc

    Each different member type has joined their own (Main) group via regitration and called the groups the same as the user roles and member types:

    Member Type A –> User Role A –> Group A
    Member Type B –> User Role B –> Group B
    Member Type C –> User Role C –> Group C
    Member Type D –> User Role D –> Group D
    Member Type E –> User Role E –> Group E
    etc

    I am trying to give, artists more group permissions over fans:

    Member Type A

    – Can view ALL groups, like and comment posts etc inc groups B,C,D,E.
    – Can create their own aditional groups and join each others (Member Type A) groups.
    – Can NOT join artist groups B,C,D,E or newly created artist groups by members B,C,D,E.

    Member Types B,C,D,E

    – Can view ALL groups, like and comment posts etc inc groups A, B,C,D,E.
    – Can create their own aditional groups and join each others.
    – CAN join groups A, B,C,D,E or newly created groups by members A, B,C,D,E.

    In short:

    Member Type A can ONLY join Member Type A groups BUT is allowed to view/particiapate in Member Type B,C,D,E groups – like, comment etc

    Member Type B can join and participate in ALL groups by ALL member types.

    Brajesh, the code (see below) you provided to someone previously for a similar request is almost there. Any suggestions would be appreciated.

    Apologies for the long message 🙂

    Regards,
    A

    /**
     * Restrict user from accesisng groups of which he/she is not a member and it's not allowed as part of their member type.
     */
    function buddydev_restrict_group_access_based_on_member_type() {
    
    	if ( ! is_user_logged_in() || ! bp_is_group() || is_super_admin() ) {
    		return;// it's not group or the user is not logged in, do not restrict.
    	}
    
    	$group_id = groups_get_current_group()->id;
    	$user_id  = bp_loggedin_user_id();
    
    	// user's own group, do not restrict.
    	if ( groups_is_user_member( $user_id, $group_id ) ) {
    		return;
    	}
    
    	// Member types pro is not active, return.
    	if ( ! function_exists( 'bpmtp_member_types_pro' ) ) {
    		return;
    	}
    
    	// Get all member types for user, user may have multiple member type.
    	$member_types = bp_get_member_type( $user_id, false );
    
    	if ( empty( $member_types ) ) {
    	    return ;
    		// what should we do? the user does not have any member type.
    	}
    
    	// let us build a list of group ids allowed with these member types.
    	$group_ids = array();
    
    	$active_types = bpmtp_get_active_member_type_entries();
    
    	foreach ( $member_types as $member_type ) {
    		// not valid.
    	    if ( empty( $member_type ) || empty( $active_types[ $member_type ] ) ) {
    			continue;
    		}
    
    		$mt_object            = $active_types[ $member_type ];
    		$associate_groups_ids = get_post_meta( $mt_object->post_id, '_bp_member_type_groups', true );
    	    // merge.
    		if ( ! empty( $associate_groups_ids ) ) {
    			$group_ids = array_merge( $group_ids, $associate_groups_ids );
    		}
    	}
    
    	if ( ! empty( $group_ids ) && in_array( $group_id, $group_ids ) ) {
    		return;// this group is associated with the member types the user has, so do not restrict.
    	}
    
    	// Restrict if we are here,
    	$referrer = wp_get_referer();
    	$referrer = $referrer ? $referrer : site_url( '/' );
    	// add notice.
    	bp_core_add_message( 'Access restricted', 'error' );
    	bp_core_redirect( $referrer );
    }
    
    add_action( 'bp_template_redirect', 'buddydev_restrict_group_access_based_on_member_type' );
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #33764

    Hi Adam,
    Thank you for the question.

    I am sorry but the kind of restrictions you are looking for is will need time way beyond we offer here as support.

    The other user had a basic question about limiting viewing of a group. Your issue is more of a restrictions. We are working on a solution for this and will be offering in future.

    If you need custom assistance with it, Please feel free to use our services.
    https://buddydev.com/services/

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 19
    adam gibbs on #33785

    OK thanks Brajesh

    A

The topic ‘Restrict joining of certain buddypress groups to specific member types’ is closed to new replies.

This topic is: not resolved