Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/12/2014 01:26:36 AM (12 years ago)
Author:
boonebgorges
Message:

Overhaul access and visibility control for group tabs

Previously, access control to group tabs was handled in two ways:

  • for BP_Group_Extension tabs, the 'enable_nav_item' and 'visibility' provided some control over access to plugin developers, though it was inconsistent, buggy, and difficult to implement properly
  • for tabs provided by bp-groups, access to the tabs of non-public groups was controlled directly in the BP_Groups_Component::setup_globals() method

Aside from being unclear for developers, this technique for controlling access
was also inflexible. For non-public groups, tab access was hardcoded and
handled before BP_Group_Extension plugins even had a chance to load. As a
result, it was essentially impossible to add public tabs to non-public groups
(among other non-standard customizations).

The current changeset comprises a number of changes that make tab access more
consistent and flexible:

  • Access control is moved to the new bp_groups_group_access_protection() function. This function has the necessary filters to customize access protection in arbitrary ways. And because it loads at 'bp_actions' - just before the page begins to render - all extensions have had a chance to load and register themselves with the desired access settings.
  • The 'visibility' and 'enable_nav_item' properties of BP_Group_Extension are phased out in favor of 'access' and 'show_tab' params. 'access' controls who can visit the tab, while 'show_tab' controls who can see the item in the navigation. These new properties have intelligent defaults (based on the privacy level of the group), but can be overridden with a number of custom settings: 'admin', 'mod', 'member', 'loggedin', 'anyone', or 'noone'. Backward compatibility is maintained, so that existing BP_Group_Extension plugins that use enable_nav_item or visibility will continue to work as before.

Fixes #4785

Props boonebgorges, dcavins, imath

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-actions.php

    r8125 r8605  
    1414// Exit if accessed directly
    1515if ( !defined( 'ABSPATH' ) ) exit;
     16
     17/**
     18 * Protect access to single groups.
     19 *
     20 * @since BuddyPress (2.1.0)
     21 */
     22function bp_groups_group_access_protection() {
     23        if ( ! bp_is_group() ) {
     24                return;
     25        }
     26
     27        $current_group   = groups_get_current_group();
     28        $user_has_access = $current_group->user_has_access;
     29        $no_access_args  = array();
     30
     31        if ( ! $user_has_access && 'hidden' !== $current_group->status ) {
     32                // Always allow access to home and request-membership
     33                if ( bp_is_current_action( 'home' ) || bp_is_current_action( 'request-membership' ) ) {
     34                        $user_has_access = true;
     35
     36                // User doesn't have access, so set up redirect args
     37                } else if ( is_user_logged_in() ) {
     38                        $no_access_args = array(
     39                                'message'  => __( 'You do not have access to this group.', 'buddypress' ),
     40                                'root'     => bp_get_group_permalink( $current_group ) . 'home/',
     41                                'redirect' => false
     42                        );
     43                }
     44        }
     45
     46        // Protect the admin tab from non-admins
     47        if ( bp_is_current_action( 'admin' ) && ! bp_is_item_admin() ) {
     48                $user_has_access = false;
     49                $no_access_args  = array(
     50                        'message'  => __( 'You are not an admin of this group.', 'buddypress' ),
     51                        'root'     => bp_get_group_permalink( $current_group ),
     52                        'redirect' => false
     53                );
     54        }
     55
     56        /**
     57         * Allow plugins to filter whether the current user has access to this group content.
     58         *
     59         * Note that if a plugin sets $user_has_access to false, it may also
     60         * want to change the $no_access_args, to avoid problems such as
     61         * logged-in users being redirected to wp-login.php.
     62         *
     63         * @since BuddyPress (2.1.0)
     64         *
     65         * @param bool $user_has_access True if the user has access to the
     66         *        content, otherwise false.
     67         * @param array $no_access_args Arguments to be passed to
     68         *        bp_core_no_access() in case of no access. Note that this
     69         *        value is passed by reference, so it can be modified by the
     70         *        filter callback.
     71         */
     72        $user_has_access = apply_filters_ref_array( 'bp_group_user_has_access', array( $user_has_access, &$no_access_args ) );
     73
     74        // If user has access, we return rather than redirect
     75        if ( $user_has_access ) {
     76                return;
     77        }
     78
     79        // Hidden groups should return a 404 for non-members.
     80        // Unset the current group so that you're not redirected
     81        // to the default group tab
     82        if ( 'hidden' == $current_group->status ) {
     83                buddypress()->groups->current_group = 0;
     84                buddypress()->is_single_item        = false;
     85                bp_do_404();
     86                return;
     87        } else {
     88                bp_core_no_access( $no_access_args );
     89        }
     90
     91}
     92add_action( 'bp_actions', 'bp_groups_group_access_protection' );
    1693
    1794/**
Note: See TracChangeset for help on using the changeset viewer.