[Rt-commit] rt branch, 4.6/disable-db-config-option, created. rt-4.4.4-879-g0b7e1f31b

Aaron Trevena ast at bestpractical.com
Mon Feb 17 08:28:48 EST 2020


The branch, 4.6/disable-db-config-option has been created
        at  0b7e1f31be8c589aee5f4e816601e8a3612ad88f (commit)

- Log -----------------------------------------------------------------
commit 0890e17196b75a5d2cd06f6251ccd2eb138e44d4
Author: Aaron Trevena <ast at bestpractical.com>
Date:   Fri Feb 14 13:51:57 2020 +0000

    New option to disable configuration in db
    
    New configuration option 'DisableConfigInDatabase' to Disable Config In Database

diff --git a/lib/RT.pm b/lib/RT.pm
index 6116333a4..a3e8bdd8d 100644
--- a/lib/RT.pm
+++ b/lib/RT.pm
@@ -199,6 +199,7 @@ sub Init {
     ConnectToDatabase();
     InitSystemObjects();
     InitClasses(%args);
+    # will use only files if db config disabled
     RT->Config->LoadConfigFromDatabase();
     InitLogging();
     ProcessPreInitMessages();
@@ -765,8 +766,8 @@ our %CORED_PLUGINS = (
     'RT::Extension::ParentTimeWorked' => '4.4',
     'RT::Extension::FutureMailgate' => '4.4',
     'RT::Extension::AdminConditionsAndActions' => '4.4.2',
-    'RT::Extension::RightsInspector' => '5.0',
-    'RT::Extension::ConfigInDatabase' => '5.0',
+    'RT::Extension::RightsInspector' => '4.6',
+    'RT::Extension::ConfigInDatabase' => '4.6',
 );
 
 sub InitPlugins {
diff --git a/lib/RT/Config.pm b/lib/RT/Config.pm
index 9362e2a66..23d9759fa 100644
--- a/lib/RT/Config.pm
+++ b/lib/RT/Config.pm
@@ -1435,9 +1435,6 @@ our %META;
     DateDayBeforeMonth => {
         Widget => '/Widgets/Form/Boolean',
     },
-    DisplayTotalTimeWorked => {
-        Widget => '/Widgets/Form/Boolean',
-    },
     DontSearchFileAttachments => {
         Widget => '/Widgets/Form/Boolean',
     },
@@ -1709,9 +1706,6 @@ our %META;
     OwnerEmail => {
         Widget => '/Widgets/Form/String',
     },
-    QuoteWrapWidth => {
-        Widget => '/Widgets/Form/Integer',
-    },
     RedistributeAutoGeneratedMessages => {
         Widget => '/Widgets/Form/String',
     },
@@ -1796,6 +1790,11 @@ our %META;
     LogToSyslogConf => {
         Immutable     => 1,
     },
+    DisableConfigInDatabase => {
+        Immutable => 1,
+        Widget    => '/Widgets/Form/Boolean',
+    },
+
 );
 my %OPTIONS = ();
 my @LOADED_CONFIGS = ();
@@ -2540,61 +2539,62 @@ sub LoadConfigFromDatabase {
     my $self = shift;
 
     $database_config_cache_time = time;
-
     my $settings = RT::Configurations->new(RT->SystemUser);
     $settings->UnLimit;
 
+    # skip pulling config from db if db config disabled
     my %seen;
+    unless ($original_setting_from_files{DisableConfigInDatabase}) {
+        while (my $setting = $settings->Next) {
+            my $name = $setting->Name;
+            my ($value, $error) = $setting->DecodedContent;
+            next if $error;
+
+            if (!exists $original_setting_from_files{$name}) {
+                $original_setting_from_files{$name} = [
+                    scalar($self->Get($name)),
+                    Clone::clone(scalar($self->Meta($name))),
+                ];
+            }
 
-    while (my $setting = $settings->Next) {
-        my $name = $setting->Name;
-        my ($value, $error) = $setting->DecodedContent;
-        next if $error;
-
-        if (!exists $original_setting_from_files{$name}) {
-            $original_setting_from_files{$name} = [
-                scalar($self->Get($name)),
-                Clone::clone(scalar($self->Meta($name))),
-            ];
-        }
-
-        $seen{$name}++;
+            $seen{$name}++;
 
-        # are we inadvertantly overriding RT_SiteConfig.pm?
-        my $meta = $META{$name};
-        if ($meta->{'Source'}) {
-            my %source = %{ $meta->{'Source'} };
-            if ($source{'SiteConfig'} && $source{'File'} ne 'database') {
-                warn("Change of config option '$name' at $source{File} line $source{Line} has been overridden by the config setting from the database. Please remove it from $source{File} or from the database to avoid confusion.");
+            # are we inadvertantly overriding RT_SiteConfig.pm?
+            my $meta = $META{$name};
+            if ($meta->{'Source'}) {
+                my %source = %{ $meta->{'Source'} };
+                if ($source{'SiteConfig'} && $source{'File'} ne 'database') {
+                    warn("Change of config option '$name' at $source{File} line $source{Line} has been overridden by the config setting from the database. Please remove it from $source{File} or from the database to avoid confusion.");
+                }
             }
-        }
 
-        my $type = $meta->{Type} || 'SCALAR';
+            my $type = $meta->{Type} || 'SCALAR';
 
-        # hashes combine, but we don't want that behavior because the previous
-        # config settings will shadow any change that the database config makes
-        if ($type eq 'HASH') {
-            $self->Set($name, ());
-        }
+            # hashes combine, but we don't want that behavior because the previous
+            # config settings will shadow any change that the database config makes
+            if ($type eq 'HASH') {
+                $self->Set($name, ());
+            }
 
-        my $val = $type eq 'ARRAY' ? $value
+            my $val = $type eq 'ARRAY' ? $value
                 : $type eq 'HASH'  ? [ %$value ]
-                                   : [ $value ];
-
-        $self->SetFromConfig(
-            Option     => \$name,
-            Value      => $val,
-            Package    => 'N/A',
-            File       => 'database',
-            Line       => 'N/A',
-            Database   => 1,
-            SiteConfig => 1,
-        );
+                : [ $value ];
+
+            $self->SetFromConfig(
+                Option     => \$name,
+                Value      => $val,
+                Package    => 'N/A',
+                File       => 'database',
+                Line       => 'N/A',
+                Database   => 1,
+                SiteConfig => 1,
+            );
+        }
     }
 
-    # anything that wasn't loaded from the database but has been set in
-    # %original_setting_from_files must have been disabled from the database,
-    # so we want to restore the original setting
+    # if not using database or for anything that wasn't loaded from the database
+    # but has been set in %original_setting_from_files so must have been disabled
+    # from the database, we want to restore/fetch the original setting
     for my $name (keys %original_setting_from_files) {
         next if $seen{$name};
 
diff --git a/lib/RT/Interface/Web/MenuBuilder.pm b/lib/RT/Interface/Web/MenuBuilder.pm
index 5ce829c46..afc079a99 100644
--- a/lib/RT/Interface/Web/MenuBuilder.pm
+++ b/lib/RT/Interface/Web/MenuBuilder.pm
@@ -703,7 +703,9 @@ sub BuildMainNav {
 
     if ( $request_path =~ m{^/Admin/Tools/(Configuration|EditConfig)} ) {
         $page->child( display => title => loc('View'), path => "/Admin/Tools/Configuration.html" );
-        $page->child( history => title => loc('Edit'), path => "/Admin/Tools/EditConfig.html" );
+        unless (RT->Config->Get('DisableConfigInDatabase')) {
+            $page->child( history => title => loc('Edit'), path => "/Admin/Tools/EditConfig.html" );
+        }
     }
 
     # due to historical reasons of always having been in /Elements/Tabs
diff --git a/share/html/Admin/Tools/EditConfig.html b/share/html/Admin/Tools/EditConfig.html
index 603c46ce8..9fb344041 100644
--- a/share/html/Admin/Tools/EditConfig.html
+++ b/share/html/Admin/Tools/EditConfig.html
@@ -51,6 +51,10 @@ unless ($session{'CurrentUser'}->HasRight( Object=> $RT::System, Right => 'Super
  Abort(loc('This feature is only available to system administrators'));
 }
 
+if (RT->Config->Get('DisableConfigInDatabase')) {
+  Abort(loc('[_1] disabled', 'Configuration in database'));
+}
+
 my $has_execute_code = $session{CurrentUser}->HasRight(Right => 'ExecuteCode', Object => RT->System);
 
 my $options = RT->Config->LoadSectionMap();
@@ -216,4 +220,4 @@ my $nav_type='tab'; # 'tab' or 'pill'
 % }
   </div><!-- content-all -->
 </div><!-- titlebox-content -->
-</div><!-- configuration -->
\ No newline at end of file
+</div><!-- configuration -->

commit 0b7e1f31be8c589aee5f4e816601e8a3612ad88f
Author: Aaron Trevena <ast at bestpractical.com>
Date:   Mon Feb 17 13:26:08 2020 +0000

    Added tests for new option disabling config in database

diff --git a/t/web/admin_tools_nodbconfig.t b/t/web/admin_tools_nodbconfig.t
new file mode 100644
index 000000000..160ebd1bd
--- /dev/null
+++ b/t/web/admin_tools_nodbconfig.t
@@ -0,0 +1,96 @@
+use strict;
+use warnings;
+
+use Test::Deep;
+use Data::Dumper ();
+
+use RT::Test tests => undef;
+RT->Config->Set('DisableConfigInDatabase', 1);
+
+my ( $url, $m ) = RT::Test->started_ok;
+ok( $m->login(), 'logged in' );
+
+$m->follow_link_ok( { text => 'System Configuration' }, 'followed link to "System Configuration"' );
+
+ok(! (grep { $_->url =~ /EditConfig\.html/ } $m->links()), 'no link to edit page');
+
+$m->get_ok($url.'/Admin/Tools/EditConfig.html');
+$m->content_contains('Configuration in database disabled');
+$m->warning_like( qr/Configuration in database disabled/, 'logged warning about db config being disabled');
+
+my $tests = [
+    {
+        name      => 'change a boolean value',
+        form_id   => 'form-System-Outgoing_mail',
+        setting   => 'NotifyActor',
+        new_value => 1,
+    },
+    {
+        name      => 'change an arrayref value',
+        form_id   => 'form-System-Extra_security',
+        setting   => 'ReferrerWhitelist',
+        new_value => ['www.example.com:443', 'www3.example.com:80'],
+    },
+    {
+        name      => 'change a hashref value',
+        form_id   => 'form-System-Outgoing_mail',
+        setting   => 'OverrideOutgoingMailFrom',
+        new_value => { 1 => 'new-outgoing-from at example.com' },
+    },
+];
+
+run_test( %{$_} ) for @{$tests};
+
+is(RT->Config->Get('NotifyActor'), 0, 'db config not used for NotifyActor');
+
+sub run_test {
+    my %args = @_;
+
+    diag $args{name} if $ENV{TEST_VERBOSE};
+
+    $m->post_ok(
+        $url.'/Admin/Tools/EditConfig.html',
+        {
+            form_id => $args{form_id},
+            fields  => {
+                $args{setting} => stringify( $args{new_value} ),
+            },
+        },
+        'form was submitted successfully'
+    );
+
+    # check user and logs notified that config in db is disabled
+    $m->content_contains('Configuration in database disabled');
+    $m->warning_like( qr/Configuration in database disabled/, 'logged warning about db config being disabled');
+
+    # RT::Config in the test is not running in the same process as the one in the test server.
+    # ensure the config object in the test is up to date and has no changes.
+    RT->Config->LoadConfigFromDatabase();
+
+    $m->content_unlike( qr/$args{setting} changed from/, 'UI indicated the value was changed' );
+
+    # check that configuration has not been updated
+    my $rt_configuration = RT::Configuration->new( RT->SystemUser );
+    $rt_configuration->Load( $args{setting} );
+    my $rt_configuration_value = $rt_configuration->Content;
+    my $rt_config_value = RT->Config->Get( $args{setting} );
+
+    isnt( $rt_configuration_value, stringify($args{new_value}), 'value from RT::Configuration->Load matches new value' );
+    isnt( stringify($rt_config_value), stringify($args{new_value}), 'value from RT->Config->Get matches new value' );
+}
+
+sub stringify {
+    my $value = shift;
+
+    return $value unless ref $value;
+
+    local $Data::Dumper::Terse = 1;
+    local $Data::Dumper::Indent = 2;
+    local $Data::Dumper::Sortkeys = 1;
+
+    my $output = Data::Dumper::Dumper $value;
+    chomp $output;
+    return $output;
+}
+
+done_testing;

-----------------------------------------------------------------------


More information about the rt-commit mailing list