mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
Convert wan page to Django forms
This commit is contained in:
parent
c05c3ba3c8
commit
ed26274d38
36
modules/installed/system/templates/wan.html
Normal file
36
modules/installed/system/templates/wan.html
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
{% extends "login_nav.html" %}
|
||||||
|
{% load bootstrap %}
|
||||||
|
|
||||||
|
{% block main_block %}
|
||||||
|
|
||||||
|
{% if cfg.users.expert %}
|
||||||
|
|
||||||
|
{% for severity, message in messages %}
|
||||||
|
<div class='alert alert-{{ severity }}'>{{ message }}</div>
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
<p>For security reasons, neither WAN Administration nor WAN SSH is
|
||||||
|
available to the `admin` user account.</p>
|
||||||
|
|
||||||
|
<p>TODO: in expert mode, tell user they can ssh in to enable admin
|
||||||
|
from WAN, do their business, then disable it. It would be good to
|
||||||
|
enable the option and autodisable it when the ssh connection
|
||||||
|
dies.</p>
|
||||||
|
|
||||||
|
<form class="form" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
|
||||||
|
{{ form|bootstrap }}
|
||||||
|
|
||||||
|
<input type="submit" class="btn-primary" value="Submit"/>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
|
||||||
|
{% else %}
|
||||||
|
|
||||||
|
<p>This page is available only in expert mode.</p>
|
||||||
|
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
@ -1,78 +1,85 @@
|
|||||||
import os
|
|
||||||
import cherrypy
|
import cherrypy
|
||||||
try:
|
from django import forms
|
||||||
import simplejson as json
|
|
||||||
except ImportError:
|
|
||||||
import json
|
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
from modules.auth import require
|
from modules.auth import require
|
||||||
from plugin_mount import PagePlugin, FormPlugin
|
from plugin_mount import PagePlugin
|
||||||
import cfg
|
import cfg
|
||||||
from forms import Form
|
import util
|
||||||
from model import User
|
|
||||||
from util import *
|
|
||||||
|
|
||||||
class wan(FormPlugin, PagePlugin):
|
|
||||||
url = ["/sys/config"]
|
|
||||||
order = 20
|
|
||||||
|
|
||||||
def help(self, *args, **kwargs):
|
class WanForm(forms.Form): # pylint: disable-msg=W0232
|
||||||
if not cfg.users.expert():
|
"""Form to configure wan settings"""
|
||||||
return ''
|
|
||||||
return _(#"""<h4>Admin from WAN</h4>
|
|
||||||
"""<p>If you check this box, this front
|
|
||||||
end will be reachable from the WAN. If your %(box)s
|
|
||||||
connects you to the internet, that means you'll be able to log
|
|
||||||
in to the front end from the internet. This might be
|
|
||||||
convenient, but it is also <strong>dangerous</strong>, since it can
|
|
||||||
enable attackers to gain access to your %(box)s from the
|
|
||||||
outside world. All they'll need is your username and
|
|
||||||
passphrase, which they might guess or they might simply try
|
|
||||||
every posible combination of letters and numbers until they
|
|
||||||
get in. If you enable the WAN administration option, you
|
|
||||||
<strong>must</strong> use long and complex passphrases.</p>
|
|
||||||
|
|
||||||
<p>For security reasons, neither WAN Administration nor WAN
|
wan_admin = forms.BooleanField(
|
||||||
SSH is available to the `admin` user account.</p>
|
label=_('Allow access to Plinth from WAN'),
|
||||||
|
required=False,
|
||||||
|
help_text=_('If you check this box, this front end will be reachable \
|
||||||
|
from the WAN. If your {{ box_name }} connects you to the internet, that \
|
||||||
|
means you\'ll be able to log in to the front end from the internet. This \
|
||||||
|
might be convenient, but it is also <strong>dangerous</strong>, since it can \
|
||||||
|
enable attackers to gain access to your {{ box_name }} from the outside \
|
||||||
|
world. All they\'ll need is your username and passphrase, which they might \
|
||||||
|
guess or they might simply try every posible combination of letters and \
|
||||||
|
numbers until they get in. If you enable the WAN administration option, you \
|
||||||
|
<strong>must</strong> use long and complex passphrases.').format(
|
||||||
|
box_name=cfg.box_name))
|
||||||
|
|
||||||
<p>TODO: in expert mode, tell user they can ssh in to enable
|
lan_ssh = forms.BooleanField(
|
||||||
admin from WAN, do their business, then disable it. It would
|
label=_('Allow SSH access from LAN'),
|
||||||
be good to enable the option and autodisable it when the ssh
|
required=False)
|
||||||
connection dies.</p>
|
|
||||||
""" % {'product':cfg.product_name, 'box':cfg.box_name})
|
|
||||||
|
|
||||||
def main(self, message='', **kwargs):
|
wan_ssh = forms.BooleanField(
|
||||||
store = filedict_con(cfg.store_file, 'sys')
|
label=_('Allow SSH access from WAN'),
|
||||||
|
required=False)
|
||||||
|
|
||||||
defaults = {'wan_admin': '',
|
# XXX: Only present due to issue with submitting empty form
|
||||||
'wan_ssh': '',
|
dummy = forms.CharField(label='Dummy', initial='dummy',
|
||||||
'lan_ssh': '',
|
widget=forms.HiddenInput())
|
||||||
}
|
|
||||||
for key, value in defaults.items():
|
|
||||||
if not key in kwargs:
|
|
||||||
try:
|
|
||||||
kwargs[key] = store[key]
|
|
||||||
except KeyError:
|
|
||||||
store[key] = kwargs[key] = value
|
|
||||||
|
|
||||||
form = Form(title=_("Accessing the %s" % cfg.box_name),
|
|
||||||
action=cfg.server_dir + "/sys/config/wan/",
|
|
||||||
name="admin_wan_form",
|
|
||||||
message=message)
|
|
||||||
form.html(self.help())
|
|
||||||
if cfg.users.expert():
|
|
||||||
form.checkbox(_("Allow access to Plinth from WAN"), name="wan_admin", checked=kwargs['wan_admin'])
|
|
||||||
form.checkbox(_("Allow SSH access from LAN"), name="lan_ssh", checked=kwargs['lan_ssh'])
|
|
||||||
form.checkbox(_("Allow SSH access from WAN"), name="wan_ssh", checked=kwargs['wan_ssh'])
|
|
||||||
|
|
||||||
# Hidden field is needed because checkbox doesn't post if not checked
|
class Wan(PagePlugin):
|
||||||
form.hidden(name="submitted", value="True")
|
order = 60
|
||||||
|
|
||||||
form.submit(_("Submit"))
|
def __init__(self, *args, **kwargs):
|
||||||
return form.render()
|
PagePlugin.__init__(self, *args, **kwargs)
|
||||||
|
self.register_page('sys.config.wan')
|
||||||
|
|
||||||
def process_form(self, wan_admin='', wan_ssh='', lan_ssh='', *args, **kwargs):
|
cfg.html_root.sys.config.menu.add_item(_('WAN'), 'icon-cog',
|
||||||
store = filedict_con(cfg.store_file, 'sys')
|
'/sys/config/wan', 20)
|
||||||
|
|
||||||
|
@cherrypy.expose
|
||||||
|
@require()
|
||||||
|
def index(self, **kwargs):
|
||||||
|
"""Serve the configuration form"""
|
||||||
|
status = self.get_status()
|
||||||
|
|
||||||
|
form = None
|
||||||
|
messages = []
|
||||||
|
|
||||||
|
if kwargs and cfg.users.expert():
|
||||||
|
form = WanForm(kwargs, prefix='wan')
|
||||||
|
# pylint: disable-msg=E1101
|
||||||
|
if form.is_valid():
|
||||||
|
self._apply_changes(form.cleaned_data, messages)
|
||||||
|
status = self.get_status()
|
||||||
|
form = WanForm(initial=status, prefix='wan')
|
||||||
|
else:
|
||||||
|
form = WanForm(initial=status, prefix='wan')
|
||||||
|
|
||||||
|
title = _('Accessing the {box_name}').format(box_name=cfg.box_name)
|
||||||
|
return util.render_template(template='wan', title=title, form=form,
|
||||||
|
messages=messages)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_status():
|
||||||
|
"""Return the current status"""
|
||||||
|
return util.filedict_con(cfg.store_file, 'sys')
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _apply_changes(new_status, messages):
|
||||||
|
"""Apply the changes after form submission"""
|
||||||
|
store = util.filedict_con(cfg.store_file, 'sys')
|
||||||
for field in ['wan_admin', 'wan_ssh', 'lan_ssh']:
|
for field in ['wan_admin', 'wan_ssh', 'lan_ssh']:
|
||||||
store[field] = locals()[field]
|
store[field] = new_status[field]
|
||||||
return "Settings updated."
|
|
||||||
|
messages.append(('success', _('Setting updated')))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user