diff --git a/Menu/MenuBuilder.php b/Menu/MenuBuilder.php new file mode 100644 index 000000000..f11aa28bc --- /dev/null +++ b/Menu/MenuBuilder.php @@ -0,0 +1,88 @@ + + */ +class MenuBuilder implements LocalMenuBuilderInterface +{ + /** + * + * @var TokenStorageInterface + */ + protected $tokenStorage; + + /** + * + * @var TranslatorInterface + */ + protected $translator; + + /** + * + * @var AuthorizationHelper + */ + protected $authorizationHelper; + + public function __construct( + TokenStorageInterface $tokenStorage, + TranslatorInterface $translator, + AuthorizationHelper $authorizationHelper + ) { + $this->tokenStorage = $tokenStorage; + $this->translator = $translator; + $this->authorizationHelper = $authorizationHelper; + } + + + public function buildMenu($menuId, MenuItem $menu, array $parameters) + { + /* @var $person \Chill\PersonBundle\Entity\Person */ + $person = $parameters['person']; + $user = $this->tokenStorage->getToken()->getUser(); + $roleSee = new Role(ActivityVoter::SEE); + $roleAdd = new Role(ActivityVoter::CREATE); + + if ($this->authorizationHelper->userHasAccess($user, $person, $roleSee)) { + $menu->addChild($this->translator->trans('Activity list'), [ + 'route' => 'chill_activity_activity_list', + 'routeParameters' => [ + 'person_id' => $person->getId() + ] + ]) + ->setExtras([ + 'order' => 201 + ]); + } + + if ($this->authorizationHelper->userHasAccess($user, $person, $roleAdd)) { + $menu->addChild($this->translator->trans('Add a new activity'), [ + 'route' => 'chill_activity_activity_new', + 'routeParameters' => [ + 'person_id' => $person->getId() + ] + ]) + ->setExtras([ + 'order' => 200 + ]); + } + } + + public static function getMenuIds(): array + { + return [ 'person' ]; + } +} diff --git a/Resources/config/services/menu.yml b/Resources/config/services/menu.yml index a70eb0e05..2a0434996 100644 --- a/Resources/config/services/menu.yml +++ b/Resources/config/services/menu.yml @@ -1,7 +1,8 @@ services: - Chill\ActivityBundle\Menu\PersonMenuBuilder: + Chill\ActivityBundle\Menu\MenuBuilder: arguments: - $authorizationChecker: '@Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface' + $authorizationHelper: '@Chill\MainBundle\Security\Authorization\AuthorizationHelper' + $tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface' $translator: '@Symfony\Component\Translation\TranslatorInterface' tags: - - { name: 'chill.menu_builder' } \ No newline at end of file + - { name: 'chill.menu_builder' }