Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/14/2017 01:24:01 PM (7 years ago)
Author:
dcavins
Message:

Use bp_user_can for group-related permissions.

Centralize and de-duplicate permissions checks for group-related functions, like checking whether a user can join a group or send invitations from a group.

Props dcavins, djpaul.

Fixes #7610.

File:
1 edited

Legend:

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

    r11764 r11776  
    159159    }
    160160
    161     if ( 'groups' === $activity->component ) {
    162         $group = groups_get_group( $activity->item_id );
    163         if ( 'public' !== $group->status && ! groups_is_user_member( $user_id, $group->id ) ) {
    164             $send = false;
    165         }
     161    if ( 'groups' === $activity->component && ! bp_user_can( $user_id, 'groups_access_group', array( 'group_id' => $activity->item_id ) ) ) {
     162        $send = false;
    166163    }
    167164
     
    191188    return $avatar;
    192189}
     190
     191/**
     192 * Filter the bp_user_can value to determine what the user can do
     193 * with regards to a specific group.
     194 *
     195 * @since 3.0.0
     196 *
     197 * @param bool   $retval     Whether or not the current user has the capability.
     198 * @param int    $user_id
     199 * @param string $capability The capability being checked for.
     200 * @param int    $site_id    Site ID. Defaults to the BP root blog.
     201 * @param array  $args       Array of extra arguments passed.
     202 *
     203 * @return bool
     204 */
     205function bp_groups_user_can_filter( $retval, $user_id, $capability, $site_id, $args ) {
     206    if ( empty( $args['group_id'] ) ) {
     207        $group_id = bp_get_current_group_id();
     208    } else {
     209        $group_id = (int) $args['group_id'];
     210    }
     211
     212    switch ( $capability ) {
     213        case 'groups_join_group':
     214            // Return early if the user isn't logged in or the group ID is unknown.
     215            if ( ! $user_id || ! $group_id ) {
     216                break;
     217            }
     218
     219            // The group must allow joining, and the user should not currently be a member.
     220            $group = groups_get_group( $group_id );
     221            if ( 'public' === bp_get_group_status( $group )
     222                && ! groups_is_user_member( $user_id, $group->id )
     223                && ! groups_is_user_banned( $user_id, $group->id )
     224            ) {
     225                $retval = true;
     226            }
     227            break;
     228
     229        case 'groups_request_membership':
     230            // Return early if the user isn't logged in or the group ID is unknown.
     231            if ( ! $user_id || ! $group_id ) {
     232                break;
     233            }
     234
     235            /*
     236            * The group must accept membership requests, and the user should not
     237            * currently be a member, have an active request, or be banned.
     238            */
     239            $group = groups_get_group( $group_id );
     240            if ( 'private' === bp_get_group_status( $group )
     241                && ! groups_is_user_member( $user_id, $group->id )
     242                && ! groups_check_for_membership_request( $user_id, $group->id )
     243                && ! groups_is_user_banned( $user_id, $group->id )
     244            ) {
     245                $retval = true;
     246            }
     247            break;
     248
     249        case 'groups_send_invitation':
     250            // Return early if the user isn't logged in or the group ID is unknown.
     251            if ( ! $user_id || ! $group_id ) {
     252                break;
     253            }
     254
     255            /*
     256            * The group must allow invitations, and the user should not
     257            * currently be a member or be banned from the group.
     258            */
     259            $group = groups_get_group( $group_id );
     260            // Users with the 'bp_moderate' cap can always send invitations.
     261            if ( bp_user_can( $user_id, 'bp_moderate' ) ) {
     262                $retval = true;
     263            } else {
     264                $invite_status = bp_group_get_invite_status( $group_id );
     265
     266                switch ( $invite_status ) {
     267                    case 'admins' :
     268                        if ( groups_is_user_admin( $user_id, $group_id ) ) {
     269                            $retval = true;
     270                        }
     271                        break;
     272
     273                    case 'mods' :
     274                        if ( groups_is_user_mod( $user_id, $group_id ) || groups_is_user_admin( $user_id, $group_id ) ) {
     275                            $retval = true;
     276                        }
     277                        break;
     278
     279                    case 'members' :
     280                        if ( groups_is_user_member( $user_id, $group_id ) ) {
     281                            $retval = true;
     282                        }
     283                        break;
     284                }
     285            }
     286            break;
     287
     288        case 'groups_receive_invitation':
     289            // Return early if the user isn't logged in or the group ID is unknown.
     290            if ( ! $user_id || ! $group_id ) {
     291                break;
     292            }
     293
     294            /*
     295            * The group must allow invitations, and the user should not
     296            * currently be a member or be banned from the group.
     297            */
     298            $group = groups_get_group( $group_id );
     299            if ( in_array( bp_get_group_status( $group ), array( 'private', 'hidden' ), true )
     300                && ! groups_is_user_member( $user_id, $group->id )
     301                && ! groups_is_user_banned( $user_id, $group->id )
     302            ) {
     303                $retval = true;
     304            }
     305            break;
     306
     307        case 'groups_access_group':
     308            // Return early if the group ID is unknown.
     309            if ( ! $group_id ) {
     310                break;
     311            }
     312
     313            $group = groups_get_group( $group_id );
     314
     315            // If the check is for the logged-in user, use the BP_Groups_Group property.
     316            if ( $user_id === bp_loggedin_user_id() ) {
     317                $retval = $group->user_has_access;
     318
     319            /*
     320             * If the check is for a specified user who is not the logged-in user
     321             * run the check manually.
     322             */
     323            } elseif ( 'public' === bp_get_group_status( $group ) || groups_is_user_member( $user_id, $group->id ) ) {
     324                $retval = true;
     325            }
     326            break;
     327
     328        case 'groups_see_group':
     329            // Return early if the group ID is unknown.
     330            if ( ! $group_id ) {
     331                break;
     332            }
     333
     334            $group = groups_get_group( $group_id );
     335
     336            // If the check is for the logged-in user, use the BP_Groups_Group property.
     337            if ( $user_id === bp_loggedin_user_id() ) {
     338                $retval = $group->is_visible;
     339
     340            /*
     341             * If the check is for a specified user who is not the logged-in user
     342             * run the check manually.
     343             */
     344            } elseif ( 'hidden' !== bp_get_group_status( $group ) || groups_is_user_member( $user_id, $group->id ) ) {
     345                $retval = true;
     346            }
     347            break;
     348    }
     349
     350    return $retval;
     351
     352}
     353add_filter( 'bp_user_can', 'bp_groups_user_can_filter', 10, 5 );
Note: See TracChangeset for help on using the changeset viewer.