When a user that does have access to edit a group but does not have the 'edit groups hierarchy' permission edits and saves a group that already has a parent node, the parent assignment is lost. The problem lies in the function og_subgroups_set_parent(). When this function is called using the nodeapi hook, and the user saving the node doesn't have access to set the parent, then the parent for the node object is empty.
The function og_subgroups_set_parent() does not properly ensure that the parent is set before it runs a delete statement that deletes all the relationships for the node being edited.
In the code below, the $pid is going to always be empty if the user doesn't have access to set the parent.
// Either objects or node ids can be passed in
$nid = is_object($node) ? $node->nid : $node;
$pid = is_object($parent) ? $parent->nid : $parent;
This statement is executed even when the parent is not set. So, any parent assignment is lost.
// Remove any existing parent for this group
$success = db_query("DELETE FROM {og_subgroups} WHERE gid = %d", $nid);
I have attached a patch that fixes this issue.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | updated_set_parent_id_for_insert_update_og_subgroups_nodeapi-1589064-6834402.patch | 1020 bytes | james marks |
| #2 | set_parent_id_for_insert_update_og_subgroups_nodeapi-1589064.patch | 962 bytes | james marks |
| og_subgroups.module.patch | 899 bytes | justageek |
Comments
Comment #1
kscheirerTested patch and it works great - when saving the group node the parent is not lost anymore. Small note: had to apply patch with
patch -p6 < og_subgroups.module.patch- you should create patches from the root of the og_subgroups directory - not from Drupal root.Comment #2
james marks commentedAfter looking at the patch and the original code, I think the problem is actually with the implementation of hook_nodeapi.
To begin with, the $node parameter is not passed by reference. As a result, when $op = 'load', the parent id is gotten and set but, because $node is not passed by reference nor is the $node returned, it never actually updates the $node object.
This:
should be this:
The second problem is in the switch statement. When the operation is 'load', the parent id is set in the $node object. When the operation is 'insert' or 'update', however, the parent id is not set resulting in og_subgroups_set_parent() being called with a null value for $node->og_parent which, then, removes the parent node as designed.
The solution is to get and set the parent id.
This removes the need to modify the og_subgroups_set_parent() function as the previous patch does (meaning, if I'm correct, that this patch should supercede the previous patch).
This patch may also affect #1346100: Audience settings removed when saving the node. and #1482408: Parent node being missed in node form under certain conditions. although I haven't tested those.
Comment #3
james marks commentedChanging status to 'needs review'.
Comment #4
james marks commentedFixing patch. Previous patch did not respect $node->og_parent if it was already set in 'insert' and 'update' cases of switch statement in og_subgroups_nodeapi(). Added a test for existing $node->og_parent value.
Comment #5
vglocus commentedThis worked great for me. Thank you so much.
--
V
Comment #6
underq commentedWith this solution it's impossible to remove the parent from the group.
Comment #7
srees commentedOne of our devs has suggested to solve the problem by modifying the 'if' statement in the nodeapi call for update/insert as follows (line 266):
This has the benefit of utilizing the already existing permission for whether the user is able to edit the hierarchy...
I haven't thoroughly tested it yet, but it seems good to me initially...more like how it should have been, since there is that permission...