mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-13 10:30:16 +00:00
sharing: Allow directories to be publicly shared
Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
d0ed538bfa
commit
1eec1cf9ce
@ -43,6 +43,8 @@ def parse_arguments():
|
|||||||
add_parser.add_argument('--path', required=True, help='Disk path to share')
|
add_parser.add_argument('--path', required=True, help='Disk path to share')
|
||||||
add_parser.add_argument('--groups', nargs='*',
|
add_parser.add_argument('--groups', nargs='*',
|
||||||
help='List of groups that can access the share')
|
help='List of groups that can access the share')
|
||||||
|
add_parser.add_argument('--is-public', default=False, required=False,
|
||||||
|
help='1 if public share else 0')
|
||||||
|
|
||||||
remove_parser = subparsers.add_parser('remove',
|
remove_parser = subparsers.add_parser('remove',
|
||||||
help='Remove an existing share')
|
help='Remove an existing share')
|
||||||
@ -71,6 +73,7 @@ def subcommand_add(arguments):
|
|||||||
name = arguments.name
|
name = arguments.name
|
||||||
path = arguments.path
|
path = arguments.path
|
||||||
groups = arguments.groups
|
groups = arguments.groups
|
||||||
|
is_public = arguments.is_public
|
||||||
url = '/share/' + name
|
url = '/share/' + name
|
||||||
|
|
||||||
if not os.path.exists(APACHE_CONFIGURATION):
|
if not os.path.exists(APACHE_CONFIGURATION):
|
||||||
@ -90,6 +93,8 @@ def subcommand_add(arguments):
|
|||||||
aug.set('$conf/Location[last()]/directive[last() + 1]', 'Include')
|
aug.set('$conf/Location[last()]/directive[last() + 1]', 'Include')
|
||||||
aug.set('$conf/Location[last()]/directive[last()]/arg',
|
aug.set('$conf/Location[last()]/directive[last()]/arg',
|
||||||
'includes/freedombox-sharing.conf')
|
'includes/freedombox-sharing.conf')
|
||||||
|
|
||||||
|
if is_public != "true":
|
||||||
aug.set('$conf/Location[last()]/directive[last() + 1]', 'Include')
|
aug.set('$conf/Location[last()]/directive[last() + 1]', 'Include')
|
||||||
aug.set('$conf/Location[last()]/directive[last()]/arg',
|
aug.set('$conf/Location[last()]/directive[last()]/arg',
|
||||||
'includes/freedombox-single-sign-on.conf')
|
'includes/freedombox-single-sign-on.conf')
|
||||||
@ -97,8 +102,13 @@ def subcommand_add(arguments):
|
|||||||
aug.set('$conf/Location[last()]/IfModule/arg', 'mod_auth_pubtkt.c')
|
aug.set('$conf/Location[last()]/IfModule/arg', 'mod_auth_pubtkt.c')
|
||||||
aug.set('$conf/Location[last()]/IfModule/directive[1]', 'TKTAuthToken')
|
aug.set('$conf/Location[last()]/IfModule/directive[1]', 'TKTAuthToken')
|
||||||
for group_name in groups:
|
for group_name in groups:
|
||||||
aug.set('$conf/Location[last()]/IfModule/directive[1]/arg[last() + 1]',
|
aug.set(
|
||||||
|
'$conf/Location[last()]/IfModule/directive[1]/arg[last() + 1]',
|
||||||
group_name)
|
group_name)
|
||||||
|
else:
|
||||||
|
aug.set('$conf/Location[last()]/directive[last() + 1]', 'Require')
|
||||||
|
aug.set('$conf/Location[last()]/directive[last()]/arg[1]', 'all')
|
||||||
|
aug.set('$conf/Location[last()]/directive[last()]/arg[2]', 'granted')
|
||||||
|
|
||||||
aug.save()
|
aug.save()
|
||||||
|
|
||||||
@ -176,9 +186,17 @@ def _list(aug=None):
|
|||||||
for group in aug.match(location + '//directive["TKTAuthToken"]/arg'):
|
for group in aug.match(location + '//directive["TKTAuthToken"]/arg'):
|
||||||
groups.append(aug.get(group))
|
groups.append(aug.get(group))
|
||||||
|
|
||||||
|
def _is_public():
|
||||||
|
"""Must contain the line 'Require all granted'."""
|
||||||
|
require = location + '//directive["Require"]'
|
||||||
|
return bool(aug.match(require)) and aug.get(
|
||||||
|
require + '/arg[1]') == 'all' and aug.get(
|
||||||
|
require + '/arg[2]') == 'granted'
|
||||||
|
|
||||||
for share in shares:
|
for share in shares:
|
||||||
if share['name'] == name:
|
if share['name'] == name:
|
||||||
share['groups'] = groups
|
share['groups'] = groups
|
||||||
|
share['is_public'] = _is_public()
|
||||||
|
|
||||||
return shares
|
return shares
|
||||||
|
|
||||||
|
|||||||
@ -69,11 +69,12 @@ def list_shares():
|
|||||||
return json.loads(output)['shares']
|
return json.loads(output)['shares']
|
||||||
|
|
||||||
|
|
||||||
def add_share(name, path, groups):
|
def add_share(name, path, groups, is_public):
|
||||||
"""Add a new share by called the action script."""
|
"""Add a new share by called the action script."""
|
||||||
actions.superuser_run(
|
actions.superuser_run('sharing', [
|
||||||
'sharing',
|
'add', '--name', name, '--path', path, '--is-public', is_public,
|
||||||
['add', '--name', name, '--path', path, '--groups'] + groups)
|
'--groups'
|
||||||
|
] + groups)
|
||||||
|
|
||||||
|
|
||||||
def remove_share(name):
|
def remove_share(name):
|
||||||
|
|||||||
@ -22,8 +22,8 @@ from django import forms
|
|||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from plinth.modules.users.forms import get_group_choices
|
|
||||||
from plinth.modules import sharing
|
from plinth.modules import sharing
|
||||||
|
from plinth.modules.users.forms import get_group_choices
|
||||||
|
|
||||||
|
|
||||||
class AddShareForm(forms.Form):
|
class AddShareForm(forms.Form):
|
||||||
@ -39,11 +39,15 @@ class AddShareForm(forms.Form):
|
|||||||
label=_('Path to share'), strip=True, help_text=_(
|
label=_('Path to share'), strip=True, help_text=_(
|
||||||
'Disk path to a folder on this server that you intend to share.'))
|
'Disk path to a folder on this server that you intend to share.'))
|
||||||
|
|
||||||
|
is_public = forms.BooleanField(
|
||||||
|
label=_('Public share'), required=False, help_text=_(
|
||||||
|
'Make files in this folder available to anyone with the link.'))
|
||||||
|
|
||||||
groups = forms.MultipleChoiceField(
|
groups = forms.MultipleChoiceField(
|
||||||
widget=forms.CheckboxSelectMultiple,
|
widget=forms.CheckboxSelectMultiple, required=False,
|
||||||
label=_('User groups who can read the files in the share'),
|
label=_('User groups that can read the files in the share'),
|
||||||
help_text=_(
|
help_text=_(
|
||||||
'Users who have these permissions will also be able to read the '
|
'Users of the selected user groups will be able to read the '
|
||||||
'files in the share.'))
|
'files in the share.'))
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@ -63,3 +67,13 @@ class AddShareForm(forms.Form):
|
|||||||
raise ValidationError(_('A share with this name already exists.'))
|
raise ValidationError(_('A share with this name already exists.'))
|
||||||
|
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
"""Check that at least one group is added for non-public shares."""
|
||||||
|
super(AddShareForm, self).clean()
|
||||||
|
is_public = self.cleaned_data.get('is_public')
|
||||||
|
groups = self.cleaned_data.get('groups')
|
||||||
|
if not is_public and not groups:
|
||||||
|
raise forms.ValidationError(
|
||||||
|
_('Shares should be either public or shared with at least one group'
|
||||||
|
))
|
||||||
|
|||||||
@ -61,8 +61,9 @@ class AddShareView(SuccessMessageMixin, FormView):
|
|||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
"""Add the share on valid form submission."""
|
"""Add the share on valid form submission."""
|
||||||
sharing.add_share(form.cleaned_data['name'], form.cleaned_data['path'],
|
data = form.cleaned_data
|
||||||
form.cleaned_data['groups'])
|
sharing.add_share(data['name'], data['path'], data['groups'],
|
||||||
|
str(data['is_public']).lower())
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
|
||||||
@ -94,9 +95,9 @@ class EditShareView(SuccessMessageMixin, FormView):
|
|||||||
"""Add the share on valid form submission."""
|
"""Add the share on valid form submission."""
|
||||||
if form.initial != form.cleaned_data:
|
if form.initial != form.cleaned_data:
|
||||||
sharing.remove_share(form.initial['name'])
|
sharing.remove_share(form.initial['name'])
|
||||||
sharing.add_share(form.cleaned_data['name'],
|
data = form.cleaned_data
|
||||||
form.cleaned_data['path'],
|
sharing.add_share(data['name'], data['path'], data['groups'],
|
||||||
form.cleaned_data['groups'])
|
str(data['is_public']).lower())
|
||||||
|
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user