You are here

function webform_group_webform_access in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_group/webform_group.module \webform_group_webform_access()

Implements hook_ENTITY_TYPE_access() for webform entities.

File

modules/webform_group/webform_group.module, line 297
Provides a Webform integration with the Group module.

Code

function webform_group_webform_access(WebformInterface $webform, $operation, AccountInterface $account) {

  // Prevent recursion when a webform is being passed as the source entity
  // via the URL.
  // @see \Drupal\webform\Plugin\WebformSourceEntity\QueryStringWebformSourceEntity::getSourceEntity
  if (\Drupal::request()->query
    ->get('source_entity_type') === 'webform') {
    return AccessResult::neutral();
  }

  /** @var \Drupal\webform_group\WebformGroupManagerInterface $webform_group_manager */
  $webform_group_manager = \Drupal::service('webform_group.manager');

  // Get the current user's group roles for the current group content.
  $current_user_group_roles = $webform_group_manager
    ->getCurrentUserGroupRoles();

  // Get webform's access rules.
  $access_rules = $webform_group_manager
    ->getAccessRules($webform);

  // Get access rules permission name.
  $permission = str_replace('submission_', '', $operation);

  // Make sure the permission exists.
  if (!isset($access_rules[$permission])) {
    return AccessResult::neutral();
  }

  // Compare the current user group roles with the admin and permission
  // access rules' group roles.
  $is_admin = array_intersect($access_rules['administer']['group_roles'], $current_user_group_roles);
  $has_permission = array_intersect($access_rules[$permission]['group_roles'], $current_user_group_roles);
  return AccessResult::allowedIf($is_admin || $has_permission)
    ->cachePerUser()
    ->addCacheableDependency($webform);
}