The primary test for whether a user has permission to create a node in og_node_access() effectively ignores the specified $account, and always tests access against the current global $user instead of $account.

The result is that calls to node_access('create', $node_type, $account) will return invalid results if OG is the module granting create access. However, when logged in as $account, calls to node_access('create', $node_type) return the correct result.

The "test" patch contains a simple addition to og.test that demonstrates the problem -- and OG currently fails the added test. The problem is also apparent when using Devel Node Access, which notices the access inconsistencies and triggers a warning about "Core seems to disagree on this item. This is a bug in either DNA or Core and should be fixed!"

The source of the problem is that OgSelectionHandler always uses the global $user when building the list of referencable entities. So when og_node_access() checks "if there are any groups the user will be able to select" using entityreference_get_selection_handler($field, $instance)->countReferencableEntities() the check is done as $user not as $account.

The "fix" patch fixes the problem by adding a setAccount() function to OgSelectionHandler, and changing all access checks in the class to respect the specified value of $account. The resulting code is able to pass the newly-added test.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch, og-node_access_create_test.patch, failed testing.

Nephele’s picture

Status: Needs work » Needs review

Just to followup given the lack of feedback:

"og-node_access_create_test.patch" fails the test intentionally, as the easiest objective way of demonstrating that there is a bug in the code. The only change in that patch is adding one simple test to the og test suite -- a test that calls node_access() and checks whether the returned value is correct. The test fails because og contains a bug when node_access is called.

The second patch ("og-node_access_create_fix.patch") -- the patch that passed the tests -- is the one that needs to be applied to the code. It contains everything in the first patch (i.e., the new node_access() test), as well as the code modifications necessary to fix the problem.

Therefore, the correct status is needs review, not needs work.

brad.bulger’s picture

reroll against current 2.x branch.

Status: Needs review » Needs work

The last submitted patch, 3: og-node_access_create_fix-2245235-3.patch, failed testing.

brad.bulger’s picture

reroll against 2.10

brad.bulger’s picture

Status: Needs work » Needs review
joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community

Seems to make sense to me, and has tests.

mdolnik’s picture

There is one issue with the latest patch above...

When we are checking the access of nodes, the query built by OgSelectionHandler::buildEntityFieldQuery() ends up having the node_query_entity_field_access_alter() query-alter called which in-turn calls _node_query_node_access_alter()

In _node_query_node_access_alter() it checks the access for the currently logged in user, not the specific user we are checking access for.

We can get around this by adding the following to OgSelectionHandler::buildEntityFieldQuery():

$query->addMetaData('account', $this->account);

I have updated the patch accordingly (re-rolled against the latest 7.x-2.x)...