added configurable view after login (#523)

This commit is contained in:
Kevin Papst
2019-01-29 02:15:12 +01:00
committed by GitHub
parent 67dab35b57
commit fe4c3c10d6
8 changed files with 144 additions and 6 deletions

View File

@@ -10,6 +10,7 @@
namespace App\Controller;
use App\Entity\User;
use App\Form\Type\InitialViewType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
@@ -30,8 +31,9 @@ class HomepageController extends AbstractController
*/
public function indexAction(Request $request)
{
// make me configurable via UserPreference
$route = 'timesheet';
/** @var User $user */
$user = $this->getUser();
$route = $user->getPreferenceValue('login.initial_view', InitialViewType::DEFAULT_VIEW);
/** @var User $user */
$user = $this->getUser();

View File

@@ -55,6 +55,7 @@ class ProfileController extends AbstractController
/**
* @Route(path="/", name="fos_user_profile_show", methods={"GET"})
* @Route(path="/", name="my_profile", methods={"GET"})
*/
public function profileAction()
{

View File

@@ -14,6 +14,7 @@ use App\Entity\UserPreference;
use App\Event\PrepareUserEvent;
use App\Event\UserPreferenceEvent;
use App\Form\Type\CalendarViewType;
use App\Form\Type\InitialViewType;
use App\Form\Type\LanguageType;
use App\Form\Type\SkinType;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -124,6 +125,11 @@ class UserPreferenceSubscriber implements EventSubscriberInterface
->setName('calendar.initial_view')
->setValue(CalendarViewType::DEFAULT_VIEW)
->setType(CalendarViewType::class),
(new UserPreference())
->setName('login.initial_view')
->setValue(InitialViewType::DEFAULT_VIEW)
->setType(InitialViewType::class),
];
}

View File

@@ -0,0 +1,89 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* Custom form field type to select the initial view, where the user should be redirected to after login.
*/
class InitialViewType extends AbstractType
{
public const DEFAULT_VIEW = 'timesheet';
public const ALLOWED_VIEWS = [
'dashboard' => 'menu.homepage',
'timesheet' => 'menu.timesheet',
'calendar' => 'calendar.title',
'my_profile' => 'profile.title',
'admin_timesheet' => 'menu.admin_timesheet',
'invoice' => 'menu.invoice',
'admin_user' => 'menu.admin_user',
'admin_customer' => 'menu.admin_customer',
'admin_project' => 'menu.admin_project',
'admin_activity' => 'menu.admin_activity',
];
protected const ROUTE_PERMISSION = [
'dashboard' => 'menu.homepage',
'timesheet' => 'view_own_timesheet',
'calendar' => 'view_own_timesheet',
'my_profile' => 'view_own_profile',
'admin_timesheet' => 'view_other_timesheet',
'invoice' => 'view_invoice',
'admin_user' => 'view_user',
'admin_customer' => 'view_customer',
'admin_project' => 'view_project',
'admin_activity' => 'view_activity',
];
/**
* @var AuthorizationCheckerInterface
*/
protected $voter;
/**
* @param AuthorizationCheckerInterface $voter
*/
public function __construct(AuthorizationCheckerInterface $voter)
{
$this->voter = $voter;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$choices = [];
foreach (self::ROUTE_PERMISSION as $route => $permission) {
if ($this->voter->isGranted($permission)) {
$name = self::ALLOWED_VIEWS[$route];
$choices[$name] = $route;
}
}
$resolver->setDefaults([
'required' => true,
'choices' => $choices,
]);
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return ChoiceType::class;
}
}