mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
Use Django dispatcher instead of CherryPy dispatcher
This commit is big because anything small breaks the code. - Django dispatcher is based on regular expressions and does not need a tree structure - Reduces a lot of unnecessary dependencies among modules - Use Django sessions middlewear instead of CherryPy sessions - Introduce dependency based modules instead of numeric load order - Remove PagePlugin and simply use Django views - Eliminate page rendering wrappers in favor of Django context processors - Use custom auth for now until replaced by Django auth middlewear - Use Django templated 404 and 500 error pages
This commit is contained in:
1 parent
657bb11bbc
commit
58d13e3ed8
42 files changed
+1281
-1289
No files matched your search
@@ -16,10 +16,10 @@
|
||||
#
|
||||
|
||||
"""
|
||||
Plinth module for apps section page
|
||||
Plinth module for Apps section page
|
||||
"""
|
||||
|
||||
from . import apps
|
||||
from .apps import init
|
||||
|
||||
|
||||
__all__ = ['apps']
|
||||
__all__ = ['apps', 'init']
|
||||
+8
-13
@@ -1,19 +1,14 @@
|
||||
import cherrypy
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
from plugin_mount import PagePlugin
|
||||
|
||||
import cfg
|
||||
import util
|
||||
|
||||
|
||||
class Apps(PagePlugin):
|
||||
def __init__(self):
|
||||
super(Apps, self).__init__()
|
||||
def init():
|
||||
"""Initailize the apps module"""
|
||||
cfg.main_menu.add_item("Apps", "icon-download-alt", "/apps", 80)
|
||||
|
||||
self.register_page("apps")
|
||||
self.menu = cfg.main_menu.add_item("Apps", "icon-download-alt", "/apps", 80)
|
||||
self.menu.add_item("Chat", "icon-comment", "/../jwchat", 30)
|
||||
|
||||
@cherrypy.expose
|
||||
def index(self):
|
||||
return util.render_template(template='apps',
|
||||
title=_('User Applications'))
|
||||
def index(request):
|
||||
"""Serve the apps index page"""
|
||||
return TemplateResponse(request, 'apps.html', {'title': _('Applications')})
|
||||
@@ -20,6 +20,8 @@ Plinth module for basic system configuration
|
||||
"""
|
||||
|
||||
from . import config
|
||||
from .config import init
|
||||
|
||||
__all__ = ['config', 'init']
|
||||
|
||||
__all__ = ['config']
|
||||
DEPENDS = ['system']
|
||||
+57
-57
@@ -19,17 +19,16 @@
|
||||
Plinth module for configuring timezone, hostname etc.
|
||||
"""
|
||||
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from django.core import validators
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
import re
|
||||
import socket
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
from ..lib.auth import require
|
||||
from plugin_mount import PagePlugin
|
||||
from ..lib.auth import login_required
|
||||
import util
|
||||
|
||||
|
||||
@@ -89,66 +88,66 @@ and must not be greater than 63 characters in length.'),
|
||||
return time_zones
|
||||
|
||||
|
||||
class Configuration(PagePlugin):
|
||||
"""System configuration page"""
|
||||
def __init__(self):
|
||||
super(Configuration, self).__init__()
|
||||
def init():
|
||||
"""Initialize the module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item(_('Configure'), 'icon-cog', '/sys/config', 10)
|
||||
|
||||
self.register_page('sys.config')
|
||||
|
||||
self.menu = cfg.html_root.sys.menu.add_item(_('Configure'), 'icon-cog',
|
||||
'/sys/config', 10)
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Serve the configuration form"""
|
||||
status = get_status()
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, **kwargs):
|
||||
"""Serve the configuration form"""
|
||||
status = self.get_status()
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
form = None
|
||||
messages = []
|
||||
is_expert = cfg.users.expert(request=request)
|
||||
if request.method == 'POST' and is_expert:
|
||||
form = ConfigurationForm(request.POST, prefix='configuration')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
_apply_changes(status, form.cleaned_data, messages)
|
||||
status = get_status()
|
||||
form = ConfigurationForm(initial=status,
|
||||
prefix='configuration')
|
||||
else:
|
||||
form = ConfigurationForm(initial=status, prefix='configuration')
|
||||
|
||||
if kwargs and cfg.users.expert():
|
||||
form = ConfigurationForm(kwargs, prefix='configuration')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._apply_changes(status, form.cleaned_data, messages)
|
||||
status = self.get_status()
|
||||
form = ConfigurationForm(initial=status,
|
||||
prefix='configuration')
|
||||
return TemplateResponse(request, 'config.html',
|
||||
{'title': _('General Configuration'),
|
||||
'form': form,
|
||||
'messages_': messages,
|
||||
'is_expert': is_expert})
|
||||
|
||||
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
return {'hostname': get_hostname(),
|
||||
'time_zone': util.slurp('/etc/timezone').rstrip()}
|
||||
|
||||
|
||||
def _apply_changes(old_status, new_status, messages):
|
||||
"""Apply the form changes"""
|
||||
if old_status['hostname'] != new_status['hostname']:
|
||||
if not set_hostname(new_status['hostname']):
|
||||
messages.append(('error', _('Setting hostname failed')))
|
||||
else:
|
||||
form = ConfigurationForm(initial=status, prefix='configuration')
|
||||
|
||||
return util.render_template(template='config',
|
||||
title=_('General Configuration'),
|
||||
form=form, messages=messages)
|
||||
|
||||
@staticmethod
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
return {'hostname': get_hostname(),
|
||||
'time_zone': util.slurp('/etc/timezone').rstrip()}
|
||||
|
||||
@staticmethod
|
||||
def _apply_changes(old_status, new_status, messages):
|
||||
"""Apply the form changes"""
|
||||
if old_status['hostname'] != new_status['hostname']:
|
||||
set_hostname(new_status['hostname'])
|
||||
messages.append(('success', _('Hostname set')))
|
||||
else:
|
||||
messages.append(('info', _('Hostname is unchanged')))
|
||||
else:
|
||||
messages.append(('info', _('Hostname is unchanged')))
|
||||
|
||||
if old_status['time_zone'] != new_status['time_zone']:
|
||||
output, error = actions.superuser_run('timezone-change',
|
||||
[new_status['time_zone']])
|
||||
del output # Unused
|
||||
if error:
|
||||
messages.append(('error',
|
||||
_('Error setting time zone - %s') % error))
|
||||
else:
|
||||
messages.append(('success', _('Time zone set')))
|
||||
if old_status['time_zone'] != new_status['time_zone']:
|
||||
output, error = actions.superuser_run('timezone-change',
|
||||
[new_status['time_zone']])
|
||||
del output # Unused
|
||||
if error:
|
||||
messages.append(('error',
|
||||
_('Error setting time zone - %s') % error))
|
||||
else:
|
||||
messages.append(('info', _('Time zone is unchanged')))
|
||||
messages.append(('success', _('Time zone set')))
|
||||
else:
|
||||
messages.append(('info', _('Time zone is unchanged')))
|
||||
|
||||
|
||||
def set_hostname(hostname):
|
||||
@@ -165,6 +164,7 @@ def set_hostname(hostname):
|
||||
# don't persist/cache change unless it was saved successfuly
|
||||
sys_store = util.filedict_con(cfg.store_file, 'sys')
|
||||
sys_store['hostname'] = hostname
|
||||
except OSError as exception:
|
||||
raise cherrypy.HTTPError(500,
|
||||
'Updating hostname failed: %s' % exception)
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
return True
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
{% block main_block %}
|
||||
|
||||
{% if cfg.users.expert %}
|
||||
{% if is_expert %}
|
||||
|
||||
{% include 'messages.html' %}
|
||||
|
||||
|
||||
@@ -16,10 +16,12 @@
|
||||
#
|
||||
|
||||
"""
|
||||
Plinth module to system diagnostics
|
||||
Plinth module for system diagnostics
|
||||
"""
|
||||
|
||||
from . import diagnostics
|
||||
from .diagnostics import init
|
||||
|
||||
__all__ = ['diagnostics', 'init']
|
||||
|
||||
__all__ = ['diagnostics']
|
||||
DEPENDS = ['system']
|
||||
@@ -19,41 +19,32 @@
|
||||
Plinth module for running diagnostics
|
||||
"""
|
||||
|
||||
import cherrypy
|
||||
from gettext import gettext as _
|
||||
from ..lib.auth import require
|
||||
from plugin_mount import PagePlugin
|
||||
from django.template.response import TemplateResponse
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
import util
|
||||
from ..lib.auth import login_required
|
||||
|
||||
|
||||
class Diagnostics(PagePlugin):
|
||||
order = 30
|
||||
def __init__(self):
|
||||
super(Diagnostics, self).__init__()
|
||||
def init():
|
||||
"""Initialize the module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item("Diagnostics", "icon-screenshot", "/sys/diagnostics", 30)
|
||||
|
||||
self.register_page("sys.diagnostics")
|
||||
cfg.html_root.sys.menu.add_item("Diagnostics", "icon-screenshot", "/sys/diagnostics", 30)
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self):
|
||||
return util.render_template(template='diagnostics',
|
||||
title=_('System Diagnostics'))
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Serve the index page"""
|
||||
return TemplateResponse(request, 'diagnostics.html',
|
||||
{'title': _('System Diagnostics')})
|
||||
|
||||
class Test(PagePlugin):
|
||||
order = 31
|
||||
def __init__(self):
|
||||
super(Test, self).__init__()
|
||||
|
||||
self.register_page("sys.diagnostics.test")
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self):
|
||||
output, error = actions.superuser_run("diagnostic-test")
|
||||
return util.render_template(template='diagnostics_test',
|
||||
title=_('Diagnostic Test'),
|
||||
diagnostics_output=output,
|
||||
diagnostics_error=error)
|
||||
@login_required
|
||||
def test(request):
|
||||
"""Run diagnostics and the output page"""
|
||||
output, error = actions.superuser_run("diagnostic-test")
|
||||
return TemplateResponse(request, 'diagnostics_test.html',
|
||||
{'title': _('Diagnostic Test'),
|
||||
'diagnostics_output': output,
|
||||
'diagnostics_error': error})
|
||||
@@ -20,6 +20,8 @@ Plinth module for expert mode configuration
|
||||
"""
|
||||
|
||||
from . import expert_mode
|
||||
from .expert_mode import init
|
||||
|
||||
__all__ = ['expert_mode', 'init']
|
||||
|
||||
__all__ = ['expert_mode']
|
||||
DEPENDS = ['system']
|
||||
@@ -1,80 +1,66 @@
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
from ..lib.auth import require
|
||||
from plugin_mount import PagePlugin
|
||||
|
||||
import cfg
|
||||
import util
|
||||
from ..lib.auth import login_required
|
||||
|
||||
|
||||
class ExpertsForm(forms.Form): # pylint: disable-msg=W0232
|
||||
"""Form to configure expert mode"""
|
||||
|
||||
expert_mode = forms.BooleanField(
|
||||
label=_('Expert Mode'), required=False)
|
||||
|
||||
# XXX: Only present due to issue with submitting empty form
|
||||
dummy = forms.CharField(label='Dummy', initial='dummy',
|
||||
widget=forms.HiddenInput())
|
||||
|
||||
def init():
|
||||
"""Initialize the module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item(_('Expert Mode'), 'icon-cog', '/sys/expert', 10)
|
||||
|
||||
|
||||
class Experts(PagePlugin):
|
||||
"""Expert forms page"""
|
||||
order = 60
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Serve the configuration form"""
|
||||
status = get_status(request)
|
||||
|
||||
def __init__(self):
|
||||
super(Experts, self).__init__()
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
self.register_page('sys.expert')
|
||||
|
||||
cfg.html_root.sys.menu.add_item(_('Expert Mode'), 'icon-cog',
|
||||
'/sys/expert', 10)
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, **kwargs):
|
||||
"""Serve the configuration form"""
|
||||
status = self.get_status()
|
||||
|
||||
cfg.log.info('Args - %s' % kwargs)
|
||||
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
if kwargs:
|
||||
form = ExpertsForm(kwargs, prefix='experts')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._apply_changes(form.cleaned_data, messages)
|
||||
status = self.get_status()
|
||||
form = ExpertsForm(initial=status, prefix='experts')
|
||||
else:
|
||||
if request.method == 'POST':
|
||||
form = ExpertsForm(request.POST, prefix='experts')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
_apply_changes(request, form.cleaned_data, messages)
|
||||
status = get_status(request)
|
||||
form = ExpertsForm(initial=status, prefix='experts')
|
||||
else:
|
||||
form = ExpertsForm(initial=status, prefix='experts')
|
||||
|
||||
return util.render_template(template='expert_mode',
|
||||
title=_('Expert Mode'), form=form,
|
||||
messages=messages)
|
||||
return TemplateResponse(request, 'expert_mode.html',
|
||||
{'title': _('Expert Mode'),
|
||||
'form': form,
|
||||
'messages_': messages})
|
||||
|
||||
@staticmethod
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
return {'expert_mode': cfg.users.expert()}
|
||||
|
||||
@staticmethod
|
||||
def _apply_changes(new_status, messages):
|
||||
"""Apply expert mode configuration"""
|
||||
message = ('info', _('Settings unchanged'))
|
||||
def get_status(request):
|
||||
"""Return the current status"""
|
||||
return {'expert_mode': cfg.users.expert(request=request)}
|
||||
|
||||
user = cfg.users.current()
|
||||
|
||||
if new_status['expert_mode']:
|
||||
if not 'expert' in user['groups']:
|
||||
user['groups'].append('expert')
|
||||
message = ('success', _('Expert mode enabled'))
|
||||
else:
|
||||
if 'expert' in user['groups']:
|
||||
user['groups'].remove('expert')
|
||||
message = ('success', _('Expert mode disabled'))
|
||||
def _apply_changes(request, new_status, messages):
|
||||
"""Apply expert mode configuration"""
|
||||
message = ('info', _('Settings unchanged'))
|
||||
|
||||
cfg.users.set(user['username'], user)
|
||||
messages.append(message)
|
||||
user = cfg.users.current(request=request)
|
||||
|
||||
if new_status['expert_mode']:
|
||||
if not 'expert' in user['groups']:
|
||||
user['groups'].append('expert')
|
||||
message = ('success', _('Expert mode enabled'))
|
||||
else:
|
||||
if 'expert' in user['groups']:
|
||||
user['groups'].remove('expert')
|
||||
message = ('success', _('Expert mode disabled'))
|
||||
|
||||
cfg.users.set(user['username'], user)
|
||||
messages.append(message)
|
||||
@@ -20,6 +20,8 @@ Plinth module to configure a firewall
|
||||
"""
|
||||
|
||||
from . import firewall
|
||||
from .firewall import init
|
||||
|
||||
__all__ = ['firewall', 'init']
|
||||
|
||||
__all__ = ['firewall']
|
||||
DEPENDS = ['system']
|
||||
+104
-106
@@ -19,136 +19,134 @@
|
||||
Plinth module to configure a firewall
|
||||
"""
|
||||
|
||||
import cherrypy
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
from ..lib.auth import require
|
||||
from plugin_mount import PagePlugin
|
||||
from ..lib.auth import login_required
|
||||
import service as service_module
|
||||
import util
|
||||
|
||||
|
||||
class Firewall(PagePlugin):
|
||||
"""Firewall menu entry and introduction page"""
|
||||
order = 40
|
||||
def init():
|
||||
"""Initailze firewall module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item(_('Firewall'), 'icon-flag', '/sys/firewall', 50)
|
||||
|
||||
def __init__(self):
|
||||
super(Firewall, self).__init__()
|
||||
service_module.ENABLED.connect(on_service_enabled)
|
||||
|
||||
self.register_page('sys.firewall')
|
||||
cfg.html_root.sys.menu.add_item(_('Firewall'), 'icon-flag',
|
||||
'/sys/firewall', 50)
|
||||
|
||||
service_module.ENABLED.connect(self.on_service_enabled)
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Serve introcution page"""
|
||||
if not get_installed_status():
|
||||
return TemplateResponse(request, 'firewall.html',
|
||||
{'title': _('Firewall'),
|
||||
'firewall_status': 'not_installed'})
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self):
|
||||
"""Serve introcution page"""
|
||||
if not self.get_installed_status():
|
||||
return util.render_template(template='firewall',
|
||||
title=_("Firewall"),
|
||||
firewall_status='not_installed')
|
||||
if not get_enabled_status():
|
||||
return TemplateResponse(request, 'firewall.html',
|
||||
{'title': _('Firewall'),
|
||||
'firewall_status': 'not_running'})
|
||||
|
||||
if not self.get_enabled_status():
|
||||
return util.render_template(template='firewall',
|
||||
title=_("Firewall"),
|
||||
firewall_status='not_running')
|
||||
internal_enabled_services = get_enabled_services(zone='internal')
|
||||
external_enabled_services = get_enabled_services(zone='external')
|
||||
|
||||
internal_enabled_services = self.get_enabled_services(zone='internal')
|
||||
external_enabled_services = self.get_enabled_services(zone='external')
|
||||
return TemplateResponse(
|
||||
request, 'firewall.html',
|
||||
{'title': _('Firewall'),
|
||||
'services': service_module.SERVICES.values(),
|
||||
'internal_enabled_services': internal_enabled_services,
|
||||
'external_enabled_services': external_enabled_services})
|
||||
|
||||
return util.render_template(
|
||||
template='firewall', title=_('Firewall'),
|
||||
services=service_module.SERVICES.values(),
|
||||
internal_enabled_services=internal_enabled_services,
|
||||
external_enabled_services=external_enabled_services)
|
||||
|
||||
def get_installed_status(self):
|
||||
"""Return whether firewall is installed"""
|
||||
output = self._run(['get-installed'], superuser=True)
|
||||
return output.split()[0] == 'installed'
|
||||
def get_installed_status():
|
||||
"""Return whether firewall is installed"""
|
||||
output = _run(['get-installed'], superuser=True)
|
||||
return output.split()[0] == 'installed'
|
||||
|
||||
def get_enabled_status(self):
|
||||
"""Return whether firewall is installed"""
|
||||
output = self._run(['get-status'], superuser=True)
|
||||
return output.split()[0] == 'running'
|
||||
|
||||
def get_enabled_services(self, zone):
|
||||
"""Return the status of various services currently enabled"""
|
||||
output = self._run(['get-enabled-services', '--zone', zone],
|
||||
superuser=True)
|
||||
return output.split()
|
||||
def get_enabled_status():
|
||||
"""Return whether firewall is installed"""
|
||||
output = _run(['get-status'], superuser=True)
|
||||
return output.split()[0] == 'running'
|
||||
|
||||
def add_service(self, port, zone):
|
||||
"""Enable a service in firewall"""
|
||||
self._run(['add-service', port, '--zone', zone], superuser=True)
|
||||
|
||||
def remove_service(self, port, zone):
|
||||
"""Remove a service in firewall"""
|
||||
self._run(['remove-service', port, '--zone', zone], superuser=True)
|
||||
def get_enabled_services(zone):
|
||||
"""Return the status of various services currently enabled"""
|
||||
output = _run(['get-enabled-services', '--zone', zone], superuser=True)
|
||||
return output.split()
|
||||
|
||||
def on_service_enabled(self, sender, service_id, enabled, **kwargs):
|
||||
"""
|
||||
Enable/disable firewall ports when a service is
|
||||
enabled/disabled.
|
||||
"""
|
||||
del sender # Unused
|
||||
del kwargs # Unused
|
||||
|
||||
internal_enabled_services = self.get_enabled_services(zone='internal')
|
||||
external_enabled_services = self.get_enabled_services(zone='external')
|
||||
def add_service(port, zone):
|
||||
"""Enable a service in firewall"""
|
||||
_run(['add-service', port, '--zone', zone], superuser=True)
|
||||
|
||||
cfg.log.info('Service enabled - %s, %s' % (service_id, enabled))
|
||||
service = service_module.SERVICES[service_id]
|
||||
for port in service.ports:
|
||||
if enabled:
|
||||
if port not in internal_enabled_services:
|
||||
self.add_service(port, zone='internal')
|
||||
|
||||
if (service.is_external and
|
||||
port not in external_enabled_services):
|
||||
self.add_service(port, zone='external')
|
||||
else:
|
||||
# service already configured.
|
||||
pass
|
||||
def remove_service(port, zone):
|
||||
"""Remove a service in firewall"""
|
||||
_run(['remove-service', port, '--zone', zone], superuser=True)
|
||||
|
||||
|
||||
def on_service_enabled(sender, service_id, enabled, **kwargs):
|
||||
"""
|
||||
Enable/disable firewall ports when a service is
|
||||
enabled/disabled.
|
||||
"""
|
||||
del sender # Unused
|
||||
del kwargs # Unused
|
||||
|
||||
internal_enabled_services = get_enabled_services(zone='internal')
|
||||
external_enabled_services = get_enabled_services(zone='external')
|
||||
|
||||
cfg.log.info('Service enabled - %s, %s' % (service_id, enabled))
|
||||
service = service_module.SERVICES[service_id]
|
||||
for port in service.ports:
|
||||
if enabled:
|
||||
if port not in internal_enabled_services:
|
||||
add_service(port, zone='internal')
|
||||
|
||||
if (service.is_external and
|
||||
port not in external_enabled_services):
|
||||
add_service(port, zone='external')
|
||||
else:
|
||||
if port in internal_enabled_services:
|
||||
enabled_services_on_port = [
|
||||
service_.is_enabled()
|
||||
for service_ in service_module.SERVICES.values()
|
||||
if port in service_.ports and
|
||||
service_id != service_.service_id]
|
||||
if not any(enabled_services_on_port):
|
||||
self.remove_service(port, zone='internal')
|
||||
|
||||
if port in external_enabled_services:
|
||||
enabled_services_on_port = [
|
||||
service_.is_enabled()
|
||||
for service_ in service_module.SERVICES.values()
|
||||
if port in service_.ports and
|
||||
service_id != service_.service_id and
|
||||
service_.is_external]
|
||||
if not any(enabled_services_on_port):
|
||||
self.remove_service(port, zone='external')
|
||||
|
||||
@staticmethod
|
||||
def _run(arguments, superuser=False):
|
||||
"""Run an given command and raise exception if there was an error"""
|
||||
command = 'firewall'
|
||||
|
||||
cfg.log.info('Running command - %s, %s, %s' % (command, arguments,
|
||||
superuser))
|
||||
|
||||
if superuser:
|
||||
output, error = actions.superuser_run(command, arguments)
|
||||
# service already configured.
|
||||
pass
|
||||
else:
|
||||
output, error = actions.run(command, arguments)
|
||||
if port in internal_enabled_services:
|
||||
enabled_services_on_port = [
|
||||
service_.is_enabled()
|
||||
for service_ in service_module.SERVICES.values()
|
||||
if port in service_.ports and
|
||||
service_id != service_.service_id]
|
||||
if not any(enabled_services_on_port):
|
||||
remove_service(port, zone='internal')
|
||||
|
||||
if error:
|
||||
raise Exception('Error setting/getting firewalld confguration - %s'
|
||||
% error)
|
||||
if port in external_enabled_services:
|
||||
enabled_services_on_port = [
|
||||
service_.is_enabled()
|
||||
for service_ in service_module.SERVICES.values()
|
||||
if port in service_.ports and
|
||||
service_id != service_.service_id and
|
||||
service_.is_external]
|
||||
if not any(enabled_services_on_port):
|
||||
remove_service(port, zone='external')
|
||||
|
||||
return output
|
||||
|
||||
def _run(arguments, superuser=False):
|
||||
"""Run an given command and raise exception if there was an error"""
|
||||
command = 'firewall'
|
||||
|
||||
cfg.log.info('Running command - %s, %s, %s' % (command, arguments,
|
||||
superuser))
|
||||
|
||||
if superuser:
|
||||
output, error = actions.superuser_run(command, arguments)
|
||||
else:
|
||||
output, error = actions.run(command, arguments)
|
||||
|
||||
if error:
|
||||
raise Exception('Error setting/getting firewalld confguration - %s'
|
||||
% error)
|
||||
|
||||
return output
|
||||
+103
-111
@@ -18,16 +18,16 @@ The Plinth first-connection process has several stages:
|
||||
4. The user interacts with the box normally.
|
||||
"""
|
||||
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from django.core import validators
|
||||
from django.http.response import HttpResponseRedirect
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
from plugin_mount import PagePlugin
|
||||
|
||||
from ..lib.auth import add_user
|
||||
from ..config import config
|
||||
from withsqlite.withsqlite import sqlite_db
|
||||
import cfg
|
||||
import util
|
||||
|
||||
|
||||
## TODO: flesh out these tests values
|
||||
@@ -66,132 +66,124 @@ below. This key should not be the same as your key because you are not your \
|
||||
FreedomBox!'))
|
||||
|
||||
|
||||
class FirstBoot(PagePlugin):
|
||||
"""First boot wizard"""
|
||||
def index(request):
|
||||
"""Serve the index first boot page"""
|
||||
return state0(request)
|
||||
|
||||
def __init__(self):
|
||||
super(FirstBoot, self).__init__()
|
||||
|
||||
# this is the url this page will hang off of (/firstboot)
|
||||
self.register_page('firstboot')
|
||||
self.register_page('firstboot/state0')
|
||||
self.register_page('firstboot/state1')
|
||||
def generate_box_key():
|
||||
"""Generate a box key"""
|
||||
return "fake key"
|
||||
|
||||
@cherrypy.expose
|
||||
def index(self, *args, **kwargs):
|
||||
return self.state0(*args, **kwargs)
|
||||
|
||||
def generate_box_key(self):
|
||||
return "fake key"
|
||||
def state0(request):
|
||||
"""
|
||||
In this state, we do time config over HTTP, name the box and
|
||||
server key selection.
|
||||
|
||||
@cherrypy.expose
|
||||
def state0(self, **kwargs):
|
||||
"""
|
||||
In this state, we do time config over HTTP, name the box and
|
||||
server key selection.
|
||||
All the parameters are form inputs. They get passed in when
|
||||
the form is submitted. This method checks the inputs and if
|
||||
they validate, uses them to take action. If they do not
|
||||
validate, it displays the form to give the user a chance to
|
||||
input correct values. It might display an error message (in
|
||||
the message parameter).
|
||||
|
||||
All the parameters are form inputs. They get passed in when
|
||||
the form is submitted. This method checks the inputs and if
|
||||
they validate, uses them to take action. If they do not
|
||||
validate, it displays the form to give the user a chance to
|
||||
input correct values. It might display an error message (in
|
||||
the message parameter).
|
||||
message is an optional string that we can display to the
|
||||
user. It's a good place to put error messages.
|
||||
"""
|
||||
try:
|
||||
if _read_state() >= 5:
|
||||
return HttpResponseRedirect(cfg.server_dir)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
message is an optional string that we can display to the
|
||||
user. It's a good place to put error messages.
|
||||
"""
|
||||
try:
|
||||
if FirstBoot._read_state() >= 5:
|
||||
raise cherrypy.HTTPRedirect(cfg.server_dir, 302)
|
||||
except KeyError:
|
||||
pass
|
||||
## Until LDAP is in place, we'll put the box key in the cfg.store_file
|
||||
status = get_state0()
|
||||
|
||||
## Until LDAP is in place, we'll put the box key in the cfg.store_file
|
||||
status = self.get_state0()
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
form = None
|
||||
messages = []
|
||||
if request.method == 'POST':
|
||||
form = State0Form(request.POST, prefix='firstboot')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
success = _apply_state0(status, form.cleaned_data, messages)
|
||||
|
||||
if kwargs:
|
||||
form = State0Form(kwargs, prefix='firstboot')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
success = self._apply_state0(status, form.cleaned_data,
|
||||
messages)
|
||||
if success:
|
||||
# Everything is good, permanently mark and move to page 2
|
||||
_write_state(1)
|
||||
return HttpResponseRedirect(
|
||||
cfg.server_dir + '/firstboot/state1')
|
||||
else:
|
||||
form = State0Form(initial=status, prefix='firstboot')
|
||||
|
||||
if success:
|
||||
# Everything is good, permanently mark and move to page 2
|
||||
FirstBoot._write_state(1)
|
||||
raise cherrypy.HTTPRedirect('state1', 302)
|
||||
return TemplateResponse(request, 'firstboot_state0.html',
|
||||
{'title': _('First Boot!'),
|
||||
'form': form,
|
||||
'messages_': messages})
|
||||
|
||||
|
||||
def get_state0():
|
||||
"""Return the state for form state 0"""
|
||||
with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as \
|
||||
database:
|
||||
return {'hostname': config.get_hostname(),
|
||||
'box_key': database.get('box_key', None)}
|
||||
|
||||
|
||||
def _apply_state0(old_state, new_state, messages):
|
||||
"""Apply changes in state 0 form"""
|
||||
success = True
|
||||
with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as \
|
||||
database:
|
||||
database['about'] = 'Information about this FreedomBox'
|
||||
|
||||
if new_state['box_key']:
|
||||
database['box_key'] = new_state['box_key']
|
||||
elif not old_state['box_key']:
|
||||
database['box_key'] = generate_box_key()
|
||||
|
||||
if old_state['hostname'] != new_state['hostname']:
|
||||
config.set_hostname(new_state['hostname'])
|
||||
|
||||
error = add_user(new_state['username'], new_state['password'],
|
||||
'First user, please change', '', True)
|
||||
if error:
|
||||
messages.append(
|
||||
('error', _('User account creation failed: %s') % error))
|
||||
success = False
|
||||
else:
|
||||
form = State0Form(initial=status, prefix='firstboot')
|
||||
messages.append(('success', _('User account created')))
|
||||
|
||||
return util.render_template(template='firstboot_state0',
|
||||
title=_('First Boot!'), form=form,
|
||||
messages=messages)
|
||||
return success
|
||||
|
||||
@staticmethod
|
||||
def get_state0():
|
||||
"""Return the state for form state 0"""
|
||||
with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as \
|
||||
database:
|
||||
return {'hostname': config.get_hostname(),
|
||||
'box_key': database.get('box_key', None)}
|
||||
|
||||
def _apply_state0(self, old_state, new_state, messages):
|
||||
"""Apply changes in state 0 form"""
|
||||
success = True
|
||||
with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as \
|
||||
database:
|
||||
database['about'] = 'Information about this FreedomBox'
|
||||
def state1(request):
|
||||
"""
|
||||
State 1 is when we have a box name and key. In this state,
|
||||
our task is to provide a certificate and maybe to guide the
|
||||
user through installing it. We automatically move to State 2,
|
||||
which is an HTTPS connection.
|
||||
|
||||
if new_state['box_key']:
|
||||
database['box_key'] = new_state['box_key']
|
||||
elif not old_state['box_key']:
|
||||
database['box_key'] = self.generate_box_key()
|
||||
TODO: HTTPS failure in State 2 should returns to state 1.
|
||||
"""
|
||||
# TODO complete first_boot handling
|
||||
# Make sure the user is not stuck on a dead end for now.
|
||||
_write_state(5)
|
||||
|
||||
if old_state['hostname'] != new_state['hostname']:
|
||||
config.set_hostname(new_state['hostname'])
|
||||
return TemplateResponse(request, 'firstboot_state1.html',
|
||||
{'title': _('Installing the Certificate')})
|
||||
|
||||
error = add_user(new_state['username'], new_state['password'],
|
||||
'First user, please change', '', True)
|
||||
if error:
|
||||
messages.append(
|
||||
('error', _('User account creation failed: %s') % error))
|
||||
success = False
|
||||
else:
|
||||
messages.append(('success', _('User account created')))
|
||||
|
||||
return success
|
||||
def _read_state():
|
||||
"""Read the current state from database"""
|
||||
with sqlite_db(cfg.store_file, table='firstboot',
|
||||
autocommit=True) as database:
|
||||
return database['state']
|
||||
|
||||
@staticmethod
|
||||
@cherrypy.expose
|
||||
def state1():
|
||||
"""
|
||||
State 1 is when we have a box name and key. In this state,
|
||||
our task is to provide a certificate and maybe to guide the
|
||||
user through installing it. We automatically move to State 2,
|
||||
which is an HTTPS connection.
|
||||
|
||||
TODO: HTTPS failure in State 2 should returns to state 1.
|
||||
"""
|
||||
# TODO complete first_boot handling
|
||||
# Make sure the user is not stuck on a dead end for now.
|
||||
FirstBoot._write_state(5)
|
||||
|
||||
return util.render_template(template='firstboot_state1',
|
||||
title=_('Installing the Certificate'))
|
||||
|
||||
@staticmethod
|
||||
def _read_state():
|
||||
"""Read the current state from database"""
|
||||
with sqlite_db(cfg.store_file, table='firstboot',
|
||||
autocommit=True) as database:
|
||||
return database['state']
|
||||
|
||||
@staticmethod
|
||||
def _write_state(state):
|
||||
"""Write state to database"""
|
||||
with sqlite_db(cfg.store_file, table='firstboot',
|
||||
autocommit=True) as database:
|
||||
database['state'] = state
|
||||
def _write_state(state):
|
||||
"""Write state to database"""
|
||||
with sqlite_db(cfg.store_file, table='firstboot',
|
||||
autocommit=True) as database:
|
||||
database['state'] = state
|
||||
@@ -20,6 +20,6 @@ Plinth module for help pages
|
||||
"""
|
||||
|
||||
from . import help # pylint: disable-msg=W0622
|
||||
from .help import init
|
||||
|
||||
|
||||
__all__ = ['help']
|
||||
__all__ = ['help', 'init']
|
||||
+30
-39
@@ -1,49 +1,40 @@
|
||||
import os
|
||||
import cherrypy
|
||||
from gettext import gettext as _
|
||||
from plugin_mount import PagePlugin
|
||||
from django.template.response import TemplateResponse
|
||||
|
||||
import cfg
|
||||
import util
|
||||
|
||||
|
||||
class Help(PagePlugin):
|
||||
order = 20 # order of running init in PagePlugins
|
||||
|
||||
def __init__(self):
|
||||
super(Help, self).__init__()
|
||||
|
||||
self.register_page("help")
|
||||
self.menu = cfg.main_menu.add_item(_("Documentation"), "icon-book", "/help", 101)
|
||||
self.menu.add_item(_("Where to Get Help"), "icon-search", "/help/index", 5)
|
||||
self.menu.add_item(_("Developer's Manual"), "icon-info-sign", "/help/view/plinth", 10)
|
||||
self.menu.add_item(_("FAQ"), "icon-question-sign", "/help/view/faq", 20)
|
||||
self.menu.add_item(_("%s Wiki" % cfg.box_name), "icon-pencil", "http://wiki.debian.org/FreedomBox", 30)
|
||||
self.menu.add_item(_("About"), "icon-star", "/help/about", 100)
|
||||
|
||||
@cherrypy.expose
|
||||
def index(self):
|
||||
return util.render_template(template='help',
|
||||
title=_('Documentation and FAQ'))
|
||||
|
||||
@cherrypy.expose
|
||||
def about(self):
|
||||
return util.render_template(
|
||||
template='about',
|
||||
title=_('About the {box_name}').format(box_name=cfg.box_name))
|
||||
def init():
|
||||
"""Initialize the Help module"""
|
||||
menu = cfg.main_menu.add_item(_('Documentation'), 'icon-book', '/help',
|
||||
101)
|
||||
menu.add_item(_("Where to Get Help"), "icon-search", "/help/index", 5)
|
||||
menu.add_item(_('Developer\'s Manual'), 'icon-info-sign',
|
||||
'/help/view/plinth', 10)
|
||||
menu.add_item(_('FAQ'), 'icon-question-sign', '/help/view/faq', 20)
|
||||
menu.add_item(_('%s Wiki' % cfg.box_name), 'icon-pencil',
|
||||
'http://wiki.debian.org/FreedomBox', 30)
|
||||
menu.add_item(_('About'), 'icon-star', '/help/about', 100)
|
||||
|
||||
|
||||
class View(PagePlugin):
|
||||
def __init__(self):
|
||||
super(View, self).__init__()
|
||||
def index(request):
|
||||
"""Serve the index page"""
|
||||
return TemplateResponse(request, 'help.html',
|
||||
{'title': _('Documentation and FAQ')})
|
||||
|
||||
self.register_page("help.view")
|
||||
|
||||
@cherrypy.expose
|
||||
def default(self, page=''):
|
||||
if page not in ['design', 'plinth', 'hacking', 'faq']:
|
||||
raise cherrypy.HTTPError(404, "The path '/help/view/%s' was not found." % page)
|
||||
def about(request):
|
||||
"""Serve the about page"""
|
||||
title = _('About the {box_name}').format(box_name=cfg.box_name)
|
||||
return TemplateResponse(request, 'about.html', {'title': title})
|
||||
|
||||
with open(os.path.join("doc", "%s.part.html" % page), 'r') as IF:
|
||||
main = IF.read()
|
||||
return util.render_template(title=_("%s Documentation" %
|
||||
cfg.product_name), main=main)
|
||||
|
||||
def default(request, page=''):
|
||||
"""Serve the documentation pages"""
|
||||
with open(os.path.join('doc', '%s.part.html' % page), 'r') as input_file:
|
||||
main = input_file.read()
|
||||
|
||||
title = _('%s Documentation') % cfg.product_name
|
||||
return TemplateResponse(request, 'login_nav.html',
|
||||
{'title': title, 'main': main})
|
||||
+18
-83
@@ -1,20 +1,13 @@
|
||||
# Form based authentication for CherryPy. Requires the
|
||||
# Session tool to be loaded.
|
||||
#
|
||||
# Thanks for this code is owed to Arnar Birgisson -at - gmail.com. It
|
||||
# is based on code he wrote that was retrieved from
|
||||
# http://tools.cherrypy.org/wiki/AuthenticationAndAccessRestrictions
|
||||
# on 1 February 2011.
|
||||
|
||||
import cherrypy
|
||||
import urllib
|
||||
from django.http.response import HttpResponseRedirect
|
||||
import functools
|
||||
from passlib.hash import bcrypt
|
||||
from passlib.exc import PasswordSizeError
|
||||
|
||||
import cfg
|
||||
import random
|
||||
from model import User
|
||||
|
||||
cfg.session_key = '_cp_username'
|
||||
cfg.session_key = '_username'
|
||||
|
||||
|
||||
def add_user(username, passphrase, name='', email='', expert=False):
|
||||
"""Add a new user with specified username and passphrase.
|
||||
@@ -49,6 +42,7 @@ def add_user(username, passphrase, name='', email='', expert=False):
|
||||
cfg.log(error)
|
||||
return error
|
||||
|
||||
|
||||
def check_credentials(username, passphrase):
|
||||
"""Verifies credentials for username and passphrase.
|
||||
|
||||
@@ -87,79 +81,20 @@ def check_credentials(username, passphrase):
|
||||
|
||||
if error:
|
||||
cfg.log(error)
|
||||
|
||||
return error
|
||||
|
||||
def check_auth(*args, **kwargs):
|
||||
"""A tool that looks in config for 'auth.require'. If found and it
|
||||
is not None, a login is required and the entry is evaluated as a
|
||||
list of conditions that the user must fulfill"""
|
||||
conditions = cherrypy.request.config.get('auth.require', None)
|
||||
# format GET params
|
||||
get_params = urllib.quote(cherrypy.request.request_line.split()[1])
|
||||
if conditions is not None:
|
||||
username = cherrypy.session.get(cfg.session_key)
|
||||
if username:
|
||||
cherrypy.request.login = username
|
||||
for condition in conditions:
|
||||
# A condition is just a callable that returns true or false
|
||||
if not condition():
|
||||
# Send old page as from_page parameter
|
||||
raise cherrypy.HTTPRedirect(cfg.server_dir + "/auth/login?from_page=%s" % get_params)
|
||||
else:
|
||||
# Send old page as from_page parameter
|
||||
raise cherrypy.HTTPRedirect(cfg.server_dir + "/auth/login?from_page=%s" % get_params)
|
||||
|
||||
cherrypy.tools.auth = cherrypy.Tool('before_handler', check_auth)
|
||||
|
||||
def require(*conditions):
|
||||
"""A decorator that appends conditions to the auth.require config
|
||||
variable."""
|
||||
def decorate(f):
|
||||
if not hasattr(f, '_cp_config'):
|
||||
f._cp_config = dict()
|
||||
if 'auth.require' not in f._cp_config:
|
||||
f._cp_config['auth.require'] = []
|
||||
f._cp_config['auth.require'].extend(conditions)
|
||||
return f
|
||||
return decorate
|
||||
|
||||
|
||||
# Conditions are callables that return True
|
||||
# if the user fulfills the conditions they define, False otherwise
|
||||
#
|
||||
# They can access the current username as cherrypy.request.login
|
||||
#
|
||||
# Define those at will however suits the application.
|
||||
|
||||
def member_of(groupname):
|
||||
def check():
|
||||
# replace with actual check if <username> is in <groupname>
|
||||
return cherrypy.request.login == 'joe' and groupname == 'admin'
|
||||
return check
|
||||
|
||||
def name_is(reqd_username):
|
||||
return lambda: reqd_username == cherrypy.request.login
|
||||
|
||||
# These might be handy
|
||||
|
||||
def any_of(*conditions):
|
||||
"""Returns True if any of the conditions match"""
|
||||
def check():
|
||||
for c in conditions:
|
||||
if c():
|
||||
return True
|
||||
return False
|
||||
return check
|
||||
|
||||
# By default all conditions are required, but this might still be
|
||||
# needed if you want to use it inside of an any_of(...) condition
|
||||
def all_of(*conditions):
|
||||
"""Returns True if all of the conditions match"""
|
||||
def check():
|
||||
for c in conditions:
|
||||
if not c():
|
||||
return False
|
||||
return True
|
||||
return check
|
||||
# XXX: Only required until we start using Django authentication system properly
|
||||
def login_required(func):
|
||||
"""A decorator to ensure that user is logged in before accessing a view"""
|
||||
@functools.wraps(func)
|
||||
def wrapper(request, *args, **kwargs):
|
||||
"""Check that user is logged in"""
|
||||
if not request.session.get(cfg.session_key, None):
|
||||
return HttpResponseRedirect(
|
||||
cfg.server_dir + "/auth/login?from_page=%s" % request.path)
|
||||
|
||||
return func(request, *args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
+31
-44
@@ -2,19 +2,17 @@
|
||||
Controller to provide login and logout actions
|
||||
"""
|
||||
|
||||
import cherrypy
|
||||
import cfg
|
||||
from django import forms
|
||||
from django.http.response import HttpResponseRedirect
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
from plugin_mount import PagePlugin
|
||||
import auth
|
||||
import util
|
||||
|
||||
from . import auth
|
||||
|
||||
|
||||
class LoginForm(forms.Form): # pylint: disable-msg=W0232
|
||||
"""Login form"""
|
||||
from_page = forms.CharField(widget=forms.HiddenInput(), required=False)
|
||||
|
||||
username = forms.CharField(label=_('Username'))
|
||||
password = forms.CharField(label=_('Passphrase'),
|
||||
widget=forms.PasswordInput())
|
||||
@@ -33,48 +31,37 @@ class LoginForm(forms.Form): # pylint: disable-msg=W0232
|
||||
return self.cleaned_data
|
||||
|
||||
|
||||
class AuthController(PagePlugin):
|
||||
"""Login and logout pages"""
|
||||
def login(request):
|
||||
"""Serve the login page"""
|
||||
form = None
|
||||
|
||||
def __init__(self):
|
||||
super(AuthController, self).__init__()
|
||||
if request.method == 'POST':
|
||||
form = LoginForm(request.POST, prefix='auth')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
username = form.cleaned_data['username']
|
||||
request.session[cfg.session_key] = username
|
||||
return HttpResponseRedirect(_get_from_page(request))
|
||||
else:
|
||||
form = LoginForm(prefix='auth')
|
||||
|
||||
self.register_page('auth')
|
||||
return TemplateResponse(request, 'form.html',
|
||||
{'title': _('Login'),
|
||||
'form': form,
|
||||
'submit_text': _('Login')})
|
||||
|
||||
def on_login(self, username):
|
||||
"""Called on successful login"""
|
||||
|
||||
def on_logout(self, username):
|
||||
"""Called on logout"""
|
||||
def logout(request):
|
||||
"""Logout and redirect to origin page"""
|
||||
try:
|
||||
del request.session[cfg.session_key]
|
||||
request.session.flush()
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
@cherrypy.expose
|
||||
def login(self, from_page=cfg.server_dir+"/", **kwargs):
|
||||
"""Serve the login page"""
|
||||
form = None
|
||||
return HttpResponseRedirect(_get_from_page(request))
|
||||
|
||||
if kwargs:
|
||||
form = LoginForm(kwargs, prefix='auth')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
username = form.cleaned_data['username']
|
||||
cherrypy.session[cfg.session_key] = username
|
||||
cherrypy.request.login = username
|
||||
self.on_login(username)
|
||||
raise cherrypy.HTTPRedirect(from_page or
|
||||
(cfg.server_dir + "/"))
|
||||
else:
|
||||
form = LoginForm(prefix='auth')
|
||||
|
||||
return util.render_template(template='form', title=_('Login'),
|
||||
form=form, submit_text=_('Login'))
|
||||
|
||||
@cherrypy.expose
|
||||
def logout(self, from_page=cfg.server_dir+"/"):
|
||||
sess = cherrypy.session
|
||||
username = sess.get(cfg.session_key, None)
|
||||
sess[cfg.session_key] = None
|
||||
if username:
|
||||
cherrypy.request.login = None
|
||||
self.on_logout(username)
|
||||
|
||||
raise cherrypy.HTTPRedirect(from_page or (cfg.server_dir + "/"))
|
||||
def _get_from_page(request):
|
||||
"""Return the 'from page' of a request"""
|
||||
return request.GET.get('from_page', cfg.server_dir + '/')
|
||||
+24
-14
@@ -1,4 +1,3 @@
|
||||
import cherrypy
|
||||
import cfg
|
||||
from model import User
|
||||
from plugin_mount import UserStoreModule
|
||||
@@ -16,25 +15,36 @@ class UserStore(UserStoreModule, sqlite_db):
|
||||
def close(self):
|
||||
self.__exit__(None,None,None)
|
||||
|
||||
def current(self, name=False):
|
||||
def current(self, request=None, name=False):
|
||||
"""Return current user, if there is one, else None.
|
||||
If name = True, return the username instead of the user."""
|
||||
try:
|
||||
username = cherrypy.session.get(cfg.session_key)
|
||||
if name:
|
||||
return username
|
||||
else:
|
||||
return self.get(username)
|
||||
except AttributeError:
|
||||
if not request:
|
||||
return None
|
||||
|
||||
def expert(self, username=None):
|
||||
|
||||
try:
|
||||
username = request.session[cfg.session_key]
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
if name:
|
||||
return username
|
||||
|
||||
return self.get(username)
|
||||
|
||||
def expert(self, username=None, request=None):
|
||||
"""Return whether the current or provided user is an expert"""
|
||||
if not username:
|
||||
username = self.current(name=True)
|
||||
groups = self.attr(username,"groups")
|
||||
if not request:
|
||||
return False
|
||||
|
||||
username = self.current(request=request, name=True)
|
||||
|
||||
groups = self.attr(username, 'groups')
|
||||
|
||||
if not groups:
|
||||
return False
|
||||
return 'expert' in groups
|
||||
|
||||
return 'expert' in groups
|
||||
|
||||
def attr(self, username=None, field=None):
|
||||
return self.get(username)[field]
|
||||
|
||||
@@ -20,6 +20,8 @@ Plinth module to configure ownCloud
|
||||
"""
|
||||
|
||||
from . import owncloud
|
||||
from .owncloud import init
|
||||
|
||||
__all__ = ['owncloud', 'init']
|
||||
|
||||
__all__ = ['owncloud']
|
||||
DEPENDS = ['apps']
|
||||
@@ -1,86 +1,81 @@
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
from ..lib.auth import require
|
||||
from plugin_mount import PagePlugin
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
from ..lib.auth import login_required
|
||||
import service
|
||||
import util
|
||||
|
||||
|
||||
SERVICE = None
|
||||
|
||||
|
||||
class OwnCloudForm(forms.Form): # pylint: disable-msg=W0232
|
||||
"""ownCloud configuration form"""
|
||||
enabled = forms.BooleanField(label=_('Enable ownCloud'), required=False)
|
||||
|
||||
# XXX: Only present due to issue with submitting empty form
|
||||
dummy = forms.CharField(label='Dummy', initial='dummy',
|
||||
widget=forms.HiddenInput())
|
||||
|
||||
def init():
|
||||
"""Initialize the ownCloud module"""
|
||||
menu = cfg.main_menu.find('/apps')
|
||||
menu.add_item('Owncloud', 'icon-picture', '/apps/owncloud', 35)
|
||||
|
||||
status = get_status()
|
||||
|
||||
global SERVICE # pylint: disable-msg=W0603
|
||||
SERVICE = service.Service('owncloud', _('ownCloud'), ['http', 'https'],
|
||||
is_external=True, enabled=status['enabled'])
|
||||
|
||||
|
||||
class OwnCloud(PagePlugin):
|
||||
"""ownCloud configuration page"""
|
||||
order = 90
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Serve the ownCloud configuration page"""
|
||||
status = get_status()
|
||||
|
||||
def __init__(self):
|
||||
super(OwnCloud, self).__init__()
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
self.register_page('apps.owncloud')
|
||||
|
||||
cfg.html_root.apps.menu.add_item('Owncloud', 'icon-picture',
|
||||
'/apps/owncloud', 35)
|
||||
|
||||
status = self.get_status()
|
||||
self.service = service.Service('owncloud', _('ownCloud'),
|
||||
['http', 'https'], is_external=True,
|
||||
enabled=status['enabled'])
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, **kwargs):
|
||||
"""Serve the ownCloud configuration page"""
|
||||
status = self.get_status()
|
||||
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
if kwargs:
|
||||
form = OwnCloudForm(kwargs, prefix='owncloud')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._apply_changes(status, form.cleaned_data, messages)
|
||||
status = self.get_status()
|
||||
form = OwnCloudForm(initial=status, prefix='owncloud')
|
||||
else:
|
||||
if request.method == 'POST':
|
||||
form = OwnCloudForm(request.POST, prefix='owncloud')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
_apply_changes(status, form.cleaned_data, messages)
|
||||
status = get_status()
|
||||
form = OwnCloudForm(initial=status, prefix='owncloud')
|
||||
else:
|
||||
form = OwnCloudForm(initial=status, prefix='owncloud')
|
||||
|
||||
return util.render_template(template='owncloud', title=_('ownCloud'),
|
||||
form=form, messages=messages)
|
||||
return TemplateResponse(request, 'owncloud.html',
|
||||
{'title': _('ownCloud'),
|
||||
'form': form,
|
||||
'messages_': messages})
|
||||
|
||||
@staticmethod
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
output, error = actions.run('owncloud-setup', 'status')
|
||||
if error:
|
||||
raise Exception('Error getting ownCloud status: %s' % error)
|
||||
|
||||
return {'enabled': 'enable' in output.split()}
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
output, error = actions.run('owncloud-setup', 'status')
|
||||
if error:
|
||||
raise Exception('Error getting ownCloud status: %s' % error)
|
||||
|
||||
def _apply_changes(self, old_status, new_status, messages):
|
||||
"""Apply the changes"""
|
||||
if old_status['enabled'] == new_status['enabled']:
|
||||
messages.append(('info', _('Setting unchanged')))
|
||||
return
|
||||
return {'enabled': 'enable' in output.split()}
|
||||
|
||||
if new_status['enabled']:
|
||||
messages.append(('success', _('ownCloud enabled')))
|
||||
option = 'enable'
|
||||
else:
|
||||
messages.append(('success', _('ownCloud disabled')))
|
||||
option = 'noenable'
|
||||
|
||||
actions.superuser_run('owncloud-setup', [option], async=True)
|
||||
def _apply_changes(old_status, new_status, messages):
|
||||
"""Apply the changes"""
|
||||
if old_status['enabled'] == new_status['enabled']:
|
||||
messages.append(('info', _('Setting unchanged')))
|
||||
return
|
||||
|
||||
# Send a signal to other modules that the service is
|
||||
# enabled/disabled
|
||||
self.service.notify_enabled(self, new_status['enabled'])
|
||||
if new_status['enabled']:
|
||||
messages.append(('success', _('ownCloud enabled')))
|
||||
option = 'enable'
|
||||
else:
|
||||
messages.append(('success', _('ownCloud disabled')))
|
||||
option = 'noenable'
|
||||
|
||||
actions.superuser_run('owncloud-setup', [option], async=True)
|
||||
|
||||
# Send a signal to other modules that the service is
|
||||
# enabled/disabled
|
||||
SERVICE.notify_enabled(None, new_status['enabled'])
|
||||
@@ -20,6 +20,8 @@ Plinth module to manage packages
|
||||
"""
|
||||
|
||||
from . import packages
|
||||
from .packages import init
|
||||
|
||||
__all__ = ['packages', 'init']
|
||||
|
||||
__all__ = ['packages']
|
||||
DEPENDS = ['system']
|
||||
@@ -1,11 +1,10 @@
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
from ..lib.auth import require
|
||||
from plugin_mount import PagePlugin
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
import util
|
||||
from ..lib.auth import login_required
|
||||
|
||||
|
||||
def get_modules_available():
|
||||
@@ -28,10 +27,6 @@ def get_modules_enabled():
|
||||
|
||||
class PackagesForm(forms.Form):
|
||||
"""Packages form"""
|
||||
# XXX: Only present due to issue with submitting empty form
|
||||
dummy = forms.CharField(label='Dummy', initial='dummy',
|
||||
widget=forms.HiddenInput())
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# pylint: disable-msg=E1002, E1101
|
||||
super(forms.Form, self).__init__(*args, **kwargs)
|
||||
@@ -44,89 +39,83 @@ class PackagesForm(forms.Form):
|
||||
label=label, required=False)
|
||||
|
||||
|
||||
class Packages(PagePlugin):
|
||||
"""Package page"""
|
||||
order = 20
|
||||
def init():
|
||||
"""Initialize the Packages module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item('Package Manager', 'icon-gift', '/sys/packages', 20)
|
||||
|
||||
def __init__(self):
|
||||
super(Packages, self).__init__()
|
||||
|
||||
self.register_page('sys.packages')
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Serve the form"""
|
||||
status = get_status()
|
||||
|
||||
cfg.html_root.sys.menu.add_item('Package Manager', 'icon-gift',
|
||||
'/sys/packages', 20)
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, **kwargs):
|
||||
"""Serve the form"""
|
||||
status = self.get_status()
|
||||
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
if kwargs:
|
||||
form = PackagesForm(kwargs, prefix='packages')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._apply_changes(status, form.cleaned_data, messages)
|
||||
status = self.get_status()
|
||||
form = PackagesForm(initial=status, prefix='packages')
|
||||
else:
|
||||
if request.method == 'POST':
|
||||
form = PackagesForm(request.POST, prefix='packages')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
_apply_changes(status, form.cleaned_data, messages)
|
||||
status = get_status()
|
||||
form = PackagesForm(initial=status, prefix='packages')
|
||||
else:
|
||||
form = PackagesForm(initial=status, prefix='packages')
|
||||
|
||||
return util.render_template(template='packages',
|
||||
title=_('Add/Remove Plugins'),
|
||||
form=form, messages=messages)
|
||||
return TemplateResponse(request, 'packages.html',
|
||||
{'title': _('Add/Remove Plugins'),
|
||||
'form': form,
|
||||
'messages_': messages})
|
||||
|
||||
@staticmethod
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
modules_available = get_modules_available()
|
||||
modules_enabled = get_modules_enabled()
|
||||
|
||||
return {module + '_enabled': module in modules_enabled
|
||||
for module in modules_available}
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
modules_available = get_modules_available()
|
||||
modules_enabled = get_modules_enabled()
|
||||
|
||||
@staticmethod
|
||||
def _apply_changes(old_status, new_status, messages):
|
||||
"""Apply form changes"""
|
||||
for field, enabled in new_status.items():
|
||||
if not field.endswith('_enabled'):
|
||||
continue
|
||||
return {module + '_enabled': module in modules_enabled
|
||||
for module in modules_available}
|
||||
|
||||
if old_status[field] == new_status[field]:
|
||||
continue
|
||||
|
||||
module = field.split('_enabled')[0]
|
||||
if enabled:
|
||||
output, error = actions.superuser_run(
|
||||
'module-manager', ['enable', cfg.python_root, module])
|
||||
del output # Unused
|
||||
def _apply_changes(old_status, new_status, messages):
|
||||
"""Apply form changes"""
|
||||
for field, enabled in new_status.items():
|
||||
if not field.endswith('_enabled'):
|
||||
continue
|
||||
|
||||
# TODO: need to get plinth to load the module we just
|
||||
# enabled
|
||||
if error:
|
||||
messages.append(
|
||||
('error', _('Error enabling module - {module}').format(
|
||||
module=module)))
|
||||
else:
|
||||
messages.append(
|
||||
('success', _('Module enabled - {module}').format(
|
||||
if old_status[field] == new_status[field]:
|
||||
continue
|
||||
|
||||
module = field.split('_enabled')[0]
|
||||
if enabled:
|
||||
output, error = actions.superuser_run(
|
||||
'module-manager', ['enable', cfg.python_root, module])
|
||||
del output # Unused
|
||||
|
||||
# TODO: need to get plinth to load the module we just
|
||||
# enabled
|
||||
if error:
|
||||
messages.append(
|
||||
('error', _('Error enabling module - {module}').format(
|
||||
module=module)))
|
||||
else:
|
||||
messages.append(
|
||||
('success', _('Module enabled - {module}').format(
|
||||
module=module)))
|
||||
else:
|
||||
output, error = actions.superuser_run(
|
||||
'module-manager', ['disable', cfg.python_root, module])
|
||||
del output # Unused
|
||||
|
||||
# TODO: need a smoother way for plinth to unload the
|
||||
# module
|
||||
if error:
|
||||
messages.append(
|
||||
('error',
|
||||
_('Error disabling module - {module}').format(
|
||||
module=module)))
|
||||
else:
|
||||
output, error = actions.superuser_run(
|
||||
'module-manager', ['disable', cfg.python_root, module])
|
||||
del output # Unused
|
||||
|
||||
# TODO: need a smoother way for plinth to unload the
|
||||
# module
|
||||
if error:
|
||||
messages.append(
|
||||
('error',
|
||||
_('Error disabling module - {module}').format(
|
||||
module=module)))
|
||||
else:
|
||||
messages.append(
|
||||
('success', _('Module disabled - {module}').format(
|
||||
module=module)))
|
||||
messages.append(
|
||||
('success', _('Module disabled - {module}').format(
|
||||
module=module)))
|
||||
@@ -20,6 +20,8 @@ Plinth module to configure PageKite
|
||||
"""
|
||||
|
||||
from . import pagekite
|
||||
from .pagekite import init
|
||||
|
||||
__all__ = ['pagekite', 'init']
|
||||
|
||||
__all__ = ['pagekite']
|
||||
DEPENDS = ['apps']
|
||||
+113
-125
@@ -19,43 +19,38 @@
|
||||
Plinth module for configuring PageKite service
|
||||
"""
|
||||
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from django.core import validators
|
||||
from django.template import RequestContext
|
||||
from django.template.loader import render_to_string
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
from ..lib.auth import require
|
||||
from plugin_mount import PagePlugin
|
||||
import util
|
||||
from ..lib.auth import login_required
|
||||
|
||||
|
||||
class PageKite(PagePlugin):
|
||||
"""PageKite menu entry and introduction page"""
|
||||
order = 60
|
||||
def init():
|
||||
"""Intialize the PageKite module"""
|
||||
menu = cfg.main_menu.find('/apps')
|
||||
menu.add_item(_('Public Visibility (PageKite)'), 'icon-flag',
|
||||
'/apps/pagekite', 50)
|
||||
|
||||
def __init__(self):
|
||||
super(PageKite, self).__init__()
|
||||
|
||||
self.register_page("apps.pagekite")
|
||||
cfg.html_root.apps.menu.add_item(
|
||||
_("Public Visibility (PageKite)"), "icon-flag",
|
||||
"/apps/pagekite", 50)
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Serve introdution page"""
|
||||
menu = {'title': _('PageKite'),
|
||||
'items': [{'url': '/apps/pagekite/configure',
|
||||
'text': _('Configure PageKite')}]}
|
||||
|
||||
@staticmethod
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index():
|
||||
"""Serve introdution page"""
|
||||
menu = {'title': _('PageKite'),
|
||||
'items': [{'url': '/apps/pagekite/configure',
|
||||
'text': _('Configure PageKite')}]}
|
||||
sidebar_right = util.render_template(template='menu_block', menu=menu)
|
||||
sidebar_right = render_to_string('menu_block.html', {'menu': menu},
|
||||
RequestContext(request))
|
||||
|
||||
return util.render_template(template='pagekite_introduction',
|
||||
title=_("Public Visibility (PageKite)"),
|
||||
sidebar_right=sidebar_right)
|
||||
return TemplateResponse(request, 'pagekite_introduction.html',
|
||||
{'title': _('Public Visibility (PageKite)'),
|
||||
'sidebar_right': sidebar_right})
|
||||
|
||||
|
||||
class TrimmedCharField(forms.CharField):
|
||||
@@ -103,121 +98,114 @@ for your account if no secret is set on the kite'))
|
||||
https://pagekite.net/wiki/Howto/SshOverPageKite/">instructions</a>'))
|
||||
|
||||
|
||||
class Configure(PagePlugin): # pylint: disable-msg=C0103
|
||||
"""Main configuration form"""
|
||||
order = 65
|
||||
@login_required
|
||||
def configure(request):
|
||||
"""Serve the configuration form"""
|
||||
status = get_status()
|
||||
|
||||
def __init__(self):
|
||||
super(Configure, self).__init__()
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
self.register_page("apps.pagekite.configure")
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, **kwargs):
|
||||
"""Serve the configuration form"""
|
||||
status = self.get_status()
|
||||
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
if kwargs:
|
||||
form = ConfigureForm(kwargs, prefix='pagekite')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._apply_changes(status, form.cleaned_data, messages)
|
||||
status = self.get_status()
|
||||
form = ConfigureForm(initial=status, prefix='pagekite')
|
||||
else:
|
||||
if request.method == 'POST':
|
||||
form = ConfigureForm(request.POST, prefix='pagekite')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
_apply_changes(status, form.cleaned_data, messages)
|
||||
status = get_status()
|
||||
form = ConfigureForm(initial=status, prefix='pagekite')
|
||||
else:
|
||||
form = ConfigureForm(initial=status, prefix='pagekite')
|
||||
|
||||
return util.render_template(template='pagekite_configure',
|
||||
title=_('Configure PageKite'), form=form,
|
||||
messages=messages)
|
||||
return TemplateResponse(request, 'pagekite_configure.html',
|
||||
{'title': _('Configure PageKite'),
|
||||
'form': form,
|
||||
'messages_': messages})
|
||||
|
||||
def get_status(self):
|
||||
"""
|
||||
Return the current status of PageKite configuration by
|
||||
executing various actions.
|
||||
"""
|
||||
status = {}
|
||||
|
||||
# Check if PageKite is installed
|
||||
output = self._run(['get-installed'])
|
||||
cfg.log('Output - %s' % output)
|
||||
if output.split()[0] != 'installed':
|
||||
return None
|
||||
def get_status():
|
||||
"""
|
||||
Return the current status of PageKite configuration by
|
||||
executing various actions.
|
||||
"""
|
||||
status = {}
|
||||
|
||||
# PageKite service enabled/disabled
|
||||
output = self._run(['get-status'])
|
||||
status['enabled'] = (output.split()[0] == 'enabled')
|
||||
# Check if PageKite is installed
|
||||
output = _run(['get-installed'])
|
||||
cfg.log('Output - %s' % output)
|
||||
if output.split()[0] != 'installed':
|
||||
return None
|
||||
|
||||
# PageKite kite details
|
||||
output = self._run(['get-kite'])
|
||||
kite_details = output.split()
|
||||
status['kite_name'] = kite_details[0]
|
||||
status['kite_secret'] = kite_details[1]
|
||||
# PageKite service enabled/disabled
|
||||
output = _run(['get-status'])
|
||||
status['enabled'] = (output.split()[0] == 'enabled')
|
||||
|
||||
# Service status
|
||||
status['service'] = {}
|
||||
for service in ('http', 'ssh'):
|
||||
output = self._run(['get-service-status', service])
|
||||
status[service + '_enabled'] = (output.split()[0] == 'enabled')
|
||||
# PageKite kite details
|
||||
output = _run(['get-kite'])
|
||||
kite_details = output.split()
|
||||
status['kite_name'] = kite_details[0]
|
||||
status['kite_secret'] = kite_details[1]
|
||||
|
||||
return status
|
||||
# Service status
|
||||
status['service'] = {}
|
||||
for service in ('http', 'ssh'):
|
||||
output = _run(['get-service-status', service])
|
||||
status[service + '_enabled'] = (output.split()[0] == 'enabled')
|
||||
|
||||
def _apply_changes(self, old_status, new_status, messages):
|
||||
"""Apply the changes to PageKite configuration"""
|
||||
cfg.log.info('New status is - %s' % new_status)
|
||||
return status
|
||||
|
||||
if old_status != new_status:
|
||||
self._run(['stop'])
|
||||
|
||||
if old_status['enabled'] != new_status['enabled']:
|
||||
if new_status['enabled']:
|
||||
self._run(['set-status', 'enable'])
|
||||
messages.append(('success', _('PageKite enabled')))
|
||||
else:
|
||||
self._run(['set-status', 'disable'])
|
||||
messages.append(('success', _('PageKite disabled')))
|
||||
def _apply_changes(old_status, new_status, messages):
|
||||
"""Apply the changes to PageKite configuration"""
|
||||
cfg.log.info('New status is - %s' % new_status)
|
||||
|
||||
if old_status['kite_name'] != new_status['kite_name'] or \
|
||||
old_status['kite_secret'] != new_status['kite_secret']:
|
||||
self._run(['set-kite', '--kite-name', new_status['kite_name'],
|
||||
'--kite-secret', new_status['kite_secret']])
|
||||
messages.append(('success', _('Kite details set')))
|
||||
if old_status != new_status:
|
||||
_run(['stop'])
|
||||
|
||||
for service in ['http', 'ssh']:
|
||||
if old_status[service + '_enabled'] != \
|
||||
new_status[service + '_enabled']:
|
||||
if new_status[service + '_enabled']:
|
||||
self._run(['set-service-status', service, 'enable'])
|
||||
messages.append(('success', _('Service enabled: {service}')
|
||||
.format(service=service)))
|
||||
else:
|
||||
self._run(['set-service-status', service, 'disable'])
|
||||
messages.append(('success',
|
||||
_('Service disabled: {service}')
|
||||
.format(service=service)))
|
||||
|
||||
if old_status != new_status:
|
||||
self._run(['start'])
|
||||
|
||||
@staticmethod
|
||||
def _run(arguments, superuser=True):
|
||||
"""Run an given command and raise exception if there was an error"""
|
||||
command = 'pagekite-configure'
|
||||
|
||||
cfg.log.info('Running command - %s, %s, %s' % (command, arguments,
|
||||
superuser))
|
||||
|
||||
if superuser:
|
||||
output, error = actions.superuser_run(command, arguments)
|
||||
if old_status['enabled'] != new_status['enabled']:
|
||||
if new_status['enabled']:
|
||||
_run(['set-status', 'enable'])
|
||||
messages.append(('success', _('PageKite enabled')))
|
||||
else:
|
||||
output, error = actions.run(command, arguments)
|
||||
_run(['set-status', 'disable'])
|
||||
messages.append(('success', _('PageKite disabled')))
|
||||
|
||||
if error:
|
||||
raise Exception('Error setting/getting PageKite confguration - %s'
|
||||
% error)
|
||||
if old_status['kite_name'] != new_status['kite_name'] or \
|
||||
old_status['kite_secret'] != new_status['kite_secret']:
|
||||
_run(['set-kite', '--kite-name', new_status['kite_name'],
|
||||
'--kite-secret', new_status['kite_secret']])
|
||||
messages.append(('success', _('Kite details set')))
|
||||
|
||||
return output
|
||||
for service in ['http', 'ssh']:
|
||||
if old_status[service + '_enabled'] != \
|
||||
new_status[service + '_enabled']:
|
||||
if new_status[service + '_enabled']:
|
||||
_run(['set-service-status', service, 'enable'])
|
||||
messages.append(('success', _('Service enabled: {service}')
|
||||
.format(service=service)))
|
||||
else:
|
||||
_run(['set-service-status', service, 'disable'])
|
||||
messages.append(('success',
|
||||
_('Service disabled: {service}')
|
||||
.format(service=service)))
|
||||
|
||||
if old_status != new_status:
|
||||
_run(['start'])
|
||||
|
||||
|
||||
def _run(arguments, superuser=True):
|
||||
"""Run an given command and raise exception if there was an error"""
|
||||
command = 'pagekite-configure'
|
||||
|
||||
cfg.log.info('Running command - %s, %s, %s' % (command, arguments,
|
||||
superuser))
|
||||
|
||||
if superuser:
|
||||
output, error = actions.superuser_run(command, arguments)
|
||||
else:
|
||||
output, error = actions.run(command, arguments)
|
||||
|
||||
if error:
|
||||
raise Exception('Error setting/getting PageKite confguration - %s'
|
||||
% error)
|
||||
|
||||
return output
|
||||
@@ -20,6 +20,7 @@ Plinth module for system section page
|
||||
"""
|
||||
|
||||
from . import system
|
||||
from system import init
|
||||
|
||||
|
||||
__all__ = ['system']
|
||||
__all__ = ['system', 'init']
|
||||
@@ -1,21 +1,15 @@
|
||||
import cherrypy
|
||||
from gettext import gettext as _
|
||||
from plugin_mount import PagePlugin
|
||||
from django.template.response import TemplateResponse
|
||||
|
||||
import cfg
|
||||
import util
|
||||
|
||||
|
||||
class Sys(PagePlugin):
|
||||
order = 10
|
||||
def init():
|
||||
"""Initialize the system module"""
|
||||
cfg.main_menu.add_item(_('System'), 'icon-cog', '/sys', 100)
|
||||
|
||||
def __init__(self):
|
||||
super(Sys, self).__init__()
|
||||
|
||||
self.register_page("sys")
|
||||
self.menu = cfg.main_menu.add_item(_("System"), "icon-cog", "/sys", 100)
|
||||
self.menu.add_item(_("Users and Groups"), "icon-user", "/sys/users", 15)
|
||||
|
||||
@cherrypy.expose
|
||||
def index(self):
|
||||
return util.render_template(template='system',
|
||||
title=_("System Configuration"))
|
||||
def index(request):
|
||||
"""Serve the index page"""
|
||||
return TemplateResponse(request, 'system.html',
|
||||
{'title': _('System Configuration')})
|
||||
@@ -20,6 +20,8 @@ Plinth module to configure Tor
|
||||
"""
|
||||
|
||||
from . import tor
|
||||
from .tor import init
|
||||
|
||||
__all__ = ['tor', 'init']
|
||||
|
||||
__all__ = ['tor']
|
||||
DEPENDS = ['apps']
|
||||
+23
-26
@@ -19,38 +19,35 @@
|
||||
Plinth module for configuring Tor
|
||||
"""
|
||||
|
||||
import cherrypy
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
from plugin_mount import PagePlugin
|
||||
from ..lib.auth import require
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
import util
|
||||
from ..lib.auth import login_required
|
||||
|
||||
|
||||
class Tor(PagePlugin):
|
||||
order = 60 # order of running init in PagePlugins
|
||||
def init():
|
||||
"""Initialize the Tor module"""
|
||||
menu = cfg.main_menu.find('/apps')
|
||||
menu.add_item("Tor", "icon-eye-close", "/apps/tor", 30)
|
||||
|
||||
def __init__(self):
|
||||
super(Tor, self).__init__()
|
||||
|
||||
self.register_page("apps.tor")
|
||||
cfg.html_root.apps.menu.add_item("Tor", "icon-eye-close", "/apps/tor",
|
||||
30)
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Service the index page"""
|
||||
output, error = actions.superuser_run("tor-get-ports")
|
||||
del error # Unused
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self):
|
||||
output, error = actions.superuser_run("tor-get-ports")
|
||||
port_info = output.split("\n")
|
||||
tor_ports = {}
|
||||
for line in port_info:
|
||||
try:
|
||||
(key, val) = line.split()
|
||||
tor_ports[key] = val
|
||||
except ValueError:
|
||||
continue
|
||||
port_info = output.split("\n")
|
||||
tor_ports = {}
|
||||
for line in port_info:
|
||||
try:
|
||||
(key, val) = line.split()
|
||||
tor_ports[key] = val
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
return util.render_template(template='tor',
|
||||
title=_('Tor Control Panel'),
|
||||
tor_ports=tor_ports)
|
||||
return TemplateResponse(request, 'tor.html',
|
||||
{'title': _('Tor Control Panel'),
|
||||
'tor_ports': tor_ports})
|
||||
@@ -20,6 +20,8 @@ Plinth module to manage users
|
||||
"""
|
||||
|
||||
from . import users
|
||||
from .users import init
|
||||
|
||||
__all__ = ['users', 'init']
|
||||
|
||||
__all__ = ['users']
|
||||
DEPENDS = ['system']
|
||||
+93
-109
@@ -1,36 +1,36 @@
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from django.core import validators
|
||||
from django.template import RequestContext
|
||||
from django.template.loader import render_to_string
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
from ..lib.auth import require, add_user
|
||||
from plugin_mount import PagePlugin
|
||||
|
||||
import cfg
|
||||
from ..lib.auth import add_user, login_required
|
||||
from model import User
|
||||
import util
|
||||
|
||||
|
||||
class Users(PagePlugin):
|
||||
order = 20 # order of running init in PagePlugins
|
||||
def init():
|
||||
"""Intialize the module"""
|
||||
menu = cfg.main_menu.find('/sys')
|
||||
menu.add_item(_('Users and Groups'), 'icon-user', '/sys/users', 15)
|
||||
|
||||
def __init__(self):
|
||||
super(Users, self).__init__()
|
||||
|
||||
self.register_page("sys.users")
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Return a rendered users page"""
|
||||
menu = {'title': _('Users and Groups'),
|
||||
'items': [{'url': '/sys/users/add',
|
||||
'text': _('Add User')},
|
||||
{'url': '/sys/users/edit',
|
||||
'text': _('Edit Users')}]}
|
||||
|
||||
@staticmethod
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index():
|
||||
"""Return a rendered users page"""
|
||||
menu = {'title': _('Users and Groups'),
|
||||
'items': [{'url': '/sys/users/add',
|
||||
'text': _('Add User')},
|
||||
{'url': '/sys/users/edit',
|
||||
'text': _('Edit Users')}]}
|
||||
sidebar_right = util.render_template(template='menu_block',
|
||||
menu=menu)
|
||||
return util.render_template(title="Manage Users and Groups",
|
||||
sidebar_right=sidebar_right)
|
||||
sidebar_right = render_to_string('menu_block.html', {'menu': menu},
|
||||
RequestContext(request))
|
||||
|
||||
return TemplateResponse(request, 'login_nav.html',
|
||||
{'title': _('Manage Users and Groups'),
|
||||
'sidebar_right': sidebar_right})
|
||||
|
||||
|
||||
class UserAddForm(forms.Form): # pylint: disable-msg=W0232
|
||||
@@ -50,48 +50,40 @@ and alphabet'),
|
||||
email = forms.EmailField(label=_('Email'), required=False)
|
||||
|
||||
|
||||
class UserAdd(PagePlugin):
|
||||
"""Add user page"""
|
||||
order = 30
|
||||
@login_required
|
||||
def add(request):
|
||||
"""Serve the form"""
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
def __init__(self):
|
||||
super(UserAdd, self).__init__()
|
||||
|
||||
self.register_page('sys.users.add')
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, **kwargs):
|
||||
"""Serve the form"""
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
if kwargs:
|
||||
form = UserAddForm(kwargs, prefix='user')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._add_user(form.cleaned_data, messages)
|
||||
form = UserAddForm(prefix='user')
|
||||
else:
|
||||
if request.method == 'POST':
|
||||
form = UserAddForm(request.POST, prefix='user')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
_add_user(form.cleaned_data, messages)
|
||||
form = UserAddForm(prefix='user')
|
||||
else:
|
||||
form = UserAddForm(prefix='user')
|
||||
|
||||
return util.render_template(template='users_add', title=_('Add User'),
|
||||
form=form, messages=messages)
|
||||
return TemplateResponse(request, 'users_add.html',
|
||||
{'title': _('Add User'),
|
||||
'form': form,
|
||||
'messages_': messages})
|
||||
|
||||
@staticmethod
|
||||
def _add_user(data, messages):
|
||||
"""Add a user"""
|
||||
if cfg.users.exists(data['username']):
|
||||
messages.append(
|
||||
('error', _('User "{username}" already exists').format(
|
||||
username=data['username'])))
|
||||
return
|
||||
|
||||
add_user(data['username'], data['password'], data['full_name'],
|
||||
data['email'], False)
|
||||
def _add_user(data, messages):
|
||||
"""Add a user"""
|
||||
if cfg.users.exists(data['username']):
|
||||
messages.append(
|
||||
('success', _('User "{username}" added').format(
|
||||
('error', _('User "{username}" already exists').format(
|
||||
username=data['username'])))
|
||||
return
|
||||
|
||||
add_user(data['username'], data['password'], data['full_name'],
|
||||
data['email'], False)
|
||||
messages.append(
|
||||
('success', _('User "{username}" added').format(
|
||||
username=data['username'])))
|
||||
|
||||
|
||||
class UserEditForm(forms.Form): # pylint: disable-msg=W0232
|
||||
@@ -110,64 +102,56 @@ class UserEditForm(forms.Form): # pylint: disable-msg=W0232
|
||||
self.fields['delete_user_' + user['username']] = field
|
||||
|
||||
|
||||
class UserEdit(PagePlugin):
|
||||
"""User edit page"""
|
||||
order = 35
|
||||
@login_required
|
||||
def edit(request):
|
||||
"""Serve the edit form"""
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
def __init__(self):
|
||||
super(UserEdit, self).__init__()
|
||||
|
||||
self.register_page('sys.users.edit')
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, **kwargs):
|
||||
"""Serve the form"""
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
if kwargs:
|
||||
form = UserEditForm(kwargs, prefix='user')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._apply_changes(form.cleaned_data, messages)
|
||||
form = UserEditForm(prefix='user')
|
||||
else:
|
||||
if request.method == 'POST':
|
||||
form = UserEditForm(request.POST, prefix='user')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
_apply_edit_changes(request, form.cleaned_data, messages)
|
||||
form = UserEditForm(prefix='user')
|
||||
else:
|
||||
form = UserEditForm(prefix='user')
|
||||
|
||||
return util.render_template(template='users_edit',
|
||||
title=_('Edit or Delete User'),
|
||||
form=form, messages=messages)
|
||||
return TemplateResponse(request, 'users_edit.html',
|
||||
{'title': _('Edit or Delete User'),
|
||||
'form': form,
|
||||
'messages_': messages})
|
||||
|
||||
@staticmethod
|
||||
def _apply_changes(data, messages):
|
||||
"""Apply form changes"""
|
||||
for field, value in data.items():
|
||||
if not value:
|
||||
continue
|
||||
|
||||
if not field.startswith('delete_user_'):
|
||||
continue
|
||||
def _apply_edit_changes(request, data, messages):
|
||||
"""Apply form changes"""
|
||||
for field, value in data.items():
|
||||
if not value:
|
||||
continue
|
||||
|
||||
username = field.split('delete_user_')[1]
|
||||
if not field.startswith('delete_user_'):
|
||||
continue
|
||||
|
||||
cfg.log.info('%s asked to delete %s' %
|
||||
(cherrypy.session.get(cfg.session_key), username))
|
||||
username = field.split('delete_user_')[1]
|
||||
|
||||
if username == cfg.users.current(name=True):
|
||||
messages.append(
|
||||
('error',
|
||||
_('Can not delete current account - "%s"') % username))
|
||||
continue
|
||||
requesting_user = request.session.get(cfg.session_key, None)
|
||||
cfg.log.info('%s asked to delete %s' %
|
||||
(requesting_user, username))
|
||||
|
||||
if not cfg.users.exists(username):
|
||||
messages.append(('error',
|
||||
_('User "%s" does not exist') % username))
|
||||
continue
|
||||
if username == cfg.users.current(request=request, name=True):
|
||||
messages.append(
|
||||
('error',
|
||||
_('Can not delete current account - "%s"') % username))
|
||||
continue
|
||||
|
||||
try:
|
||||
cfg.users.remove(username)
|
||||
messages.append(('success', _('User "%s" deleted') % username))
|
||||
except IOError as exception:
|
||||
messages.append(('error', _('Error deleting "%s" - %s') %
|
||||
(username, exception)))
|
||||
if not cfg.users.exists(username):
|
||||
messages.append(('error',
|
||||
_('User "%s" does not exist') % username))
|
||||
continue
|
||||
|
||||
try:
|
||||
cfg.users.remove(username)
|
||||
messages.append(('success', _('User "%s" deleted') % username))
|
||||
except IOError as exception:
|
||||
messages.append(('error', _('Error deleting "%s" - %s') %
|
||||
(username, exception)))
|
||||
@@ -20,6 +20,8 @@ Plinth module to configure XMPP server
|
||||
"""
|
||||
|
||||
from . import xmpp
|
||||
from .xmpp import init
|
||||
|
||||
__all__ = ['xmpp', 'init']
|
||||
|
||||
__all__ = ['xmpp']
|
||||
DEPENDS = ['apps']
|
||||
+112
-134
@@ -1,12 +1,13 @@
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from django.template import RequestContext
|
||||
from django.template.loader import render_to_string
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
from ..lib.auth import require
|
||||
from plugin_mount import PagePlugin
|
||||
import cfg
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
from ..lib.auth import login_required
|
||||
import service
|
||||
import util
|
||||
|
||||
|
||||
SIDE_MENU = {'title': _('XMPP'),
|
||||
@@ -16,38 +17,35 @@ SIDE_MENU = {'title': _('XMPP'),
|
||||
'text': 'Register XMPP Account'}]}
|
||||
|
||||
|
||||
class XMPP(PagePlugin):
|
||||
"""XMPP Page"""
|
||||
order = 60
|
||||
def init():
|
||||
"""Initialize the XMPP module"""
|
||||
menu = cfg.main_menu.find('/apps')
|
||||
menu.add_item('Chat', 'icon-comment', '/../jwchat', 20)
|
||||
menu.add_item('XMPP', 'icon-comment', '/apps/xmpp', 40)
|
||||
|
||||
def __init__(self):
|
||||
super(XMPP, self).__init__()
|
||||
service.Service(
|
||||
'xmpp-client', _('Chat Server - client connections'),
|
||||
is_external=True, enabled=True)
|
||||
service.Service(
|
||||
'xmpp-server', _('Chat Server - server connections'),
|
||||
is_external=True, enabled=True)
|
||||
service.Service(
|
||||
'xmpp-bosh', _('Chat Server - web interface'), is_external=True,
|
||||
enabled=True)
|
||||
|
||||
self.register_page('apps.xmpp')
|
||||
cfg.html_root.apps.menu.add_item('XMPP', 'icon-comment',
|
||||
'/apps/xmpp', 40)
|
||||
|
||||
self.client_service = service.Service(
|
||||
'xmpp-client', _('Chat Server - client connections'),
|
||||
is_external=True, enabled=True)
|
||||
self.server_service = service.Service(
|
||||
'xmpp-server', _('Chat Server - server connections'),
|
||||
is_external=True, enabled=True)
|
||||
self.bosh_service = service.Service(
|
||||
'xmpp-bosh', _('Chat Server - web interface'), is_external=True,
|
||||
enabled=True)
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Serve XMPP page"""
|
||||
main = "<p>XMPP Server Accounts and Configuration</p>"
|
||||
|
||||
@staticmethod
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index():
|
||||
"""Serve XMPP page"""
|
||||
main = "<p>XMPP Server Accounts and Configuration</p>"
|
||||
sidebar_right = render_to_string('menu_block.html', {'menu': SIDE_MENU},
|
||||
RequestContext(request))
|
||||
|
||||
sidebar_right = util.render_template(template='menu_block',
|
||||
menu=SIDE_MENU)
|
||||
return util.render_template(title="XMPP Server", main=main,
|
||||
sidebar_right=sidebar_right)
|
||||
return TemplateResponse(request, 'login_nav.html',
|
||||
{'title': _('XMPP Server'),
|
||||
'main': main,
|
||||
'sidebar_right': sidebar_right})
|
||||
|
||||
|
||||
class ConfigureForm(forms.Form): # pylint: disable-msg=W0232
|
||||
@@ -57,77 +55,65 @@ class ConfigureForm(forms.Form): # pylint: disable-msg=W0232
|
||||
help_text=_('When enabled, anyone who can reach this server will be \
|
||||
allowed to register an account through an XMPP client'))
|
||||
|
||||
# XXX: Only present due to issue with submitting empty form
|
||||
dummy = forms.CharField(label='Dummy', initial='dummy',
|
||||
widget=forms.HiddenInput())
|
||||
|
||||
@login_required
|
||||
def configure(request):
|
||||
"""Serve the configuration form"""
|
||||
status = get_status()
|
||||
|
||||
class Configure(PagePlugin):
|
||||
"""Configuration page"""
|
||||
order = 65
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
def __init__(self):
|
||||
super(Configure, self).__init__()
|
||||
|
||||
self.register_page('apps.xmpp.configure')
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, **kwargs):
|
||||
"""Serve the configuration form"""
|
||||
status = self.get_status()
|
||||
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
if kwargs:
|
||||
form = ConfigureForm(kwargs, prefix='xmpp')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._apply_changes(status, form.cleaned_data, messages)
|
||||
status = self.get_status()
|
||||
form = ConfigureForm(initial=status, prefix='xmpp')
|
||||
else:
|
||||
if request.method == 'POST':
|
||||
form = ConfigureForm(request.POST, prefix='xmpp')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
_apply_changes(status, form.cleaned_data, messages)
|
||||
status = get_status()
|
||||
form = ConfigureForm(initial=status, prefix='xmpp')
|
||||
else:
|
||||
form = ConfigureForm(initial=status, prefix='xmpp')
|
||||
|
||||
sidebar_right = util.render_template(template='menu_block',
|
||||
menu=SIDE_MENU)
|
||||
return util.render_template(template='xmpp_configure',
|
||||
title=_('Configure XMPP Server'),
|
||||
form=form, messages=messages,
|
||||
sidebar_right=sidebar_right)
|
||||
sidebar_right = render_to_string('menu_block.html', {'menu': SIDE_MENU},
|
||||
RequestContext(request))
|
||||
|
||||
@staticmethod
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
output, error = actions.run('xmpp-setup', 'status')
|
||||
if error:
|
||||
raise Exception('Error getting status: %s' % error)
|
||||
return TemplateResponse(request, 'xmpp_configure.html',
|
||||
{'title': _('Configure XMPP Server'),
|
||||
'form': form,
|
||||
'messages_': messages,
|
||||
'sidebar_right': sidebar_right})
|
||||
|
||||
return {'inband_enabled': 'inband_enable' in output.split()}
|
||||
|
||||
@staticmethod
|
||||
def _apply_changes(old_status, new_status, messages):
|
||||
"""Apply the form changes"""
|
||||
cfg.log.info('Status - %s, %s' % (old_status, new_status))
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
output, error = actions.run('xmpp-setup', 'status')
|
||||
if error:
|
||||
raise Exception('Error getting status: %s' % error)
|
||||
|
||||
if old_status['inband_enabled'] == new_status['inband_enabled']:
|
||||
messages.append(('info', _('Setting unchanged')))
|
||||
return
|
||||
return {'inband_enabled': 'inband_enable' in output.split()}
|
||||
|
||||
if new_status['inband_enabled']:
|
||||
messages.append(('success', _('Inband registration enabled')))
|
||||
option = 'inband_enable'
|
||||
else:
|
||||
messages.append(('success', _('Inband registration disabled')))
|
||||
option = 'noinband_enable'
|
||||
|
||||
cfg.log.info('Option - %s' % option)
|
||||
def _apply_changes(old_status, new_status, messages):
|
||||
"""Apply the form changes"""
|
||||
cfg.log.info('Status - %s, %s' % (old_status, new_status))
|
||||
|
||||
_output, error = actions.superuser_run('xmpp-setup', [option])
|
||||
del _output # Unused
|
||||
if error:
|
||||
raise Exception('Error running command - %s' % error)
|
||||
if old_status['inband_enabled'] == new_status['inband_enabled']:
|
||||
messages.append(('info', _('Setting unchanged')))
|
||||
return
|
||||
|
||||
if new_status['inband_enabled']:
|
||||
messages.append(('success', _('Inband registration enabled')))
|
||||
option = 'inband_enable'
|
||||
else:
|
||||
messages.append(('success', _('Inband registration disabled')))
|
||||
option = 'noinband_enable'
|
||||
|
||||
cfg.log.info('Option - %s' % option)
|
||||
|
||||
_output, error = actions.superuser_run('xmpp-setup', [option])
|
||||
del _output # Unused
|
||||
if error:
|
||||
raise Exception('Error running command - %s' % error)
|
||||
|
||||
|
||||
class RegisterForm(forms.Form): # pylint: disable-msg=W0232
|
||||
@@ -138,51 +124,43 @@ class RegisterForm(forms.Form): # pylint: disable-msg=W0232
|
||||
label=_('Password'), widget=forms.PasswordInput())
|
||||
|
||||
|
||||
class Register(PagePlugin):
|
||||
"""User registration page"""
|
||||
order = 65
|
||||
@login_required
|
||||
def register(request):
|
||||
"""Serve the registration form"""
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
def __init__(self):
|
||||
super(Register, self).__init__()
|
||||
|
||||
self.register_page('apps.xmpp.register')
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, **kwargs):
|
||||
"""Serve the registration form"""
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
if kwargs:
|
||||
form = RegisterForm(kwargs, prefix='xmpp')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._register_user(form.cleaned_data, messages)
|
||||
form = RegisterForm(prefix='xmpp')
|
||||
else:
|
||||
if request.method == 'POST':
|
||||
form = RegisterForm(request.POST, prefix='xmpp')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
_register_user(form.cleaned_data, messages)
|
||||
form = RegisterForm(prefix='xmpp')
|
||||
else:
|
||||
form = RegisterForm(prefix='xmpp')
|
||||
|
||||
sidebar_right = util.render_template(template='menu_block',
|
||||
menu=SIDE_MENU)
|
||||
return util.render_template(template='xmpp_register',
|
||||
title=_('Register XMPP Account'),
|
||||
form=form, messages=messages,
|
||||
sidebar_right=sidebar_right)
|
||||
sidebar_right = render_to_string('menu_block.html', {'menu': SIDE_MENU},
|
||||
RequestContext(request))
|
||||
|
||||
@staticmethod
|
||||
def _register_user(data, messages):
|
||||
"""Register a new XMPP user"""
|
||||
output, error = actions.superuser_run(
|
||||
'xmpp-register', [data['username'], data['password']])
|
||||
if error:
|
||||
raise Exception('Error registering user - %s' % error)
|
||||
return TemplateResponse(request, 'xmpp_register.html',
|
||||
{'title': _('Register XMPP Account'),
|
||||
'form': form,
|
||||
'messages_': messages,
|
||||
'sidebar_right': sidebar_right})
|
||||
|
||||
if 'successfully registered' in output:
|
||||
messages.append(('success',
|
||||
_('Registered account for %s' %
|
||||
data['username'])))
|
||||
else:
|
||||
messages.append(('error',
|
||||
_('Failed to register account for %s: %s') %
|
||||
(data['username'], output)))
|
||||
|
||||
def _register_user(data, messages):
|
||||
"""Register a new XMPP user"""
|
||||
output, error = actions.superuser_run(
|
||||
'xmpp-register', [data['username'], data['password']])
|
||||
if error:
|
||||
raise Exception('Error registering user - %s' % error)
|
||||
|
||||
if 'successfully registered' in output:
|
||||
messages.append(('success',
|
||||
_('Registered account for %s' %
|
||||
data['username'])))
|
||||
else:
|
||||
messages.append(('error',
|
||||
_('Failed to register account for %s: %s') %
|
||||
(data['username'], output)))
|
||||
Reference in new issue
Block a user