Ticket #203: feature-203-ip-address-mod-whitelist-02.diff

File feature-203-ip-address-mod-whitelist-02.diff, 9.7 KB (added by oliver, 13 years ago)

Patch for OpenGuides which adds the moderate_whitelist option

  • lib/OpenGuides/Config.pm

    diff -urN openguides.orig/lib/OpenGuides/Config.pm openguides/lib/OpenGuides/Config.pm
    old new  
    2222   custom_template_path geo_handler ellipsoid gmaps_api_key centre_long
    2323   show_gmap_in_node_display google_analytics_key
    2424   centre_lat default_gmaps_zoom default_gmaps_search_zoom force_wgs84
    25    licence_name licence_url licence_info_url moderation_requires_password
     25   licence_name licence_url licence_info_url
     26   moderation_requires_password moderate_whitelist
    2627   enable_node_image enable_common_categories enable_common_locales
    2728   spam_detector_module host_checker_module static_path static_url
    2829   send_moderation_notifications website_link_max_chars
     
    8081                     indexing_directory => "/usr/lib/cgi-bin/openguides/indexes/",
    8182                     enable_page_deletion => 0,
    8283                     moderation_requires_password => 1,
     84                     moderate_whitelist => "",
    8385                     admin_pass => "Change This!",
    8486                     enable_node_image => 1,
    8587                     enable_common_categories => 0,
     
    336338
    337339=item * send_moderation_notifications
    338340
     341=item * moderate_whitelist
     342
    339343=item * website_link_max_chars (default: C<20>)
    340344
    341345=back
  • lib/OpenGuides/Test.pm

    diff -urN openguides.orig/lib/OpenGuides/Test.pm openguides/lib/OpenGuides/Test.pm
    old new  
    6666                     geo_handler          => 1,
    6767                     force_wgs84          => 1,
    6868                     contact_email        => 'admins@example.org',
     69                     moderate_whitelist   => "",
    6970                   }
    7071    );
    7172
  • lib/OpenGuides/Utils.pm

    diff -urN openguides.orig/lib/OpenGuides/Utils.pm openguides/lib/OpenGuides/Utils.pm
    old new  
    1010use Wiki::Toolkit::Plugin::RSS::Reader;
    1111use URI::Escape;
    1212use MIME::Lite;
     13use Net::Netmask;
     14use List::Util qw( first );
    1315use Data::Validate::URI qw( is_web_uri );
    1416
    1517=head1 NAME
     
    497499    }
    498500}
    499501
     502=item B<in_moderate_whitelist>
     503
     504 if (OpenGuides::Utils->in_moderate_whitelist( '127.0.0.1' )) {
     505     # skip moderation and apply new verson to published site
     506 }
     507
     508Admins can supply a comma separated list of IP addresses or CIDR-notation
     509subnets indicating the hosts which can bypass enforced moderation. Any
     510values which cannot be parsed by C<NetAddr::IP> will be ignored.
     511
     512=cut
     513
     514sub in_moderate_whitelist {
     515    my ($self, $config, $ip) = @_;
     516    return undef if not defined $ip;
     517
     518    # create NetAddr::IP object of the test IP
     519    my $addr = Net::Netmask->new2($ip) or return undef;
     520
     521    # load the configured whitelist
     522    my @whitelist
     523        = split ',', $config->moderate_whitelist;
     524
     525    # test each entry in the whitelist
     526    return eval{
     527        first { Net::Netmask->new2($_)->match($addr->base) } @whitelist
     528    };
     529}
     530
    500531=back
    501532
    502533=head1 AUTHOR
  • lib/OpenGuides.pm

    diff -urN openguides.orig/lib/OpenGuides.pm openguides/lib/OpenGuides.pm
    old new  
    15561556    # Skip this for nodes needing moderation - this occurs for them once
    15571557    #  they've been moderated
    15581558    my $needs_moderation = $wiki->node_required_moderation($node);
    1559     unless( $needs_moderation ) {
     1559    my $in_moderate_whitelist
     1560        = OpenGuides::Utils->in_moderate_whitelist($self->config, $new_metadata{host});
     1561
     1562    if ( $in_moderate_whitelist or not $needs_moderation ) {
    15601563        $self->_autoCreateCategoryLocale(
    15611564                                          id       => $node,
    15621565                                          metadata => \%new_metadata
     
    15671570                                     \%new_metadata );
    15681571
    15691572    if ($written) {
    1570         if ( $needs_moderation and $config->send_moderation_notifications ) {
    1571             my $body = "The node '$node' in the OpenGuides installation\n" .
    1572                 "'" . $config->site_name . "' requires moderation. ".
    1573                 "Please visit\n" .
    1574                 $config->script_url . $config->script_name .
    1575                 "?action=show_needing_moderation\nat your convenience.\n";
    1576             eval {
    1577                 OpenGuides::Utils->send_email(
    1578                     config        => $config,
    1579                     subject       => "Node requires moderation",
    1580                     body          => $body,
    1581                     admin         => 1,
    1582                     return_output => $return_output
     1573        if ( $needs_moderation ) {
     1574            if ( $in_moderate_whitelist ) {
     1575                $self->wiki->moderate_node(
     1576                                            name    => $node,
     1577                                            version => $written
    15831578                );
    1584             };
    1585             warn $@ if $@;
     1579            }
     1580            elsif ( $config->send_moderation_notifications ) {
     1581                my $body = "The node '$node' in the OpenGuides installation\n" .
     1582                    "'" . $config->site_name . "' requires moderation. ".
     1583                    "Please visit\n" .
     1584                    $config->script_url . $config->script_name .
     1585                    "?action=show_needing_moderation\nat your convenience.\n";
     1586                eval {
     1587                    OpenGuides::Utils->send_email(
     1588                        config        => $config,
     1589                        subject       => "Node requires moderation",
     1590                        body          => $body,
     1591                        admin         => 1,
     1592                        return_output => $return_output
     1593                    );
     1594                };
     1595                warn $@ if $@;
     1596            }
    15861597        }
    15871598
    15881599        my $output = $self->redirect_to_node($node);
  • t/81_node_moderate_whitelist.t

    diff -urN openguides.orig/t/81_node_moderate_whitelist.t openguides/t/81_node_moderate_whitelist.t
    old new  
     1use strict;
     2use Wiki::Toolkit::Setup::SQLite;
     3use OpenGuides;
     4use OpenGuides::Test;
     5use Test::More;
     6
     7eval { require DBD::SQLite; };
     8
     9if ( $@ ) {
     10    my ($error) = $@ =~ /^(.*?)\n/;
     11    plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)";
     12}
     13
     14plan tests => 12;
     15
     16Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } );
     17my $config = OpenGuides::Test->make_basic_config;
     18$config->script_name( "wiki.cgi" );
     19$config->script_url( "http://example.com/" );
     20$config->moderate_whitelist( "127.0.0.1" );
     21my $guide = OpenGuides->new( config => $config );
     22isa_ok( $guide, "OpenGuides" );
     23my $wiki = $guide->wiki;
     24isa_ok( $wiki, "Wiki::Toolkit" );
     25
     26# Clear out the database from any previous runs.
     27foreach my $del_node ( $wiki->list_all_nodes ) {
     28    print "# Deleting node $del_node\n";
     29    $wiki->delete_node( $del_node ) or die "Can't delete $del_node";
     30}
     31
     32
     33# Add a page
     34my $q = CGI->new;
     35$q->param( -name => "content", -value => "foo" );
     36$q->param( -name => "categories", -value => "Alpha" );
     37$q->param( -name => "locales", -value => "" );
     38$q->param( -name => "phone", -value => "" );
     39$q->param( -name => "fax", -value => "" );
     40$q->param( -name => "website", -value => "" );
     41$q->param( -name => "hours_text", -value => "" );
     42$q->param( -name => "address", -value => "" );
     43$q->param( -name => "postcode", -value => "" );
     44$q->param( -name => "map_link", -value => "" );
     45$q->param( -name => "os_x", -value => "" );
     46$q->param( -name => "os_y", -value => "" );
     47$q->param( -name => "username", -value => "bob" );
     48$q->param( -name => "comment", -value => "foo" );
     49$q->param( -name => "edit_type", -value => "Minor tidying" );
     50$ENV{REMOTE_ADDR} = "127.0.0.1";
     51
     52my $output = $guide->commit_node(
     53                                  return_output => 1,
     54                                  id => "Wombats",
     55                                  cgi_obj => $q,
     56                                );
     57
     58# Check it's moderated
     59my %details = $wiki->retrieve_node("Wombats");
     60is($details{'moderated'}, 1, "Moderated");
     61is($wiki->node_required_moderation("Wombats"), 0, "No moderation");
     62
     63# Turn on moderation
     64$wiki->set_node_moderation(
     65                            name => "Wombats",
     66                            required => 1,
     67);
     68is($wiki->node_required_moderation("Wombats"), 1, "Moderation");
     69
     70
     71# Now add a new one, with new categories and locales
     72$q->param( -name => "categories", -value => "Alpha\r\nBeta" );
     73$q->param( -name => "locales", -value => "Hello" );
     74$q->param( -name => "edit_type", -value => "Normal edit" );
     75$q->param( -name => "checksum", -value => $details{checksum} );
     76$output = $guide->commit_node(
     77                                  return_output => 1,
     78                                  id => "Wombats",
     79                                  cgi_obj => $q,
     80                                );
     81
     82# Check that the current version is now 2
     83%details = $wiki->retrieve_node("Wombats");
     84is($details{'version'}, 2, "Still on v1");
     85is($details{'moderated'}, 1, "v1 Moderated");
     86
     87# Check that version 2 is moderated
     88my %v2 = $wiki->retrieve_node(name=>"Wombats",version=>2);
     89is($v2{'version'}, 2, "Is v2");
     90is($v2{'moderated'}, 1, "Moderated");
     91
     92# Check that the new categories and locales are there
     93is(1, $wiki->node_exists("Category Alpha"), "Right Categories");
     94is(1, $wiki->node_exists("Category Beta"), "Right Categories");
     95is(1, $wiki->node_exists("Locale Hello"), "Right Locales");
     96