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

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