diff --git a/actions/searx b/actions/searx index 3398b40d6..31bba4e31 100755 --- a/actions/searx +++ b/actions/searx @@ -28,6 +28,8 @@ import augeas import yaml from plinth import action_utils +from plinth.modules.searx import is_public_access_enabled +from plinth.modules.searx.manifest import PUBLIC_ACCESS_SETTING_FILE from plinth.utils import gunzip SETTINGS_FILE = '/etc/searx/settings.yml' @@ -160,7 +162,8 @@ def subcommand_enable(_): """Enable web configuration and reload.""" action_utils.uwsgi_enable('searx') action_utils.webserver_enable('searx-freedombox') - action_utils.webserver_enable('searx-freedombox-auth') + if not is_public_access_enabled(): + action_utils.webserver_enable('searx-freedombox-auth') def subcommand_disable(_): @@ -172,11 +175,13 @@ def subcommand_disable(_): def subcommand_enable_public_access(_): """Enable public access to the SearX application.""" + open(PUBLIC_ACCESS_SETTING_FILE, 'w').close() action_utils.webserver_disable('searx-freedombox-auth') def subcommand_disable_public_access(_): """Disable public access to the SearX application.""" + os.remove(PUBLIC_ACCESS_SETTING_FILE) action_utils.webserver_enable('searx-freedombox-auth') diff --git a/functional_tests/features/searx.feature b/functional_tests/features/searx.feature index 266607720..69d67d3cc 100644 --- a/functional_tests/features/searx.feature +++ b/functional_tests/features/searx.feature @@ -53,3 +53,12 @@ Scenario: Disable public access Then searx app should not be visible on the front page And the searx site should not be available +Scenario: Preserve public access setting + Given the searx application is enabled + And I enable public access in searx + When I disable the searx application + And I enable the searx application + And I log out + Then searx app should be visible on the front page + And the searx site should be available + diff --git a/functional_tests/step_definitions/application.py b/functional_tests/step_definitions/application.py index cb0cd4fab..ae1fd23ad 100644 --- a/functional_tests/step_definitions/application.py +++ b/functional_tests/step_definitions/application.py @@ -443,6 +443,6 @@ def app_visible_on_front_page(browser, app_name): @then( parsers.parse('{app_name:w} app should not be visible on the front page')) -def app_visible_on_front_page(browser, app_name): +def app_not_visible_on_front_page(browser, app_name): shortcuts = application.find_on_front_page(browser, app_name) assert len(shortcuts) == 0 diff --git a/functional_tests/step_definitions/interface.py b/functional_tests/step_definitions/interface.py index c9bb7be4c..62a289212 100644 --- a/functional_tests/step_definitions/interface.py +++ b/functional_tests/step_definitions/interface.py @@ -29,6 +29,7 @@ def logged_in_user(browser): config['DEFAULT']['password']) +@when("I log out") @given("I'm a logged out user") def logged_out_user(browser): browser.visit(default_url + '/plinth/accounts/logout/') diff --git a/plinth/modules/searx/__init__.py b/plinth/modules/searx/__init__.py index 3d5200cc1..95d8704e7 100644 --- a/plinth/modules/searx/__init__.py +++ b/plinth/modules/searx/__init__.py @@ -18,6 +18,8 @@ FreedomBox app to configure Searx. """ +import os + from django.utils.translation import ugettext_lazy as _ from plinth import action_utils, actions @@ -26,7 +28,7 @@ from plinth import frontpage, menu from plinth import service as service_module from plinth.modules.users import register_group -from .manifest import backup, clients +from .manifest import PUBLIC_ACCESS_SETTING_FILE, backup, clients clients = clients @@ -126,7 +128,7 @@ def get_safe_search_setting(): def is_public_access_enabled(): """Check whether public access is enabled for Searx.""" - return not action_utils.webserver_is_enabled('searx-freedombox-auth') + return os.path.exists(PUBLIC_ACCESS_SETTING_FILE) def is_enabled(): @@ -156,3 +158,15 @@ def diagnose(): check_certificate=False)) return results + + +def enable_public_access(): + """Allow Searx app to be accessed by anyone with access.""" + actions.superuser_run('searx', ['enable-public-access']) + add_shortcut() + + +def disable_public_access(): + """Allow Searx app to be accessed by logged-in users only.""" + actions.superuser_run('searx', ['disable-public-access']) + add_shortcut() diff --git a/plinth/modules/searx/forms.py b/plinth/modules/searx/forms.py index 22203cd4f..68c66177c 100644 --- a/plinth/modules/searx/forms.py +++ b/plinth/modules/searx/forms.py @@ -30,7 +30,8 @@ class SearxForm(ServiceForm): label=_('Safe Search'), help_text=_( 'Select the default family filter to apply to your search results.' ), choices=((0, _('None')), (1, _('Moderate')), (2, _('Strict')))) + public_access = forms.BooleanField( - label=_('Public Access'), help_text=_( + label=_('Allow Public Access'), help_text=_( 'Allow this application to be used by anyone who can reach it.'), required=False) diff --git a/plinth/modules/searx/manifest.py b/plinth/modules/searx/manifest.py index aa0e76f3a..1fb96f62e 100644 --- a/plinth/modules/searx/manifest.py +++ b/plinth/modules/searx/manifest.py @@ -28,4 +28,6 @@ clients = validate([{ }] }]) -backup = validate_backup({}) +PUBLIC_ACCESS_SETTING_FILE = '/etc/searx/allow_public_access' + +backup = validate_backup({'config': {'files': [PUBLIC_ACCESS_SETTING_FILE]}}) diff --git a/plinth/modules/searx/views.py b/plinth/modules/searx/views.py index 1c9e3421a..aacf1f9a0 100644 --- a/plinth/modules/searx/views.py +++ b/plinth/modules/searx/views.py @@ -23,8 +23,9 @@ from django.utils.translation import ugettext as _ from plinth import actions, views from plinth.errors import ActionError -from plinth.modules.searx import (add_shortcut, clients, description, - get_safe_search_setting, +from plinth.modules.searx import (clients, description, disable_public_access, + enable_public_access, + get_safe_search_setting, is_enabled, is_public_access_enabled, manual_page) from .forms import SearxForm @@ -44,7 +45,7 @@ class SearxServiceView(views.ServiceView): """Return the status of the service to fill in the form.""" initial = super().get_initial() initial['safe_search'] = get_safe_search_setting() - initial['public_access'] = is_public_access_enabled() + initial['public_access'] = is_public_access_enabled() and is_enabled() return initial def form_valid(self, form): @@ -57,19 +58,18 @@ class SearxServiceView(views.ServiceView): actions.superuser_run( 'searx', ['set-safe-search', form_data['safe_search']]) messages.success(self.request, _('Configuration updated.')) - except ActionError as e: + except ActionError: messages.error(self.request, _('An error occurred during configuration.')) if old_data['public_access'] != form_data['public_access']: try: if form_data['public_access']: - actions.superuser_run('searx', ['enable-public-access']) + enable_public_access() else: - actions.superuser_run('searx', ['disable-public-access']) - add_shortcut() + disable_public_access() messages.success(self.request, _('Configuration updated.')) - except ActionError as e: + except ActionError: messages.error(self.request, _('An error occurred during configuration.'))