bepasty: New app for file upload and sharing

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
James Valleroy authored and Joseph Nuthalapati committed 2020-08-21 21:50:38 +05:30
1 parent 66537b1f9c
commit 7edc2f4e13
17 files changed
+738

No files matched your search

+115
View File
@@ -0,0 +1,115 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
FreedomBox app for bepasty.
"""
import json
from django.utils.translation import ugettext_lazy as _
from plinth import actions
from plinth import app as app_module
from plinth import frontpage, menu
from plinth.modules.apache.components import Uwsgi, Webserver
from plinth.modules.firewall.components import Firewall
from .manifest import backup, clients # noqa, pylint: disable=unused-import
version = 1
managed_packages = ['bepasty', 'uwsgi', 'uwsgi-plugin-python3']
managed_services = ['uwsgi']
description = [
_('bepasty is a web application that allows all types of files to be '
'uploaded and shared.'),
_('bepasty does not use usernames for login. It only uses passwords. For '
'each password, a set of permissions can be selected. Once you have '
'created a password, you can share it with the users who should have the'
' associated permissions.'),
_('You can also create multiple passwords with the same set of privileges,'
' and distribute them to different people or groups. This will allow '
'you to later revoke access for a single person or group, by removing '
'their password from the list.'),
]
app = None
PERMISSIONS = {
'read': _('Read files (using their web address)'),
'create': _('Create or upload files'),
'list': _('List all files'),
'delete': _('Delete files'),
'admin': _('Admin (lock/unlock files)'),
}
class BepastyApp(app_module.App):
"""FreedomBox app for bepasty."""
app_id = 'bepasty'
def __init__(self):
"""Create components for the app."""
super().__init__()
info = app_module.Info(self.app_id, version, name=_('bepasty'),
icon_filename='bepasty',
short_description=_('File Sharing'),
description=description, manual_page='bepasty',
clients=clients)
self.add(info)
menu_item = menu.Menu('menu-bepasty', info.name,
info.short_description, info.icon_filename,
'bepasty:index', parent_url_name='apps')
self.add(menu_item)
shortcut = frontpage.Shortcut('shortcut-bepasty', info.name,
info.short_description,
info.icon_filename, '/bepasty',
clients=clients)
self.add(shortcut)
firewall = Firewall('firewall-bepasty', info.name,
ports=['http', 'https'], is_external=True)
self.add(firewall)
uwsgi = Uwsgi('uwsgi-bepasty', 'bepasty-freedombox')
self.add(uwsgi)
webserver = Webserver('webserver-bepasty', 'bepasty-freedombox')
self.add(webserver)
def setup(helper, old_version=None):
"""Install and configure the module."""
helper.install(managed_packages)
helper.call('post', actions.superuser_run, 'bepasty',
['setup', '--domain-name', 'freedombox.local'])
helper.call('post', app.enable)
def list_passwords():
"""Get a list of passwords, their permissions and comments"""
output = actions.superuser_run('bepasty', ['list-passwords'])
return json.loads(output)
def add_password(permissions=None, comment=None):
"""Generate a password with given permissions"""
command = ['add-password']
if permissions:
command += ['--permissions'] + permissions
if comment:
command += ['--comment', comment]
actions.superuser_run('bepasty', command)
def remove_password(password):
"""Remove a password and its permissions"""
actions.superuser_run('bepasty',
['remove-password', '--password', password])
@@ -0,0 +1,16 @@
##
## On all sites, provide bepasty on a path: /bepasty
##
# Redirect /bepasty to /bepasty/
<Location ~ ^/bepasty$>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/bepasty$
RewriteRule .* /bepasty/ [R=301,L]
</IfModule>
</Location>
<Location /bepasty/>
ProxyPass unix:/run/uwsgi/app/bepasty-freedombox/socket|uwsgi://bepasty/
</Location>
@@ -0,0 +1 @@
plinth.modules.bepasty
@@ -0,0 +1,31 @@
# Use packaged file after #966314 is done.
[uwsgi]
# Who will run the code
uid = bepasty
gid = bepasty
# disable logging for privacy
#disable-logging = true
autoload = false
# Number of workers (usually CPU count)
workers = 2
# The right granted on the created socket
chmod-socket = 666
# Plugin to use and interpretor config
single-interpreter = true
master = true
plugin = python3
enable-threads = true
lazy-apps = true
# Module to import
module = bepasty.wsgi
env = BEPASTY_CONFIG=/etc/bepasty-freedombox.conf
pythonpath = /usr/lib/python3/dist-packages/
buffer-size = 32768
+24
View File
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Django forms for bepasty app.
"""
from django import forms
from django.utils.translation import ugettext_lazy as _
from plinth.modules import bepasty
class AddPasswordForm(forms.Form):
"""Form to add a new password."""
permissions = forms.MultipleChoiceField(
choices=bepasty.PERMISSIONS.items(),
widget=forms.CheckboxSelectMultiple, required=False,
label=_('Permissions'), help_text=_(
'Users that log in with this password will have the selected '
'permissions.'))
comment = forms.CharField(
label=_('Comment'), required=False, strip=True, help_text=_(
'Any comment to help you remember the purpose of this password.'))
+24
View File
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
from django.utils.translation import ugettext_lazy as _
from plinth.clients import validate
from plinth.modules.backups.api import validate as validate_backup
clients = validate([{
'name': _('bepasty'),
'platforms': [{
'type': 'web',
'url': '/bepasty'
}]
}])
backup = validate_backup({
'config': {
'files': ['/etc/bepasty-freedombox.conf']
},
'data': {
'directories': ['/var/lib/bepasty']
},
'services': ['uwsgi'],
})
@@ -0,0 +1,55 @@
{% extends "app.html" %}
{% comment %}
# SPDX-License-Identifier: AGPL-3.0-or-later
{% endcomment %}
{% load bootstrap %}
{% load i18n %}
{% block configuration %}
{{ block.super }}
<h3>{% trans "Manage Passwords" %}</h3>
<div class="btn-toolbar">
<a href="{% url 'bepasty:add' %}" class="btn btn-default"
role="button" title="{% trans 'Add password' %}">
<span class="fa fa-plus" aria-hidden="true"></span>
{% trans 'Add password' %}
</a>
</div>
{% if not passwords %}
<p>{% trans 'No passwords currently configured.' %}</p>
{% else %}
<table class="table table-bordered table-condensed table-striped" id="passwords-list">
<thead>
<tr>
<th>{% trans "Password" %}</th>
<th>{% trans "Permissions" %}</th>
<th>{% trans "Comment" %}</th>
<th></th>
</tr>
</thead>
<tbody>
{% for password in passwords %}
<tr id="password-{{ password.password }}" class="password">
<td class="password-password">{{ password.password }}</td>
<td class="password-permissions">{{ password.permissions }}</td>
<td class="password-comment">{{ password.comment }}</td>
<td class="password-operations">
<form class="form form-inline" method="post"
action="{% url 'bepasty:remove' password.password %}">
{% csrf_token %}
<button class="password-remove btn btn-sm btn-default fa fa-trash-o"
type="submit"></button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endblock %}
@@ -0,0 +1,23 @@
{% extends "base.html" %}
{% comment %}
# SPDX-License-Identifier: AGPL-3.0-or-later
{% endcomment %}
{% load bootstrap %}
{% load i18n %}
{% load static %}
{% block content %}
<h3>{{ title }}</h3>
<form class="form form-add" method="post">
{% csrf_token %}
{{ form|bootstrap }}
<input type="submit" class="btn btn-primary"
value="{% trans "Submit" %}"/>
</form>
{% endblock %}
Whitespace-only changes.
@@ -0,0 +1,39 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
@apps @bepasty
Feature: bepasty File Sharing
Run bepasty file upload and sharing app.
Background:
Given I'm a logged in user
Given the bepasty application is installed
Scenario: Enable bepasty application
Given the bepasty application is disabled
When I enable the bepasty application
Then the bepasty site should be available
Scenario: Add password
Given the bepasty application is enabled
When I add a password
Then I should be able to login to bepasty with that password
Scenario: Remove password
Given the bepasty application is enabled
When I remove all passwords
Then I should not be able to login to bepasty with that password
@backups
Scenario: Backup and restore bepasty
Given the bepasty application is enabled
When I add a password
And I create a backup of the bepasty app data with name test_bepasty
And I remove all passwords
And I restore the bepasty app data backup with name test_bepasty
Then the bepasty site should be available
And I should be able to login to bepasty with that password
Scenario: Disable bepasty application
Given the bepasty application is enabled
When I disable the bepasty application
Then the bepasty site should not be available
@@ -0,0 +1,72 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Functional, browser based tests for bepasty app.
"""
from pytest_bdd import scenarios, then, when
from plinth.tests import functional
scenarios('bepasty.feature')
last_password_added = None
@when('I add a password')
def add_password(session_browser):
global last_password_added
_remove_all_passwords(session_browser)
_add_password(session_browser)
last_password_added = _get_password(session_browser)
@when('I remove all passwords')
def remove_all_passwords(session_browser):
_remove_all_passwords(session_browser)
@then('I should be able to login to bepasty with that password')
def should_login(session_browser):
assert _can_login(session_browser, last_password_added)
@then('I should not be able to login to bepasty with that password')
def should_not_login(session_browser):
assert not _can_login(session_browser, last_password_added)
def _add_password(browser):
functional.visit(browser, '/plinth/apps/bepasty/add')
for permission in ['read', 'create', 'list', 'delete', 'admin']:
browser.find_by_css('#id_bepasty-permissions input[value="{}"]'.format(
permission)).check()
browser.fill('bepasty-comment', 'bepasty functional test')
functional.submit(browser, form_class='form-add')
def _remove_all_passwords(browser):
functional.visit(browser, '/plinth/apps/bepasty')
while True:
remove_button = browser.find_by_css('.password-remove')
if remove_button:
functional.submit(browser, remove_button)
else:
break
def _get_password(browser):
functional.visit(browser, '/plinth/apps/bepasty')
return browser.find_by_css('.password-password').first.text
def _can_login(browser, password):
functional.visit(browser, '/bepasty')
logout = browser.find_by_value('Logout')
if logout:
logout.click()
browser.fill('token', password)
login = browser.find_by_xpath('//form//button')
functional.submit(browser, login, '/bepasty')
return bool(browser.find_by_value('Logout'))
+15
View File
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
URLs for the bepasty module.
"""
from django.conf.urls import url
from .views import AddPasswordView, BepastyView, remove
urlpatterns = [
url(r'^apps/bepasty/$', BepastyView.as_view(), name='index'),
url(r'^apps/bepasty/add/$', AddPasswordView.as_view(), name='add'),
url(r'^apps/bepasty/(?P<password>[A-Za-z0-9]+)/remove/$', remove,
name='remove'),
]
+62
View File
@@ -0,0 +1,62 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Views for the bepasty app.
"""
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.http import require_POST
from django.views.generic import FormView
from plinth.modules import bepasty
from plinth.views import AppView
from .forms import AddPasswordForm
class BepastyView(AppView):
"""Serve configuration page."""
app_id = 'bepasty'
diagnostics_module_name = 'bepasty'
template_name = 'bepasty.html'
def get_context_data(self, **kwargs):
"""Return additional context for rendering the template."""
context = super().get_context_data(**kwargs)
context['passwords'] = bepasty.list_passwords()
return context
class AddPasswordView(SuccessMessageMixin, FormView):
"""View to add a new password."""
form_class = AddPasswordForm
prefix = 'bepasty'
template_name = 'bepasty_add.html'
success_url = reverse_lazy('bepasty:index')
success_message = _('Password added.')
def get_context_data(self, **kwargs):
"""Return additional context for rendering the template."""
context = super().get_context_data(**kwargs)
context['title'] = _('Add Password')
return context
def form_valid(self, form):
"""Add the password on valid form submission."""
_add_password(form.cleaned_data)
return super().form_valid(form)
def _add_password(form_data):
bepasty.add_password(form_data['permissions'], form_data['comment'])
@require_POST
def remove(request, password):
"""View to remove a password."""
bepasty.remove_password(password)
messages.success(request, _('Password deleted.'))
return redirect(reverse_lazy('bepasty:index'))