mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
configuration: Option to set a default app for FreedomBox
Closes #1315 Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
c733339719
commit
9a3af288fa
8 files changed
+166
-68
No files matched your search
+20
-15
@@ -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,
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -14,38 +14,27 @@
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
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)
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user