mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
bepasty: Simplify configuration file handling
- Stick to a subset of allowed configuration file syntax (full syntax). Only KEY = VALUE statements are allowed. Values can be full JSON (valid python). - Use augeas to read as key/value pairs and then parse the values in JSON. - Add convenience methods to read and write configuration files. - Read the entire configuration file in a single action. - Internationalize the permission strings displayed to the user. - Pass password during remove-password operation via stdin instead of command line. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Joseph Nuthalapati <njoseph@riseup.net>
This commit is contained in:
1 parent
028137a4e4
commit
3e2df420cf
3 files changed
+140
-143
No files matched your search
@@ -100,9 +100,9 @@ def setup(helper, old_version=None):
|
||||
helper.call('post', app.enable)
|
||||
|
||||
|
||||
def list_passwords():
|
||||
"""Get a list of passwords, their permissions and comments"""
|
||||
output = actions.superuser_run('bepasty', ['list-passwords'])
|
||||
def get_configuration():
|
||||
"""Get a full configuration including passwords and defaults."""
|
||||
output = actions.superuser_run('bepasty', ['get-configuration'])
|
||||
return json.loads(output)
|
||||
|
||||
|
||||
@@ -116,16 +116,9 @@ def add_password(permissions, comment=None):
|
||||
|
||||
|
||||
def remove_password(password):
|
||||
"""Remove a password and its permissions"""
|
||||
actions.superuser_run('bepasty',
|
||||
['remove-password', '--password', password])
|
||||
|
||||
|
||||
def get_default_permissions():
|
||||
"""Get default permissions"""
|
||||
output = actions.superuser_run('bepasty', ['get-default']).strip()
|
||||
output = 'read list' if output == 'list read' else output
|
||||
return output.strip()
|
||||
"""Remove a password and its permissions."""
|
||||
actions.superuser_run('bepasty', ['remove-password'],
|
||||
input=password.encode())
|
||||
|
||||
|
||||
def set_default_permissions(permissions):
|
||||
|
||||
@@ -24,16 +24,51 @@ class BepastyView(AppView):
|
||||
form_class = SetDefaultPermissionsForm
|
||||
template_name = 'bepasty.html'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initialize the view."""
|
||||
super().__init__(*args, **kwargs)
|
||||
self.conf = None
|
||||
|
||||
def _get_configuration(self):
|
||||
"""Return the current configuration."""
|
||||
if not self.conf:
|
||||
self.conf = bepasty.get_configuration()
|
||||
|
||||
return self.conf
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""Return additional context for rendering the template."""
|
||||
permissions_short_text = {
|
||||
'read': _('Read'),
|
||||
'create': _('Create'),
|
||||
'list': _('List'),
|
||||
'delete': _('Delete'),
|
||||
'admin': _('Admin')
|
||||
}
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['passwords'] = bepasty.list_passwords()
|
||||
conf = self._get_configuration()
|
||||
passwords = []
|
||||
for password, permissions in conf['PERMISSIONS'].items():
|
||||
permissions = permissions.split(',')
|
||||
permissions = [
|
||||
str(permissions_short_text[permission])
|
||||
for permission in permissions if permission
|
||||
]
|
||||
passwords.append({
|
||||
'password': password,
|
||||
'permissions': ', '.join(permissions),
|
||||
'comment': conf['PERMISSION_COMMENTS'].get(password) or ''
|
||||
})
|
||||
context['passwords'] = passwords
|
||||
return context
|
||||
|
||||
def get_initial(self):
|
||||
"""Return the status of the service to fill in the form."""
|
||||
initial = super().get_initial()
|
||||
initial['default_permissions'] = bepasty.get_default_permissions()
|
||||
default = self._get_configuration().get('DEFAULT_PERMISSIONS', '')
|
||||
default = ' '.join(default.split(','))
|
||||
default = 'read list' if default == 'list read' else default
|
||||
initial['default_permissions'] = default
|
||||
return initial
|
||||
|
||||
def form_valid(self, form):
|
||||
|
||||
Reference in new issue
Block a user