improve autowiring and add workflow to tasks

This commit is contained in:
Julien Fastré 2018-04-25 12:03:16 +02:00
parent 196fc2c38f
commit adc830142b
10 changed files with 4669 additions and 117 deletions

View File

@ -14,28 +14,11 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Symfony\Component\Security\Core\Role\Role;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\TaskBundle\Repository\SingleTaskRepository;
class SingleTaskController extends Controller
{
/**
*
* @var EntityManager
*/
protected $em;
/**
*
* @var FormFactoryInterface
*/
protected $formFactory;
/*public function __construct(
EntityManager $em,
FormFactoryInterface $formFactory)
{
$this->em = $em;
$this->formFactory = $formFactory;
}*/
/**
* @Route("/{_locale}/task/single-task/new")
@ -104,5 +87,92 @@ class SingleTaskController extends Controller
return $form;
}
/**
* @Route(
* "/{_locale}/task/task/list/person/{personId}",
* name="chill_task_task_list_by_person"
* )
*/
public function listAction(
Request $request,
Person $personId,
PaginatorFactory $paginatorFactory,
SingleTaskRepository $taskRepository
) {
$person = $personId;
/* @var $viewParams array The parameters for the view */
$viewParams['person'] = $person;
// collect parameters for filter
$params['person'] = $person;
if ($request->query->has('date_status')) {
$statuses = $request->query->get('date_status');
$singleStatus = count($statuses) === 1;
// check for invalid parameters
$diff = \array_diff(
$statuses,
SingleTaskRepository::DATE_STATUSES)
;
if (count($diff) > 0) {
return new Response(
'date_status not allowed: '. \implode(', ', $diff),
Response::HTTP_BAD_REQUEST
);
}
}
foreach(SingleTaskRepository::DATE_STATUSES as $type) {
if($request->query->has('date_status')
&& FALSE === \in_array($type, $statuses ?? [])) {
continue;
}
$params['date_status'] = $type;
$count = $taskRepository
->countByParameters($params, $this->getUser())
;
$paginator = $paginatorFactory->create($count);
$viewParams['single_task_'.$type.'_count'] = $count;
$viewParams['single_task_'.$type.'_paginator'] = $paginator;
$viewParams['single_task_'.$type.'_tasks'] = $taskRepository
->findByParameters($params, $this->getUser(),
$singleStatus ? $paginator->getCurrentPage()->getFirstItemNumber() : 0,
$singleStatus ? $paginator->getItemsPerPage() : 10)
;
}
return $this->render('ChillTaskBundle:Task:index.html.twig', $viewParams);
}
protected function getPersonParam(Request $request, EntityManagerInterface $em)
{
$person = $em->getRepository(Person::class)
->find($request->query->getInt('person_id'))
;
if (NULL === $person) {
throw $this->createNotFoundException('person not found');
}
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person, "You are "
. "not allowed to see this person");
return $person;
}
protected function getUserParam(Request $request, EntityManagerInterface $em)
{
$user = $em->getRepository(User::class)
->find($request->query->getInt('user_id'))
;
if (NULL === $user) {
throw $this->createNotFoundException('user not found');
}
return $user;
}
}

View File

