workflow_access.workflow.inc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * @file
  4. * Provides node access permissions based on workflow states.
  5. */
  6. /**
  7. * Implements hook_workflow().
  8. *
  9. * Update grants when a node changes workflow state.
  10. * This is already called when node_save is called.
  11. */
  12. function workflow_access_workflow($op, $id, $new_sid, $entity, $force, $entity_type = '', $field_name = '', $transition = NULL) {
  13. // ALERT:
  14. // This is a tricky spot when called on node_insert as part of the transition from create to state1.
  15. // node_save invokes this function as a hook before calling node_access_acquire_grants.
  16. // But when it calls node_access_acquire_grants later, it does so without deleting the access
  17. // set when calling workflow_node_insert because it is an insert and no prior grants are expected.
  18. // This leads to a SQL error of duplicate grants which causes a rollback of all changes.
  19. // Unfortunately, setting access rights isn't the only thing we're doing on node_insert so we
  20. // can't skip the whole thing. So we need to fix it further downstream in order to get this to work.
  21. // Here we don't want to run this in the case of (and ONLY in the case of) a brand new node.
  22. // Node access grants will be run as part of node_save's own granting after this.
  23. //
  24. // NOTE: Any module that sets node access rights on insert will hit this situation.
  25. //
  26. switch ($op) {
  27. case 'transition post':
  28. // This is only used for Workflow Node.
  29. // In some other location, we make sure that 'transition post' is only
  30. // called for Workflow Node, never for Workflow Field. We do not need
  31. // that hook, since you can use the core event 'node_presave' and 'node_save'.
  32. // Node_access functions are only for nodes with a state change and a valid new state.
  33. if ($entity_type == 'node' && !isset($entity->is_new)) {
  34. node_access_acquire_grants($entity);
  35. }
  36. break;
  37. }
  38. }