Index: branches/5.2.x/core/units/user_groups/user_groups_eh.php =================================================================== diff -u -N -r14244 -r14437 --- branches/5.2.x/core/units/user_groups/user_groups_eh.php (.../user_groups_eh.php) (revision 14244) +++ branches/5.2.x/core/units/user_groups/user_groups_eh.php (.../user_groups_eh.php) (revision 14437) @@ -1,6 +1,6 @@ Prefix == 'u-ug') { - $new_groups = $this->Application->GetVar('g'); - if (!$new_groups) { - return ; - } + $new_groups = $this->Application->GetVar('g'); + if (!$new_groups) { + return ; + } - $new_groups = array_keys($new_groups); + $new_groups = array_keys($new_groups); - // don't insert duplicate group membership record - $user_id = $this->Application->GetVar('u_id'); - $table_name = $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:u'); - $sql = 'SELECT GroupId - FROM '.$table_name.' - WHERE PortalUserId = '.(int)$user_id; - $old_groups = $this->Conn->GetCol($sql); + // don't insert duplicate group membership record + $user_id = $this->Application->GetVar('u_id'); + $table_name = $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:u'); - $new_groups = array_diff($new_groups, $old_groups); - if ($new_groups) { - foreach ($new_groups as $new_group) { - $fields_hash = Array ( - 'GroupId' => $new_group, - 'PortalUserId' => $user_id, - 'PrimaryGroup' => 0, - ); - $this->Conn->doInsert($fields_hash, $table_name); + $sql = 'SELECT GroupId + FROM '.$table_name.' + WHERE PortalUserId = '.(int)$user_id; + $old_groups = $this->Conn->GetCol($sql); + + $new_groups = array_diff($new_groups, $old_groups); + + if ($new_groups) { + foreach ($new_groups as $new_group) { + $fields_hash = Array ( + 'GroupId' => $new_group, + 'PortalUserId' => $user_id, + ); + $this->Conn->doInsert($fields_hash, $table_name); + } } } - } - if ($event->Prefix == 'g-ug') { + elseif ($event->Prefix == 'g-ug') { $new_users = $this->Application->GetVar('u'); + if (!$new_users) { return ; } @@ -62,18 +64,19 @@ // don't insert duplicate group membership record $group_id = $this->Application->GetVar('g_id'); $table_name = $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:g'); + $sql = 'SELECT PortalUserId FROM ' . $table_name . ' WHERE GroupId = ' . (int)$group_id; $old_users = $this->Conn->GetCol($sql); $new_users = array_diff($new_users, $old_users); + if ($new_users) { foreach ($new_users as $new_user) { $fields_hash = Array ( 'GroupId' => $group_id, 'PortalUserId' => $new_user, - 'PrimaryGroup' => 0, ); $this->Conn->doInsert($fields_hash, $table_name); } @@ -92,20 +95,13 @@ function OnSetPrimary(&$event) { $ids = $this->StoreSelectedIDs($event); + if ($ids) { - $id = array_shift($ids); - $object =& $event->getObject( Array('skip_autoload' => true) ); - $user_id = $this->Application->GetVar('u_id'); + $user =& $this->Application->recallObject('u'); + /* @var $user kDBItem */ - $sql = 'UPDATE ' . $object->TableName . ' - SET PrimaryGroup = 0 - WHERE PortalUserId = ' . (int)$user_id; - $this->Conn->Query($sql); - - $sql = 'UPDATE ' . $object->TableName . ' - SET PrimaryGroup = 1 - WHERE ' . $object->IDField . ' = ' . $id . ' AND PortalUserId = ' . (int)$user_id; - $this->Conn->Query($sql); + $user->SetDBField('PrimaryGroupId', array_shift($ids)); + $user->Update(); } $this->clearSelectedIDs($event); @@ -120,26 +116,36 @@ { if ($event->Name == 'OnMassDelete' && $type == 'before') { $ids = $event->getEventParam('ids'); + if ($ids) { $object =& $event->getObject( Array('skip_autoload' => true) ); if ($event->Prefix == 'u-ug') { - $user_id = $this->Application->GetVar('u_id'); - $sql = 'SELECT '.$object->IDField.' - FROM '.$object->TableName.' - WHERE '.$object->IDField.' IN ('.implode(',', $ids).') AND PortalUserId = '.(int)$user_id.' AND PrimaryGroup = 0'; - $ids = $this->Conn->GetCol($sql); + // allow deleting non-primary group of current user ($ids - groups) + $sql = 'SELECT PrimaryGroupId + FROM ' . $this->Application->GetTempName(TABLE_PREFIX . 'PortalUser', 'prefix:u') . ' + WHERE PortalUserId = ' . (int)$this->Application->GetVar('u_id'); + $primary_group_id = (int)$this->Conn->GetOne($sql); + + $index = array_search($primary_group_id, $ids); + + if ($index !== false) { + unset($ids[$index]); + $event->setEventParam('ids', $ids); + } } elseif ($event->Prefix == 'g-ug') { - $group_id = $this->Application->GetVar('g_id'); - $sql = 'SELECT '.$object->IDField.' - FROM '.$object->TableName.' - WHERE '.$object->IDField.' IN ('.implode(',', $ids).') AND GroupId = '.(int)$group_id.' AND PrimaryGroup = 0'; - $ids = $this->Conn->GetCol($sql); + // allow deleting users from group record, then it's not their primary group ($ids - users) + $group_id = (int)$this->Application->GetVar('g_id'); + + $sql = 'SELECT PortalUserId + FROM ' . TABLE_PREFIX . 'PortalUser' . ' + WHERE PortalUserId IN (' . implode(',', $ids) . ') AND PrimaryGroupId = ' . $group_id; + $exclude_users = $this->Conn->GetCol($sql); + + $event->setEventParam('ids', array_diff($ids, $exclude_users)); } - $event->setEventParam('ids', $ids); } } } - } \ No newline at end of file