getPermissionName example


  protected function checkAccess(EntityInterface $filter_format$operation, AccountInterface $account) {
    /** @var \Drupal\filter\FilterFormatInterface $filter_format */

    // All users are allowed to use the fallback filter.     if ($operation == 'use') {
      if ($filter_format->isFallbackFormat()) {
        return AccessResult::allowed();
      }
      else {
        return AccessResult::allowedIfHasPermission($account$filter_format->getPermissionName());
      }
    }

    // The fallback format may not be disabled.     if ($operation == 'disable' && $filter_format->isFallbackFormat()) {
      return AccessResult::forbidden();
    }

    // We do not allow filter formats to be deleted through the UI, because that     // would render any content that uses them unusable.     if ($operation == 'delete') {
      
/** @var \Drupal\filter\FilterFormatInterface $filter_test_format */
    $filter_test_format = FilterFormat::load('filter_test');
    /** @var \Drupal\filter\FilterFormatInterface $filtered_html_format */
    $filtered_html_format = FilterFormat::load('filtered_html');
    /** @var \Drupal\filter\FilterFormatInterface $full_html_format */
    $full_html_format = FilterFormat::load('full_html');

    // Create users.     $this->adminUser = $this->drupalCreateUser([
      'administer filters',
      $filtered_html_format->getPermissionName(),
      $full_html_format->getPermissionName(),
      $filter_test_format->getPermissionName(),
    ]);

    $this->webUser = $this->drupalCreateUser([
      $filtered_html_format->getPermissionName(),
      $filter_test_format->getPermissionName(),
    ]);
  }

  /** * Tests various different configurations of the 'text_format' element. */
$restricted_html_format->save();
    $full_html_format = FilterFormat::create([
      'format' => 'full_html',
      'name' => 'Full HTML',
      'weight' => 1,
      'filters' => [],
    ]);
    $full_html_format->save();

    $this->adminUser = $this->drupalCreateUser([
      'administer filters',
      $basic_html_format->getPermissionName(),
      $restricted_html_format->getPermissionName(),
      $full_html_format->getPermissionName(),
      'access site reports',
    ]);

    $this->webUser = $this->drupalCreateUser([
      'create page content',
      'edit own page content',
    ]);
    user_role_grant_permissions('authenticated', [$basic_html_format->getPermissionName()]);
    user_role_grant_permissions('anonymous', [$restricted_html_format->getPermissionName()]);
    
$this->submitForm($edit, 'Save configuration');
      $this->resetFilterCaches();
      $formats[] = FilterFormat::load($edit['format']);
    }
    [$this->allowedFormat, $this->secondAllowedFormat, $this->disallowedFormat] = $formats;
    $this->drupalLogout();

    // Create a regular user with access to two of the formats.     $this->webUser = $this->drupalCreateUser([
      'create page content',
      'edit any page content',
      $this->allowedFormat->getPermissionName(),
      $this->secondAllowedFormat->getPermissionName(),
    ]);

    // Create an administrative user who has access to use all three formats.     $this->adminUser = $this->drupalCreateUser([
      'administer filters',
      'create page content',
      'edit any page content',
      $this->allowedFormat->getPermissionName(),
      $this->secondAllowedFormat->getPermissionName(),
      $this->disallowedFormat->getPermissionName(),
    ]);
parent::setUp();

    $format = FilterFormat::create([
      'format' => 'media_embed_test',
      'name' => 'Test format',
      'filters' => [],
    ]);
    $format->save();

    $this->drupalLogin($this->drupalCreateUser([
      'administer filters',
      $format->getPermissionName(),
    ]));
  }

  /** * @covers ::media_form_filter_format_add_form_alter * @dataProvider providerTestValidations */
  public function testValidationWhenAdding($filter_html_status$filter_align_status$filter_caption_status$filter_html_image_secure_status$media_embed$allowed_html$expected_error_message) {
    $this->drupalGet('admin/config/content/formats/add');

    // Enable the `filter_html` and `media_embed` filters.
$full_html_format = FilterFormat::create([
      'format' => 'full_html',
      'name' => 'Full HTML',
    ]);
    $full_html_format->save();
    $html_user = $this->drupalCreateUser([
      'access comments',
      'post comments',
      'edit own comments',
      'skip comment approval',
      'access content',
      $filtered_html_format->getPermissionName(),
      $full_html_format->getPermissionName(),
    ]);
    $this->drupalLogin($html_user);
    $this->drupalGet('node/' . $this->node->id());

    // HTML should not be included in the character count.     $body_text1 = '<span></span><strong> </strong><span> </span><strong></strong>Hello World<br />';
    $edit1 = [
      'comment_body[0][value]' => $body_text1,
      'comment_body[0][format]' => 'filtered_html',
    ];
    
$format->set($key$value);
      }
      else {
        foreach ($value as $instance_id => $config) {
          $format->setFilterConfig($instance_id$config);
        }
      }
    }
    $format->save();

    // Save user permissions.     if ($permission = $format->getPermissionName()) {
      foreach ($form_state->getValue('roles') as $rid => $enabled) {
        user_role_change_permissions($rid[$permission => $enabled]);
      }
    }

    return $this->entity;
  }

  /** * {@inheritdoc} */
  
