allow to customize dashboard permissions for latest row (#2806)

This commit is contained in:
Kevin Papst
2021-10-03 17:17:01 +02:00
committed by GitHub
parent 6fcd50ecdf
commit b6430d5622
15 changed files with 612 additions and 310 deletions

View File

@@ -201,6 +201,11 @@ kimai:
order: 50
permission: view_all_data
widgets: [amountToday, amountWeek, amountMonth, amountYear]
totals:
title: ~
order: 100
permission: ROLE_USER
widgets: [TotalsUser, TotalsCustomer, TotalsProject, TotalsActivity]
# --------------------------------------------------------------------------------

View File

@@ -1,161 +0,0 @@
<?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\EventSubscriber;
use App\Event\DashboardEvent;
use App\Repository\ActivityRepository;
use App\Repository\CustomerRepository;
use App\Repository\ProjectRepository;
use App\Repository\Query\ActivityQuery;
use App\Repository\Query\CustomerQuery;
use App\Repository\Query\ProjectQuery;
use App\Repository\Query\UserQuery;
use App\Repository\UserRepository;
use App\Widget\Type\CompoundRow;
use App\Widget\Type\More;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* Used to add Dashboard widgets for users with ROLE_ADMIN.
*/
class DashboardSubscriber implements EventSubscriberInterface
{
/**
* @var AuthorizationCheckerInterface
*/
protected $security;
/**
* @var UserRepository
*/
protected $user;
/**
* @var ActivityRepository
*/
protected $activity;
/**
* @var ProjectRepository
*/
protected $project;
/**
* @var CustomerRepository
*/
protected $customer;
/**
* @param AuthorizationCheckerInterface $security
* @param UserRepository $user
* @param ActivityRepository $activity
* @param ProjectRepository $project
* @param CustomerRepository $customer
*/
public function __construct(
AuthorizationCheckerInterface $security,
UserRepository $user,
ActivityRepository $activity,
ProjectRepository $project,
CustomerRepository $customer
) {
$this->security = $security;
$this->user = $user;
$this->activity = $activity;
$this->project = $project;
$this->customer = $customer;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
DashboardEvent::class => ['onDashboardEvent', 100],
];
}
/**
* @param DashboardEvent $event
*/
public function onDashboardEvent(DashboardEvent $event)
{
$user = $event->getUser();
$section = new CompoundRow();
$section->setTitle('');
$section->setOrder(100);
if ($this->security->isGranted('view_user')) {
$query = new UserQuery();
$query->setCurrentUser($user);
$section->addWidget(
(new More())
->setId('userTotal')
->setTitle('stats.userTotal')
->setData($this->user->countUsersForQuery($query))
->setOptions([
'route' => 'admin_user',
'icon' => 'user',
'color' => 'primary',
])
);
}
if ($this->security->isGranted('view_customer')) {
$query = new CustomerQuery();
$query->setCurrentUser($user);
$section->addWidget(
(new More())
->setId('customerTotal')
->setTitle('stats.customerTotal')
->setData($this->customer->countCustomersForQuery($query))
->setOptions([
'route' => 'admin_customer',
'icon' => 'customer',
'color' => 'primary',
])
);
}
if ($this->security->isGranted('view_project')) {
$query = new ProjectQuery();
$query->setCurrentUser($user);
$section->addWidget(
(new More())
->setId('projectTotal')
->setTitle('stats.projectTotal')
->setData($this->project->countProjectsForQuery($query))
->setOptions([
'route' => 'admin_project',
'icon' => 'project',
'color' => 'primary',
])
);
}
if ($this->security->isGranted('view_activity')) {
$query = new ActivityQuery();
$query->setCurrentUser($user);
$section->addWidget(
(new More())
->setId('activityTotal')
->setTitle('stats.activityTotal')
->setData($this->activity->countActivitiesForQuery($query))
->setOptions([
'route' => 'admin_activity',
'icon' => 'activity',
'color' => 'primary',
])
);
}
if (\count($section->getWidgets()) > 0) {
$event->addSection($section);
}
}
}

View File

@@ -24,7 +24,6 @@ final class PaginatedWorkingTimeChart extends SimpleWidget implements UserWidget
{
$this->repository = $repository;
$this->systemConfiguration = $systemConfiguration;
$this->setId('PaginatedWorkingTimeChart');
$this->setTitle('stats.yourWorkingHours');
}

View File