@ -3,103 +3,62 @@
namespace Chill\TaskBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Chill\PersonBundle\Entity\Person;
use Chill\TaskBundle\Entity\SingleTask;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\TaskBundle\Repository\SingleTaskRepository;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
class TaskController extends Controller
{
/**
* @Route(
* "/{_locale}/task/task/list/{personId}",
* name="chill_task_task_list"
* )
*/
public function listAction(Request $request, Person $personId)
{
$person = $personId;
/* @var $taskRepository SingleTaskRepository */
$taskRepository = $this->get('chill_task.single_task_repository');
/* @var $paginatorFactory \Chill\MainBundle\Pagination\PaginatorFactory */
$paginatorFactory = $this->get('chill_main.paginator_factory');
/* @var $viewParams array The parameters for the view */
$viewParams['person'] = $person;
// collect parameters for filter
$params['person'] = $person;
if ($request->query->has('date_status')) {
$statuses = $request->query->get('date_status');
$singleStatus = count($statuses) === 1;
// check for invalid parameters
$diff = \array_diff(
$statuses,
SingleTaskRepository::DATE_STATUSES)
;
if (count($diff) > 0) {
return new Response(
'date_status not allowed: '. \implode(', ', $diff),
Response::HTTP_BAD_REQUEST
public function applyTransitionAction(
$type,
$taskId,
$transition,
SingleTaskRepository $singleTaskRepository,
Registry $registry,
EntityManagerInterface $em,
Request $request
) {
switch ($type) {
case 'single-task':
$task = $singleTaskRepository
->find($taskId)
;
$defaultReturnPath = $this->generateUrl(
'chill_task_task_list_by_person',
[ 'personId' => $task->getPerson() ]
);
}
}
foreach(SingleTaskRepository::DATE_STATUSES as $type) {
if($request->query->has('date_status')
&& FALSE === \in_array($type, $statuses ?? [])) {
continue;
}
$params['date_status'] = $type;
$count = $taskRepository
->countByParameters($params, $this->getUser())
;
$paginator = $paginatorFactory->create($count);
$viewParams['single_task_'.$type.'_count'] = $count;
$viewParams['single_task_'.$type.'_paginator'] = $paginator;
$viewParams['single_task_'.$type.'_tasks'] = $taskRepository
->findByParameters($params, $this->getUser(),
$singleStatus ? $paginator->getCurrentPage()->getFirstItemNumber() : 0,
$singleStatus ? $paginator->getItemsPerPage() : 10)
;
break;
default:
return new Response("The type '$type' is not implemented",
Response::HTTP_BAD_REQUEST);
}
return $this->render('ChillTaskBundle:Task:index.html.twig', $viewParams);
}
protected function getPersonParam(Request $request, EntityManagerInterface $em)
{
$person = $em->getRepository(Person::class)
->find($request->query->getInt('person_id'))
;
if (NULL === $person) {
throw $this->createNotFoundException('person not found');
if (NULL === $task) {
$this->createHttpNotFoundException("task with id '$taskId' and type "
. "'$type' does not exists");
}
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person, "You are "
. "not allowed to see this person");
return $person;
}
protected function getUserParam(Request $request, EntityManagerInterface $em)
{
$user = $em->getRepository(User::class)
->find($request->query->getInt('user_id'))
;
if (NULL === $user) {
throw $this->createNotFoundException('user not found');
}
return $user;
}
// we simply check that the user can see the task. Other ACL checks
// should be performed using `guard` events.
$this->denyAccessUnlessGranted($task, TaskVoter::SHOW);
$workflow = $registry->get($task);
if ($workflow->can($task, $transition)) {
$workflow->apply($task, $transition);
$em->flush();
$this->addFlash('success', 'The transition is sucessfully applyed');
} else {
$this->addFlash('error', 'The transition could not be applyed');
}
return $this->redirect($request->query->get('return_path', $defaultReturnPath));
}
}

View File

@ -8,6 +8,7 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Chill\TaskBundle\Workflow\TaskWorkflowManager;
/**
* This is the class that loads and manages your bundle configuration.
@ -28,11 +29,26 @@ class ChillTaskExtension extends Extension implements PrependExtensionInterface
$loader->load('services/controller.yml');
$loader->load('services/security.yml');
$loader->load('services/repositories.yml');
$loader->load('services/workflow.yml');
}
public function prepend(ContainerBuilder $container)
{
$this->prependAuthorization($container);
$this->prependRoute($container);
$this->prependWorkflows($container);
}
protected function prependRoute(ContainerBuilder $container)
{
//declare routes for task bundle
$container->prependExtensionConfig('chill_main', array(
'routing' => array(
'resources' => array(
'@ChillTaskBundle/Resources/config/routing.yml'
)
)
));
}
public function prependAuthorization(ContainerBuilder $container)
@ -44,5 +60,38 @@ class ChillTaskExtension extends Extension implements PrependExtensionInterface
)
));
}
protected function prependWorkflows(ContainerBuilder $container)
{
$container->prependExtensionConfig('framework', [
'workflows' => [
'task_default' => [
'marking_store' => [
'type' => 'multiple_state',
'arguments' => [
'currentStates'
],
],
'type' => 'workflow',
'support_strategy' => TaskWorkflowManager::class,
'places' => [ 'new', 'in_progress', 'closed', 'canceled'],
'initial_place' => 'new',
'transitions' => [
'start' => [
'from' => 'new',
'to' => 'in_progress'
],
'close' => [
'from' => 'in_progress',
'to' => 'closed'
],
'cancel' => [
'from' => ['new', 'in_progress'],
'to' => 'canceled'
]
]
]
]
]);
}
}

View File

@ -107,26 +107,30 @@ abstract class AbstractTask implements HasScopeInterface, HasCenterInterface
/**
* Set currentStates
*
* The current states are sorted in a single array, non associative.
*
* @param json $currentStates
* @param $currentStates
*
* @return AbstractTask
*/
public function setCurrentStates($currentStates)
{
$this->currentStates = $currentStates;
$this->currentStates = \array_keys($currentStates);
return $this;
}
/**
* Get currentStates
*
* The states are returned as required by marking store format.
*
* @return json
* @return array
*/
public function getCurrentStates()
{
return $this->currentStates;
return \array_fill_keys($this->currentStates, 1);
}
/**

View File

@ -1,4 +1,4 @@
services:
#chill_task.single_task_controller:
# class: Chill\TaskBundle\Controller\SingleTaskController
# autowire: true
Chill\TaskBundle\Controller\:
resource: ../../../Controller
tags: ['controller.service_arguments']

View File

@ -8,3 +8,4 @@ services:
- method: setAuthorizationHelper
arguments:
- "@chill.main.security.authorization.helper"
Chill\TaskBundle\Repository\SingleTaskRepository: '@chill_task.single_task_repository'

View File

@ -0,0 +1,2 @@
services:
Chill\TaskBundle\Workflow\TaskWorkflowManager: ~

View File

@ -0,0 +1,41 @@
<?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\TaskBundle\Workflow;
use Chill\TaskBundle\Entity\AbstractTask;
use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface;
use Symfony\Component\Workflow\Workflow;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class TaskWorkflowManager implements SupportStrategyInterface
{
public function supports(Workflow $workflow, $subject): bool
{
if (!$subject instanceof AbstractTask) {
return false;
}
dump($workflow->getName());
return $workflow->getName() === 'task_default';
}
}

View File

@ -7,15 +7,38 @@
"autoload": {
"psr-4": { "Chill\\TaskBundle\\" : "" }
},
"autoload-dev": {
"classmap": [ "Resources/test/Fixtures/App/app/AppKernel.php" ]
},
"require": {
"php": "~7.2",
"chill-project/person": "dev-upgrade-sf3@dev"
},
"require-dev": {
"fzaninotto/faker": "^1.7",
"phpunit/phpunit": "^7.1"
},
"minimum-stability": "dev",
"prefer-stable": true,
"license": "AGPL-3.0-or-later",
"authors": [
{
"name": "Champs-Libres",
"email": "info@champs-libres.coop"
}
]
],
"scripts": {
"post-install-cmd": [
"ComposerBundleMigration\\Composer\\Migrations::synchronizeMigrations",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap"
],
"post-update-cmd": [
"ComposerBundleMigration\\Composer\\Migrations::synchronizeMigrations",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap"
]
},
"extra": {
"app-migrations-dir": "Resources/test/Fixtures/App/app/DoctrineMigrations",
"symfony-app-dir": "Resources/test/Fixtures/App/"
}
}

4403
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff