mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-02-04 08:13:38 +00:00
Add an option to enable/disable public registrations in mediawiki
Reviewed-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
parent
98b39aaf63
commit
5e2151d6b4
@ -41,6 +41,11 @@ def parse_arguments():
|
||||
subparsers.add_parser('enable', help='Enable MediaWiki')
|
||||
subparsers.add_parser('disable', help='Disable MediaWiki')
|
||||
subparsers.add_parser('setup', help='Setup MediaWiki')
|
||||
help_pubreg = 'Enable/Disable/Status public user registration.'
|
||||
pubreg = subparsers.add_parser('public-registration', help=help_pubreg)
|
||||
pubreg.add_argument('command', choices=('true', 'false', 'status'),
|
||||
help=help_pubreg)
|
||||
|
||||
change_password = subparsers.add_parser('change-password',
|
||||
help='Change user password')
|
||||
change_password.add_argument('--username', default='admin',
|
||||
@ -129,6 +134,34 @@ def subcommand_disable(_):
|
||||
action_utils.service_disable('mediawiki-jobrunner')
|
||||
|
||||
|
||||
def subcommand_public_registration(argument):
|
||||
""" Enable public registrations for mediawiki"""
|
||||
with open(CONF_FILE, 'r+') as file_handle:
|
||||
config_line_index = None
|
||||
lines = file_handle.readlines()
|
||||
config_line = "$wgGroupPermissions['*']['createaccount'] = "
|
||||
for i in range(len(lines)):
|
||||
line = lines[i]
|
||||
if line.startswith("$wgGroupPermissions['*']['createaccount']"):
|
||||
config_line_index = i
|
||||
break
|
||||
|
||||
if config_line_index is None:
|
||||
lines.append(config_line)
|
||||
print('disabled')
|
||||
else:
|
||||
status = lines[config_line_index].strip(' \n').split('=')[1]
|
||||
if argument.command == 'status':
|
||||
print(status)
|
||||
return
|
||||
elif status != argument.command:
|
||||
config_line = config_line + argument.command + '\n'
|
||||
lines[config_line_index] = config_line
|
||||
file_handle.seek(0)
|
||||
file_handle.writelines(lines)
|
||||
file_handle.truncate()
|
||||
|
||||
|
||||
def main():
|
||||
"""Parse arguments and perform all duties."""
|
||||
arguments = parse_arguments()
|
||||
|
||||
@ -126,3 +126,10 @@ def diagnose():
|
||||
check_certificate=False))
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def get_public_registration_status():
|
||||
"""Return whether public registration is enabled."""
|
||||
output = actions.superuser_run('mediawiki',
|
||||
['public-registration', 'status'])
|
||||
return output.strip() == 'true'
|
||||
|
||||
@ -30,3 +30,10 @@ class MediaWikiForm(ServiceForm): # pylint: disable=W0232
|
||||
'Set a new password for MediaWiki\'s administrator account (admin). '
|
||||
'Leave this field blank to keep the current password.'),
|
||||
required=False, widget=forms.PasswordInput)
|
||||
enable_public_registration = forms.BooleanField(
|
||||
label=_('Enable Public Registration'),
|
||||
required=False,
|
||||
help_text=_(
|
||||
'Disabling public registration means that you restrict account '
|
||||
'creation from outside the freedombox'))
|
||||
|
||||
|
||||
@ -27,6 +27,7 @@ from plinth import actions, views
|
||||
from plinth.modules import mediawiki
|
||||
|
||||
from .forms import MediaWikiForm
|
||||
from . import get_public_registration_status
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -41,13 +42,47 @@ class MediaWikiServiceView(views.ServiceView):
|
||||
manual_page = mediawiki.manual_page
|
||||
show_status_block = False
|
||||
|
||||
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):
|
||||
"""Apply the changes submitted in the form."""
|
||||
form_data = form.cleaned_data
|
||||
|
||||
if form_data['password']:
|
||||
old_config = self.get_initial()
|
||||
new_config = form.cleaned_data
|
||||
app_same = old_config['is_enabled'] == new_config['is_enabled']
|
||||
pubreg_same = old_config['enable_public_registration'] == \
|
||||
new_config['enable_public_registration']
|
||||
if new_config['password']:
|
||||
actions.superuser_run('mediawiki', ['change-password'],
|
||||
input=form_data['password'].encode())
|
||||
input=new_config['password'].encode())
|
||||
messages.success(self.request, _('Password updated'))
|
||||
if app_same and pubreg_same:
|
||||
if not self.request._messages._queued_messages:
|
||||
messages.info(self.request, _('Setting unchanged'))
|
||||
elif not app_same:
|
||||
if new_config['is_enabled']:
|
||||
self.service.enable()
|
||||
messages.success(self.request, _('Application enabled'))
|
||||
else:
|
||||
self.service.disable()
|
||||
messages.success(self.request, _('Application disabled'))
|
||||
|
||||
if not pubreg_same:
|
||||
# note action public-registration restarts, if running now
|
||||
if new_config['enable_public_registration']:
|
||||
actions.superuser_run('mediawiki',
|
||||
['public-registration', 'true'])
|
||||
messages.success(self.request,
|
||||
_('Public registration enabled'))
|
||||
else:
|
||||
actions.superuser_run('mediawiki',
|
||||
['public-registration', 'false'])
|
||||
messages.success(self.request,
|
||||
_('Public registration disabled'))
|
||||
|
||||
return super().form_valid(form)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user