@@ -0,0 +1,65 @@
<?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\Widget\Type;
use App\Entity\User;
use App\Repository\ActivityRepository;
use App\Repository\Query\ActivityQuery;
final class TotalsActivity extends SimpleWidget implements UserWidget, AuthorizedWidget
{
use UserWidgetTrait;
private $activity;
public function __construct(ActivityRepository $activity)
{
$this->activity = $activity;
$this->setTitle('stats.activityTotal');
}
public function getOptions(array $options = []): array
{
return array_merge([
'route' => 'admin_activity',
'icon' => 'activity',
'color' => 'primary',
'dataType' => 'int',
], parent::getOptions($options));
}
public function getData(array $options = [])
{
$options = $this->getOptions($options);
$user = $options['user'];
if (null === $user || !($user instanceof User)) {
throw new \InvalidArgumentException('Widget option "user" must be an instance of ' . User::class);
}
$query = new ActivityQuery();
$query->setCurrentUser($user);
return $this->activity->countActivitiesForQuery($query);
}
/**
* @return string[]
*/
public function getPermissions(): array
{
return ['view_activity', 'view_teamlead_activity', 'view_team_activity'];
}
public function getTemplateName(): string
{
return 'widget/widget-more.html.twig';
}
}

View File

@@ -0,0 +1,65 @@
<?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\Widget\Type;
use App\Entity\User;
use App\Repository\CustomerRepository;
use App\Repository\Query\CustomerQuery;
final class TotalsCustomer extends SimpleWidget implements UserWidget, AuthorizedWidget
{
use UserWidgetTrait;
private $customer;
public function __construct(CustomerRepository $customer)
{
$this->customer = $customer;
$this->setTitle('stats.customerTotal');
}
public function getOptions(array $options = []): array
{
return array_merge([
'route' => 'admin_customer',
'icon' => 'customer',
'color' => 'primary',
'dataType' => 'int',
], parent::getOptions($options));
}
public function getData(array $options = [])
{
$options = $this->getOptions($options);
$user = $options['user'];
if (null === $user || !($user instanceof User)) {
throw new \InvalidArgumentException('Widget option "user" must be an instance of ' . User::class);
}
$query = new CustomerQuery();
$query->setCurrentUser($user);
return $this->customer->countCustomersForQuery($query);
}
/**
* @return string[]
*/
public function getPermissions(): array
{
return ['view_customer', 'view_teamlead_customer', 'view_team_customer'];
}
public function getTemplateName(): string
{
return 'widget/widget-more.html.twig';
}
}

View File

@@ -0,0 +1,65 @@
<?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\Widget\Type;
use App\Entity\User;
use App\Repository\ProjectRepository;
use App\Repository\Query\ProjectQuery;
final class TotalsProject extends SimpleWidget implements UserWidget, AuthorizedWidget
{
use UserWidgetTrait;
private $project;
public function __construct(ProjectRepository $project)
{
$this->project = $project;
$this->setTitle('stats.projectTotal');
}
public function getOptions(array $options = []): array
{
return array_merge([
'route' => 'admin_project',
'icon' => 'project',
'color' => 'primary',
'dataType' => 'int',
], parent::getOptions($options));
}
public function getData(array $options = [])
{
$options = $this->getOptions($options);
$user = $options['user'];
if (null === $user || !($user instanceof User)) {
throw new \InvalidArgumentException('Widget option "user" must be an instance of ' . User::class);
}
$query = new ProjectQuery();
$query->setCurrentUser($user);
return $this->project->countProjectsForQuery($query);
}
/**
* @return string[]
*/
public function getPermissions(): array
{
return ['view_project', 'view_teamlead_project', 'view_team_project'];
}
public function getTemplateName(): string
{
return 'widget/widget-more.html.twig';
}
}

View File

@@ -0,0 +1,65 @@
<?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\Widget\Type;
use App\Entity\User;
use App\Repository\Query\UserQuery;
use App\Repository\UserRepository;
final class TotalsUser extends SimpleWidget implements UserWidget, AuthorizedWidget
{
use UserWidgetTrait;
private $user;
public function __construct(UserRepository $user)
{
$this->user = $user;
$this->setTitle('stats.userTotal');
}
public function getOptions(array $options = []): array
{
return array_merge([
'route' => 'admin_user',
'icon' => 'user',
'color' => 'primary',
'dataType' => 'int',
], parent::getOptions($options));
}
public function getData(array $options = [])
{
$options = $this->getOptions($options);
$user = $options['user'];
if (null === $user || !($user instanceof User)) {
throw new \InvalidArgumentException('Widget option "user" must be an instance of ' . User::class);
}
$query = new UserQuery();
$query->setCurrentUser($user);
return $this->user->countUsersForQuery($query);
}
/**
* @return string[]
*/
public function getPermissions(): array
{
return ['view_user'];
}
public function getTemplateName(): string
{
return 'widget/widget-more.html.twig';
}
}

