matrix-synapse: feature to enable/disable public registrations

Signed-off-by: Hemanth Kumar Veeranki <hemanthveeranki@gmail.com>

Reviewed-by: Johannes Keyser <johanneskeyser@posteo.de>
This commit is contained in:
Hemanth Kumar Veeranki 2017-08-12 01:55:54 +05:30 committed by Johannes Keyser
parent 732a7842f9
commit a1014946d2
No known key found for this signature in database
GPG Key ID: D1431C2C533CF0D0
4 changed files with 111 additions and 4 deletions

View File

@ -25,6 +25,7 @@ import argparse
from ruamel.yaml import round_trip_dump, round_trip_load
from plinth import action_utils
from plinth.modules.matrixsynapse import CONFIG_FILE_PATH
def parse_arguments():
@ -35,6 +36,11 @@ def parse_arguments():
subparsers.add_parser('post-install', help='Perform post install steps')
subparsers.add_parser('enable', help='Enable matrix-synapse service')
subparsers.add_parser('disable', help='Disable matrix-synapse service')
subparsers.add_parser('restart', help='Restart matrix-synapse service')
subparsers.add_parser('enable-registration',
help='Turn on Public registrations')
subparsers.add_parser('disable-registration',
help='Turn off Public registrations')
setup = subparsers.add_parser('setup', help='Set domain name for Matrix')
setup.add_argument(
'--domain-name',
@ -46,8 +52,7 @@ def parse_arguments():
def subcommand_post_install(_):
"""Perform post installation configuration."""
file_path = '/etc/matrix-synapse/homeserver.yaml'
with open(file_path) as config_file:
with open(CONFIG_FILE_PATH) as config_file:
config = round_trip_load(config_file)
config['max_upload_size'] = '100M'
@ -56,7 +61,7 @@ def subcommand_post_install(_):
if listener['port'] == 8448:
listener['bind_address'] = '0.0.0.0'
with open(file_path, 'w') as config_file:
with open(CONFIG_FILE_PATH, 'w') as config_file:
round_trip_dump(config, config_file)
@ -80,6 +85,30 @@ def subcommand_disable(_):
action_utils.service_disable('matrix-synapse')
def subcommand_enable_registration(_):
""" Enable public registration"""
with open(CONFIG_FILE_PATH) as config_file:
config = round_trip_load(config_file)
config['enable_registration'] = True
with open(CONFIG_FILE_PATH, 'w') as config_file:
round_trip_dump(config, config_file)
if action_utils.is_service_running('matrix-synapse'):
action_utils.service_restart('matrix-synapse')
def subcommand_disable_registration(_):
""" Disable public registration"""
with open(CONFIG_FILE_PATH) as config_file:
config = round_trip_load(config_file)
config['enable_registration'] = False
with open(CONFIG_FILE_PATH, 'w') as config_file:
round_trip_dump(config, config_file)
if action_utils.is_service_running('matrix-synapse'):
action_utils.service_restart('matrix-synapse')
def main():
arguments = parse_arguments()

View File

@ -62,7 +62,7 @@ service = None
logger = logging.getLogger(__name__)
SERVER_NAME_PATH = "/etc/matrix-synapse/conf.d/server_name.yaml"
CONFIG_FILE_PATH = '/etc/matrix-synapse/homeserver.yaml'
def init():
"""Initialize the matrix-synapse module."""
@ -149,3 +149,14 @@ def get_configured_domain_name():
config, _, _ = load_yaml_guess_indent(config_file)
return config['server_name']
def get_public_registration_status():
""" Return whether public registration is turned on"""
if not is_setup():
return None
with open(CONFIG_FILE_PATH) as config_file:
config, _, _ = load_yaml_guess_indent(config_file)
return config['enable_registration']

View File

@ -0,0 +1,36 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Forms for the Matrix Synapse module.
"""
from django import forms
from django.utils.translation import ugettext_lazy as _
from plinth.forms import ServiceForm
class MatrixSynapseForm(ServiceForm):
enable_public_registration = forms.BooleanField(
label=_('Enable Public Registration'),
required=False,
help_text=_(
'Enable or disable public registrations for matrix-synapse. '
'Enabling public registrations means that anyone on the internet '
'can register a new account on your Matrix server.Disable the '
'this if you only want existing users to be able to use it.'))

View File

@ -19,8 +19,10 @@
Views for the Matrix Synapse module.
"""
from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from django.views.generic import FormView
from plinth import actions
@ -29,6 +31,8 @@ from plinth.modules import matrixsynapse
from plinth.forms import DomainSelectionForm
from plinth.utils import get_domain_names
from .forms import MatrixSynapseForm
from . import get_public_registration_status
class SetupView(FormView):
"""Show matrix-synapse setup page."""
@ -61,6 +65,7 @@ class ServiceView(views.ServiceView):
template_name = 'matrix-synapse.html'
description = matrixsynapse.description
diagnostics_module_name = 'matrixsynapse'
form_class = MatrixSynapseForm
def dispatch(self, request, *args, **kwargs):
"""Redirect to setup page if setup is not done yet."""
@ -74,3 +79,29 @@ class ServiceView(views.ServiceView):
context = super().get_context_data(**kwargs)
context['domain_name'] = matrixsynapse.get_configured_domain_name()
return context
def get_initial(self):
"""Return the values to fill in the form."""
initial = super().get_initial()
initial.update({
'enable_public_registration': get_public_registration_status()})
return initial
def form_valid(self, form):
"""Handle valid form submission."""
old_config = self.get_initial()
new_config = form.cleaned_data
if old_config['enable_public_registration'] !=\
new_config['enable_public_registration']:
enable_registration = new_config['enable_public_registration']
if enable_registration:
actions.superuser_run('matrixsynapse', ['enable-registration'])
messages.success(self.request,
_('Public registration enabled'))
else:
actions.superuser_run('matrixsynapse', ['disable-registration'])
messages.success(self.request,
_('Public registration disabled'))
if new_config['is_enabled'] == True:
actions.superuser_run('matrixsynapse', ['restart'])
return super().form_valid(form)