copy teams from logged-in user for new projects (#3599)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()]);
|
||||
|
||||
@@ -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([
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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
27
src/Utils/Context.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user