View File

@@ -0,0 +1,24 @@
<?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\Widget\Type;
use App\Entity\User;
/**
* Needs to be used on a SimpleWidget
* @internal
*/
trait UserWidgetTrait
{
public function setUser(User $user): void
{
$this->setOption('user', $user);
}
}

View File

@@ -1,91 +0,0 @@
<?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\Tests\EventSubscriber;
use App\Entity\User;
use App\Event\DashboardEvent;
use App\EventSubscriber\DashboardSubscriber;
use App\Repository\ActivityRepository;
use App\Repository\CustomerRepository;
use App\Repository\ProjectRepository;
use App\Repository\UserRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* @covers \App\EventSubscriber\DashboardSubscriber
*/
class DashboardSubscriberTest extends TestCase
{
public function testGetSubscribedEvents()
{
$events = DashboardSubscriber::getSubscribedEvents();
$this->assertArrayHasKey(DashboardEvent::class, $events);
$methodName = $events[DashboardEvent::class][0];
$this->assertTrue(method_exists(DashboardSubscriber::class, $methodName));
}
public function testWithNonAdminUser()
{
$sut = $this->getSubscriber(false, 13, 28, 37, 5);
$event = new DashboardEvent(new User());
$this->assertEquals(0, \count($event->getSections()));
$sut->onDashboardEvent($event);
$this->assertEquals(0, \count($event->getSections()));
}
public function testWithAdminUser()
{
$sut = $this->getSubscriber(true, 13, 28, 37, 5);
$event = new DashboardEvent(new User());
$this->assertEquals(0, \count($event->getSections()));
$sut->onDashboardEvent($event);
$sections = $event->getSections();
$widgets = $sections[0]->getWidgets();
$this->assertEquals(1, \count($sections));
$this->assertEquals(4, \count($widgets));
$this->assertEquals('stats.userTotal', $widgets[0]->getTitle());
$this->assertEquals(13, $widgets[0]->getData());
$this->assertEquals('stats.customerTotal', $widgets[1]->getTitle());
$this->assertEquals(5, $widgets[1]->getData());
$this->assertEquals('stats.projectTotal', $widgets[2]->getTitle());
$this->assertEquals(37, $widgets[2]->getData());
$this->assertEquals('stats.activityTotal', $widgets[3]->getTitle());
$this->assertEquals(28, $widgets[3]->getData());
}
protected function getSubscriber(bool $isAdmin, int $userCount, int $activityCount, int $projectCount, int $customerCount)
{
$authMock = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
$authMock->method('isGranted')->willReturn($isAdmin);
$userMock = $this->getMockBuilder(UserRepository::class)->disableOriginalConstructor()->getMock();
$userMock->method('countUsersForQuery')->willReturn($userCount);
$projectMock = $this->getMockBuilder(ProjectRepository::class)->disableOriginalConstructor()->getMock();
$projectMock->method('countProjectsForQuery')->willReturn($projectCount);
$activityMock = $this->getMockBuilder(ActivityRepository::class)->disableOriginalConstructor()->getMock();
$activityMock->method('countActivitiesForQuery')->willReturn($activityCount);
$customerMock = $this->getMockBuilder(CustomerRepository::class)->disableOriginalConstructor()->getMock();
$customerMock->method('countCustomersForQuery')->willReturn($customerCount);
return new DashboardSubscriber($authMock, $userMock, $activityMock, $projectMock, $customerMock);
}
}

View File

