getPermissionGranularity example

/** * {@inheritdoc} */
  public function getTranslationAccess(EntityInterface $entity$op) {
    // @todo Move this logic into a translation access control handler checking also     // the translation language and the given account.     $entity_type = $entity->getEntityType();
    $translate_permission = TRUE;
    // If no permission granularity is defined this entity type does not need an     // explicit translate permission.     if (!$this->currentUser->hasPermission('translate any entity') && $permission_granularity = $entity_type->getPermissionGranularity()) {
      $translate_permission = $this->currentUser->hasPermission($permission_granularity == 'bundle' ? "translate {$entity->bundle()} {$entity->getEntityTypeId()}" : "translate {$entity->getEntityTypeId()}");
    }
    $access = AccessResult::allowedIf(($translate_permission && $this->currentUser->hasPermission("$op content translations")))->cachePerPermissions();
    if (!$access->isAllowed()) {
      return AccessResult::allowedIfHasPermission($this->currentUser, 'translate editable entities')->andIf($entity->access('update', $this->currentUser, TRUE));
    }
    return $access;
  }

  /** * {@inheritdoc} */

  protected function getTranslatorPermissions() {
    return array_filter([$this->getTranslatePermission(), 'create content translations', 'update content translations', 'delete content translations']);
  }

  /** * Returns the translate permissions for the current entity and bundle. */
  protected function getTranslatePermission() {
    $entity_type = \Drupal::entityTypeManager()->getDefinition($this->entityTypeId);
    if ($permission_granularity = $entity_type->getPermissionGranularity()) {
      return $permission_granularity == 'bundle' ? "translate {$this->bundle} {$this->entityTypeId}" : "translate {$this->entityTypeId}";
    }
  }

  /** * Returns an array of permissions needed for the editor. */
  protected function getEditorPermissions() {
    // Every entity-type-specific test needs to define these.     return [];
  }

  
$module_handler = $this->prophesize(ModuleHandlerInterface::class)->reveal();
    $prophecy = $this->prophesize(ConfigManagerInterface::class);
    $prophecy->getConfigEntitiesToChangeOnDependencyRemoval('config', ['node.type.article'])
      ->willReturn([
        'delete' => [
          new ConfigEntityDependency('core.entity_view_display.node.article.full'),
          new ConfigEntityDependency('field.field.node.article.body'),
        ],
      ]);
    $config_manager = $prophecy->reveal();
    $prophecy = $this->prophesize(EntityTypeInterface::class);
    $prophecy->getPermissionGranularity()
      ->willReturn('entity_type');
    $entity_type = $prophecy->reveal();
    $prophecy = $this->prophesize(EntityTypeManagerInterface::class);
    $prophecy->getDefinition('entity_type')
      ->willReturn($entity_type);
    $entity_type_manager = $prophecy->reveal();

    $bundle_form = new EntityPermissionsForm($permission_handler$role_storage$module_handler$config_manager$entity_type_manager);

    // Mock the method parameters.     $route = new Route('some.path');
    
/** * Returns an array of content translation permissions. * * @return array */
  public function contentPermissions() {
    $permissions = [];
    // Create a translate permission for each enabled entity type and (optionally)     // bundle.     foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      if ($permission_granularity = $entity_type->getPermissionGranularity()) {
        switch ($permission_granularity) {
          case 'bundle':
            foreach ($this->entityTypeBundleInfo->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
              if ($this->contentTranslationManager->isEnabled($entity_type_id$bundle)) {
                $permissions["translate $bundle $entity_type_id"] = $this->buildBundlePermission($entity_type$bundle$bundle_info);
              }
            }
            break;

          case 'entity_type':
            if ($this->contentTranslationManager->isEnabled($entity_type_id)) {
              
if ($access->isAllowed()) {
        return $access;
      }

      // Check "translate any entity" permission.       if ($account->hasPermission('translate any entity')) {
        return AccessResult::allowed()->cachePerPermissions()->inheritCacheability($access);
      }

      // Check per entity permission.       $permission = "translate {$entity_type_id}";
      if ($definition->getPermissionGranularity() == 'bundle') {
        $permission = "translate {$bundle} {$entity_type_id}";
      }
      return AccessResult::allowedIfHasPermission($account$permission)->inheritCacheability($access);
    }

    // No opinion.     return AccessResult::neutral();
  }

}
Home | Imprint | This part of the site doesn't use cookies.