Using this old thread to clarify a problem I have with the joinGroup method (MODX Revo 2.3.1):
I'm just using the joinGroup method for a new add-on an it seems this method has a problem when trying to only assign a group
without a role.
$user->joinGroup(intval($group), intval($role)); // where $role = '0' (= NONE in 'modx-combo-role' ExtJS element)
or
$user->joinGroup(intval($group));
Both method calls writes a role with id 1 to the modx_member_groups table.
Don't know why this happens!? Could be a bug in joinGroup method:
/**
* Join a User Group, and optionally assign a Role.
*
* @access public
* @param mixed $groupId Either the name or ID of the User Group to join.
* @param mixed $roleId Optional. Either the name or ID of the Role to
* assign to for the group.
* @return boolean True if successful.
*/
public function joinGroup($groupId,$roleId = null) {
$joined = false;
$groupPk = is_string($groupId) ? array('name' => $groupId) : $groupId;
/** @var modUserGroup $userGroup */
$userGroup = $this->xpdo->getObject('modUserGroup',$groupPk);
if (empty($userGroup)) {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'User Group not found with key: '.$groupId);
return $joined;
}
/** @var modUserGroupRole $role */
if (!empty($roleId)) {
$rolePk = is_string($roleId) ? array('name' => $roleId) : $roleId;
$role = $this->xpdo->getObject('modUserGroupRole',$rolePk);
if (empty($role)) {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'Role not found with key: '.$role);
return $joined;
}
}
/** @var modUserGroupMember $member */
$member = $this->xpdo->getObject('modUserGroupMember',array(
'member' => $this->get('id'),
'user_group' => $userGroup->get('id'),
));
if (empty($member)) {
$rank = count($this->getMany('UserGroupMembers'));
$member = $this->xpdo->newObject('modUserGroupMember');
$member->set('member',$this->get('id'));
$member->set('user_group',$userGroup->get('id'));
$member->set('rank', $rank);
if (!empty($role)) {
$member->set('role',$role->get('id'));
}
$joined = $member->save();
if (!$joined) {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'An unknown error occurred preventing adding the User to the User Group.');
} else {
unset($_SESSION["modx.user.{$this->get('id')}.userGroupNames"]);
}
} else {
$joined = true;
}
return $joined;
}
I think the problem lies in line 42, where we try to determine whether the non existent object $role is empty().
[ed. note: gadgetto last edited this post 12 years, 2 months ago.]