@@ -10,20 +10,12 @@
namespace App\Tests\EventSubscriber;
use App\Configuration\MailConfiguration;
use App\Entity\User;
use App\Event\DashboardEvent;
use App\Event\EmailEvent;
use App\EventSubscriber\DashboardSubscriber;
use App\EventSubscriber\EmailSubscriber;
use App\Mail\KimaiMailer;
use App\Repository\ActivityRepository;
use App\Repository\CustomerRepository;
use App\Repository\ProjectRepository;
use App\Repository\UserRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* @covers \App\EventSubscriber\EmailSubscriber
@@ -54,51 +46,4 @@ class EmailSubscriberTest extends TestCase
$sut->onMailEvent($event);
}
public function testWithAdminUser()
{
$sut = $this->getSubscriber(true, 13, 28, 37, 5);
$event = new DashboardEvent(new User());
$this->assertEquals(0, \count($event->getSections()));
$sut->onDashboardEvent($event);
$sections = $event->getSections();
$widgets = $sections[0]->getWidgets();
$this->assertEquals(1, \count($sections));
$this->assertEquals(4, \count($widgets));
$this->assertEquals('stats.userTotal', $widgets[0]->getTitle());
$this->assertEquals(13, $widgets[0]->getData());
$this->assertEquals('stats.customerTotal', $widgets[1]->getTitle());
$this->assertEquals(5, $widgets[1]->getData());
$this->assertEquals('stats.projectTotal', $widgets[2]->getTitle());
$this->assertEquals(37, $widgets[2]->getData());
$this->assertEquals('stats.activityTotal', $widgets[3]->getTitle());
$this->assertEquals(28, $widgets[3]->getData());
}
protected function getSubscriber(bool $isAdmin, int $userCount, int $activityCount, int $projectCount, int $customerCount)
{
$authMock = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
$authMock->method('isGranted')->willReturn($isAdmin);
$userMock = $this->getMockBuilder(UserRepository::class)->disableOriginalConstructor()->getMock();
$userMock->method('countUsersForQuery')->willReturn($userCount);
$projectMock = $this->getMockBuilder(ProjectRepository::class)->disableOriginalConstructor()->getMock();
$projectMock->method('countProjectsForQuery')->willReturn($projectCount);
$activityMock = $this->getMockBuilder(ActivityRepository::class)->disableOriginalConstructor()->getMock();
$activityMock->method('countActivitiesForQuery')->willReturn($activityCount);
$customerMock = $this->getMockBuilder(CustomerRepository::class)->disableOriginalConstructor()->getMock();
$customerMock->method('countCustomersForQuery')->willReturn($customerCount);
return new DashboardSubscriber($authMock, $userMock, $activityMock, $projectMock, $customerMock);
}
}

View File

@@ -21,12 +21,17 @@ abstract class AbstractWidgetTypeTest extends TestCase
abstract public function getDefaultOptions(): array;
public function testDefaultValues()
protected function assertDefaultData(AbstractWidgetType $sut)
{
self::assertNull($sut->getData());
}
public function testDefaultData()
{
$sut = $this->createSut();
self::assertInstanceOf(AbstractWidgetType::class, $sut);
self::assertEquals($this->getDefaultOptions(), $sut->getOptions());
self::assertNull($sut->getData());
$this->assertDefaultData($sut);
self::assertEquals('bar', $sut->getOption('foo', 'bar'));
}

View File

@@ -0,0 +1,79 @@
<?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\Tests\Widget\Type;
use App\Entity\User;
use App\Repository\ActivityRepository;
use App\Widget\Type\AbstractWidgetType;
use App\Widget\Type\TotalsActivity;
/**
* @covers \App\Widget\Type\TotalsActivity
* @covers \App\Widget\Type\SimpleWidget
*/
class TotalsActivityTest extends AbstractWidgetTypeTest
{
/** @var User */
private $user;
protected function setUp(): void
{
parent::setUp();
$user = new User();
$user->setAlias('foo');
$this->user = $user;
}
public function createSut(): AbstractWidgetType
{
return $this->createWidget();
}
private function createWidget(int $results = 1): TotalsActivity
{
$repository = $this->createMock(ActivityRepository::class);
$repository->expects($this->any())->method('countActivitiesForQuery')->willReturn($results);
$widget = new TotalsActivity($repository);
$widget->setUser($this->user);
return $widget;
}
public function getDefaultOptions(): array
{
return [
'route' => 'admin_activity',
'icon' => 'activity',
'color' => 'primary',
'dataType' => 'int',
'user' => $this->user,
];
}
protected function assertDefaultData(AbstractWidgetType $sut)
{
self::assertEquals(1, $sut->getData());
}
public function testData()
{
$user = new User();
$user->setAlias('foo');
$sut = $this->createWidget(99);
self::assertEquals('widget/widget-more.html.twig', $sut->getTemplateName());
$sut->setUser($user);
self::assertEquals(['view_activity', 'view_teamlead_activity', 'view_team_activity'], $sut->getPermissions());
self::assertEquals(99, $sut->getData([]));
}
}

View File

