From 33e139cae64bb78157ebca56a5d1a752da95555a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Fri, 23 Oct 2020 16:22:55 +0200 Subject: [PATCH] Let admins lock user preferences --- searx/preferences.py | 45 ++++++++++++++++++++-- searx/settings.yml | 9 +++++ searx/settings_robot.yml | 3 ++ searx/templates/courgette/preferences.html | 16 ++++++++ searx/templates/legacy/preferences.html | 16 ++++++++ searx/templates/oscar/preferences.html | 22 +++++++++++ searx/templates/pix-art/preferences.html | 8 ++++ searx/templates/simple/preferences.html | 20 ++++++++++ searx/webadapter.py | 10 ++++- searx/webapp.py | 5 +++ tests/unit/test_preferences.py | 2 +- 11 files changed, 151 insertions(+), 5 deletions(-) diff --git a/searx/preferences.py b/searx/preferences.py index f149dc976..e5ad6ead7 100644 --- a/searx/preferences.py +++ b/searx/preferences.py @@ -35,9 +35,10 @@ class ValidationException(Exception): class Setting: """Base class of user settings""" - def __init__(self, default_value, **kwargs): + def __init__(self, default_value, locked=False, **kwargs): super().__init__() self.value = default_value + self.locked = locked for key, value in kwargs.items(): setattr(self, key, value) @@ -115,6 +116,9 @@ class MultipleChoiceSetting(EnumStringSetting): self.value = elements def parse_form(self, data): # pylint: disable=missing-function-docstring + if self.locked: + return + self.value = [] for choice in data: if choice in self.choices and choice not in self.value: # pylint: disable=no-member @@ -149,6 +153,9 @@ class SetSetting(Setting): self.values.add(element) def parse_form(self, data): # pylint: disable=missing-function-docstring + if self.locked: + return + elements = data.split(',') self.values = set(elements) # pylint: disable=attribute-defined-outside-init @@ -233,6 +240,9 @@ class SwitchableSetting(Setting): self.enabled = set(data[ENABLED].split(',')) def parse_form(self, items): # pylint: disable=missing-function-docstring + if self.locked: + return + items = self.transform_form_items(items) self.disabled = set() # pylint: disable=attribute-defined-outside-init self.enabled = set() # pylint: disable=attribute-defined-outside-init @@ -317,22 +327,28 @@ class Preferences: self.key_value_settings = { 'categories': MultipleChoiceSetting( - ['general'], choices=categories + ['none'] + ['general'], + is_locked('categories'), + choices=categories + ['none'] ), 'language': SearchLanguageSetting( settings['search'].get('default_lang', ''), + is_locked('language'), choices=list(LANGUAGE_CODES) + [''] ), 'locale': EnumStringSetting( settings['ui'].get('default_locale', ''), + is_locked('locale'), choices=list(settings['locales'].keys()) + [''] ), 'autocomplete': EnumStringSetting( settings['search'].get('autocomplete', ''), + is_locked('autocomplete'), choices=list(autocomplete.backends.keys()) + [''] ), 'image_proxy': MapSetting( settings['server'].get('image_proxy', False), + is_locked('image_proxy'), map={ '': settings['server'].get('image_proxy', 0), '0': False, @@ -343,10 +359,12 @@ class Preferences: ), 'method': EnumStringSetting( settings['server'].get('method', 'POST'), + is_locked('method'), choices=('GET', 'POST') ), 'safesearch': MapSetting( settings['search'].get('safe_search', 0), + is_locked('safesearch'), map={ '0': 0, '1': 1, @@ -355,10 +373,12 @@ class Preferences: ), 'theme': EnumStringSetting( settings['ui'].get('default_theme', 'oscar'), + is_locked('theme'), choices=themes ), 'results_on_new_tab': MapSetting( settings['ui'].get('results_on_new_tab', False), + is_locked('results_on_new_tab'), map={ '0': False, '1': True, @@ -367,10 +387,13 @@ class Preferences: } ), 'doi_resolver': MultipleChoiceSetting( - ['oadoi.org'], choices=DOI_RESOLVERS + ['oadoi.org'], + is_locked('doi_resolver'), + choices=DOI_RESOLVERS ), 'oscar-style': EnumStringSetting( settings['ui'].get('theme_args', {}).get('oscar_style', 'logicodev'), + is_locked('oscar-style'), choices=['', 'logicodev', 'logicodev-dark', 'pointhi']), } @@ -383,6 +406,8 @@ class Preferences: """Return preferences as URL parameters""" settings_kv = {} for k, v in self.key_value_settings.items(): + if v.locked: + continue if isinstance(v, MultipleChoiceSetting): settings_kv[k] = ','.join(v.get_value()) else: @@ -410,6 +435,8 @@ class Preferences: """parse preferences from request (``flask.request.form``)""" for user_setting_name, user_setting in input_data.items(): if user_setting_name in self.key_value_settings: + if self.key_value_settings[user_setting_name].locked: + continue self.key_value_settings[user_setting_name].parse(user_setting) elif user_setting_name == 'disabled_engines': self.engines.parse_cookie((input_data.get('disabled_engines', ''), @@ -464,6 +491,8 @@ class Preferences: """Save cookie in the HTTP reponse obect """ for user_setting_name, user_setting in self.key_value_settings.items(): + if self.key_value_settings[user_setting_name].locked: + continue user_setting.save(user_setting_name, resp) self.engines.save(resp) self.plugins.save(resp) @@ -482,3 +511,13 @@ class Preferences: break return valid + + +def is_locked(setting_name): + """Checks if a given setting name is locked by settings.yml + """ + if 'preferences' not in settings: + return False + if 'lock' not in settings['preferences']: + return False + return setting_name in settings['preferences']['lock'] diff --git a/searx/settings.yml b/searx/settings.yml index a599ab285..b23f48b45 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -33,6 +33,15 @@ ui: # - it # - science +# Lock arbitrary settings on the preferences page. +# To find the ID of the user setting you want to lock, check +# the ID of the form on the page "preferences". +#preferences: +# lock: +# - language +# - autocomplete +# - method + # searx supports result proxification using an external service: https://github.com/asciimoo/morty # uncomment below section if you have running morty proxy # the key is base64 encoded (keep the !!binary notation) diff --git a/searx/settings_robot.yml b/searx/settings_robot.yml index 941a43f83..05c8eeb8c 100644 --- a/searx/settings_robot.yml +++ b/searx/settings_robot.yml @@ -17,6 +17,9 @@ ui: templates_path : "" default_theme : oscar +preferences: + lock: [] + outgoing: request_timeout : 1.0 # seconds useragent_suffix : "" diff --git a/searx/templates/courgette/preferences.html b/searx/templates/courgette/preferences.html index c67f7662b..6480694b5 100644 --- a/searx/templates/courgette/preferences.html +++ b/searx/templates/courgette/preferences.html @@ -5,10 +5,13 @@

