Veiko Aasa 31eb35cb9e
samba: Hide common system partitions
Hide some common partitions where users shouldn't create a share -
/.snapshots, /boot and /boot/firmware.

Tested that mount points 'boot' and '.snapshots' do not appear in the
list of shareable disks on Pioneer Freedombox.

Signed-off-by: Veiko Aasa <veiko17@disroot.org>
[sunil: Add /boot/efi to list of ignored mount points]
[sunil: Update test to check if mounts points are ignored]
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
2020-07-21 17:14:43 -07:00

105 lines
3.3 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Views for samba module.
"""
import logging
import urllib.parse
from collections import defaultdict
from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST
from plinth import views
from plinth.errors import ActionError
from plinth.modules import samba, storage
logger = logging.getLogger(__name__)
def get_share_mounts():
"""Return list of mount points."""
ignore_points = ('/boot', '/boot/efi', '/boot/firmware', '/.snapshots')
return [
mount for mount in storage.get_mounts()
if mount['mount_point'] not in ignore_points
]
class SambaAppView(views.AppView):
"""Samba sharing basic configuration."""
app_id = 'samba'
template_name = 'samba.html'
def get_context_data(self, *args, **kwargs):
"""Return template context data."""
context = super().get_context_data(*args, **kwargs)
disks = get_share_mounts()
shares = samba.get_shares()
for disk in disks:
disk['name'] = samba.disk_name(disk['mount_point'])
context['disks'] = disks
shared_mounts = defaultdict(list)
for share in shares:
shared_mounts[share['mount_point']].append(share['share_type'])
context['shared_mounts'] = shared_mounts
context['share_types'] = [('open', _('Open Share')),
('group', _('Group Share')),
('home', _('Home Share'))]
unavailable_shares = []
for share in shares:
for disk in disks:
if share['mount_point'] == disk['mount_point']:
break
else:
unavailable_shares.append(share)
context['unavailable_shares'] = unavailable_shares
context['users'] = samba.get_users()
return context
@require_POST
def share(request, mount_point):
"""Enable sharing, given its root path.
mount_point is urlquoted.
"""
mount_point = urllib.parse.unquote(mount_point)
filesystem = request.POST.get('filesystem_type', '')
share_types = ['open', 'group', 'home']
for share_type in share_types:
action = request.POST.get(share_type + '_share', '')
if action == 'enable':
try:
samba.add_share(mount_point, share_type, filesystem)
messages.success(request, _('Share enabled.'))
except ActionError as exception:
logger.exception('Error enabling share')
messages.error(
request,
_('Error enabling share: {error_message}').format(
error_message=exception))
elif action == 'disable':
try:
samba.delete_share(mount_point, share_type)
messages.success(request, _('Share disabled.'))
except ActionError as exception:
logger.exception('Error disabling share')
messages.error(
request,
_('Error disabling share: {error_message}').format(
error_message=exception))
return redirect(reverse('samba:index'))