copy teams from logged-in user for new projects (#3599)

This commit is contained in:
Kevin Papst
2022-10-25 16:31:34 +02:00
committed by GitHub
parent 8b4828f236
commit 78749ad3b4
14 changed files with 376 additions and 268 deletions

View File

@@ -16,6 +16,7 @@
"ext-json": "*",
"ext-mbstring": "*",
"ext-pdo": "*",
"ext-tokenizer": "*",
"ext-xml": "*",
"ext-xsl": "*",
"ext-zip": "*",

499
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -423,4 +423,11 @@ class SystemConfiguration implements SystemBundleConfiguration
{
return (bool) $this->find('theme.tags_create');
}
// ========== Projects ==========
public function isProjectCopyTeamsOnCreate(): bool
{
return $this->find('project.copy_teams_on_create') === true;
}
}

View File

@@ -38,6 +38,7 @@ use App\Repository\ProjectRepository;
use App\Repository\Query\ActivityQuery;
use App\Repository\Query\ProjectQuery;
use App\Repository\TeamRepository;
use App\Utils\Context;
use Pagerfanta\Pagerfanta;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -166,7 +167,7 @@ final class ProjectController extends AbstractController
if ($editForm->isSubmitted() && $editForm->isValid()) {
try {
$this->projectService->saveNewProject($project);
$this->projectService->saveNewProject($project, new Context($this->getUser()));
$this->flashSuccess('action.update.success');
return $this->redirectToRoute('project_details', ['id' => $project->getId()]);

View File

@@ -533,6 +533,11 @@ final class SystemConfigurationController extends AbstractController
->setName('project.choice_pattern')
->setLabel('choice_pattern')
->setType(ProjectTypePatternType::class),
(new Configuration())
->setName('project.copy_teams_on_create')
->setLabel('copy_teams_on_create')
->setType(YesNoType::class)
->setTranslationDomain('system-configuration'),
]),
(new SystemConfigurationModel('activity'))
->setConfiguration([

View File

@@ -70,6 +70,7 @@ class Configuration implements ConfigurationInterface
->append($this->getLdapNode())
->append($this->getSamlNode())
->append($this->getQuickEntryNode())
->append($this->getProjectNode())
->end()
->end();
@@ -100,6 +101,24 @@ class Configuration implements ConfigurationInterface
return $node;
}
private function getProjectNode()
{
$builder = new TreeBuilder('project');
/** @var ArrayNodeDefinition $node */
$node = $builder->getRootNode();
$node
->addDefaultsIfNotSet()
->children()
->booleanNode('copy_teams_on_create')
->defaultValue(false)
->end()
->end()
;
return $node;
}
private function getTimesheetNode(): ArrayNodeDefinition
{
$builder = new TreeBuilder('timesheet');

View File

@@ -9,6 +9,7 @@
namespace App\Project;
use App\Configuration\SystemConfiguration;
use App\Entity\Customer;
use App\Entity\Project;
use App\Event\ProjectCreateEvent;
@@ -18,6 +19,7 @@ use App\Event\ProjectMetaDefinitionEvent;
use App\Event\ProjectUpdatePostEvent;
use App\Event\ProjectUpdatePreEvent;
use App\Repository\ProjectRepository;
use App\Utils\Context;
use App\Validator\ValidationFailedException;
use InvalidArgumentException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -28,21 +30,18 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
*/
class ProjectService
{
/**
* @var ProjectRepository
*/
private $configuration;
private $repository;
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @var ValidatorInterface
*/
private $validator;
public function __construct(ProjectRepository $projectRepository, EventDispatcherInterface $dispatcher, ValidatorInterface $validator)
{
public function __construct(
SystemConfiguration $configuration,
ProjectRepository $projectRepository,
EventDispatcherInterface $dispatcher,
ValidatorInterface $validator
) {
$this->configuration = $configuration;
$this->repository = $projectRepository;
$this->dispatcher = $dispatcher;
$this->validator = $validator;
@@ -62,7 +61,7 @@ class ProjectService
return $project;
}
public function saveNewProject(Project $project): Project
public function saveNewProject(Project $project, ?Context $context = null): Project
{
if (null !== $project->getId()) {
throw new InvalidArgumentException('Cannot create project, already persisted');
@@ -70,6 +69,13 @@ class ProjectService
$this->validateProject($project);
if ($context !== null && $this->configuration->isProjectCopyTeamsOnCreate()) {
foreach ($context->getUser()->getTeams() as $team) {
$project->addTeam($team);
$team->addProject($project);
}
}
$this->dispatcher->dispatch(new ProjectCreatePreEvent($project));
$this->repository->saveProject($project);
$this->dispatcher->dispatch(new ProjectCreatePostEvent($project));

27
src/Utils/Context.php Normal file
View File

@@ -0,0 +1,27 @@
<?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\Utils;
use App\Entity\User;
final class Context
{
private $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function getUser(): User
{
return $this->user;
}
}

View File

@@ -469,6 +469,9 @@ class ConfigurationTest extends TestCase
'recent_activity_weeks' => null,
'minimum_rows' => 3,
],
'project' => [
'copy_teams_on_create' => false,
],
];
$this->assertConfig($this->getMinConfig(), $fullDefaultConfig);

View File

@@ -9,8 +9,11 @@
namespace App\Tests\Project;
use App\Configuration\SystemConfiguration;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Team;
use App\Entity\User;
use App\Event\ProjectCreateEvent;
use App\Event\ProjectCreatePostEvent;
use App\Event\ProjectCreatePreEvent;
@@ -19,6 +22,7 @@ use App\Event\ProjectUpdatePostEvent;
use App\Event\ProjectUpdatePreEvent;
use App\Project\ProjectService;
use App\Repository\ProjectRepository;
use App\Utils\Context;
use App\Validator\ValidationFailedException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -34,11 +38,9 @@ class ProjectServiceTest extends TestCase
private function getSut(
?EventDispatcherInterface $dispatcher = null,
?ValidatorInterface $validator = null,
?ProjectRepository $repository = null
bool $copyTeamsOnCreate = false
): ProjectService {
if ($repository === null) {
$repository = $this->createMock(ProjectRepository::class);
}
$repository = $this->createMock(ProjectRepository::class);
if ($dispatcher === null) {
$dispatcher = $this->createMock(EventDispatcherInterface::class);
@@ -49,7 +51,10 @@ class ProjectServiceTest extends TestCase
$validator->method('validate')->willReturn(new ConstraintViolationList());
}
$service = new ProjectService($repository, $dispatcher, $validator);
$configuration = $this->createMock(SystemConfiguration::class);
$configuration->method('isProjectCopyTeamsOnCreate')->willReturn($copyTeamsOnCreate);
$service = new ProjectService($configuration, $repository, $dispatcher, $validator);
return $service;
}
@@ -64,7 +69,7 @@ class ProjectServiceTest extends TestCase
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot create project, already persisted');
$sut->saveNewProject($project);
$sut->saveNewProject($project, new Context(new User()));
}
public function testSaveNewProjectHasValidationError()
@@ -80,7 +85,7 @@ class ProjectServiceTest extends TestCase
$this->expectException(ValidationFailedException::class);
$this->expectExceptionMessage('Validation Failed');
$sut->saveNewProject(new Project());
$sut->saveNewProject(new Project(), new Context(new User()));
}
public function testUpdateDispatchesEvents()
@@ -141,7 +146,26 @@ class ProjectServiceTest extends TestCase
$sut = $this->getSut($dispatcher);
$project = new Project();
$sut->saveNewProject($project);
$sut->saveNewProject($project, new Context(new User()));
self::assertCount(0, $project->getTeams());
}
public function testCreateNewProjectCopiesTeam()
{
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$sut = $this->getSut($dispatcher, null, true);
$team1 = new Team();
$team2 = new Team();
$user = new User();
$user->addTeam($team1);
$user->addTeam($team2);
$project = new Project();
$sut->saveNewProject($project, new Context($user));
self::assertCount(2, $project->getTeams());
}
public function testCreateNewProjectWithoutCustomer()

View File

@@ -49,6 +49,7 @@ abstract class BaseFormTypeQueryTest extends TestCase
$team = new Team();
self::assertInstanceOf(BaseFormTypeQuery::class, $sut->addTeam($team));
self::assertCount(1, $sut->getTeams());
/* @phpstan-ignore-next-line */
self::assertSame($team, $sut->getTeams()[0]);
self::assertInstanceOf(BaseFormTypeQuery::class, $sut->setTeams([]));

View File

@@ -119,6 +119,7 @@ class BaseQueryTest extends TestCase
$team = new Team();
self::assertInstanceOf(BaseQuery::class, $sut->setTeams([$team]));
self::assertEquals(1, \count($sut->getTeams()));
/* @phpstan-ignore-next-line */
self::assertSame($team, $sut->getTeams()[0]);
}

View File

@@ -318,6 +318,10 @@
<source>label.calendar.title_pattern</source>
<target>Darstellung der Titel von Kalendereinträgen</target>
</trans-unit>
<trans-unit id="MXasdfgbpD7" resname="label.copy_teams_on_create" approved="yes">
<source>label.copy_teams_on_create</source>
<target>Übernehme Teams beim Erstellen neuer Einträge vom angemeldeten Benutzer</target>
</trans-unit>
</body>
</file>
</xliff>

View File

@@ -318,6 +318,10 @@
<source>label.calendar.title_pattern</source>
<target>Display of the titles of calendar entries</target>
</trans-unit>
<trans-unit id="MXasdfgbpD7" resname="label.copy_teams_on_create" approved="yes">
<source>label.copy_teams_on_create</source>
<target>Take over teams from the logged-in user when creating new entries</target>
</trans-unit>
</body>
</file>
</xliff>