in a drupal 7 site i have been given to maintain, it seems that permissions are not working as expected. I check and uncheck the buttons for a certain user role, but that user role always has the right to edit any node that belongs to this particular type. 1. What could be the cause for this? 2. If this is not working from the gui, how i could achieve something similar with code? I would appreciate an example
2 Answers
In /admin/people/permissions
Under "Node" category you can uncheck the permission "Edit content X" for your desired role.
I would recommend using this module if your site has a lot of modules/permissions since due max upload vars limitation by PHP could led to not storing your chages when submitting the form.
Also by code you can implement hook_node_access
and put there some logic. Take a look on how core does it.
/**
* Implements hook_node_access().
*/
function node_node_access($node, $op, $account) {
$type = is_string($node) ? $node : $node->type;
if (in_array($type, node_permissions_get_configured_types())) {
if ($op == 'create' && user_access('create ' . $type . ' content', $account)) {
return NODE_ACCESS_ALLOW;
}
if ($op == 'update') {
if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
return NODE_ACCESS_ALLOW;
}
}
if ($op == 'delete') {
if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
return NODE_ACCESS_ALLOW;
}
}
}
return NODE_ACCESS_IGNORE;
}
-
permissions via GUI do not take into effect. The module also did not help. How the above code should be transformed to a module, so as a user role cannot edit a certain content type?– heviJan 18, 2020 at 10:19
In answer to your first question, this existing answer about debugging access permissions should be able to help you uncover what is causing this unexpected behavior.