generate entry "my tasks" dynamically

This commit is contained in:
Julien Fastré 2018-07-17 17:34:59 +02:00
parent f9221f9f90
commit e0e7376c1d
3 changed files with 25 additions and 2 deletions

View File

@ -315,7 +315,7 @@ class SingleTaskController extends Controller
* @return Response * @return Response
* @Route( * @Route(
* "/{_locale}/task/single-task/list/my", * "/{_locale}/task/single-task/list/my",
* options={ "menus": { "user": { "order": -10, "label": "My tasks", "icon": "tasks" } } } * name="chill_task_single_my_tasks"
* ) * )
*/ */
public function myTasksAction(TranslatorInterface $translator) public function myTasksAction(TranslatorInterface $translator)

View File

@ -24,6 +24,8 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInt
use Chill\TaskBundle\Repository\SingleTaskRepository; use Chill\TaskBundle\Repository\SingleTaskRepository;
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
/** /**
* *
@ -50,14 +52,22 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
*/ */
public $translator; public $translator;
/**
*
* @var AuthorizationCheckerInterface
*/
public $authorizationChecker;
public function __construct( public function __construct(
CountNotificationTask $counter, CountNotificationTask $counter,
TokenStorageInterface $tokenStorage, TokenStorageInterface $tokenStorage,
TranslatorInterface $translator TranslatorInterface $translator,
AuthorizationCheckerInterface $authorizationChecker
) { ) {
$this->counter = $counter; $this->counter = $counter;
$this->tokenStorage = $tokenStorage; $this->tokenStorage = $tokenStorage;
$this->translator = $translator; $this->translator = $translator;
$this->authorizationChecker = $authorizationChecker;
} }
public static function getMenuIds(): array public static function getMenuIds(): array
@ -67,6 +77,10 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
public function buildMenu($menuId, MenuItem $menu, array $parameters) public function buildMenu($menuId, MenuItem $menu, array $parameters)
{ {
if (FALSE === $this->authorizationChecker->isGranted(TaskVoter::SHOW)) {
return;
}
$user = $this->tokenStorage->getToken()->getUser(); $user = $this->tokenStorage->getToken()->getUser();
$ended = $this->counter->countNotificationEnded($user); $ended = $this->counter->countNotificationEnded($user);
$warning = $this->counter->countNotificationWarning($user); $warning = $this->counter->countNotificationWarning($user);
@ -92,6 +106,14 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
$warning, $warning,
-14); -14);
} }
$menu->addChild("My tasks", [
'route' => 'chill_task_single_my_tasks'
])
->setExtras([
'order' => -10,
'icon' => 'tasks'
]);
} }
protected function addItemInMenu(MenuItem $menu, User $u, $message, $title, $status, $number, $order) protected function addItemInMenu(MenuItem $menu, User $u, $message, $title, $status, $number, $order)

View File

@ -4,6 +4,7 @@ services:
$tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface' $tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'
$counter: '@Chill\TaskBundle\Templating\UI\CountNotificationTask' $counter: '@Chill\TaskBundle\Templating\UI\CountNotificationTask'
$translator: '@Symfony\Component\Translation\TranslatorInterface' $translator: '@Symfony\Component\Translation\TranslatorInterface'
$authorizationChecker: '@Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface'
tags: tags:
- { name: 'chill.menu_builder' } - { name: 'chill.menu_builder' }