When I edit a group membership for a user in a group whose status is "Active", and assigning a role, the membership has updated. But when I change the user status to "Pending", the user status has been changed and the role is "unchecked". Then, when I try to change the user status to "Active" and assigning the role gives me the following error

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3-3-1' for key 'PRIMARY': INSERT INTO {og_users_roles} (uid, rid, gid, group_type) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3); Array ( [:db_insert_placeholder_0] => 3 [:db_insert_placeholder_1] => 3 [:db_insert_placeholder_2] => 1 [:db_insert_placeholder_3] => node ) in drupal_write_record() (line 7239 of /var/www/ogdemo/includes/common.inc).

This is because the role I assigned to the user still exists but the role is "unchecked" visually while in the edit page. Need a workaround for this issue.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

criznach’s picture

I found that og_role_grant() was not loading existing roles for non-active users. I changed the default $check_active flag for a call to og_get_user_roles() to FALSE, and the error appears to be fixed. This makes it load existing role records even if the user is not active. Then drupal_write_record does not try and overwrite them.

amitaibu’s picture

Version: 7.x-2.7 » 7.x-2.x-dev
Status: Active » Needs review

Can we add a simpleTest here as-well?

Status: Needs review » Needs work

The last submitted patch, 1: og-duplicate-entry-og_users_roles-2380967-1.patch, failed testing.

rbayliss’s picture

Here is a simpletest demonstrating the problem. When called on an existing membership that already has a role and is not in the active state, og_role_grant fails to detect the presence of the existing role, and tries to write another record. This should fail.

rbayliss’s picture

Here's the whole thing. This should pass.

jrb’s picture

Status: Needs review » Reviewed & tested by the community

The patch in #5 is working for me.

peterpearson’s picture

#5 resolves this issue.

Flow1990’s picture

Same problem when you switch status between "Active", to user status "blocked", the user status has been changed and the role is "unchecked" and cause an error.

I use this to patch problem :

function og_get_user_roles($group_type, $gid, $uid = NULL, $include = TRUE, $check_active = TRUE) {

  .....

  // Check if roles are overriden for the group.
  $query_gid = og_is_group_default_access($group_type, $gid) ? 0 : $gid;

  if ($check_active && $is_blocked) {
    //$roles[$identifier] = array();
    //return $roles[$identifier];

    $query = db_select('og_users_roles', 'ogur');
    $query->innerJoin('og_role', 'ogr', 'ogur.rid = ogr.rid');

    $rids = $query
      ->fields('ogur', array('rid'))
      ->fields('ogr', array('name'))
      ->condition('ogr.group_type', $group_type, '=')
      ->condition('ogr.group_bundle', $bundle, '=')
      ->condition('ogr.gid', $query_gid, '=')
      ->condition('ogur.uid', $uid, '=')
      ->condition('ogur.gid', $gid, '=')
      ->orderBy('rid')
      ->execute()
      ->fetchAllkeyed();
  }
  .....
rbayliss’s picture

@amitaibu - Anything further needed on this one? We've got a fix and a test.

zeev’s picture

#5 fixed the pdo exception.

  • amitaibu committed 268da5a on 7.x-2.x authored by rbayliss
    Issue #2380967 by rbayliss, criznach: Edit Group membership of a user...
amitaibu’s picture

Status: Reviewed & tested by the community » Fixed

Merged, thanks! (Sorry for the late merge, I tend to be a little more quicker when it's a PR to GitHub)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

rbayliss’s picture

Awesome. Thanks!

Xilis’s picture

There is an addition to this fix on github, specifically this issue/this PR.

The same issue persists when trying to change status back to active and assign an existing role to a currently blocked user.

denzabasa’s picture

#5 works for me. Thanks!