Skip to content

Commit

Permalink
Item9790: Several bulk registration changes
Browse files Browse the repository at this point in the history
 - Fix error in Register.pm, caught in ManageDotPMTests
 - Add "templatetopic" to Register script, and change it to support a
   fully specified web.topic name.  (The default search of
   Main.templatetopic and System.templatetopic is still active)
  • Loading branch information
gac410 committed Mar 2, 2016
1 parent 3381126 commit de1b48a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 49 deletions.
@@ -1,13 +1,10 @@
%META:TOPICINFO{author="ProjectContributor" date="1456871285" format="1.1" version="1"}%
%META:TOPICPARENT{name="WikiUsers"}%
---+ <nop>%TOPIC%

<noautolink>
%EDITTABLE{ headerrows="1" }%%STARTSECTION{type="expandvariables"}%
| *WikiName* |%IF{"{Register}{AllowLoginName}" then=" *LoginName* |"}% *Email* | *Password* | *AddToGroups* |\
*%CALCULATE{"$LISTJOIN(* | *, $LISTIF($NOT($EXACT($item,Email)),%QUERY{"'%URLPARAM{"usertemplate" default="%SYSTEMWEB%.NewUserTemplate"}%'/fields.name"}%))"}%* |
%ENDSECTION%
</noautolink>

%META:PREFERENCE{name="ALLOWTOPICVIEW" title="ALLOWTOPICVIEW" type="Set" value="AdminGroup"}%
%META:PREFERENCE{name="VIEW_TEMPLATE" title="VIEW_TEMPLATE" type="Set" value="System.BulkRegistrationInputView"}%
%META:PREFERENCE{name="NOAUTOLINK" title="NOAUTOLINK" type="Set" value="1"}%
Expand Up @@ -16,19 +16,19 @@
---------------------------------------------------
}%%TMPL:DEF{"content"}%
---+ %SPACEOUT{"%TOPIC%"}%

%TEXT%
---
_Be sure to remember to save any table changes!_
%IF{"context isadmin" then="
<div class='foswikiFormSteps'>
<form name='bulkRegister' action='%SCRIPTURLPATH{"manage"}%/%TOPIC%' method='post'>
<input type='hidden' name='action' value='bulkRegister' /><sticky>&nbsp;</sticky>
<input type='submit' class='foswikiSubmit' value='Bulk Register these people' />
<input type='hidden' name='redirectto' value='%TOPIC%' />
<input type='hidden' name='LogTopic' value='%TOPIC%Log' /><br/>
</form>
</div>"
<input type='hidden' name='LogTopic' value='%TOPIC%Log' />
<input type='hidden' name='templatetopic' value='%USER_TEMPLATE{default=""}%' />
</form> "
else="<div class='foswikiAlert'> *Bulk registration is restricted to Administrators.* </div>"}%

%TEXT%
%INCLUDE{"%TOPIC%Log" warn="off"}%

*Back to:* [[%SYSTEMWEB%.BulkRegistration][Bulk Registration]]
Expand Down
2 changes: 1 addition & 1 deletion core/data/System/CommandAndCGIScripts.txt
Expand Up @@ -220,7 +220,7 @@ If neither =action_save= or =action_cancel= are provided, an oops error is issue
See BulkRegistration.
| *Parameter* | *Description* | *Default* |
| =logtopic= | Topic to save the log in | _same as topic name, with 'Result' appended_ |
| =overwritehometopics= | Whether to overwrite existing home topics or not | _do not overwrite_ |
| =templatetopic= | Template used to create the new user topics. This can be fully specified as Webname.Topicname. Simple topic name is retrieved from %USERSWEB% and if not found, %SYSTEMWEB%. | !NewUserTemplate |
| =redirectto= | Redirect to this topic or URL after successfully running the bulkRegister function. The parameter value can be a =TopicName=, a =Web.TopicName=, or a URL.%BR% \
*Note:* Redirect to a URL only works if it is enabled in =configure= (Expert setting: Security and Authentication > Environment > ={AllowRedirectUrl}=). | =logtopic= |

Expand Down
121 changes: 83 additions & 38 deletions core/lib/Foswiki/UI/Register.pm
Expand Up @@ -345,6 +345,13 @@ topic = the page with the entries on it.
NB. bulkRegister is invoked from ManageCgiScript. Why? Who knows.
Query parameters applying to all registrations:
* =LogTopic=: Name of the topic that is created / replaced with the results.
* =redirectto=: Topic displayed after registration is complete. (Defaults to !LogTopic).
* =templatetopic=: Default New user topic template name, defaults to NewUserTemplate. This topic name must exist either in %USERSWEB% or %SYSTEMWEB%
Note that the templatetopic can be set per user, but because the template topic is likely to have different fields
in it's respective form definition, and would require different table columns.
=cut

