BuddyDev

Search

[Resolved] Member type to restrict and grant access to groups

  • Participant
    Level: Initiated
    Posts: 2
    Tom Migot on #11633

    Hi,

    i have bought your member type plugin but I am not sure whether this is actually what I need. I have a buddypress site with different cascading membership levels.
    Depending on the level, one has access to one or more groups. My current problem is that woocomerce membership does not prevent a user from access a url unlike other membership plugins. But even the other membership plugins seems to be limited when it comes to buddypress.
    I noticed that if a low level user (A) goes on the activity page and see that a higher level user (B) posted a comment in a forum that A is not supposed to have access, A can still click on the name of the group or forum mentioned in the activity and access the group and its forum.

    My understanding is that member types would enable me to restrict the access to a group and while a user could see a higher level group activity in the main activity page, even if he clicked on the link he would be notified the content is not available to him because of his low privileges.

    The issue I have is there does not seem to be a way to link the user membership (based on a subscription purchase) and a member type. I understand your plugin enables me to change the membership plan when changing the member type but I need the other way around.

    The last thing I want is a system that requires me to manually set the member type of a user.
    I even tried to utilise the WP role feature. I was able to automatically set a custom user role based on the membership plan purchased. But again your plugin works the other way around of what i need. I do not want the member type to define the role but have the role define the member type.

    I cannot imagine I am the only one faced with such issue.

    Any suggestion would greatly help otherwise I will ask for a refund since your plugin does not help my case.

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #11645

    Hi Tom,
    Thank you for purchasing the plugin.

    Let me clarify a few things first:-

    1. you can set the member type manually using any of the following option
    a) from the User->All Users screen you can select the change member type option and change it in bulk/single.
    b) If you have xprofile field(member types, It is not necessary though), You can do it from user profile edit on the front end or the User Profile edit extended section in the backend.

    2. You can assign member types based on roles, The plugin allows you to map that too. Please see
    https://buddydev.com/docs/guides/plugins/buddypress-plugins/buddypress-member-types-pro/assigning-buddypress-member-types-based-wordpress-roles/

    Now, here is the solution I am proposing.

    1. Map role to Member types(role change should update user member type)
    2. Use WooCommerce membership to make role change, It will trigger the change of member type
    3. Do not let member type change WooCommerce Membership(Just don’t associate on the member type screen)

    Now, we have a system where your user registers via WooCommerce or purchases membership via WooCommerce
    and his member type is based on which membership he/she purchase.

    The first part of your issue is resolved by that approach.

    Now, restricting the content of a group based on member types.

    With the above setup, we know which member type has which groups associated(We are changing the meaning of group association here, will use them to sandbox the user).

    We can write some code to stop users from accessing groups not in their member type’s allowed list. I will post code example today/tomorrow for this.

    Hope it helps.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 2
    Tom Migot on #11646

    Hi Brajesh,

    thanks you for your swift reply. I am impressed. I Have read the link you gave me and i am glad to see that we can set the type dynamically based on the user role.
    One thing i had not realised (you response seems to imply) that while we assign group to a member type, that actually does not mean that another user of a different member type which does not include that group is prevented from seeing it.

    Is my understanding correct?

    Type A is associated to group A
    Type B is associated to Group AB

    Users of type A can access A but also B? If so what is the point of associating a type and a group?

    Thanks again for your help and look forward to the code you come up with.

    Cheers,
    Tom

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #11647

    Hi Tom,
    Thank you.

    I am sorry for the confusion about group association, The goal is to automatically assign groups to user when their member type changes.

    I do see other user cases and most probably we will extend it more in future. At the moment, we do not restrict based on member types but if you or someone can suggest a list of extensive restrictions, I will be happy to have them in the plugin. We do want to look into this territory.

    Regards
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #11662

    Hi Tom,
    You may put thhis code in your bp-custom.php

    
    
    /**
     * 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' );
    
    

    It will redirect logged in user to previous page if they are not member of the group or is not allowed by member type.

    Hope that helps.

  • Participant
    Level: Initiated
    Posts: 2
    Tom Migot on #11663

    Hi Brajesh,

    thank you for writting this so quikcly. Can you confirm I can add this to my child theme functions.php? I can see you added the function as an action. When is this action being triggered?

    I am going to test this this saturday and will let you know the outcome.

    Cheers,
    Tom

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #11664

    Hi Tom,
    Yes, you can. bp_template_redirect is hooked to WordPress template_redirect action, so it will work from the child theme’s functions.php too.

    Regards
    Brajesh

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

    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 and particiapate in Member Type B,C,D,E groups – like, comment etc (but not upload media via mediapress)

    Member Type B can join and participate in ALL groups by ALL member types and upload media to any group.

    Brajesh, the code you provided above is almost there. Any suggestions would be appreciated.

    Apologies for the long message 🙂

    Regards,
    A

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

    EDIT: seconds thoughts don’t worry about the mediapress permission…

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #33728

    Hi Adam,
    Thank you fro the question.

    Please open a separate thread and link to here. The original author might be getting unnecessary notices about the replies. Let us keep our discussion separate.

    Thank you
    Brajesh

The topic ‘ [Resolved] Member type to restrict and grant access to groups’ is closed to new replies.

This topic is: resolved