You are here

function _opigno_forum_access in Opigno forum 8

Same name and namespace in other branches
  1. 3.x opigno_forum.module \_opigno_forum_access()

Checks user access to the forum.

Parameters

int $tid: Forum tid.

\Drupal\Core\Session\AccountInterface $account: User to check.

Return value

bool Forum access.

6 calls to _opigno_forum_access()
ForumAccessCheck::access in src/Access/ForumAccessCheck.php
Returns forum access.
opigno_forum_entity_presave in ./opigno_forum.module
Implements hook_entity_presave().
opigno_forum_form_node_form_alter in ./opigno_forum.module
Implements hook_form_BASE_FORM_ID_alter().
opigno_forum_node_access in ./opigno_forum.module
Implements hook_node_access().
opigno_forum_preprocess_forum_list in ./opigno_forum.module
Implements hook_preprocess_HOOK().

... See full list

File

./opigno_forum.module, line 122
Contains opigno_forum.module.

Code

function _opigno_forum_access($tid, AccountInterface $account) {
  if ($account
    ->hasPermission('manage group content in any group') || $account
    ->hasPermission('manage group members in any group')) {

    // Allow access if user is admin or platform-level manager.
    return TRUE;
  }

  // Get user groups.
  $membership_service = \Drupal::service('group.membership_loader');
  $memberships = $membership_service
    ->loadByUser($account);

  /** @var \Drupal\group\Entity\GroupInterface[] $groups */
  $groups = array_map(function ($membership) {

    /** @var \Drupal\group\GroupMembership $membership */
    return $membership
      ->getGroup();
  }, $memberships);

  // Allow access to the forum if it is attached to the group
  // that user belongs to.
  foreach ($groups as $group) {
    if ($group
      ->hasField('field_learning_path_enable_forum') && $group
      ->hasField('field_learning_path_forum')) {
      $enable_forum_field = $group
        ->get('field_learning_path_enable_forum')
        ->getValue();
      $forum_field = $group
        ->get('field_learning_path_forum')
        ->getValue();
      if (!empty($enable_forum_field) && !empty($forum_field)) {
        $is_forum_enabled = $enable_forum_field[0]['value'];
        $forum_tids = array_map(function ($item) {
          return $item['target_id'];
        }, $forum_field);
        if ($is_forum_enabled && in_array($tid, $forum_tids)) {
          return TRUE;
        }
      }
    }
  }
  return FALSE;
}