{{ _('Preferences') }}

+ {% if 'categories' not in locked_preferences %}
{{ _('Default categories') }} {% include 'courgette/categories.html' %}
+ {% endif %} + {% if 'language' not in locked_preferences %}
{{ _('Search language') }}

@@ -20,6 +23,8 @@

+ {% endif %} + {% if 'locale' not in locked_preferences %}
{{ _('Interface language') }}

@@ -30,6 +35,8 @@

+ {% endif %} + {% if 'autocomplete' not in locked_preferences %}
{{ _('Autocomplete') }}

@@ -41,6 +48,8 @@

+ {% endif %} + {% if 'image_proxy' not in locked_preferences %}
{{ _('Image proxy') }}

@@ -50,6 +59,8 @@

+ {% endif %} + {% if 'method' not in locked_preferences %}
{{ _('Method') }}

@@ -59,6 +70,8 @@

+ {% endif %} + {% if 'safesearch' not in locked_preferences %}
{{ _('SafeSearch') }}

@@ -69,6 +82,8 @@

+ {% endif %} + {% if 'theme' not in locked_preferences %}
{{ _('Themes') }}

@@ -92,6 +107,7 @@

+ {% endif %}
{{ _('Currently used search engines') }} diff --git a/searx/templates/legacy/preferences.html b/searx/templates/legacy/preferences.html index 414b3f6c0..23b3875d2 100644 --- a/searx/templates/legacy/preferences.html +++ b/searx/templates/legacy/preferences.html @@ -10,6 +10,7 @@ {% set display_tooltip = false %} {% include 'legacy/categories.html' %}
+ {% if 'language' not in locked_preferences %}
{{ _('Search language') }}