// Clear the static caches of filter_formats() and others.     filter_formats_reset();

    if (!$update && !$this->isSyncing()) {
      // Default configuration of modules and installation profiles is allowed       // to specify a list of user roles to grant access to for the new format;       // apply the defined user role permissions when a new format is inserted       // and has a non-empty $roles property.       // Note: user_role_change_permissions() triggers a call chain back into       // \Drupal\filter\FilterPermissions::permissions() and lastly       // filter_formats(), so its cache must be reset upfront.       if (($roles = $this->get('roles')) && $permission = $this->getPermissionName()) {
        foreach (user_roles() as $rid => $name) {
          $enabled = in_array($rid$roles, TRUE);
          user_role_change_permissions($rid[$permission => $enabled]);
        }
      }
    }
  }

  /** * Returns if this format is the fallback format. * * The fallback format can never be disabled. It must always be available. * * @return bool * TRUE if this format is the fallback format, FALSE otherwise. */
$format->set('filters', [
      'filter_align' => ['status' => TRUE],
      'filter_caption' => ['status' => TRUE],
      'media_embed' => ['status' => $media_embed_enabled],
    ]);
    $format->save();

    $permissions = [
      'bypass node access',
    ];
    if ($can_use_format) {
      $permissions[] = $format->getPermissionName();
    }
    $this->drupalLogin($this->drupalCreateUser($permissions));
    $this->drupalGet($this->host->toUrl('edit-form'));

    $assert_session = $this->assertSession();
    if ($can_use_format) {
      $this->waitForEditor();
      if ($media_embed_enabled) {
        // The preview rendering, which in this test will use Starterkit theme's         // media.html.twig template, will fail without the CSRF token/header.         // @see ::testEmbeddedMediaPreviewWithCsrfToken()
$manager->getDefinitions();
    /** @var \Drupal\filter\FilterFormatInterface $filter_test_format */
    $filter_test_format = FilterFormat::load('filter_test');
    $full_html_format = FilterFormat::load('full_html');
    $filtered_html_format = FilterFormat::load('filtered_html');

    /** @var \Drupal\user\RoleInterface $role */
    $role = Role::create([
      'id' => 'admin',
      'label' => 'admin',
    ]);
    $role->grantPermission($filter_test_format->getPermissionName());
    $role->grantPermission($full_html_format->getPermissionName());
    $role->grantPermission($filtered_html_format->getPermissionName());
    $role->save();
    $this->testUser = User::create([
      'name' => 'foobar',
      'mail' => 'foobar@example.com',
    ]);
    $this->testUser->addRole($role->id());
    $this->testUser->save();
    \Drupal::service('current_user')->setAccount($this->testUser);
  }

  
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Create Basic page node type.     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);

    /** @var \Drupal\filter\Entity\FilterFormat $filtered_html_format */
    $filtered_html_format = FilterFormat::load('filtered_html');
    $filtered_html_permission = $filtered_html_format->getPermissionName();
    user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [$filtered_html_permission]);

    $this->adminUser = $this->drupalCreateUser([
      'administer modules',
      'administer filters',
      'administer site configuration',
    ]);
    $this->drupalLogin($this->adminUser);
  }

  /** * Tests removal of filtered content when an active filter is disabled. * * Tests that filtered content is emptied when an actively used filter module * is disabled. */
/** @var \Drupal\filter\FilterFormatInterface $full_html_format */
    $full_html_format = FilterFormat::load('full_html');

    $admin_permissions = array_merge(
      $translator_permissions,
      [
        'administer languages',
        'administer site configuration',
        'link to any page',
        'administer contact forms',
        'administer filters',
        $filtered_html_format->getPermissionName(),
        $full_html_format->getPermissionName(),
        $filter_test_format->getPermissionName(),
        'access site-wide contact form',
        'access contextual links',
        'administer views',
        'administer account settings',
        'administer themes',
        'bypass node access',
        'administer content types',
        'translate interface',
      ]
    );
'format' => 'full_html',
      'name' => 'Full HTML',
      'weight' => 1,
      'filters' => [],
    ]);
    $full_html_format->save();

    // Create and log in an administrative user having access to the Full HTML     // text format.     $permissions = [
      'administer filters',
      $full_html_format->getPermissionName(),
      'administer permissions',
      'create page content',
      'post comments',
      'skip comment approval',
      'access comments',
    ];
    $this->adminUser = $this->drupalCreateUser($permissions);
    $this->drupalLogin($this->adminUser);
    // Add a comment field.     $this->addDefaultCommentField('node', 'article');
  }

  
/** * Tests the function of the typed data type. */
  public function testTypedDataAPI() {
    $definition = DataDefinition::create('filter_format');
    $data = \Drupal::typedDataManager()->create($definition);

    $this->assertInstanceOf(OptionsProviderInterface::class$data);

    $filtered_html_user = $this->createUser(
      [FilterFormat::load('filtered_html')->getPermissionName()],
      NULL,
      FALSE,
      ['uid' => 2]
    );

    // Test with anonymous user.     $user = new AnonymousUserSession();
    \Drupal::currentUser()->setAccount($user);

    $expected_available_options = [
      'filtered_html' => 'Filtered HTML',
      
/** @var \Drupal\filter\FilterFormatInterface $filtered_html_format */
    $filtered_html_format = FilterFormat::load('filtered_html');
    /** @var \Drupal\filter\FilterFormatInterface $full_html_format */
    $full_html_format = FilterFormat::load('full_html');

    $admin_permissions = array_merge($translator_permissions[
      'administer languages',
      'administer site configuration',
      'link to any page',
      'administer contact forms',
      'administer filters',
      $filtered_html_format->getPermissionName(),
      $full_html_format->getPermissionName(),
      $filter_test_format->getPermissionName(),
      'access site-wide contact form',
      'access contextual links',
      'administer account settings',
      'administer themes',
      'bypass node access',
      'administer content types',
      'translate interface',
      'administer entity_test fields',
    ]);
    
Home | Imprint | This part of the site doesn't use cookies.