We have the patches applied to implement multiple Parent Groups per Node as per the Parent Issue #2317195-55: Allow more than one parent by node / subgroup.

When logged in as User 1, this works fine. But when logged in as even the Group Admin, an attempt to merely view any group node (“…/node/##”), let alone edit it (“…/node/##/edit”), the following is generated within the theme and no other output is displayed:

Error

Error message

  • Notice: Undefined variable: group in gnode_node_access() (line 306 of …/sites/all/modules/group/modules/gnode/gnode.module).
  • Recoverable fatal error: Argument 2 passed to group_access() must be an instance of Group, null given, called in …/sites/all/modules/group/modules/gnode/gnode.module on line 306 and defined in group_access() (line 24 of …/sites/all/modules/group/helpers/group.inc).

The website encountered an unexpected error. Please try again later.

It works fine and no error is generated if the “##” is the nid of a node of the same Content Type that doesn’t have any Parent Groups set, or if the currently logged-in user is User 1 (as stated before). But when logged in as the Group Admin and the node belongs to any Group(s) (even the one that the logged-in user is Group Admin of, or any other Groups), the error is generated.

Perhaps related: I assigned these nodes to groups manually by using the “Groups” tab in the Edit Node page (I was unable to get the Rules Action to work with the multiple Parent Groups patch in place as per another Issue I commented in before #2530000-18: Bulk assign of content to groups). I just noticed that even when Editing a Node with one or more Parent Groups checked, that the Group Tab still says, “Group Settings” | “Not a group nodeeven if one or more Groups had already been assigned to the Node before entering the Edit page this session!

Is there some flag indicating a Node as a GNode beyond the Parent Groups gid list or array that isn’t being set somehow?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Joel MMCC created an issue. See original summary.

Joel MMCC’s picture

I saw Comment #61 in the Parent Issue #2317195-61: Allow more than one parent by node / subgroup by the patch author which basically said to try re-saving the Group configurations if there were any problems. So, I did. Two of the three Groups we have took a long time to re-save. After re-saving all three, I tried again to view gnodes while logged in as a Group Admin, and again got the errors.

Joel MMCC’s picture

FileSize
11.27 KB
Joel MMCC’s picture

Also, after doing the re-saving of Group configurations, editing a gnode (which apparently only User 1 can do without generating the errors) still shows “Not a group node” under “Group settings” even if it’s assigned to one or more Parent Groups:
Group Settings shows “Not a Group Node” despite being in all three available Parent Groups.

Joel MMCC’s picture

Has anyone looked at this issue? Do we need to offer a bounty? How do we go about doing that? This is an absolute showstopper for a major project at a critical stage.

We especially need attention from @ctrlADel and @josephdpurcell as developer and refiner (respectively) of the Parent Issue patch which seems to be causing this, and of course @kristiaanvandeneynde as Project Maintainer. Thanks.

Joel MMCC’s picture

Title: "Undefined group in gnode_node_access() line 306" when viewing gnode if logged in as Group Admin » "Undefined group in gnode_node_access() line 306" when viewing gnode if logged in as any non-Administrator
Issue summary: View changes

Just verified that this error happens regardless of the logged-in user unless the user is User 1 or a member of the Administrators role. This includes Group Admins as stated before, but also Group Members, Outsiders, and Users who are not a member of any Group.

If the node has been assigned to any Group(s), then the error appears on any attempt to view or edit it. If the node has not been assigned to any Group(s), then viewing and editing (where permissions permit) work just fine.

Again: this is a showstopper for a major system under development. We’re offering to pay for a resolution.

Going to Drupal 8 (which does seem to have a more robust version of Group) is not an option since other modules that this system absolutely depends on (such as IP Geolocation & Maps) have no usable D8 version, nor one in the pipeline (#2624188: [ip_geoloc] IP Geolocation Views & Mapsso far all that has been done is to convert the .info file to .yml format, and that has been the status since August 11, 2013).

Joel MMCC’s picture

Priority: Major » Critical
Joel MMCC’s picture

Title: "Undefined group in gnode_node_access() line 306" when viewing gnode if logged in as any non-Administrator » "Undefined group in gnode_node_access() line 306" after Patch #55 of Parent Issue when viewing gnode if logged in as any non-Administrator
Joel MMCC’s picture

Title: "Undefined group in gnode_node_access() line 306" after Patch #55 of Parent Issue when viewing gnode if logged in as any non-Administrator » "Undefined group in gnode_node_access() line 306" after Patch #55 of Parent Issue when viewing gnode if logged in without “Bypass group access control” permission

It finally occurred to me to try to figure out just which Permission the Administrators had that the others lacked that the lack thereof triggered the error. I figured to start with the permissions actually implemented by Group.

Of the three default permissions plus the one created for each individual group type itself, the first two are Administrator-level. The third I had already assigned to some of the roles generating the error, so I knew that couldn’t be it.

So, to test, I assigned to a non-Admin Role the second Permission:

Bypass group access control
View, edit and delete all groups regardless of permission restrictions Warning: Give to trusted roles only; this permission has security implications.

And lo! The users of that Role could now safely view gnode content without the error!

But since this is a “security implications” Permission, I’d rather not assign that to non-Admin-level Roles, and also it defeats the whole point of having group access if we bypass the access rules.

Still, though, maybe this information will help track down the underlying cause of these errors?

I may later try assigning just the other Admin-level Permission:

Configure Group module
Configure group types, group roles, member fields, etc. Warning: Give to trusted roles only; this permission has security implications.

to a non-Admin Role and see what happens.

Joel MMCC’s picture

Status: Active » Needs review

Okay, I think I finally fixed it, even though I know next to nothing about Drupal module development.

In looking over the Line 306 area of the patched …/sites/all/modules/group/modules/gnode/gnode.module, I found that it was in this function:

function gnode_node_access($node, $op, $account) {
  if (_gnode_get_mode() === GROUP_NODE_COMPLIANCE_MODE && $op !== 'create') {
    return NODE_ACCESS_IGNORE;
  }

  if (is_string($node)) {
    if ($op == 'create') {
      if (gnode_group_node_create_access($node, $account)) {
        return NODE_ACCESS_ALLOW;
      }
    }
  }

  // Make sure we are dealing with a published group node.
  if (empty($node->group) || !$node->status) {
    return NODE_ACCESS_IGNORE;
  }

  // If the user can bypass group access, he is allowed access.
  if (user_access('bypass group access', $account)) {
    return NODE_ACCESS_ALLOW;
  }

  // If the user can administer the group, he is allowed access.
  if (group_access('administer group', $group, $account)) {
    return NODE_ACCESS_ALLOW;
  }

  // Check access within all of the node's groups.
  foreach (group_entity_parent_gids($node) as $group) {
    if (group_access('administer group', $group, $account)) {
      return NODE_ACCESS_ALLOW;
    }

    switch ($op) {
      case "view":
        if (group_access("view $node->type node", $group, $account)) {
          return NODE_ACCESS_ALLOW;
        }
        break;

      case "update":
      case "delete":
        if (group_access("$op any $node->type node", $group, $account)) {
          return NODE_ACCESS_ALLOW;
        }

        if ($account->uid == $node->uid && group_access("$op own $node->type node", $group, $account)) {
          return NODE_ACCESS_ALLOW;
        }

        break;
    }
  }

  return NODE_ACCESS_DENY;
}

The problem is that it still has code (which was apparently added by the patch!) to check for single parent group access for a Group Administrator, as well as (and just before) the same code safely within a foreach right below it. The comment “// If the user can administer the group, he is allowed access.” appears twice in short succession, with nearly identical code and functionality, differing only in that the second is properly enclosed in a foreach loop since the $node->group is now an array instead of a single instance of Group.

The original version of this function had a line not far up that assigned $group to $node->group, but that line was removed in the patch without replacement. This is why this line fails: $group is undefined!

Since the functionality is duplicated below properly, all I had to do was comment out this snippet (I use three slashes instead of two for my debugging comments to help me find them later):

  // If the user can administer the group, he is allowed access.
///  if (group_access('administer group', $group, $account)) {
///    return NODE_ACCESS_ALLOW;
///  } /// ERRONEOUS with multiple parent groups! Redundant, too. See below:

This solved the problem, and so far as I know has no adverse side effects. I’ll do further tests to make sure that it maintains enforcement of proper gnode access, but it should since the same functionality properly implemented appears right below that.

If commenting that out has no adverse side-effects, then that section of code should probably be deleted entirely in the next revision of the patch.

I’m not familiar with how to go about rolling this into a new patch (as I said, I’m not yet a Drupal module developer nor maintainer, but would like to learn). So if someone could take care of this, I’d appreciate it.

Ryan Osītis’s picture

It's DrupalCon time, so I'll jump on this. I re-rolled the patch from the parent issue to include your fix. Give it a try. You may need to un-patch and re-patch.

Joel MMCC’s picture

Thanks, @rositis@gmail.com! It took me a few tries to successfully reverse the patch (I forgot that I had to first undo all of my own changes), but once I did and applied yours, it seems to be working fine (despite giving a “warning: 1 line adds whitespace errors.” message when I git-applied the patch).

You should submit this over at the parent thread.

kristiaanvandeneynde’s picture

Just to let you guys know, I'm currently reworking the D7 gnode access layer to match the one in D8 for a client. "Safety mode" will be buried in favor of "compliance mode" and all of the performance boosts recently introduced in D8 will be backported. Both this issue and the multiple parents issue can then (finally) move forward and perhaps get committed.

Joel MMCC’s picture

Okay, I see a potential problem with this patch. It works fine for me, but it may not work fine in all situations. According to the PHP Manual, foreach will issue an error if used on a variable that isn’t an array or object.

Now here’s the thing: the former (and this) patch implement a “Group entity settings” admin tab at “…/admin/group/settings/entity”. The bottom section of this is as follows:

Entities in multiple groups

🗹 Group
🗹 Node
Choose if an entity can be in multiple groups. Entities in multiple groups can be updated and deleted by users with the necessary permissions in any of the groups it is in. After changing this session you must clear the site cache. It is not recommended that you go from multiple to single on a site which has entities in multiple groups without testing.

Firstly, in the help text, the word “session” (which I boldfaced above) should probably be “setting.”

But more importantly, what do those checkboxes actually do? Does un-checking the “Node” checkbox switch back to the former, pre-patch implentation of gnodes, with $node->group returning a scalar Group instead of an Array of Groups? If so, then the seemingly redundant (and error-producing) code that I commented out and rositis@gmail.com deleted may turn out to be needed after all.

If this is how it works, then, for those who don’t want Multiple Parent Group functionality for Nodes despite having the patch installed (maybe they only want [sub]Groups to be able to have multiple parent Groups?), I strongly suspect that the code will now produce an error on the foreach loops for such sites.

What should probably be done is to enclose the whole node access testing code within a conditional (if (…) {…} else {…}) block testing on the setting of that checkbox (I’m not sure how to check for the configuration setting in code), with the if (…) {…} block enclosing the new version of the code in this patch which uses foreach loops to iterate through the $node->group array of parent groups, while the else {…} block would instead enclose the former version of the gnode code which acts on $node->group directly as a scalar Group reference.

Either that or remove the setting entirely and simply assume that Multiple Parent Groups should always be allowed, and that $node->group would always return an array, even if with only one element (or zero for that matter). Or maybe have the checkbox work by limiting the size of the array to no more than one for those sites who want to enforce having only one Parent Group for any GNode, but still have it stored as an array for consistency and compatibility with other code.

I assume that a similar issue could arise with the Subgroups functionality and the “Group” checkbox.

Joel MMCC’s picture

In reading up on the differences between Drupal 7 and 8 Entity APIs, D8 resolves this sort of thing by making everything a list. If I read and understood it right, all properties of all Entities are lists, even if it’s something like a Node’s “Title” that can logically only have one entry. D8 has constraints to enforce validations at more than just the form level, including enforcing minimum and maximum counts in the list.

I think that the D7 Group module should be written like the D8 one as much as possible, and Parent Group should be a list even if the checkbox to allow multiple Parent Groups is unchecked. That way all code to access parameters would access them as lists so there wouldn’t have to be constant checks to see if it should be a list or a scalar, thus preventing such errors.

I’ll post more details in the Parent Issue thread.

kristiaanvandeneynde’s picture

Component: Group Node (gnode) » Code
Status: Needs review » Closed (outdated)

The Drupal 7 version is no longer supported, cleaning up the issue queue.