power: Use privileged decorator for actions

Tests:

- Reboot works, the process works in the background showing apps page
- Shutdown works, the process works in the background showing apps page

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-08-25 15:54:43 -07:00 committed by James Valleroy
parent e8ea6fff17
commit 7870d43c33
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 23 additions and 49 deletions

View File

@ -1,43 +0,0 @@
#!/usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Configuration helper for power controls.
"""
import argparse
import subprocess
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('restart', help='Restart the system')
subparsers.add_parser('shutdown', help='Shut down the system')
subparsers.required = True
return parser.parse_args()
def subcommand_restart(_):
"""Restart the system."""
subprocess.call('reboot')
def subcommand_shutdown(_):
"""Shut down the system."""
subprocess.call(['shutdown', 'now'])
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

@ -0,0 +1,18 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Shutdown/restart the system."""
import subprocess
from plinth.actions import privileged
@privileged
def restart():
"""Restart the system."""
subprocess.call('reboot')
@privileged
def shutdown():
"""Shut down the system."""
subprocess.call(['shutdown', 'now'])

View File

@ -1,18 +1,17 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
FreedomBox app for power module.
"""
"""FreedomBox app for power controls."""
from django.forms import Form
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.urls import reverse
from plinth import actions
from plinth import app as app_module
from plinth import package
from plinth.views import AppView
from . import privileged
class PowerAppView(AppView):
"""Show power app main page."""
@ -32,7 +31,7 @@ def restart(request):
form = None
if request.method == 'POST':
actions.superuser_run('power', ['restart'], run_in_background=True)
privileged.restart(_run_in_background=True)
return redirect(reverse('apps'))
app = app_module.App.get('power')
@ -51,7 +50,7 @@ def shutdown(request):
form = None
if request.method == 'POST':
actions.superuser_run('power', ['shutdown'], run_in_background=True)
privileged.shutdown(_run_in_background=True)
return redirect(reverse('apps'))
app = app_module.App.get('power')