From 9a3af288fac28bbdbe6f2a0be94e2f5c44b318e8 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Tue, 24 Jul 2018 21:45:53 +0530 Subject: [PATCH] configuration: Option to set a default app for FreedomBox Closes #1315 Signed-off-by: Joseph Nuthalapati Reviewed-by: James Valleroy --- .../apache2/conf-available/freedombox.conf | 3 + .../features/configuration.feature | 7 ++ functional_tests/step_definitions/system.py | 15 +++ functional_tests/support/system.py | 13 +++ plinth/frontpage.py | 35 ++++--- plinth/modules/config/__init__.py | 11 +++ plinth/modules/config/forms.py | 97 ++++++++++--------- plinth/modules/config/views.py | 53 ++++++++-- 8 files changed, 166 insertions(+), 68 deletions(-) diff --git a/data/etc/apache2/conf-available/freedombox.conf b/data/etc/apache2/conf-available/freedombox.conf index 3156b3722..4138b3b9f 100644 --- a/data/etc/apache2/conf-available/freedombox.conf +++ b/data/etc/apache2/conf-available/freedombox.conf @@ -9,3 +9,6 @@ Header set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=H ## other services. ## RedirectMatch "^/$" "/plinth" +RedirectMatch "^/freedombox" "/plinth" +RedirectMatch "^/home" "/plinth" + diff --git a/functional_tests/features/configuration.feature b/functional_tests/features/configuration.feature index 4dc34c994..eff892d72 100644 --- a/functional_tests/features/configuration.feature +++ b/functional_tests/features/configuration.feature @@ -29,3 +29,10 @@ Scenario: Change hostname Scenario: Change domain name When I change the domain name to mydomain Then the domain name should be mydomain + +Scenario: Change default app + Given the syncthing application is installed + And the default app is syncthing + When I change the default app to plinth + Then the default app should be plinth + diff --git a/functional_tests/step_definitions/system.py b/functional_tests/step_definitions/system.py index 316b45ffe..196cf928a 100644 --- a/functional_tests/step_definitions/system.py +++ b/functional_tests/step_definitions/system.py @@ -36,6 +36,11 @@ language_codes = { } +@given(parsers.parse('the default app is {app_name:w}')) +def set_default_app(browser, app_name): + system.set_default_app(browser, app_name) + + @given(parsers.parse('the domain name is set to {domain:w}')) def set_domain_name(browser, domain): system.set_domain_name(browser, domain) @@ -51,6 +56,11 @@ def change_domain_name_to(browser, domain): system.set_domain_name(browser, domain) +@when(parsers.parse('I change the default app to {app_name:w}')) +def change_default_app_to(browser, app_name): + system.set_default_app(browser, app_name) + + @when('I change the language to ') def change_language(browser, language): system.set_language(browser, language_codes[language]) @@ -85,3 +95,8 @@ def create_snapshot(browser): def verify_snapshot_count(browser, count): num_snapshots = system.get_snapshot_count(browser) assert num_snapshots == count + + +@then(parsers.parse('the default app should be {app_name:w}')) +def default_app_should_be(browser, app_name): + assert system.check_home_page_redirect(browser, app_name) diff --git a/functional_tests/support/system.py b/functional_tests/support/system.py index 3171d7e4c..6edc5a951 100644 --- a/functional_tests/support/system.py +++ b/functional_tests/support/system.py @@ -58,6 +58,13 @@ def set_domain_name(browser, domain_name): submit(browser) +def set_default_app(browser, app_name): + nav_to_module(browser, 'config') + drop_down = browser.find_by_id('id_configuration-defaultapp') + drop_down.select(app_name) + submit(browser) + + def set_language(browser, language_code): username = config['DEFAULT']['username'] browser.visit(config['DEFAULT']['url'] + @@ -86,3 +93,9 @@ def get_snapshot_count(browser): browser.visit(config['DEFAULT']['url'] + '/plinth/sys/snapshot/manage/') # Subtract 1 for table header return len(browser.find_by_xpath('//tr')) - 1 + + +def check_home_page_redirect(browser, app_name): + browser.visit(config['DEFAULT']['url']) + return browser.find_by_xpath( + "//a[contains(@href, '/plinth/') and @title='FreedomBox']") diff --git a/plinth/frontpage.py b/plinth/frontpage.py index 6b0eb630c..e3429bb99 100644 --- a/plinth/frontpage.py +++ b/plinth/frontpage.py @@ -22,28 +22,33 @@ from . import actions shortcuts = {} -def get_shortcuts(username): +def get_shortcuts(username=None, web_apps_only=False, sort_by='label'): """Return menu items in sorted order according to current locale.""" + shortcuts_to_return = {} if username: - shortcuts_to_return = {} output = actions.superuser_run('users', ['get-user-groups', username]) user_groups = set(output.strip().split('\n')) - if 'admin' in user_groups: - # Admin has access to all services - return sorted(shortcuts.values(), key=lambda item: item['label']) - - for shortcut_id, shortcut in shortcuts.items(): - if shortcut['allowed_groups']: - if not user_groups.isdisjoint(shortcut['allowed_groups']): + if 'admin' in user_groups: # Admin has access to all services + shortcuts_to_return = shortcuts + else: + for shortcut_id, shortcut in shortcuts.items(): + if shortcut['allowed_groups']: + if not user_groups.isdisjoint(shortcut['allowed_groups']): + shortcuts_to_return[shortcut_id] = shortcut + else: shortcuts_to_return[shortcut_id] = shortcut - else: - shortcuts_to_return[shortcut_id] = shortcut - - return sorted(shortcuts_to_return.values(), - key=lambda item: item['label']) else: - return sorted(shortcuts.values(), key=lambda item: item['label']) + shortcuts_to_return = shortcuts + + if web_apps_only: + shortcuts_to_return = { + _id: shortcut + for _id, shortcut in shortcuts_to_return.items() + if not shortcut['url'].startswith('?selected=') + } + + return sorted(shortcuts_to_return.values(), key=lambda item: item[sort_by]) def add_shortcut(shortcut_id, name, short_description="", login_required=False, diff --git a/plinth/modules/config/__init__.py b/plinth/modules/config/__init__.py index 4a0ff041f..910168096 100644 --- a/plinth/modules/config/__init__.py +++ b/plinth/modules/config/__init__.py @@ -18,6 +18,7 @@ FreedomBox app for basic system configuration. """ +import re import socket from django.utils.translation import ugettext_lazy @@ -48,6 +49,16 @@ def get_hostname(): return socket.gethostname() +def get_default_app(): + """Get the default application for the domain.""" + with open('/etc/apache2/conf-available/freedombox.conf') as conf_file: + for line in conf_file: + if re.findall(r'\^\/\$', line): + app_path = line.split()[-1].strip('"') + break + return app_path.strip("/") + + def init(): """Initialize the module""" menu = main_menu.get('system') diff --git a/plinth/modules/config/forms.py b/plinth/modules/config/forms.py index 21eef1235..734abe1cb 100644 --- a/plinth/modules/config/forms.py +++ b/plinth/modules/config/forms.py @@ -14,38 +14,27 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # - """ Forms for basic system configuration """ - -from django import forms -from django.utils.translation import ugettext as _, ugettext_lazy -from django.core import validators -from django.core.exceptions import ValidationError - -from plinth import cfg -from plinth.utils import format_lazy - import logging import re +from django import forms +from django.core import validators +from django.core.exceptions import ValidationError +from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext_lazy + +from plinth import cfg, frontpage +from plinth.utils import format_lazy + logger = logging.getLogger(__name__) HOSTNAME_REGEX = r'^[a-zA-Z0-9]([-a-zA-Z0-9]{,61}[a-zA-Z0-9])?$' -class TrimmedCharField(forms.CharField): - """Trim the contents of a CharField""" - def clean(self, value): - """Clean and validate the field value""" - if value: - value = value.strip() - - return super(TrimmedCharField, self).clean(value) - - def domain_label_validator(domainname): """Validate domain name labels.""" for label in domainname.split('.'): @@ -53,6 +42,12 @@ def domain_label_validator(domainname): raise ValidationError(_('Invalid domain name')) +def get_default_app_choices(): + shortcuts = frontpage.get_shortcuts(web_apps_only=True, sort_by='name') + apps = [(shortcut['id'], shortcut['name']) for shortcut in shortcuts] + return [('plinth', 'FreedomBox Service (Plinth)')] + apps + + class ConfigurationForm(forms.Form): """Main system configuration form""" # See: @@ -60,32 +55,44 @@ class ConfigurationForm(forms.Form): # https://tools.ietf.org/html/rfc1035#section-2.3.1 # https://tools.ietf.org/html/rfc1123#section-2 # https://tools.ietf.org/html/rfc2181#section-11 - hostname = TrimmedCharField( - label=ugettext_lazy('Hostname'), - help_text=format_lazy(ugettext_lazy( - 'Hostname is the local name by which other devices on the local ' - 'network can reach your {box_name}. It must start and end with ' - 'an alphabet or a digit and have as interior characters only ' - 'alphabets, digits and hyphens. Total length must be 63 ' - 'characters or less.'), box_name=ugettext_lazy(cfg.box_name)), + hostname = forms.CharField( + label=ugettext_lazy('Hostname'), help_text=format_lazy( + ugettext_lazy( + 'Hostname is the local name by which other devices on the local ' + 'network can reach your {box_name}. It must start and end with ' + 'an alphabet or a digit and have as interior characters only ' + 'alphabets, digits and hyphens. Total length must be 63 ' + 'characters or less.'), box_name=ugettext_lazy(cfg.box_name)), validators=[ - validators.RegexValidator( - HOSTNAME_REGEX, - ugettext_lazy('Invalid hostname'))]) + validators.RegexValidator(HOSTNAME_REGEX, + ugettext_lazy('Invalid hostname')) + ], strip=True) - domainname = TrimmedCharField( - label=ugettext_lazy('Domain Name'), - help_text=format_lazy(ugettext_lazy( - 'Domain name is the global name by which other devices on the ' - 'Internet can reach your {box_name}. It must consist of labels ' - 'separated by dots. Each label must start and end with an ' - 'alphabet or a digit and have as interior characters only ' - 'alphabets, digits and hyphens. Length of each label must be 63 ' - 'characters or less. Total length of domain name must be 253 ' - 'characters or less.'), box_name=ugettext_lazy(cfg.box_name)), - required=False, - validators=[ + domainname = forms.CharField( + label=ugettext_lazy('Domain Name'), help_text=format_lazy( + ugettext_lazy( + 'Domain name is the global name by which other devices on the ' + 'Internet can reach your {box_name}. It must consist of labels ' + 'separated by dots. Each label must start and end with an ' + 'alphabet or a digit and have as interior characters only ' + 'alphabets, digits and hyphens. Length of each label must be 63 ' + 'characters or less. Total length of domain name must be 253 ' + 'characters or less.'), box_name=ugettext_lazy(cfg.box_name)), + required=False, validators=[ validators.RegexValidator( r'^[a-zA-Z0-9]([-a-zA-Z0-9.]{,251}[a-zA-Z0-9])?$', - ugettext_lazy('Invalid domain name')), - domain_label_validator]) + ugettext_lazy('Invalid domain name')), domain_label_validator + ], strip=True) + + defaultapp = forms.ChoiceField( + label=ugettext_lazy('Default App'), help_text=format_lazy( + ugettext_lazy( + 'Choose the default web application that must be served when ' + 'someone visits your {box_name} on the web. A typical use ' + 'case is to set your blog or wiki as the landing page when ' + 'someone visits the domain name. Note that once the default ' + 'app is set to something other than {box_name} Service ' + '(Plinth), your users must explicitly type /plinth or ' + '/freedombox to reach {box_name} Service (Plinth).'), + box_name=ugettext_lazy(cfg.box_name)), required=False, + choices=get_default_app_choices) diff --git a/plinth/modules/config/views.py b/plinth/modules/config/views.py index 11fddb775..cc2f9b076 100644 --- a/plinth/modules/config/views.py +++ b/plinth/modules/config/views.py @@ -19,12 +19,13 @@ FreedomBox views for basic system configuration. """ import logging +import re from django.contrib import messages from django.template.response import TemplateResponse from django.utils.translation import ugettext as _ -from plinth import actions +from plinth import action_utils, actions, frontpage from plinth.modules import config, firewall from plinth.modules.names import SERVICES from plinth.signals import (domain_added, domain_removed, domainname_change, @@ -63,6 +64,7 @@ def get_status(request): return { 'hostname': config.get_hostname(), 'domainname': config.get_domainname(), + 'defaultapp': config.get_default_app(), } @@ -72,9 +74,10 @@ def _apply_changes(request, old_status, new_status): try: set_hostname(new_status['hostname']) except Exception as exception: - messages.error(request, - _('Error setting hostname: {exception}') - .format(exception=exception)) + messages.error( + request, + _('Error setting hostname: {exception}') + .format(exception=exception)) else: messages.success(request, _('Hostname set')) @@ -82,12 +85,46 @@ def _apply_changes(request, old_status, new_status): try: set_domainname(new_status['domainname']) except Exception as exception: - messages.error(request, - _('Error setting domain name: {exception}') - .format(exception=exception)) + messages.error( + request, + _('Error setting domain name: {exception}') + .format(exception=exception)) else: messages.success(request, _('Domain name set')) + if old_status['defaultapp'] != new_status['defaultapp']: + try: + change_default_app(new_status['defaultapp']) + except Exception as exception: + messages.error( + request, + _('Error setting default app: {exception}') + .format(exception=exception)) + else: + messages.success(request, _('Default app set')) + + +def change_default_app(app_id): + """Changes the FreedomBox's default app to the app specified by app_id.""" + if app_id == 'plinth': + url = '/plinth' + else: + shortcuts = frontpage.get_shortcuts() + url = [ + shortcut['url'] for shortcut in shortcuts + if shortcut['id'] == app_id + ][0] + lines = [] + freedombox_apache_conf = '/etc/apache2/conf-available/freedombox.conf' + with open(freedombox_apache_conf, 'r') as conf_file: + for line in conf_file: + if re.findall(r'\^\/\$', line): + line = 'RedirectMatch "^/$" ' + '"{}"'.format(url) + lines.append(line) + with open(freedombox_apache_conf, 'w') as conf_file: + conf_file.write("\n".join(lines)) + action_utils.service_reload('apache2') + def set_hostname(hostname): """Sets machine hostname to hostname""" @@ -95,7 +132,7 @@ def set_hostname(hostname): domainname = config.get_domainname() # Hostname should be ASCII. If it's unicode but passed our - # valid_hostname check, convert to ASCII. + # valid_hostname check, convert hostname = str(hostname) pre_hostname_change.send_robust(sender='config', old_hostname=old_hostname,