Skip to content

Instantly share code, notes, and snippets.

@dantleech
Created July 22, 2016 10:22
Show Gist options
  • Select an option

  • Save dantleech/63df1601c83c40633f00762a52fcd143 to your computer and use it in GitHub Desktop.

Select an option

Save dantleech/63df1601c83c40633f00762a52fcd143 to your computer and use it in GitHub Desktop.
<?php
namespace Ylly\Bolt\Extension\Fnair\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Bolt\Config;
use Symfony\Component\HttpFoundation\Session\Session;
use Ylly\Bolt\Extension\Fnair\Repository\RegionRepository;
use Bolt\Storage\Entity\Users;
use Bolt\AccessControl\Token\Token;
use Bolt\Exception\AccessControlException;
/**
* This event subscriber handles three things:
*
* - The region taxonomy which is available to the logged in user:
*
* If the user has the role "region-admin" then the region taxonomy
* will be limited to those regions of which he is an admin.
*
* Otherwise the taxomomy is populated with all the available regions.
*
* - Permission roles for Regions.
*
* A role is added for each region of the form: region-admin-<region slug>
*/
class PermissionSubscriber implements EventSubscriberInterface
{
private $config;
private $session;
private $repository;
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => [
[ 'handlePermissionConfig' ],
]
];
}
public function __construct(
Config $config,
Session $session,
RegionRepository $repository
)
{
$this->config = $config;
$this->session = $session;
$this->repository = $repository;
}
public function handlePermissionConfig(GetResponseEvent $event)
{
$auth = $this->session->get('authentication');
if (null === $auth) {
return;
}
$regionChoices = $this->getRegionChoices($auth);
$this->configureRoles($regionChoices);
$this->configureTaxonomy($regionChoices);
$this->configureSitename($auth, $regionChoices);
}
private function configureSitename(Token $auth, array $regionChoices)
{
if (in_array('root', $auth->getUser()->getRoles())) {
return;
}
$regions = implode(', ' , $regionChoices);
$this->config->set('general/sitename', 'La FNAIR: ' . $regions);
}
private function getRegionChoices(Token $auth)
{
$user = $auth->getUser();
$regions = $this->repository->findAll();
$regionChoices = [];
$userRoles = $user->getRoles();
// add one role for each region. If the user is an region-admin and then only
// add the regions which they are administrating.
foreach ($regions as $region) {
$regionRoleName = PermissionHelper::getRegionRoleName($region->get('slug'));
if (in_array(PermissionHelper::REGION_ADMINISTRATOR, $userRoles)) {
if (false === in_array($regionRoleName, $userRoles)) {
continue;
}
}
$regionChoices[$region->get('slug')] = $region->get('title');
}
return $regionChoices;
}
private function configureRoles(array $regionChoices)
{
foreach ($regionChoices as $slug => $title) {
$regionRoleName = PermissionHelper::getRegionRoleName($slug);
$this->config->set(sprintf(
'permissions/roles/%s', $regionRoleName
), [
'description' => 'Admistrateur ' . $title,
'label' => 'Admistrateur ' . $title,
]);
}
}
private function configureTaxonomy(array $regionChoices)
{
$taxonomy = [
'label' => 'Regions',
'slug' => 'regions',
'singular_slug' => 'region',
'behaves_like' => 'grouping',
'options' => $regionChoices,
'name' => 'Regions',
'singular_name' => 'Region',
'has_sortorder' => false,
'allow_spaces' => false,
];
$this->config->set('taxonomy/regions', $taxonomy);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment