roundcube: Use privileged to simplify actions

Tests:

- Functional tests pass

- Same tests as previous patch for setting logging to syslog.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-07-12 17:41:50 -07:00 committed by James Valleroy
parent 6c86da022e
commit 20081ee5d1
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 20 additions and 66 deletions

View File

@ -3,11 +3,8 @@
FreedomBox app to configure Roundcube.
"""
import json
from django.utils.translation import gettext_lazy as _
from plinth import actions
from plinth import app as app_module
from plinth import frontpage, menu
from plinth.modules.apache.components import Webserver
@ -15,7 +12,7 @@ from plinth.modules.backups.components import BackupRestore
from plinth.modules.firewall.components import Firewall
from plinth.package import Packages
from . import manifest
from . import manifest, privileged
_description = [
_('Roundcube webmail is a browser-based multilingual IMAP '
@ -94,14 +91,14 @@ class RoundcubeApp(app_module.App):
def setup(helper, old_version=None):
"""Install and configure the module."""
helper.call('pre', actions.superuser_run, 'roundcube', ['pre-install'])
helper.call('pre', privileged.pre_install)
app.setup(old_version)
helper.call('post', actions.superuser_run, 'roundcube', ['setup'])
helper.call('post', privileged.setup)
if old_version == 0:
set_config(local_only=True)
privileged.set_config(local_only=True)
helper.call('post', app.enable)
elif old_version <= 2:
set_config(get_config()['local_only'])
privileged.set_config(privileged.get_config()['local_only'])
def force_upgrade(helper, packages):
@ -116,16 +113,3 @@ def force_upgrade(helper, packages):
app.get_component('webserver-roundcube-freedombox').enable()
return True
def get_config():
"""Return Rouncube configuration."""
value = actions.superuser_run('roundcube', ['get-config'])
return json.loads(value)
def set_config(local_only):
"""Set whether only local server should be allowed."""
actions.superuser_run('roundcube',
['set-config', '--local-only',
str(local_only)])

View File

@ -1,37 +1,17 @@
#!/usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Configuration helper for Roundcube server.
"""
"""Configure roundcube."""
import argparse
import json
import pathlib
import re
from plinth import action_utils
from plinth.actions import privileged
_config_file = pathlib.Path('/etc/roundcube/freedombox-config.php')
def parse_arguments():
"""Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparsers.add_parser('pre-install',
help='Perform Roundcube pre-install configuration')
subparsers.add_parser('setup', help='Setup basic configuration')
subparsers.add_parser('get-config', help='Print current configuration')
subparser = subparsers.add_parser('set-config', help='Set configuration')
subparser.add_argument('--local-only', choices=['True', 'False'],
help='Set current configuration')
subparsers.required = True
return parser.parse_args()
def subcommand_pre_install(_):
@privileged
def pre_install():
"""Preseed debconf values before packages are installed."""
action_utils.debconf_set_selections([
'roundcube-core roundcube/dbconfig-install boolean true',
@ -39,7 +19,8 @@ def subcommand_pre_install(_):
])
def subcommand_setup(_):
@privileged
def setup():
"""Add FreedomBox configuration and include from main configuration."""
if not _config_file.exists():
_config_file.write_text('<?php\n', encoding='utf-8')
@ -52,7 +33,8 @@ def subcommand_setup(_):
base_config.write_text('\n'.join(lines), encoding='utf-8')
def subcommand_get_config(_):
@privileged
def get_config() -> dict[str, bool]:
"""Print the current configuration as JSON."""
pattern = r'\s*\$config\[\s*\'([^\']*)\'\s*\]\s*=\s*\'([^\']*)\'\s*;'
_config = {}
@ -65,16 +47,17 @@ def subcommand_get_config(_):
pass
local_only = _config.get('default_host') == 'localhost'
print(json.dumps({'local_only': local_only}))
return {'local_only': local_only}
def subcommand_set_config(arguments):
@privileged
def set_config(local_only: bool):
"""Set the configuration."""
config = '''<?php
$config['log_driver'] = 'syslog';
'''
if arguments.local_only == 'True':
if local_only:
config += '''
$config['default_host'] = 'localhost';
$config['mail_domain'] = '%n';
@ -85,16 +68,3 @@ $config['smtp_helo_host'] = 'localhost';
'''
_config_file.write_text(config, encoding='utf-8')
def main():
"""Parse arguments and perform all duties."""
arguments = parse_arguments()
subcommand = arguments.subcommand.replace('-', '_')
subcommand_method = globals()['subcommand_' + subcommand]
subcommand_method(arguments)
if __name__ == '__main__':
main()

View File

@ -6,9 +6,9 @@ Views for roundcube.
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from plinth.modules import roundcube
from plinth.views import AppView
from . import privileged
from .forms import RoundcubeForm
@ -20,7 +20,7 @@ class RoundcubeAppView(AppView):
def get_initial(self):
"""Return the values to fill in the form."""
initial = super().get_initial()
initial['local_only'] = roundcube.get_config()['local_only']
initial['local_only'] = privileged.get_config()['local_only']
return initial
def form_valid(self, form):
@ -28,7 +28,7 @@ class RoundcubeAppView(AppView):
old_data = form.initial
data = form.cleaned_data
if old_data['local_only'] != data['local_only']:
roundcube.set_config(data['local_only'])
privileged.set_config(data['local_only'])
messages.success(self.request, _('Configuration updated'))
return super().form_valid(form)