From 51b34425719d636fc7adbd257b28393a4cf92123 Mon Sep 17 00:00:00 2001 From: loostro Date: Sat, 23 Jul 2016 12:49:09 +0200 Subject: [PATCH 1/8] Add echoIfWorkflow twig extension --- Twig/Extension/EchoExtension.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Twig/Extension/EchoExtension.php b/Twig/Extension/EchoExtension.php index 291c119d..07b888ac 100644 --- a/Twig/Extension/EchoExtension.php +++ b/Twig/Extension/EchoExtension.php @@ -26,6 +26,7 @@ public function getFunctions() { return array( 'echo_if_granted' => new \Twig_SimpleFunction('echo_if_granted', array($this, 'getEchoIfGranted')), + 'echo_if_workflow' => new \Twig_SimpleFunction('echo_if_workflow', array($this, 'getEchoIfWorkflow')), 'echo_path' => new \Twig_SimpleFunction('echo_path', array($this, 'getEchoPath')), 'echo_trans' => new \Twig_SimpleFunction('echo_trans', array($this, 'getEchoTrans')), 'echo_render' => new \Twig_SimpleFunction('echo_render', array($this, 'getEchoRender')) @@ -188,6 +189,27 @@ public function getEchoIfGranted($credentials, $modelName = null) ); } + /** + * Print "if" tag with condition workflow_can() + * + * @param string $modelName + * @param array $workflows + * @return string + */ + public function getEchoIfWorkflow($modelName, $workflows = array()) + { + if (empty($workflows)) { + return "{% if (true) %}"; + } + + return sprintf( + "{%% if %s(%s, '%s') %%}", + 'workflow_can', + $modelName, + implode("', '", $workflows) + ); + } + /** * Print "echo tag with render call" to the controller $controller * with $params parameters. From 817662471043eb463159b045036a197e80425fc2 Mon Sep 17 00:00:00 2001 From: loostro Date: Sun, 24 Jul 2016 17:15:14 +0200 Subject: [PATCH 2/8] Wrap object action in workflow_can if block Wrap object action in workflow_can if block, if there are workflows defined for action. --- Resources/templates/CommonAdmin/object_actions.php.twig | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Resources/templates/CommonAdmin/object_actions.php.twig b/Resources/templates/CommonAdmin/object_actions.php.twig index 45d36e4e..72f24028 100644 --- a/Resources/templates/CommonAdmin/object_actions.php.twig +++ b/Resources/templates/CommonAdmin/object_actions.php.twig @@ -14,7 +14,13 @@ {% if action.credentials %} {{ echo_if_granted(action.credentials, builder.ModelClass) }} {% endif -%} + {% if action.workflows is not empty %} + {{ echo_if_workflow(builder.ModelClass, action.workflows) }} + {% endif -%} {{ block('object_action_block') }} + {%- if action.workflows is not empty %} + {{ echo_endif() }} + {% endif %} {%- if action.credentials %} {{ echo_endif() }} {% endif %} @@ -63,4 +69,4 @@ {{ echo_endblock() }} {{ echo_endblock() }} -{% endblock %} \ No newline at end of file +{% endblock %} From c081cb4bc99ca9b30971a3b40e44441083930118 Mon Sep 17 00:00:00 2001 From: loostro Date: Sun, 24 Jul 2016 17:18:40 +0200 Subject: [PATCH 3/8] Add workflows field --- Generator/Action.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Generator/Action.php b/Generator/Action.php index 331d859c..80b92b16 100644 --- a/Generator/Action.php +++ b/Generator/Action.php @@ -40,6 +40,8 @@ class Action protected $credentials = 'AdmingenAllowed'; + protected $workflows = array(); + public function __construct($name, $type = 'custom') { $this->name = $name; @@ -180,11 +182,6 @@ public function getCsrfProtected() return $this->csrfProtected; } - public function setCredentials($credentials) - { - $this->credentials = $credentials; - } - public function setForceIntermediate($forceIntermediate) { $this->forceIntermediate = $forceIntermediate; @@ -195,11 +192,26 @@ public function getForceIntermediate() return $this->forceIntermediate; } + public function setCredentials($credentials) + { + $this->credentials = $credentials; + } + public function getCredentials() { return $this->credentials; } + public function setWorkflows(array $workflows) + { + $this->workflows = $workflows; + } + + public function getWorkflows() + { + return $this->workflows; + } + public function getParams() { return $this->params; From baac4bb1057eeef147c3aef47e052206a4b5f9e6 Mon Sep 17 00:00:00 2001 From: loostro Date: Sun, 24 Jul 2016 17:45:28 +0200 Subject: [PATCH 4/8] Update doc with workflows configuration example --- Resources/doc/customization/actions.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Resources/doc/customization/actions.md b/Resources/doc/customization/actions.md index e5144e5f..84238fdb 100644 --- a/Resources/doc/customization/actions.md +++ b/Resources/doc/customization/actions.md @@ -58,6 +58,10 @@ params: pk: "{{ User.id }}" action: lock csrfProtected: true + workflows: + # if set, protects the action with a `workflow_can` check + # see Symfony 3.2 workflow component + - lock options: # this is the title for intermediate page # if JS is available then intermediate page will not be used @@ -309,6 +313,12 @@ it here. For more documenation about credentials, check our [security documentat > __NOTE__ Credentials given here are valid for the whole admin, but can be overridden in specific builders or even specific fields. +##### Workflows + +`workflows` __type__: `array` + +This parameter is implemented only for **object actions** as Workflow Component transition checks can be only made given entity context. Empty by default, if set - the action will check for if given transitions can be made - and only then the button will be rendered and corresponding controller will allow to complete the action. + ##### CSRF protected `crsfProtected` __type__: `bool` __default__: `false` From 4fcfcaec9d3611842e47c436b903636b6b8b510b Mon Sep 17 00:00:00 2001 From: loostro Date: Sun, 24 Jul 2016 17:52:31 +0200 Subject: [PATCH 5/8] Update actions.md --- Resources/doc/customization/actions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Resources/doc/customization/actions.md b/Resources/doc/customization/actions.md index 84238fdb..0868433c 100644 --- a/Resources/doc/customization/actions.md +++ b/Resources/doc/customization/actions.md @@ -59,7 +59,7 @@ params: action: lock csrfProtected: true workflows: - # if set, protects the action with a `workflow_can` check + # if set, wraps action link with a `workflow_can` check # see Symfony 3.2 workflow component - lock options: @@ -317,7 +317,7 @@ specific fields. `workflows` __type__: `array` -This parameter is implemented only for **object actions** as Workflow Component transition checks can be only made given entity context. Empty by default, if set - the action will check for if given transitions can be made - and only then the button will be rendered and corresponding controller will allow to complete the action. +This parameter is implemented only for **object actions** as Workflow Component transition checks can be only made given entity context. Empty by default, if set - the button link will be wrapped in a `workflow_can` check. ##### CSRF protected From 88dc3fb9f9f9764c44e4566c30e054615e88dfc5 Mon Sep 17 00:00:00 2001 From: loostro Date: Sun, 24 Jul 2016 17:53:22 +0200 Subject: [PATCH 6/8] Update actions.md --- Resources/doc/customization/actions.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Resources/doc/customization/actions.md b/Resources/doc/customization/actions.md index 0868433c..c0a4577a 100644 --- a/Resources/doc/customization/actions.md +++ b/Resources/doc/customization/actions.md @@ -368,8 +368,7 @@ Set the action of the button. When this is set, there will be no controller STUB rendered as simple URL. ##### Submit -`submit` __type -__: `bool` +`submit` __type__: `bool` If set to true, the button will behave as a submit button for the form on that page. From 8d142b2162a54cc50b1b8167938ae307586c15ec Mon Sep 17 00:00:00 2001 From: loostro Date: Sun, 24 Jul 2016 18:01:56 +0200 Subject: [PATCH 7/8] Add example workflow usage --- Resources/doc/customization/actions.md | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Resources/doc/customization/actions.md b/Resources/doc/customization/actions.md index c0a4577a..2b8108b1 100644 --- a/Resources/doc/customization/actions.md +++ b/Resources/doc/customization/actions.md @@ -155,6 +155,38 @@ For each custom object action there are four methods generated: > **Note:** The only method you **have to** overwrite is `executeObject{{ ActionName }}`. +#### Workflow transition actions + +If you use the [Symfony Workflow Component](http://symfony.com/blog/new-in-symfony-3-2-workflow-component), you can use the `workflows` option to wrap object actions with a `workflow_can` check. Example config and controller: + +```yaml +# config +params: + object_actions: + lock: + label: Lock account + icon: glyphicon-lock + route: Acme_SecurityBundle_User_object # Optional + params: # Optional + pk: "{{ User.id }}" + action: lock + csrfProtected: true + workflows: + - lock +``` + +```php + + /** + * This function is for you to customize what action actually does + */ + protected function executeObjectLock($User) + { + $this->get('worflow.user_management')->apply($User, 'lock'); + } + +``` + ### Custom batch action example #### Batch actions configuration From 3107e9ae0fa7c50df1fb896f8d3d3c25f7527975 Mon Sep 17 00:00:00 2001 From: loostro Date: Sun, 24 Jul 2016 18:03:59 +0200 Subject: [PATCH 8/8] Update actions.md --- Resources/doc/customization/actions.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Resources/doc/customization/actions.md b/Resources/doc/customization/actions.md index 2b8108b1..763af57d 100644 --- a/Resources/doc/customization/actions.md +++ b/Resources/doc/customization/actions.md @@ -166,8 +166,8 @@ params: lock: label: Lock account icon: glyphicon-lock - route: Acme_SecurityBundle_User_object # Optional - params: # Optional + route: Acme_SecurityBundle_User_object + params: pk: "{{ User.id }}" action: lock csrfProtected: true @@ -182,6 +182,7 @@ params: */ protected function executeObjectLock($User) { + // this service is provieded by Workflow Component $this->get('worflow.user_management')->apply($User, 'lock'); }