View source
<?php
namespace Drupal\apigee_edge_teams\Plugin\views\access;
use Drupal\apigee_edge_teams\Structure\TeamPermission as TeamPermissionEntity;
use Drupal\apigee_edge_teams\TeamPermissionHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Plugin\views\access\AccessPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
class TeamPermission extends AccessPluginBase {
protected $usesOptions = TRUE;
protected $teamPermissionHandler;
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, TeamPermissionHandlerInterface $team_permission_handler, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->teamPermissionHandler = $team_permission_handler;
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('apigee_edge_teams.team_permissions'), $container
->get('current_route_match'));
}
public function access(AccountInterface $account) {
if ((int) $account
->id() === 1) {
return TRUE;
}
$team = $this->routeMatch
->getParameter('team');
if (!$team) {
return FALSE;
}
return in_array($this->options['permission'], $this->teamPermissionHandler
->getDeveloperPermissionsByTeam($team, $account));
}
public function alterRouteDefinition(Route $route) {
$route
->setRequirement('_team_permission', $this->options['permission']);
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['permission'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$options = array_reduce($this->teamPermissionHandler
->getPermissions(), function (array $options, TeamPermissionEntity $permission) {
$options[$permission
->getName()] = $permission
->getLabel();
return $options;
}, []);
$form['permission'] = [
'#type' => 'select',
'#options' => $options,
'#title' => $this
->t('Team permission'),
'#default_value' => $this->options['permission'],
'#description' => $this
->t('Only users with the selected permission for the current team will be able to access this display.'),
];
}
public function summaryTitle() {
$permissions = $this->teamPermissionHandler
->getPermissions();
if (isset($permissions[$this->options['permission']])) {
return $permissions[$this->options['permission']]
->getLabel();
}
return $this
->t($this->options['permission']);
}
}