Skip to content

Commit

Permalink
101. workflow guard
Browse files Browse the repository at this point in the history
  • Loading branch information
teebbstudios committed Jul 27, 2021
1 parent fdd324e commit 8ba36e8
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 12 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"sensio/framework-extra-bundle": "^6.1",
"symfony/console": "5.3.*",
"symfony/dotenv": "5.3.*",
"symfony/expression-language": "5.3.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "5.3.*",
"symfony/mime": "5.3.*",
Expand Down
72 changes: 71 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions config/packages/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ framework:
from: draft
to: [wait_for_review, wait_for_check]
editor_review:
guard: 'is_granted(''ROLE_EDITOR'')'
from: wait_for_review
to: approved_by_editor
checker_check:
guard: 'is_granted(''ROLE_CHECKER'')'
from: wait_for_check
to: approved_by_checker
publish:
Expand Down
82 changes: 71 additions & 11 deletions src/Controller/Admin/PostCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Entity\Post;
use App\Security\Voter\PostVoter;
use App\Utils\Transition;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
Expand Down Expand Up @@ -70,37 +71,96 @@ public function configureFilters(Filters $filters): Filters
public function configureActions(Actions $actions): Actions
{
$workflow = $this->container->get('workflow.blog_publishing');
$reviewRequestAction = Action::new('review_request', 'review_request')
->displayIf(fn($entity) => $workflow->can($entity, 'review_request'))
$reviewRequestAction = Action::new(Transition::REVIEW_REQUEST, Transition::REVIEW_REQUEST)
->displayIf(fn($entity) => $workflow->can($entity, Transition::REVIEW_REQUEST))
->linkToCrudAction('reviewRequestAction');

$editorReviewAction = Action::new(Transition::EDITOR_REVIEW, Transition::EDITOR_REVIEW)
->displayIf(fn($entity) => $workflow->can($entity, Transition::EDITOR_REVIEW)
// && $this->isGranted('ROLE_EDITOR')
)
->linkToCrudAction('editorReviewAction');

$checkerCheckAction = Action::new(Transition::CHECKER_CHECK, Transition::CHECKER_CHECK)
->displayIf(fn($entity) => $workflow->can($entity, Transition::CHECKER_CHECK)
// && $this->isGranted('ROLE_CHECKER')
)
->linkToCrudAction('checkerCheckAction');

$publishAction = Action::new(Transition::PUBLISH, Transition::PUBLISH)
->displayIf(fn($entity) => $workflow->can($entity, Transition::PUBLISH))
->linkToCrudAction('publishAction');

return $actions->update(Crud::PAGE_INDEX, Action::EDIT,
function (Action $action) {
return $action->displayIf(fn($entity) => $this->isGranted(PostVoter::POST_OWNER_EDIT, $entity));
})->update(Crud::PAGE_INDEX, Action::DELETE,
function (Action $action) {
return $action->displayIf(fn($entity) => $this->isGranted(PostVoter::POST_OWNER_DELETE, $entity));
})
->add(Crud::PAGE_INDEX, $reviewRequestAction);
->add(Crud::PAGE_INDEX, $reviewRequestAction)
->add(Crud::PAGE_INDEX, $editorReviewAction)
->add(Crud::PAGE_INDEX, $checkerCheckAction)
->add(Crud::PAGE_INDEX, $publishAction);
}

public function reviewRequestAction(AdminContext $adminContext)
{
$post = $adminContext->getEntity()->getInstance();
if ($post instanceof Post) {

$this->applyTransition($post, Transition::REVIEW_REQUEST);

return $this->redirect($this->generatePageUrl(PostCrudController::class, Action::INDEX));
}

public function editorReviewAction(AdminContext $adminContext)
{
$this->denyAccessUnlessGranted('ROLE_EDITOR');
$post = $adminContext->getEntity()->getInstance();

$this->applyTransition($post, Transition::EDITOR_REVIEW);

return $this->redirect($this->generatePageUrl(PostCrudController::class, Action::INDEX));
}

public function checkerCheckAction(AdminContext $adminContext)
{
$this->denyAccessUnlessGranted('ROLE_CHECKER');
$post = $adminContext->getEntity()->getInstance();

$this->applyTransition($post, Transition::CHECKER_CHECK);

return $this->redirect($this->generatePageUrl(PostCrudController::class, Action::INDEX));
}

public function publishAction(AdminContext $adminContext)
{
$post = $adminContext->getEntity()->getInstance();

$this->applyTransition($post, Transition::PUBLISH);

return $this->redirect($this->generatePageUrl(PostCrudController::class, Action::INDEX));
}

private function applyTransition($entity, $transitionName)
{
if ($entity instanceof Post) {
$workflow = $this->container->get('workflow.blog_publishing');
if ($workflow->can($post, 'review_request')) {
$workflow->apply($post, 'review_request');
if ($workflow->can($entity, $transitionName)) {
$workflow->apply($entity, $transitionName);
$this->container->get('doctrine')->getManager()->flush();
}
}
}

private function generatePageUrl(string $crudControllerName, string $actionName)
{
$adminUrlGenerator = $this->container->get(AdminUrlGenerator::class);

return $this->redirect($adminUrlGenerator
->setController(PostCrudController::class)
->setAction(Action::INDEX)
->generateUrl()
);
return $adminUrlGenerator
->setController($crudControllerName)
->setAction($actionName)
->generateUrl();
}

public static function getSubscribedServices()
Expand Down
13 changes: 13 additions & 0 deletions src/Utils/Transition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


namespace App\Utils;


class Transition
{
public const REVIEW_REQUEST = 'review_request';
public const EDITOR_REVIEW = 'editor_review';
public const CHECKER_CHECK = 'checker_check';
public const PUBLISH = 'publish';
}
3 changes: 3 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@
"symfony/event-dispatcher-contracts": {
"version": "v2.4.0"
},
"symfony/expression-language": {
"version": "v5.3.4"
},
"symfony/filesystem": {
"version": "v5.3.0"
},
Expand Down

0 comments on commit 8ba36e8

Please sign in to comment.