@@ -21,6 +22,8 @@

+ {% endif %} + {% if 'locale' not in locked_preferences %}
{{ _('Interface language') }}

@@ -31,6 +34,8 @@

+ {% endif %} + {% if 'autocomplete' not in locked_preferences %}
{{ _('Autocomplete') }}

@@ -42,6 +47,8 @@

+ {% endif %} + {% if 'image_proxy' not in locked_preferences %}
{{ _('Image proxy') }}

@@ -51,6 +58,8 @@

+ {% endif %} + {% if 'method' not in locked_preferences %}
{{ _('Method') }}

@@ -60,6 +69,8 @@

+ {% endif %} + {% if 'safesearch' not in locked_preferences %}
{{ _('SafeSearch') }}

@@ -70,6 +81,8 @@

+ {% endif %} + {% if 'theme' not in locked_preferences %}
{{ _('Themes') }}

@@ -80,6 +93,8 @@

+ {% endif %} + {% if 'results_on_new_tab' not in locked_preferences %}
{{ _('Results on new tabs') }}

@@ -89,6 +104,7 @@

+ {% endif %}
{{ _('Currently used search engines') }} diff --git a/searx/templates/oscar/preferences.html b/searx/templates/oscar/preferences.html index 8f67cbf93..b1023a02b 100644 --- a/searx/templates/oscar/preferences.html +++ b/searx/templates/oscar/preferences.html @@ -25,6 +25,7 @@
+ {% if 'categories' not in locked_preferences %}
{% if rtl %}
@@ -38,12 +39,16 @@
{% endif %}
+ {% endif %} + {% if 'language' not in locked_preferences %} {% set language_label = _('Search language') %} {% set language_info = _('What language do you prefer for search?') %} {{ preferences_item_header(language_info, language_label, rtl, 'language') }} {% include 'oscar/languages.html' %} {{ preferences_item_footer(language_info, language_label, rtl) }} + {% endif %} + {% if 'locale' not in locked_preferences %} {% set locale_label = _('Interface language') %} {% set locale_info = _('Change the language of the layout') %} {{ preferences_item_header(locale_info, locale_label, rtl, 'locale') }} @@ -53,7 +58,9 @@ {% endfor %} {{ preferences_item_footer(locale_info, locale_label, rtl) }} + {% endif %} + {% if 'autocomplete' not in locked_preferences %} {% set autocomplete_label = _('Autocomplete') %} {% set autocomplete_info = _('Find stuff as you type') %} {{ preferences_item_header(autocomplete_info, autocomplete_label, rtl, 'autocomplete') }} @@ -64,7 +71,9 @@ {% endfor %} {{ preferences_item_footer(autocomplete_info, autocomplete_label, rtl) }} + {% endif %} + {% if 'image_proxy' not in locked_preferences %} {% set image_proxy_label = _('Image proxy') %} {% set image_proxy_info = _('Proxying image results through searx') %} {{ preferences_item_header(image_proxy_info, image_proxy_label, rtl, 'image_proxy') }} @@ -73,7 +82,9 @@ {{ preferences_item_footer(image_proxy_info, image_proxy_label, rtl) }} + {% endif %} + {% if 'method' not in locked_preferences %} {% set method_label = _('Method') %} {% set method_info = _('Change how forms are submited, learn more about request methods') %} {{ preferences_item_header(method_info, method_label, rtl, 'method') }} @@ -82,7 +93,9 @@ {{ preferences_item_footer(method_info, method_label, rtl) }} + {% endif %} + {% if 'safesearch' not in locked_preferences %} {% set safesearch_label = _('SafeSearch') %} {% set safesearch_info = _('Filter content') %} {{ preferences_item_header(safesearch_info, safesearch_label, rtl, 'safesearch') }} @@ -92,7 +105,9 @@ {{ preferences_item_footer(safesearch_info, safesearch_label, rtl) }} + {% endif %} + {% if 'theme' not in locked_preferences %} {% set theme_label = _('Themes') %} {% set theme_info = _('Change searx layout') %} {{ preferences_item_header(theme_info, theme_label, rtl, 'theme') }} @@ -102,7 +117,9 @@ {% endfor %} {{ preferences_item_footer(theme_info, theme_label, rtl) }} + {% endif %} + {% if 'oscar-style' not in locked_preferences %} {{ preferences_item_header(_('Choose style for this theme'), _('Style'), rtl, 'oscar_style') }} {{ preferences_item_footer(_('Choose style for this theme'), _('Style'), rtl) }} + {% endif %} + {% if 'results_on_new_tab' not in locked_preferences %} {% set label = _('Results on new tabs') %} {% set info = _('Open result links on new browser tabs') %} {{ preferences_item_header(info, label, rtl, 'results_on_new_tab') }} @@ -119,7 +138,9 @@ {{ preferences_item_footer(info, label, rtl) }} + {% endif %} + {% if 'doi_resolver' not in locked_preferences %} {% set label = _('Open Access DOI resolver') %} {% set info = _('Redirect to open-access versions of publications when available (plugin required)') %} {{ preferences_item_header(info, label, rtl, 'doi_resolver') }} @@ -131,6 +152,7 @@ {% endfor %} {{ preferences_item_footer(info, label, rtl) }} + {% endif %} {% set label = _('Engine tokens') %} {% set info = _('Access tokens for private engines') %} diff --git a/searx/templates/pix-art/preferences.html b/searx/templates/pix-art/preferences.html index 05876dedf..ee415435f 100644 --- a/searx/templates/pix-art/preferences.html +++ b/searx/templates/pix-art/preferences.html @@ -5,6 +5,7 @@

