--- /home/chycadmin/Twiki_dev/vanilla/lib/TWiki/UI/Register.pm 2008-09-12 04:41:58.000000000 +0100 +++ Register.pm 2008-10-07 20:21:03.000000000 +0100 @@ -784,6 +784,9 @@ $data->{Password}, $data->{Email} ); my $log = _createUserTopic($session, $data); $users->setEmails($cUID, $data->{Email}); + foreach my $group (split(/,/, $data->{AddToGroups})) { + _addUserToGroup($session, $group, $data->{WikiName}); + } } catch Error::Simple with { my $e = shift; # Log the error @@ -845,6 +848,93 @@ params => [ $status, $data->{WikiName} ] ); } +sub _addUserToGroup { + my ($session, $groupName, $userName) = @_; + $groupName = TWiki::Sandbox::untaintUnchecked($groupName); + my ( $groupWeb, $groupTopic ) = $session->normalizeWebTopicName( $TWiki::cfg{UsersWebName}, $groupName ); + + + + #open Group topic, parse for the GROUPs setting, append new user + #find where GROUP is set, use that code if we can, so that when it goes multi-line it copes + #TODO: LATER: check for duplicates - should not happen, this is registration. + + #run this as calling user, if the registration is being run by an existing user + # (often done by admins) + my $user = $session->{user}; + $session->writeDebug("TRYING to add $userName to $groupTopic, as ".$user->wikiName) if DEBUG; + if (($session->{users}->getWikiName($user) eq $TWiki::cfg{DefaultUserWikiName}) || + ($session->{users}->getWikiName($user) eq $userName)) { + $user=$session->{users}->getCanonicalUserID($agent); + $session->writeWarning("using $agent") if DEBUG; + } + + #code duplicated from 4.1.2 TWikiUserMapping::groupMembers + my $store = $session->{store}; + if( $store->topicExists( $groupWeb, $groupTopic ) #&& + #$session->security->checkAccessPermission + #( $session, 'CHANGE', $user, undef, undef, $groupWeb, $groupTopic)) { + ){ + my ($meta, $text) = + $store->readTopic($user, $groupWeb, $groupTopic, undef); + my $before = ''; + my $groupSetting = ''; + my $after = ''; + foreach my $line ( split( /\r?\n/, $text ) ) { + if( $line =~ /$TWiki::regex{setRegex}GROUP\s*=\s*(.+)$/ ) { + next unless( $1 eq 'Set' ); + # Note: if there are multiple GROUP assignments in the + # topic, only the last will be taken. + if ($groupSetting eq '') { + $groupSetting = $line; + } else { + $before .= $line."\n".$after; + $after = ''; + $groupSetting = $line; + } + } else { + if ($groupSetting eq '') { + $before .= $line."\n"; + } else { + $after .= $line."\n"; + } + } + } + if ($groupSetting ne '') { + #TODO: should probably normalise for saftey. + $groupSetting .= ', '.$userName."\n"; + $text = $before.$groupSetting.$after; + # Error catching appears broken so I have commented it out, and added createGroupforWeb option + # If there is an access control violation this will throw. + try { + $store->saveTopic( $user, $groupWeb, $groupTopic, + $text, $meta, { minor => 1, comment => "adding $userName to $groupName" } ); + $session->writeWarning("added $userName to $groupName"); + } catch TWiki::AccessControlException with { + my $e = shift; + $session->writeWarning("ERROR1: cannot Save $groupName topic (".$e->stringify( $session ).")"); + } catch TWiki::OopsException with { + my $e = shift; + $session->writeWarning("ERROR2: cannot Save $groupName topic (".$e->stringify( $session ).")"); + } catch Error::Simple with { + my $e = shift; + $session->writeWarning("ERROR3: fallback cannot Save $groupName topic (".$e.")"); + } + } else { + #error, not an ok GROUP topic + $session->writeWarning("ERROR4: cannot add $userName to $groupName - GROUP setting not valid"); + } + } else { + #error, Group topic does not exist + $session->writeWarning("ERROR5: cannot add $userName to $groupName - Group topic does not exist / not changeable"); + } +} + + + + + + #Given a template and a hash, creates a new topic for a user # 1 reads the template topic # 2 calls RegistrationHandler::register with the row details, so that a plugin can augment/delete/change the entries