sub bulkRegister {
Expand Down Expand Up @@ -375,6 +382,11 @@ sub bulkRegister {
# Validate
Foswiki::UI::checkValidationKey($session);

my $templateTopic = $query->param('templatetopic');
if ($templateTopic) {
$templateTopic = _validateTemplateTopic( $session, $templateTopic );
}

#-- Read the topic containing the table of people to be registered
my $meta = Foswiki::Meta->load( $session, $web, $topic );

Expand All @@ -388,6 +400,8 @@ sub bulkRegister {
if ($gotHdr) {
my $i = 0;
my %row = map { $fields[ $i++ ] => $_ } split( /\s*\|\s*/, $1 );
$row{templatetopic} = $templateTopic
unless ( $row{templatetopic} );
push( @data, \%row );
}
else {
Expand Down Expand Up @@ -421,7 +435,7 @@ sub bulkRegister {

# If password column is empty, just delete them to avoid errors in validation.
# Passwords are generally sent as a bulk reset after registration.
unless ( length( $row->{Password} ) > 0 ) {
unless ( defined $row->{Password} && length( $row->{Password} ) > 0 ) {
delete $row->{Password};
delete $row->{Confirm};
}
Expand Down Expand Up @@ -1368,6 +1382,9 @@ sub _createUserTopic {
# This is safe because the $template name is fully validated in _validateRegistration()
($template) = $template =~ m/^(.*)$/;

( $fromWeb, $template ) =
Foswiki::Func::normalizeWebTopicName( $fromWeb, $template );

if ( !$session->topicExists( $fromWeb, $template ) ) {

# Use the default version
Expand Down Expand Up @@ -1847,43 +1864,10 @@ sub _validateRegistration {
}

if ( $data->{templatetopic} ) {
$data->{templatetopic} = Foswiki::Sandbox::untaint(
$data->{templatetopic},
sub {
my $template = shift;
return $template if Foswiki::isValidTopicName($template);
$session->logger->log( 'warning',
'Registration rejected: invalid templatetopic requested: '
. $data->{templatetopic} );
throw Foswiki::OopsException(
'register',
web => $data->{webName},
topic => $session->{topicName},
def => 'bad_templatetopic',
);
}
);
if (
!$session->topicExists( $Foswiki::cfg{UsersWebName},
$data->{templatetopic} )
&& !$session->topicExists(
$Foswiki::cfg{SystemWebName},
$data->{templatetopic}
)
)
{
$session->logger->log( 'warning',
'Registration rejected: requested templatetopic does not exist: '
. $data->{templatetopic} );
throw Foswiki::OopsException(
'register',
uweb => $Foswiki::cfg{UsersWebName},
tmpl => $data->{templatetopic},
web => $data->{webName},
topic => $session->{topicName},
def => 'bad_templatetopic',
);
}

# Validate and untaint
$data->{templatetopic} =
_validateTemplateTopic( $session, $data->{templatetopic} );
}

if ($requireForm) {
Expand Down Expand Up @@ -1947,6 +1931,67 @@ sub _validateRegistration {
};
}

sub _validateTemplateTopic {
my $session = shift;

my ( $templateWeb, $templateTopic ) =
Foswiki::Func::normalizeWebTopicName( $Foswiki::cfg{UserWebName},
$_[0] || 'NewUserTemplate' );

$templateTopic = Foswiki::Sandbox::untaint(
$templateTopic,
sub {
my $template = shift;
return $template if Foswiki::isValidTopicName($template);
$session->logger->log( 'warning',
'Registration rejected: invalid templatetopic requested: '
. $template );
throw Foswiki::OopsException(
'register',
web => $session->{webName},
topic => $session->{topicName},
def => 'bad_templatetopic',
);
}
);

$templateWeb = Foswiki::Sandbox::untaint(
$templateWeb,
sub {
my $web = shift;
return $web if Foswiki::isValidWebName($web);
$session->logger->log( 'warning',
'Registration rejected: invalid templatetopic webname requested: '
. $web );
throw Foswiki::OopsException(
'register',
web => $session->{webName},
topic => $session->{topicName},
def => 'bad_templatetopic',
);
}
);

if ( !$session->topicExists( $templateWeb, $templateTopic )
&& !$session->topicExists( $Foswiki::cfg{SystemWebName},
$templateTopic ) )
{
$session->logger->log( 'warning',
'Registration rejected: requested templatetopic does not exist: '
. $templateWeb . "."
. $templateTopic );
throw Foswiki::OopsException(
'register',
uweb => $Foswiki::cfg{UsersWebName},
tmpl => $templateTopic,
web => $session->{webName},
topic => $session->{topicName},
def => 'bad_templatetopic',
);
}
return "$templateWeb.$templateTopic";
}

# sends $p->{template} to $p->{Email} with substitutions from $data
sub _sendEmail {
my ( $session, $template, $data ) = @_;
Expand Down

0 comments on commit de1b48a

Please sign in to comment.