{{ _('Preferences') }}

+ {% if 'language' not in locked_preferences %}
{{ _('Search language') }}

@@ -16,6 +17,8 @@

+ {% endif %} + {% if 'locale' not in locked_preferences %}
{{ _('Interface language') }}

@@ -26,6 +29,8 @@

+ {% endif %} + {% if 'method' not in locked_preferences %}
{{ _('Method') }}

@@ -35,6 +40,8 @@

+ {% endif %} + {% if 'theme' not in locked_preferences %}
{{ _('Themes') }}

@@ -45,6 +52,7 @@

+ {% endif %}
{{ _('Currently used search engines') }} diff --git a/searx/templates/simple/preferences.html b/searx/templates/simple/preferences.html index 7437ed422..d68e4be5f 100644 --- a/searx/templates/simple/preferences.html +++ b/searx/templates/simple/preferences.html @@ -30,11 +30,14 @@ {{ tabs_open() }} {{ tab_header('maintab', 'general', _('General')) }} + {% if 'categories' not in locked_preferences %}
{{ _('Default categories') }} {% set display_tooltip = false %} {% include 'simple/categories.html' %}
+ {% endif %} + {% if 'language' not in locked_preferences %}
{{ _('Search language') }}

{{- '' -}} @@ -47,6 +50,8 @@

{{ _('What language do you prefer for search?') }}
+ {% endif %} + {% if 'autocomplete' not in locked_preferences %}
{{ _('Autocomplete') }}

@@ -59,6 +64,8 @@

{{ _('Find stuff as you type') }}
+ {% endif %} + {% if 'safesearch' not in locked_preferences %}
{{ _('SafeSearch') }}

@@ -70,7 +77,9 @@

{{ _('Filter content') }}

+ {% endif %} {{ plugin_preferences('general') }} + {% if 'doi_resolver' not in locked_preferences %}
{{ _('Open Access DOI resolver') }}

@@ -84,6 +93,7 @@

+ {% endif %} {{ tab_footer() }} {{ tab_header('maintab', 'engines', _('Engines')) }} @@ -129,6 +139,7 @@ {{ tab_footer() }} {{ tab_header('maintab', 'ui', _('User interface')) }} + {% if 'locale' not in locked_preferences %}
{{ _('Interface language') }}

