You are here

function social_group_block_access in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/social_group.module \social_group_block_access()
  2. 8.2 modules/social_features/social_group/social_group.module \social_group_block_access()
  3. 8.3 modules/social_features/social_group/social_group.module \social_group_block_access()
  4. 8.4 modules/social_features/social_group/social_group.module \social_group_block_access()
  5. 8.5 modules/social_features/social_group/social_group.module \social_group_block_access()
  6. 8.6 modules/social_features/social_group/social_group.module \social_group_block_access()
  7. 8.7 modules/social_features/social_group/social_group.module \social_group_block_access()
  8. 8.8 modules/social_features/social_group/social_group.module \social_group_block_access()
  9. 10.3.x modules/social_features/social_group/social_group.module \social_group_block_access()
  10. 10.0.x modules/social_features/social_group/social_group.module \social_group_block_access()
  11. 10.1.x modules/social_features/social_group/social_group.module \social_group_block_access()
  12. 10.2.x modules/social_features/social_group/social_group.module \social_group_block_access()

Implements hook_block_access().

Check if the user is viewing a closed_group, check if the user is a member. If the user is not a member, the user has no access to view this block.

File

modules/social_features/social_group/social_group.module, line 1006
The Social group module.

Code

function social_group_block_access(Block $block, $operation, AccountInterface $account) {
  if ($block
    ->getPluginId() == 'views_exposed_filter_block:newest_groups-page_all_groups' && $operation == 'view' && $account
    ->isAnonymous()) {
    $group_types = 0;

    /** @var \Drupal\group\Entity\GroupTypeInterface $group_type */
    foreach (GroupType::loadMultiple() as $group_type) {
      $permissions = $group_type
        ->getAnonymousRole()
        ->getPermissions();
      if (in_array('view group', $permissions)) {
        $group_types++;
      }
    }
    if ($group_types < 2) {
      return AccessResult::forbidden();
    }
  }
  $group = _social_group_get_current_group();
  $user = Drupal::currentUser();

  // Check if there is a group set and if its an closed group.
  if ($group && $group
    ->getGroupType()
    ->id() == 'closed_group' && $account
    ->id() != 1) {
    if ($account
      ->hasPermission('manage all groups')) {
      return AccessResult::neutral();
    }
    elseif (!$group
      ->getMember($user)) {

      // If it is closed and the current user is not an member of this group,
      // then he is not allowed to see these blocks.
      $forbidden_blocks = [
        'views_block:upcoming_events-upcoming_events_group',
        'views_block:latest_topics-group_topics_block',
      ];
      foreach ($forbidden_blocks as $forbidden_block) {
        if ($operation == 'view' && $block
          ->getPluginId() == $forbidden_block) {
          return AccessResult::forbidden();
        }
      }
    }
  }
  return AccessResult::neutral();
}