Author: kake Date: 2012-05-02 19:24:58 +0100 (Wed, 02 May 2012) New Revision: 1377
Modified: trunk/Changes trunk/lib/OpenGuides.pm trunk/preferences.cgi trunk/t/59_preferences.t trunk/templates/preferences.tt Log: Made the prefs page return the user to the place they came from (fixes #94). Still need to move this from preferences.cgi into OpenGuides.pm and write some tests for it.
Modified: trunk/Changes =================================================================== --- trunk/Changes 2012-05-02 17:10:39 UTC (rev 1376) +++ trunk/Changes 2012-05-02 18:24:58 UTC (rev 1377) @@ -30,6 +30,8 @@ Website truncation on node display is now a little more intelligent (you can still tweak this by setting website_link_max_chars in your wiki.conf). + The preferences page now remembers what page the user got to it from, + and sends them back there.
0.66 12 April 2012 All templates now have access to the "username" TT variable.
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2012-05-02 17:10:39 UTC (rev 1376) +++ trunk/lib/OpenGuides.pm 2012-05-02 18:24:58 UTC (rev 1377) @@ -652,15 +652,25 @@ my $config = $self->config; my $wiki = $self->wiki;
+ my $from = $ENV{HTTP_REFERER} || ""; + my $url_base = $config->script_url . $config->script_name; + if ( $from !~ /^$url_base/ ) { + $from = ""; + } + + my %tt_vars = ( + not_editable => 1, + show_form => 1, + not_deletable => 1, + return_to_url => $from, + ); + return %tt_vars if $args{return_tt_vars}; + my $output = OpenGuides::Template->output( wiki => $wiki, config => $config, template => "preferences.tt", - vars => { - not_editable => 1, - show_form => 1, - not_deletable => 1, - }, + vars => %tt_vars, noheaders => $args{noheaders}, ); return $output if $args{return_output};
Modified: trunk/preferences.cgi =================================================================== --- trunk/preferences.cgi 2012-05-02 17:10:39 UTC (rev 1376) +++ trunk/preferences.cgi 2012-05-02 18:24:58 UTC (rev 1377) @@ -57,6 +57,7 @@ not_editable => 1, not_deletable => 1, username => $prefs{username}, + return_to_url => $cgi->param( "return_to_url" ) || "", } ); }
Modified: trunk/t/59_preferences.t =================================================================== --- trunk/t/59_preferences.t 2012-05-02 17:10:39 UTC (rev 1376) +++ trunk/t/59_preferences.t 2012-05-02 18:24:58 UTC (rev 1377) @@ -17,7 +17,7 @@ exit 0; }
-plan tests => 12; +plan tests => 17;
OpenGuides::Test::refresh_db();
@@ -25,6 +25,34 @@ my $guide = OpenGuides->new( config => $config ); my $wiki = $guide->wiki;
+# Make sure "return_to" TT var is set iff referrer domain is correct. +$config->script_url( "http://example.com/" ); +$config->script_name( "wiki.cgi" ); +my $good_return_to = "http://example.com/wiki.cgi?Test_Page"; +my $evil_return_to = "http://example.org/naughty-script"; + +$ENV{HTTP_REFERER} = $good_return_to; +my %tt_vars = $guide->display_prefs_form( return_tt_vars => 1 ); +is( $tt_vars{return_to_url}, $good_return_to, + "Return URL set when referrer matches script URL/name" ); +my $output = $guide->display_prefs_form( return_output => 1, noheaders => 1 ); +Test::HTML::Content::tag_ok( $output, + "input", { type => "hidden", name => "return_to_url" }, + "...corresponding hidden input is there in the form" ); +Test::HTML::Content::tag_ok( $output, + "input", { type => "hidden", name => "return_to_url", + value => $good_return_to }, + "...with correct value" ); + +$ENV{HTTP_REFERER} = $evil_return_to; +%tt_vars = $guide->display_prefs_form( return_tt_vars => 1 ); +ok( !$tt_vars{return_to_url}, + "Return URL not set when referrer doesn't match script URL/name" ); +$output = $guide->display_prefs_form( return_output => 1, noheaders => 1 ); +Test::HTML::Content::no_tag( $output, + "input", { type => "hidden", name => "return_to_url" }, + "...and no corresponding hidden input in form" ); + # If we have a google API key and node maps are enabled, we should see the # checkbox for this pref. $config->gmaps_api_key( "This is not a real API key." ); @@ -35,7 +63,7 @@ display_google_maps => 1, ); $ENV{HTTP_COOKIE} = $cookie; -my $output = $guide->display_prefs_form( return_output => 1, noheaders => 1 ); +$output = $guide->display_prefs_form( return_output => 1, noheaders => 1 ); Test::HTML::Content::tag_ok( $output, "input", { type => "checkbox", name => "display_google_maps" }, "Node map preference checkbox shown when we have a GMaps API key." );
Modified: trunk/templates/preferences.tt =================================================================== --- trunk/templates/preferences.tt 2012-05-02 17:10:39 UTC (rev 1376) +++ trunk/templates/preferences.tt 2012-05-02 18:24:58 UTC (rev 1377) @@ -121,6 +121,10 @@
<input type="submit" value="Set it" class="form_button" /> <input type="hidden" name="action" value="set_preferences"> + [% IF return_to_url %] + <input type="hidden" name="return_to_url" + value="[% return_to_url | html %]" /> + [% END %] </form>
[% ELSE %] @@ -192,7 +196,7 @@ </p>
<ul> - <li><a href="[% full_cgi_url %]">Return to [% site_name %]</a></li> + <li><a href="[% IF return_to_url %][% return_to_url | html %][% ELSE %][% full_cgi_url %][% END %]">Return to [% site_name %]</a></li> <li><a href="preferences.cgi">Change preferences</a></li> </ul>
openguides-commits@lists.openguides.org