@@ -0,0 +1,79 @@
<?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\Tests\Widget\Type;
use App\Entity\User;
use App\Repository\CustomerRepository;
use App\Widget\Type\AbstractWidgetType;
use App\Widget\Type\TotalsCustomer;
/**
* @covers \App\Widget\Type\TotalsCustomer
* @covers \App\Widget\Type\SimpleWidget
*/
class TotalsCustomerTest extends AbstractWidgetTypeTest
{
/** @var User */
private $user;
protected function setUp(): void
{
parent::setUp();
$user = new User();
$user->setAlias('foo');
$this->user = $user;
}
public function createSut(): AbstractWidgetType
{
return $this->createWidget();
}
private function createWidget(int $results = 1): TotalsCustomer
{
$repository = $this->createMock(CustomerRepository::class);
$repository->expects($this->any())->method('countCustomersForQuery')->willReturn($results);
$widget = new TotalsCustomer($repository);
$widget->setUser($this->user);
return $widget;
}
public function getDefaultOptions(): array
{
return [
'route' => 'admin_customer',
'icon' => 'customer',
'color' => 'primary',
'dataType' => 'int',
'user' => $this->user,
];
}
protected function assertDefaultData(AbstractWidgetType $sut)
{
self::assertEquals(1, $sut->getData());
}
public function testData()
{
$user = new User();
$user->setAlias('foo');
$sut = $this->createWidget(99);
self::assertEquals('widget/widget-more.html.twig', $sut->getTemplateName());
$sut->setUser($user);
self::assertEquals(['view_customer', 'view_teamlead_customer', 'view_team_customer'], $sut->getPermissions());
self::assertEquals(99, $sut->getData([]));
}
}

View File

@@ -0,0 +1,79 @@
<?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\Tests\Widget\Type;
use App\Entity\User;
use App\Repository\ProjectRepository;
use App\Widget\Type\AbstractWidgetType;
use App\Widget\Type\TotalsProject;
/**
* @covers \App\Widget\Type\TotalsProject
* @covers \App\Widget\Type\SimpleWidget
*/
class TotalsProjectTest extends AbstractWidgetTypeTest
{
/** @var User */
private $user;
protected function setUp(): void
{
parent::setUp();
$user = new User();
$user->setAlias('foo');
$this->user = $user;
}
public function createSut(): AbstractWidgetType
{
return $this->createWidget();
}
private function createWidget(int $results = 1): TotalsProject
{
$repository = $this->createMock(ProjectRepository::class);
$repository->expects($this->any())->method('countProjectsForQuery')->willReturn($results);
$widget = new TotalsProject($repository);
$widget->setUser($this->user);
return $widget;
}
public function getDefaultOptions(): array
{
return [
'route' => 'admin_project',
'icon' => 'project',
'color' => 'primary',
'dataType' => 'int',
'user' => $this->user,
];
}
protected function assertDefaultData(AbstractWidgetType $sut)
{
self::assertEquals(1, $sut->getData());
}
public function testData()
{
$user = new User();
$user->setAlias('foo');
$sut = $this->createWidget(99);
self::assertEquals('widget/widget-more.html.twig', $sut->getTemplateName());
$sut->setUser($user);
self::assertEquals(['view_project', 'view_teamlead_project', 'view_team_project'], $sut->getPermissions());
self::assertEquals(99, $sut->getData([]));
}
}

View File

@@ -0,0 +1,79 @@
<?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\Tests\Widget\Type;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Widget\Type\AbstractWidgetType;
use App\Widget\Type\TotalsUser;
/**
* @covers \App\Widget\Type\TotalsUser
* @covers \App\Widget\Type\SimpleWidget
*/
class TotalsUserTest extends AbstractWidgetTypeTest
{
/** @var User */
private $user;
protected function setUp(): void
{
parent::setUp();
$user = new User();
$user->setAlias('foo');
$this->user = $user;
}
public function createSut(): AbstractWidgetType
{
return $this->createWidget();
}
private function createWidget(int $results = 1): TotalsUser
{
$repository = $this->createMock(UserRepository::class);
$repository->expects($this->any())->method('countUsersForQuery')->willReturn($results);
$widget = new TotalsUser($repository);
$widget->setUser($this->user);
return $widget;
}
public function getDefaultOptions(): array
{
return [
'route' => 'admin_user',
'icon' => 'user',
'color' => 'primary',
'dataType' => 'int',
'user' => $this->user,
];
}
protected function assertDefaultData(AbstractWidgetType $sut)
{
self::assertEquals(1, $sut->getData());
}
public function testData()
{
$user = new User();
$user->setAlias('foo');
$sut = $this->createWidget(99);
self::assertEquals('widget/widget-more.html.twig', $sut->getTemplateName());
$sut->setUser($user);
self::assertEquals(['view_user'], $sut->getPermissions());
self::assertEquals(99, $sut->getData([]));
}
}