summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/user/src/ModulePermissionsLinkHelper.php
blob: 7af7f8a31df172b6a627d970e6877c118390fed3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

declare(strict_types=1);

namespace Drupal\user;

use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;

/**
 * Provides a helper for generating module permissions links.
 */
class ModulePermissionsLinkHelper {

  use StringTranslationTrait;

  /**
   * Constructs a new service instance.
   *
   * @param \Drupal\user\PermissionHandlerInterface $permissionHandler
   *   The user permissions handler service.
   * @param \Drupal\Core\Access\AccessManagerInterface $accessManager
   *   The access manager service.
   * @param \Drupal\Core\Extension\ModuleExtensionList $moduleExtensionList
   *   The module handler service.
   */
  public function __construct(
    protected PermissionHandlerInterface $permissionHandler,
    protected AccessManagerInterface $accessManager,
    protected ModuleExtensionList $moduleExtensionList,
  ) {}

  /**
   * Generates a link pointing to a given module's permissions page section.
   *
   * @param string $module
   *   The module name.
   * @param string $name
   *   The module display name.
   *
   * @return array|null
   *   A module permissions link as a render array or NULL if the module doesn't
   *   expose any permission or the current user cannot access it.
   */
  public function getModulePermissionsLink(string $module, string $name): ?array {
    if ($this->permissionHandler->moduleProvidesPermissions($module)) {
      if ($this->accessManager->checkNamedRoute('user.admin_permissions.module', ['modules' => $module])) {
        $url = new Url('user.admin_permissions.module', ['modules' => $module]);
        return [
          'title' => $this->t('Configure @module permissions', ['@module' => $name]),
          'description' => '',
          'url' => $url,
        ];
      }
    }
    return NULL;
  }

}