Skip to content
Snippets Groups Projects
Commit f19eff0a authored by John Voskuilen's avatar John Voskuilen
Browse files

Issue #3513199: Review Workflow Access submodule - move CRUD functions into...

Issue #3513199: Review Workflow Access submodule - move CRUD functions into new class WorkflowAccessState
parent 475d63e9
No related branches found
No related tags found
No related merge requests found
Pipeline #449826 passed with warnings
<?php
namespace Drupal\workflow_access\Entity;
/**
* Workflow Access configuration entity to persistently store configuration.
*
* @todo Make this a wrapper class for WorkflowState.
*/
class WorkflowAccessState {
public const ROLE_ACCESS = 'workflow_access.role';
/**
* The machine name.
*
* @var string
*/
public $id;
/**
* Constructs the object.
*
* @param array $values
* The list of values.
* @param string $entity_type_id
* The name of the new State. If '(creation)', a CreationState is generated.
*/
public function __construct($values) {
$this->id = $values['id'];
}
/**
* {@inheritdoc}
*
* Avoids error on WorkflowStateListBuilder:
* "Cannot load the "workflow_state" entity with NULL ID."
*/
public static function load($id) {
return $id ? new WorkflowAccessState(['id' => $id]) : NULL;
}
/**
* Given a sid, retrieve the access information and return the row(s).
*/
public function readAccess() {
$result = \Drupal::configFactory()->getEditable(WorkflowAccessState::ROLE_ACCESS)
->get($this->id);
// Avoid errors in calling function when no data available.
$result ??= [];
return $result;
}
/**
* Given data, insert into workflow access - we never update.
*/
public function insertAccess(&$data) {
\Drupal::configFactory()->getEditable(WorkflowAccessState::ROLE_ACCESS)
->set($this->id, $data)
->save();
return $this;
}
/**
* Given data, insert into workflow access - we never update.
*/
public function updateAccess(&$data) {
return $this->insertAccess($data);
}
/**
* Given a sid, delete all access data for this state.
*/
public function deleteAccess() {
\Drupal::configFactory()->getEditable(WorkflowAccessState::ROLE_ACCESS)
->clear($this->id)
->save();
return $this;
}
}
......@@ -7,6 +7,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\workflow\Entity\WorkflowState;
use Drupal\workflow\Form\WorkflowConfigTransitionFormBase;
use Drupal\workflow_access\Entity\WorkflowAccessState;
/**
* Provides the base form for workflow add and edit forms.
......@@ -36,7 +37,7 @@ class WorkflowAccessRoleForm extends WorkflowConfigTransitionFormBase {
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['workflow_access.role'];
return [WorkflowAccessState::ROLE_ACCESS];
}
/**
......@@ -71,7 +72,8 @@ class WorkflowAccessRoleForm extends WorkflowConfigTransitionFormBase {
$view = $update = $delete = [];
$count = 0;
foreach (workflow_access_get_workflow_access_by_sid($sid) as $rid => $access) {
$access_state = new WorkflowAccessState(['id' => $sid, 'wid' => $workflow->id()]);
foreach ($access_state->readAccess() as $rid => $access) {
$count++;
$view[$rid] = $access['grant_view'] ? $rid : 0;
$update[$rid] = $access['grant_update'] ? $rid : 0;
......@@ -121,7 +123,7 @@ class WorkflowAccessRoleForm extends WorkflowConfigTransitionFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state->getValue($this->entitiesKey) as $sid => $access) {
// @todo Not waterproof; can be done smarter, using elementchildren().
if (!WorkflowState::load($sid)) {
if (!$access_state = WorkflowAccessState::load($sid)) {
continue;
}
......@@ -132,7 +134,7 @@ class WorkflowAccessRoleForm extends WorkflowConfigTransitionFormBase {
'grant_delete' => (!empty($access['delete'][$rid])) ? (bool) $access['delete'][$rid] : 0,
];
}
workflow_access_insert_workflow_access_by_sid($sid, $data);
$access_state->insertAccess($data);
// Update all nodes to reflect new settings.
node_access_needs_rebuild(TRUE);
......
......
......@@ -12,6 +12,7 @@ use Drupal\workflow\Entity\WorkflowInterface;
use Drupal\workflow\Entity\WorkflowRole;
use Drupal\workflow\Entity\WorkflowState;
use Drupal\workflow\Entity\WorkflowTargetEntity;
use Drupal\workflow_access\Entity\WorkflowAccessState;
use Drupal\workflow_access\Form\WorkflowAccessSettingsForm;
/**
......@@ -130,7 +131,8 @@ class WorkflowAccessHooks {
continue;
}
foreach (workflow_access_get_workflow_access_by_sid($current_sid) as $rid => $grant) {
$access_state = new WorkflowAccessState(['id' => $current_sid]);
foreach ($access_state->readAccess() as $rid => $grant) {
// Anonymous ($uid == 0) author is not allowed for role 'Author' (== -1).
// Both logically (Anonymous having more rights then authenticated)
// and technically ($gid must be a positive integer).
......
......
......@@ -5,7 +5,6 @@
* Provides node access permissions based on workflow states.
*/
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Hook\Attribute\LegacyHook;
use Drupal\Core\Routing\RouteMatchInterface;
......@@ -73,35 +72,3 @@ function workflow_access_user_role_update(EntityInterface $entity) {
function workflow_access_workflow_operations($op, ?EntityInterface $entity = NULL) {
return \Drupal::service(WorkflowAccessHooks::class)->workflowOperations($op, $entity);
}
/**
* DB functions - all DB interactions are isolated here to make for easy updating should our schema change.
*/
/**
* Given a sid, retrieve the access information and return the row(s).
*/
function workflow_access_get_workflow_access_by_sid($sid) {
$result = \Drupal::config('workflow_access.role')->get($sid);
// Avoid errors in calling function when no data available.
$result ??= [];
return $result;
}
/**
* Given a sid, delete all access data for this state.
*/
function workflow_access_delete_workflow_access_by_sid($sid) {
\Drupal::configFactory()->getEditable('workflow_access.role')
->clear($sid)
->save();
}
/**
* Given data, insert into workflow access - we never update.
*/
function workflow_access_insert_workflow_access_by_sid($sid, &$data, ?Config $config = NULL) {
\Drupal::configFactory()->getEditable('workflow_access.role')
->set($sid, $data)
->save();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment