Skip to:
Content

BuddyPress.org

Ticket #4785: 4785.02.patch

File 4785.02.patch, 6.6 KB (added by boonebgorges, 10 years ago)
  • bp-groups/bp-groups-actions.php

    diff --git bp-groups/bp-groups-actions.php bp-groups/bp-groups-actions.php
    index b81d244..1e54d80 100644
     
    1515if ( !defined( 'ABSPATH' ) ) exit;
    1616
    1717/**
     18 * Protect access to single groups.
     19 *
     20 * @since BuddyPress (2.0.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 {
     38                        if ( is_user_logged_in() ) {
     39                                $no_access_args = array(
     40                                        'message'  => __( 'You do not have access to this group.', 'buddypress' ),
     41                                        'root'     => bp_get_group_permalink( $current_group ) . 'home/',
     42                                        'redirect' => false
     43                                );
     44                        }
     45                }
     46        }
     47
     48        // Protect the admin tab from non-admins
     49        if ( bp_is_current_action( 'admin' ) && ! bp_is_item_admin() ) {
     50                $user_has_access = false;
     51                $no_access_args  = array(
     52                        'message'  => __( 'You are not an admin of this group.', 'buddypress' ),
     53                        'root'     => bp_get_group_permalink( $current_group ),
     54                        'redirect' => false
     55                );
     56        }
     57
     58        $user_has_access = apply_filters( 'bp_group_user_has_access', $user_has_access );
     59
     60        if ( $user_has_access ) {
     61                return;
     62        }
     63
     64        // Hidden groups should return a 404 for non-members.
     65        // Unset the current group so that you're not redirected
     66        // to the default group tab
     67        if ( 'hidden' == $current_group->status ) {
     68                buddypress()->groups->current_group = 0;
     69                buddypress()->is_single_item        = false;
     70                bp_do_404();
     71                return;
     72        } else {
     73                bp_core_no_access( $no_access_args );
     74        }
     75}
     76add_action( 'bp_actions', 'bp_groups_group_access_protection' );
     77
     78/**
    1879 * Catch and process group creation form submissions.
    1980 */
    2081function groups_action_create_group() {
    function groups_action_leave_group() { 
    281342                } else {
    282343                        bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) );
    283344                }
    284                        
     345
    285346                $redirect = bp_get_group_permalink( groups_get_current_group() );
    286                
     347
    287348                if( 'hidden' == $bp->groups->current_group->status ) {
    288349                        $redirect = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() );
    289350                }
  • bp-groups/bp-groups-classes.php

    diff --git bp-groups/bp-groups-classes.php bp-groups/bp-groups-classes.php
    index 95e102e..560815f 100644
    class BP_Group_Extension { 
    29832983                        'display_hook'      => $this->display_hook,
    29842984                        'template_file'     => $this->template_file,
    29852985                        'screens'           => $this->get_default_screens(),
     2986                        'access'            => array(
     2987                                'public'  => 'anyone',
     2988                                'private' => 'members',
     2989                                'hidden'  => 'members',
     2990                        ),
    29862991                ) );
    29872992
    29882993                $this->initialized = true;
    class BP_Group_Extension { 
    31773182
    31783183                        // When we are viewing the extension display page, set the title and options title
    31793184                        if ( bp_is_current_action( $this->slug ) ) {
     3185                                add_filter( 'bp_group_user_has_access',   array( $this, 'user_has_access' ) );
    31803186                                add_action( 'bp_template_content_header', create_function( '', 'echo "' . esc_attr( $this->name ) . '";' ) );
    31813187                                add_action( 'bp_template_title',          create_function( '', 'echo "' . esc_attr( $this->name ) . '";' ) );
    31823188                        }
    class BP_Group_Extension { 
    31963202                bp_core_load_template( apply_filters( 'bp_core_template_plugin', $this->template_file ) );
    31973203        }
    31983204
     3205        /**
     3206         * Determine whether the current user has access to this tab.
     3207         *
     3208         * @since BuddyPress (2.0.0)
     3209         *
     3210         * @return bool
     3211         */
     3212        protected function user_has_access( $user_has_access ) {
     3213                if ( current_user_can( 'bp_moderate' ) ) {
     3214                        return true;
     3215                }
     3216
     3217                $group = groups_get_group( array(
     3218                        'group_id' => $this->group_id,
     3219                ) );
     3220
     3221                $access_setting = '';
     3222                if ( isset( $this->params['access'][ $group->status ] ) ) {
     3223                        $access_setting = $this->params['access'][ $group->status ];
     3224                }
     3225
     3226                switch ( $access_setting ) {
     3227                        case 'admins' :
     3228                                $user_has_access = groups_is_user_admin( bp_loggedin_user_id(), $this->group_id );
     3229                                break;
     3230
     3231                        case 'mods' :
     3232                                $user_has_access = groups_is_user_mod( bp_loggedin_user_id(), $this->group_id );
     3233                                break;
     3234
     3235                        case 'members' :
     3236                                $user_has_access = groups_is_user_member( bp_loggedin_user_id(), $this->group_id );
     3237                                break;
     3238
     3239                        case 'loggedin' :
     3240                                $user_has_access = is_user_logged_in();
     3241                                break;
     3242                }
     3243
     3244                return $user_has_access;
     3245        }
     3246
    31993247        /** Create ************************************************************/
    32003248
    32013249        /**
  • bp-groups/bp-groups-loader.php

    diff --git bp-groups/bp-groups-loader.php bp-groups/bp-groups-loader.php
    index b40bf45..b19d196 100644
    class BP_Groups_Component extends BP_Component { 
    268268
    269269                }
    270270
    271                 // Group access control
    272                 if ( bp_is_groups_component() && !empty( $this->current_group ) ) {
    273                         if ( !$this->current_group->user_has_access ) {
    274 
    275                                 // Hidden groups should return a 404 for non-members.
    276                                 // Unset the current group so that you're not redirected
    277                                 // to the default group tab
    278                                 if ( 'hidden' == $this->current_group->status ) {
    279                                         $this->current_group = 0;
    280                                         $bp->is_single_item  = false;
    281                                         bp_do_404();
    282                                         return;
    283 
    284                                 // Skip the no_access check on home and membership request pages
    285                                 } elseif ( !bp_is_current_action( 'home' ) && !bp_is_current_action( 'request-membership' ) ) {
    286 
    287                                         // Off-limits to this user. Throw an error and redirect to the group's home page
    288                                         if ( is_user_logged_in() ) {
    289                                                 bp_core_no_access( array(
    290                                                         'message'  => __( 'You do not have access to this group.', 'buddypress' ),
    291                                                         'root'     => bp_get_group_permalink( $bp->groups->current_group ) . 'home/',
    292                                                         'redirect' => false
    293                                                 ) );
    294 
    295                                         // User does not have access, and does not get a message
    296                                         } else {
    297                                                 bp_core_no_access();
    298                                         }
    299                                 }
    300                         }
    301 
    302                         // Protect the admin tab from non-admins
    303                         if ( bp_is_current_action( 'admin' ) && !bp_is_item_admin() ) {
    304                                 bp_core_no_access( array(
    305                                         'message'  => __( 'You are not an admin of this group.', 'buddypress' ),
    306                                         'root'     => bp_get_group_permalink( $bp->groups->current_group ),
    307                                         'redirect' => false
    308                                 ) );
    309                         }
    310                 }
    311 
    312271                // Preconfigured group creation steps
    313272                $this->group_creation_steps = apply_filters( 'groups_create_group_steps', array(
    314273                        'group-details'  => array(