You are here

function social_group_can_view_groups_of_type in Open Social 8.9

Same name and namespace in other branches
  1. 8.6 modules/social_features/social_group/social_group.module \social_group_can_view_groups_of_type()
  2. 8.7 modules/social_features/social_group/social_group.module \social_group_can_view_groups_of_type()
  3. 8.8 modules/social_features/social_group/social_group.module \social_group_can_view_groups_of_type()
  4. 10.3.x modules/social_features/social_group/social_group.module \social_group_can_view_groups_of_type()
  5. 10.0.x modules/social_features/social_group/social_group.module \social_group_can_view_groups_of_type()
  6. 10.1.x modules/social_features/social_group/social_group.module \social_group_can_view_groups_of_type()
  7. 10.2.x modules/social_features/social_group/social_group.module \social_group_can_view_groups_of_type()

Determine whether a user can see groups of a given type as an outsider.

Parameters

string $type: The group type to check.

\Drupal\Core\Session\AccountInterface $account: The user to check for.

Return value

bool Whether the user is allowed to view groups of the given type.

3 calls to social_group_can_view_groups_of_type()
social_group_form_alter in modules/social_features/social_group/social_group.module
Implements hook_form_alter().
social_group_secret_can_view_secret_groups in modules/social_features/social_group/modules/social_group_secret/social_group_secret.module
Determine whether a user can see secret groups as outsider.
_social_group_get_current_group_types in modules/social_features/social_group/social_group.module
Determine the amount of group_types a user can see.

File

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

Code

function social_group_can_view_groups_of_type(string $type, AccountInterface $account) : bool {
  $group_type = GroupType::load($type);

  // If the group type doesn't exist then the user can't see the group type.
  if ($group_type === FALSE || $group_type === NULL) {
    return FALSE;
  }

  // Anonymous users have a special role.
  if ($account
    ->isAnonymous()) {

    /** @var \Drupal\group\Entity\GroupRoleInterface $role */
    return $group_type
      ->getAnonymousRole()
      ->hasPermission('view group');
  }

  // The service that keeps platform roles in sync with group roles for
  // outsiders.

  /** @var \Drupal\group\GroupRoleSynchronizerInterface $group_role_synchroniser */
  $group_role_synchroniser = \Drupal::service('group_role.synchronizer');

  /** @var \Drupal\group\Entity\GroupRoleInterface[] $group_roles */
  $group_roles = $group_type
    ->getRoles();
  $user_roles = $account
    ->getRoles(TRUE);

  // Authenticated is a locked Role,
  // So first we check outsiders also easier on the performance :)
  $outsider = $group_type
    ->getOutsiderRole();
  if (NULL !== $outsider && $outsider
    ->hasPermission('view group')) {
    return TRUE;
  }

  // If members can see it, we also need to show group types because
  // we are not calculation memberships.
  $member = $group_type
    ->getMemberRole();
  if (NULL !== $member && $member
    ->hasPermission('view group')) {
    return TRUE;
  }

  // Check each role this user has whether its group counterpart has the correct
  // permission. This counts for the advanced group permissions.
  // Admin, SM, CM.
  foreach ($user_roles as $role) {
    $group_role = $group_role_synchroniser
      ->getGroupRoleId($type, $role);
    if ($group_roles[$group_role]
      ->hasPermission('view group')) {
      return TRUE;
    }
  }

  // The user has no role that can view the group type.
  return FALSE;
}