You are here

protected function TeamPermissionHandler::buildPermissionsYaml in Apigee Edge 8

Builds all team permissions provided by .team_permissions.yml files.

Return value

\Drupal\apigee_edge_teams\Structure\TeamPermission[] Array of team permissions.

Throws

\Drupal\apigee_edge_teams\Exception\InvalidArgumentException If permission provider class does not implement DynamicTeamPermissionProviderInterface.

\InvalidArgumentException If permission provider class does not exist.

1 call to TeamPermissionHandler::buildPermissionsYaml()
TeamPermissionHandler::getPermissions in modules/apigee_edge_teams/src/TeamPermissionHandler.php
Gets all available team permissions.

File

modules/apigee_edge_teams/src/TeamPermissionHandler.php, line 210

Class

TeamPermissionHandler
Provides the available team permissions based on yml files.

Namespace

Drupal\apigee_edge_teams

Code

protected function buildPermissionsYaml() : array {
  $all_permissions = [];
  $all_dynamic_permissions = [];
  $module_names = $this
    ->getModuleNames();
  foreach ($this
    ->getYamlDiscovery()
    ->findAll() as $provider => $permissions) {
    if (isset($permissions['permission_providers'])) {
      foreach ($permissions['permission_providers'] as $fqcn) {

        // Thanks for class resolver permission providers can implement
        // ContainerInjectionInterface and access services. This should be
        // a better approach than what PermissionHandler does with controller
        // resolver.
        $permission_provider = $this->classResolver
          ->getInstanceFromDefinition($fqcn);
        if ($permission_provider instanceof DynamicTeamPermissionProviderInterface) {

          /** @var \Drupal\apigee_edge_teams\Structure\TeamPermission $dynamic_permission */
          foreach ($permission_provider
            ->permissions() as $dynamic_permission) {
            $all_dynamic_permissions[$dynamic_permission
              ->getName()] = $dynamic_permission;
          }
        }
        else {
          throw new InvalidArgumentException(sprintf('%s must implement %s.', $fqcn, DynamicTeamPermissionProviderInterface::class));
        }
      }
      unset($permissions['permission_providers']);
    }
    foreach ($permissions as $name => $permission) {
      if (!is_array($permission)) {
        $permission = [
          'title' => $permission,
        ];
      }
      $permission['name'] = $name;
      $permission['title'] = $this
        ->t($permission['title']);
      $permission['description'] = isset($permission['description']) ? $this
        ->t($permission['description']) : NULL;
      $permission['category'] = !empty($permission['category']) ? $this
        ->t($permission['category']) : $this
        ->t($module_names[$provider]);
      $all_permissions[$name] = $permission;
    }
  }
  return array_map(function (array $permission) {
    return new TeamPermission($permission['name'], $permission['title'], $permission['category'], $permission['description']);
  }, $all_permissions) + $all_dynamic_permissions;
}