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

@@ -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);
}
}