Author: kake Date: 2012-04-24 16:16:38 +0100 (Tue, 24 Apr 2012) New Revision: 1364
Added: trunk/t/89_recent_changes_ip_addr.t Modified: trunk/Changes trunk/MANIFEST trunk/lib/OpenGuides/CGI.pm trunk/t/13_cookies.t trunk/templates/recent_changes.tt Log: Don't show IP addresses on recent changes unless the user has the 'admin' preference set. As part of this, fixed a bug with the prefs TT var not getting set on the recent changes view if the user also had recent changes tracking on.
Modified: trunk/Changes =================================================================== --- trunk/Changes 2012-04-20 15:20:42 UTC (rev 1363) +++ trunk/Changes 2012-04-24 15:16:38 UTC (rev 1364) @@ -24,6 +24,8 @@ Added a basic stylesheet, installed into the static_path directory. Fixed the JSON output for action=index (using a new dependency on Template::Plugin::JSON::Escape). + IP addresses no longer show on recent changes for people without the + admin preference set.
0.66 12 April 2012 All templates now have access to the "username" TT variable.
Modified: trunk/MANIFEST =================================================================== --- trunk/MANIFEST 2012-04-20 15:20:42 UTC (rev 1363) +++ trunk/MANIFEST 2012-04-24 15:16:38 UTC (rev 1364) @@ -163,6 +163,7 @@ t/86_recent_changes.t t/87_more_recent_changes.t t/88_recent_changes_overtime.t +t/89_recent_changes_ip_addr.t t/90_css_category_locale_classes.t t/91_username_in_templates.t t/92_node_name_from_cgi_obj.t
Modified: trunk/lib/OpenGuides/CGI.pm =================================================================== --- trunk/lib/OpenGuides/CGI.pm 2012-04-20 15:20:42 UTC (rev 1363) +++ trunk/lib/OpenGuides/CGI.pm 2012-04-24 15:16:38 UTC (rev 1364) @@ -283,8 +283,8 @@ Croaks unless an LOpenGuides::Config object is supplied as C<config>. Returns default values for any parameter not specified in cookie.
-If C<cookies> is provided, this overrides any cookies submitted by the -browser. +If C<cookies> is provided, and includes a preferences cookie, this overrides +any preferences cookie submitted by the browser.
=cut
@@ -301,8 +301,9 @@ } %cookies = map { $_->name => $_ } @{ $cookies }; } - else { - %cookies = CGI::Cookie->fetch; + if ( !$cookies{$cookie_name} ) { + my %stored_cookies = CGI::Cookie->fetch; + $cookies{$cookie_name} = $stored_cookies{$cookie_name}; } my %data; if ( $cookies{$cookie_name} ) {
Modified: trunk/t/13_cookies.t =================================================================== --- trunk/t/13_cookies.t 2012-04-20 15:20:42 UTC (rev 1363) +++ trunk/t/13_cookies.t 2012-04-24 15:16:38 UTC (rev 1364) @@ -3,7 +3,7 @@ use OpenGuides::CGI; use Time::Piece; use Time::Seconds; -use Test::More tests => 29; +use Test::More tests => 30;
eval { OpenGuides::CGI->make_prefs_cookie; }; ok( $@, "->make_prefs_cookie dies if no config object supplied" ); @@ -102,3 +102,15 @@ %prefs = eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); }; is( $@, "", "->get_prefs_from_cookie doesn't die if no cookie set" ); is( keys %prefs, 11, "...and returns ten default values" ); + +# Check that the prefs cookie is still looked for even if we send along a +# non-prefs cookie. +my $rc_cookie = OpenGuides::CGI->make_recent_changes_cookie( + config => $config ); +my $prefs_cookie = OpenGuides::CGI->make_prefs_cookie( + config => $config, is_admin => 1 ); +$ENV{HTTP_COOKIE} = $prefs_cookie; +%prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config, + cookies => [ $rc_cookie ] ); +ok( $prefs{is_admin}, + "->get_prefs_from_cookie still works with ENV if we send RC cookie" );
Added: trunk/t/89_recent_changes_ip_addr.t =================================================================== --- trunk/t/89_recent_changes_ip_addr.t (rev 0) +++ trunk/t/89_recent_changes_ip_addr.t 2012-04-24 15:16:38 UTC (rev 1364) @@ -0,0 +1,118 @@ +use strict; +use Cwd; +use OpenGuides; +use OpenGuides::CGI; +use OpenGuides::Test; +use Test::More; + +eval { require DBD::SQLite; }; +if ( $@ ) { + my ($error) = $@ =~ /^(.*?)\n/; + plan skip_all => + "DBD::SQLite could not be used - no database to test with. ($error)"; +} + +plan tests => 10; + +my $config = OpenGuides::Test->make_basic_config; +my $guide = OpenGuides->new( config => $config ); +my $wiki = $guide->wiki; + +# Clear out the database from any previous runs. +OpenGuides::Test::refresh_db(); + +# First we need to make sure that the preferences are accessible +# from the recent changes view. Can't test this using return_tt_vars +# because the prefs TT var is set in OpenGuides::Template->output(), +# and if return_tt_vars is set then execution never gets that far. +# So write a custom navbar template that just prints the variable we're +# interested in. + +$config->custom_template_path( cwd . "/t/templates/" ); +eval { + unlink cwd . "/t/templates/navbar.tt"; +}; +open( FILE, ">", cwd . "/t/templates/navbar.tt" ) + or die $!; +print FILE <<EOF; +PREFS_IS_ADMIN: [% prefs.is_admin %] +EOF +close FILE or die $!; + +$ENV{HTTP_COOKIE} = make_cookie( is_admin => 1 ); +my $output = $guide->display_recent_changes( return_output => 1 ); +like( $output, qr/PREFS_IS_ADMIN:\s+1/, + "prefs available as TT var on recent changes" ); + +# Make sure this still works when they have a recent changes tracking cookie. +$ENV{HTTP_COOKIE} = make_cookie( is_admin => 1, track_rc => 1 ); +$output = $guide->display_recent_changes( return_output => 1 ); +like( $output, qr/PREFS_IS_ADMIN:\s+1/, + "...even when there's a recent changes tracking cookie set" ); + +# Clean up. +unlink cwd . "/t/templates/navbar.tt"; + +# Write a node from an IPv6 address. We can't use OG::Test->write_data() +# for this, because it calls make_cgi_object(), which overwrites REMOTE_ADDR. +my $q = OpenGuides::Test->make_cgi_object(); +$ENV{REMOTE_ADDR} = "2001:db8:ca94:869f:226:8ff:fef9:453d"; +$guide->commit_node( id => "Red Lion", cgi_obj => $q, return_output => 1 ); + +# View recent changes with admin links switched off. +$ENV{HTTP_COOKIE} = make_cookie(); +$output = $guide->display_recent_changes( return_output => 1 ); +unlike( $output, qr/2001/, + "Recent changes omits IP address when admin links switched off" ); +$ENV{HTTP_COOKIE} = make_cookie( track_rc => 1 ); +$output = $guide->display_recent_changes( return_output => 1 ); +unlike( $output, qr/2001/, + "...also with recent changes tracking on" ); + +# And with them switched on. +$ENV{HTTP_COOKIE} = make_cookie( is_admin => 1 ); +$output = $guide->display_recent_changes( return_output => 1 ); +like( $output, qr/">2001/, + "Recent changes shows IP address when admin links switched on" ); +unlike( $output, qr/">2001:db8:ca94:869f:226:8ff:fef9:453d/, + "...but not the full thing, if it's too long" ); +$ENV{HTTP_COOKIE} = make_cookie( is_admin => 1, track_rc => 1 ); +$output = $guide->display_recent_changes( return_output => 1 ); +like( $output, qr/">2001/, + "IP address also shown when admin links and rc tracking both on" ); +unlike( $output, qr/">2001:db8:ca94:869f:226:8ff:fef9:453d/, + "...and again, full thing not shown if it's too long" ); + +# Now try it from an IPv4 address, which should fit. +$q = OpenGuides::Test->make_cgi_object(); +$ENV{REMOTE_ADDR} = "198.51.100.255"; +$guide->commit_node( id => "Yellow Lion", cgi_obj => $q, return_output => 1 ); +$ENV{HTTP_COOKIE} = make_cookie( is_admin => 1 ); +$output = $guide->display_recent_changes( return_output => 1 ); +like( $output, qr/">198.51.100.255/, "Full IP address shown if short enough" ); +$ENV{HTTP_COOKIE} = make_cookie( is_admin => 1, track_rc => 1 ); +$output = $guide->display_recent_changes( return_output => 1 ); +like( $output, qr/">198.51.100.255/, + "...also if recent changes tracking is on" ); + +sub make_cookie { + my %args = @_; + + my $prefs_cookie = OpenGuides::CGI->make_prefs_cookie( + config => $config, + username => "Kake", + is_admin => $args{is_admin} || 0, + track_recent_changes_views => $args{track_rc} || 0, + ); + + if ( $args{track_rc} ) { + my $rc_cookie = OpenGuides::CGI->make_recent_changes_cookie( + config => $config, + ); + my @prefs_bits = split( qr/\s*;\s*/, $prefs_cookie ); + my @rc_bits = split( qr/\s*;\s*/, $rc_cookie ); + return $prefs_bits[0] . "; " . $rc_bits[0]; + } + + return $prefs_cookie; +}
Modified: trunk/templates/recent_changes.tt =================================================================== --- trunk/templates/recent_changes.tt 2012-04-20 15:20:42 UTC (rev 1363) +++ trunk/templates/recent_changes.tt 2012-04-24 15:16:38 UTC (rev 1364) @@ -40,11 +40,19 @@ <tr> <td></td> <td><span class="recentchanges_user"> + [% host_link = '<a href="' _ cgi_url _ '?host=' _ node.host _ ';action=userstats">' %] + [% IF node.host.length AND node.host.length > 15 %] + [% host_link = host_link _ '<span title="' _ node.host _ '">' _ node.host.substr( 0, 12 ) _ '...</span></a>' %] + [% ELSE %] + [% host_link = host_link _ node.host _ '</a>' %] + [% END %] [% IF (node.username != 'Anonymous') %] <a href="[% cgi_url %]?username=[% node.username %];action=userstats">[% node.username %]</a> - (<a href="[% cgi_url %]?host=[% node.host %];action=userstats">[% node.host %]</a>) - [% ELSE %] - <a href="[% cgi_url %]?host=[% node.host %];action=userstats">[% node.host %]</a> + [% IF is_admin %] + ([% host_link %]) + [% END %] + [% ELSIF is_admin %] + [% host_link %] [% END %] </span> [% IF node.comment %]- [% END %]<span class="recentchanges_comment">[% node.comment %]</span> </td>