0

I am using hook_node_access (https://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/hook_node_access/7.x) to provide some users the ability to edit certain nodes they couldn't otherwise edit based on certain criteria. The solution works but there are two problems

  • The users are able to edit certain nodes but they should also have a permission to attach the node to a specified place in the menu structure
  • I am using the paragraphs module (https://www.drupal.org/project/paragraphs) so although the users are able to edit the node itself, they are unable to edit the paragraphs the node is holding.

I am thinking about the following solution for the above mentioned problems:

  • In hook_init check that the current user has a certain uid
  • Check whether the current user is on a node edit page
  • Check the node has a specified node id
  • If the above criteria is met, on the fly assign a special role to the user
  • The special role has permission to edit menu and paragraphs
  • During the first page load when the criteria above is not anymore met, remove the special role

So my question boils down to whether it is acceptable to assign and remove roles on the fly enabling the same user to have different set of roles based on the node the user is editing. Or would this type of approach be problematic?

I know there are a variety of access related modules but none of those has suited the use case I am dealing with so Im looking a code based solution rather than introducing new node access modules.

  • When you say edit a menu, is this an immutable action, iow the node is always attached to the same menu or menu item? Could you just add a checkbox field to the node which if checked adds the node to the menu during node_save? – longboardnode Apr 2 '17 at 2:07
  • No, the menu is not always the same. – jjei Apr 2 '17 at 5:30
  • Ok so how about my suggestion about adding a checkbox field to the node which if checked adds the node to that menu during node_save? I think this will save you the hassle of changing roles all the time... if you think that will work I can add an answer with more details. – longboardnode Apr 3 '17 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.