@@ -140,6 +151,8 @@

{{ _('Change the language of the layout') }}
+ {% endif %} + {% if 'theme' not in locked_preferences %}
{{ _('Themes') }}

@@ -151,6 +164,8 @@

{{ _('Change searx layout') }}
+ {% endif %} + {% if 'results_on_new_tab' not in locked_preferences %}
{{ _('Results on new tabs') }}

@@ -161,6 +176,7 @@

{{_('Open result links on new browser tabs') }}
+ {% endif %} {{ plugin_preferences('ui') }} {{ tab_footer() }} @@ -197,6 +213,7 @@ {{ tab_footer() }} {{ tab_header('maintab', 'privacy', _('Privacy')) }} + {% if 'method' not in locked_preferences %}
{{ _('Method') }}

@@ -207,6 +224,8 @@

{{ _('Search language') }}
+ {% endif %} + {% if 'image_proxy' not in locked_preferences %}
{{ _('Image proxy') }}

@@ -217,6 +236,7 @@

{{ _('Proxying image results through searx') }}
+ {% endif %} {{ plugin_preferences('privacy') }} {{ tab_footer() }} diff --git a/searx/webadapter.py b/searx/webadapter.py index 7ef06b62a..e2867d99d 100644 --- a/searx/webadapter.py +++ b/searx/webadapter.py @@ -4,7 +4,7 @@ from searx.webutils import VALID_LANGUAGE_CODE from searx.query import RawTextQuery from searx.engines import categories, engines from searx.search import SearchQuery, EngineRef -from searx.preferences import Preferences +from searx.preferences import Preferences, is_locked # remove duplicate queries. @@ -48,6 +48,8 @@ def parse_pageno(form: Dict[str, str]) -> int: def parse_lang(preferences: Preferences, form: Dict[str, str], raw_text_query: RawTextQuery) -> str: + if is_locked('language'): + return preferences.get_value('langueage') # get language # set specific language if set on request, query or preferences # TODO support search with multible languages @@ -66,6 +68,9 @@ def parse_lang(preferences: Preferences, form: Dict[str, str], raw_text_query: R def parse_safesearch(preferences: Preferences, form: Dict[str, str]) -> int: + if is_locked('safesearch'): + return preferences.get_value('safesearch') + if 'safesearch' in form: query_safesearch = form.get('safesearch') # first check safesearch @@ -117,6 +122,9 @@ def parse_specific(raw_text_query: RawTextQuery) -> Tuple[List[EngineRef], List[ def parse_category_form(query_categories: List[str], name: str, value: str) -> None: + if is_locked('categories'): + return preferences.get_value('categories') + if name == 'categories': query_categories.extend(categ for categ in map(str.strip, value.split(',')) if categ in categories) elif name.startswith('category_'): diff --git a/searx/webapp.py b/searx/webapp.py index 5e6a05b19..cf9a09778 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -828,6 +828,10 @@ def preferences(): stats[engine_stat.get('name')]['warn_time'] = True # end of stats + locked_preferences = list() + if 'preferences' in settings and 'lock' in settings['preferences']: + locked_preferences = settings['preferences']['lock'] + return render('preferences.html', selected_categories=get_selected_categories(request.preferences, request.form), all_categories=_get_ordered_categories(), @@ -848,6 +852,7 @@ def preferences(): theme=get_current_theme_name(), preferences_url_params=request.preferences.get_as_url_params(), base_url=get_base_url(), + locked_preferences=locked_preferences, preferences=True) diff --git a/tests/unit/test_preferences.py b/tests/unit/test_preferences.py index 32f50c60b..bee436027 100644 --- a/tests/unit/test_preferences.py +++ b/tests/unit/test_preferences.py @@ -141,4 +141,4 @@ class TestPreferences(SearxTestCase): pref.parse_encoded_data(url_params) self.assertEqual( vars(pref.key_value_settings['categories']), - {'value': ['general'], 'choices': ['general', 'none']}) + {'value': ['general'], 'locked': False, 'choices': ['general', 'none']})