diff --git a/.gitignore b/.gitignore index cb78e40b..f1083404 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ dist docs/_build/ .tox dist +.cache/ diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 33e1de58..d2e10ca7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,12 +6,23 @@ This document describes changes between each past release. 2.0 (unreleased) ---------------- +### Breaking Changes + +- Use a hashed ``ADMIN_PASSWORD`` instead of a clear text one, ``./budget/manage.py generate_password_hash`` can be used to generate a proper password HASH (#236) +- Turn the WSGI file into a python module, renamed from budget/ihatemoney.wsgi to budget/wsgi.py. Please update your Apache configuration! +- Admin privileges are required to access the dashboard + ### Changed -- **BREAKING CHANGE** Use a hashed ``ADMIN_PASSWORD`` instead of a clear text one, ``./budget/manage.py generate_password_hash`` can be used to generate a proper password HASH (#236) -- **BREAKING CHANGE** Turn the WSGI file into a python module, renamed from budget/ihatemoney.wsgi to budget/wsgi.py. Please update your Apache configuration! - Changed the recommended gunicorn configuration to use the wsgi module as an entrypoint +### Added + +- Add a new setting to allow public project creation (ALLOW_PUBLIC_PROJECT_CREATION) +- With admin credentials, one can access every project +- Add delete and edit project actions in the dashboard +- Add a new setting to activate the dashboard (ACTIVATE_ADMIN_DASHBOARD) +- Add a link to the dashboard in the navigation bar when it is activated ### Removed diff --git a/budget/default_settings.py b/budget/default_settings.py index 15fe9cdd..e656ece2 100644 --- a/budget/default_settings.py +++ b/budget/default_settings.py @@ -12,3 +12,7 @@ MAIL_DEFAULT_SENDER = ("Budget manager", "budget@notmyidea.org") ACTIVATE_DEMO_PROJECT = True ADMIN_PASSWORD = "" + +ALLOW_PUBLIC_PROJECT_CREATION = True + +ACTIVATE_ADMIN_DASHBOARD = False diff --git a/budget/static/css/main.css b/budget/static/css/main.css index 54a00081..aedb2d15 100644 --- a/budget/static/css/main.css +++ b/budget/static/css/main.css @@ -169,6 +169,29 @@ footer{ background: url('../images/edit.png') no-repeat right; } +.project-actions { + padding-top: 10px; + text-align: center; +} + +.project-actions > .delete, .project-actions > .edit { + font-size: 0px; + display: block; + width: 16px; + height: 16px; + margin: 2px; + margin-left: 5px; + float: left; +} + +.project-actions > .delete{ + background: url('../images/delete.png') no-repeat right; +} + +.project-actions > .edit{ + background: url('../images/edit.png') no-repeat right; +} + .balance .balance-value{ text-align:right; } diff --git a/budget/templates/admin.html b/budget/templates/admin.html new file mode 100644 index 00000000..95fe68b2 --- /dev/null +++ b/budget/templates/admin.html @@ -0,0 +1,12 @@ +{% extends "layout.html" %} +{% block content %} +

Authentication

+ +{% if is_admin_auth_enabled %} +
+ {{ forms.admin(form) }} +
+{% else %} +
{{ _("Administration tasks are currently not activated.") }}
+{% endif %} +{% endblock %} diff --git a/budget/templates/authenticate.html b/budget/templates/authenticate.html index f241c487..98914d09 100644 --- a/budget/templates/authenticate.html +++ b/budget/templates/authenticate.html @@ -7,13 +7,7 @@ to") }} {{ _("create it") }}{{ _("?") }}

{% endif %} -{% if admin_auth %} -
- {{ forms.admin(form) }} -
-{% else %}
{{ forms.authenticate(form) }}
-{% endif %} {% endblock %} diff --git a/budget/templates/dashboard.html b/budget/templates/dashboard.html index 3f50915a..231462b3 100644 --- a/budget/templates/dashboard.html +++ b/budget/templates/dashboard.html @@ -1,8 +1,8 @@ {% extends "layout.html" %} {% block content %} - +{% if is_admin_dashboard_activated %} - + {% for project in projects|sort(attribute='name') %} @@ -13,9 +13,16 @@ {% endif %} + {% endfor %}
{{ _("Project") }}{{ _("Number of members") }}{{ _("Number of bills") }}{{_("Newest bill")}}{{_("Oldest bill")}}
{{ _("Project") }}{{ _("Number of members") }}{{ _("Number of bills") }}{{_("Newest bill")}}{{_("Oldest bill")}}{{_("Actions")}}
{{ project.name }}{{ project.members | count }}{{ project.get_bills().count() }} + {{ _('edit') }} + {{ _('delete') }} +
+{% else %} +
{{ _("The Dashboard is currently deactivated.") }}
+{% endif %} {% endblock %} diff --git a/budget/templates/home.html b/budget/templates/home.html index 9bfe4671..a628eccc 100644 --- a/budget/templates/home.html +++ b/budget/templates/home.html @@ -28,9 +28,7 @@
- {% if is_admin_mode_enabled %} - ...{{ _("or create a new one") }} - {% else %} + {% if is_public_project_creation_allowed %}
...{{ _("or create a new one") }} @@ -40,6 +38,8 @@
+ {% else %} + ...{{ _("or create a new one") }} {% endif %} diff --git a/budget/templates/layout.html b/budget/templates/layout.html index 6ecff413..07edb0c1 100644 --- a/budget/templates/layout.html +++ b/budget/templates/layout.html @@ -70,6 +70,9 @@ {% endif %} + {% if g.show_admin_dashboard_link %} + + {% endif %} diff --git a/budget/tests/tests.py b/budget/tests/tests.py index 386920f5..040936f3 100644 --- a/budget/tests/tests.py +++ b/budget/tests/tests.py @@ -376,8 +376,17 @@ class BudgetTestCase(TestCase): c.get("/exit") self.assertNotIn('raclette', session) + # test that whith admin credentials, one can access every project + run.app.config['ADMIN_PASSWORD'] = generate_password_hash("pass") + with run.app.test_client() as c: + resp = c.post("/admin?goto=%2Fraclette", data={'admin_password': 'pass'}) + self.assertNotIn("Authentication", resp.data.decode('utf-8')) + self.assertTrue(session['is_admin']) + def test_admin_authentication(self): run.app.config['ADMIN_PASSWORD'] = generate_password_hash("pass") + # Disable public project creation so we have an admin endpoint to test + run.app.config['ALLOW_PUBLIC_PROJECT_CREATION'] = False # test the redirection to the authentication page when trying to access admin endpoints resp = self.app.get("/create") @@ -598,8 +607,17 @@ class BudgetTestCase(TestCase): self.assertIn("Invalid email address", resp.data.decode('utf-8')) def test_dashboard(self): - response = self.app.get("/dashboard") - self.assertEqual(response.status_code, 200) + # test that the dashboard is deactivated by default + resp = self.app.post("/admin?goto=%2Fdashboard", data={'admin_password': 'adminpass'}, + follow_redirects=True) + self.assertIn('
', resp.data.decode('utf-8')) + + # test access to the dashboard when it is activated + run.app.config['ACTIVATE_ADMIN_DASHBOARD'] = True + run.app.config['ADMIN_PASSWORD'] = generate_password_hash("adminpass") + resp = self.app.post("/admin?goto=%2Fdashboard", data={'admin_password': 'adminpass'}, + follow_redirects=True) + self.assertIn('ProjectNumber of members', resp.data.decode('utf-8')) def test_settle_page(self): self.post_project("raclette") diff --git a/budget/translations/fr/LC_MESSAGES/messages.mo b/budget/translations/fr/LC_MESSAGES/messages.mo index 210852b0..5e2cc5fe 100644 Binary files a/budget/translations/fr/LC_MESSAGES/messages.mo and b/budget/translations/fr/LC_MESSAGES/messages.mo differ diff --git a/budget/translations/fr/LC_MESSAGES/messages.po b/budget/translations/fr/LC_MESSAGES/messages.po index 0f3339ef..09b5af70 100644 --- a/budget/translations/fr/LC_MESSAGES/messages.po +++ b/budget/translations/fr/LC_MESSAGES/messages.po @@ -247,6 +247,10 @@ msgstr "le créer" msgid "?" msgstr " ?" +#: templates/authenticate.html:7 +msgid "Administration tasks are currently not activated." +msgstr "Les tâches d'administration sont actuellement désactivées." + #: templates/create_project.html:4 msgid "Create a new project" msgstr "Créer un nouveau projet" @@ -271,6 +275,10 @@ msgstr "Facture la plus récente" msgid "Oldest bill" msgstr "Facture la plus ancienne" +#: templates/dashboard.html:25 +msgid "The Dashboard is currently deactivated." +msgstr "La page d'administration est actuellement désactivée." + #: templates/edit_project.html:6 templates/list_bills.html:24 msgid "you sure?" msgstr "c'est sûr ?" diff --git a/budget/web.py b/budget/web.py index ba771247..ea49a2e2 100644 --- a/budget/web.py +++ b/budget/web.py @@ -34,17 +34,26 @@ main = Blueprint("main", __name__) mail = Mail() -def requires_admin(f): +def requires_admin(bypass=None): """Require admin permissions for @requires_admin decorated endpoints. Has no effect if ADMIN_PASSWORD is empty (default value) + The bypass variable is optionnal and used to conditionnaly bypass the admin authentication + It expects a tuple containing the name of an application setting and its expected value + e.g. if you use @require_admin(bypass=("ALLOW_PUBLIC_PROJECT_CREATION", True)) + Admin authentication will be bypassed when ALLOW_PUBLIC_PROJECT_CREATION is set to True """ - @wraps(f) - def admin_auth(*args, **kws): - is_admin = session.get('is_admin') - if is_admin or not current_app.config['ADMIN_PASSWORD']: - return f(*args, **kws) - raise Redirect303(url_for('.admin', goto=request.path)) - return admin_auth + def check_admin(f): + @wraps(f) + def admin_auth(*args, **kws): + is_admin_auth_bypassed = False + if bypass is not None and current_app.config.get(bypass[0]) == bypass[1]: + is_admin_auth_bypassed = True + is_admin = session.get('is_admin') + if is_admin or is_admin_auth_bypassed: + return f(*args, **kws) + raise Redirect303(url_for('.admin', goto=request.path)) + return admin_auth + return check_admin @main.url_defaults @@ -59,10 +68,21 @@ def add_project_id(endpoint, values): values['project_id'] = g.project.id +@main.url_value_preprocessor +def set_show_admin_dashboard_link(endpoint, values): + """Set show_admin_dashboard_link application wide + so this variable can be used in the layout template + """ + + g.show_admin_dashboard_link = (current_app.config["ACTIVATE_ADMIN_DASHBOARD"] and + current_app.config["ADMIN_PASSWORD"]) + + @main.url_value_preprocessor def pull_project(endpoint, values): """When a request contains a project_id value, transform it directly into a project by checking the credentials are stored in session. + With admin credentials, one can access every project. If not, redirect the user to an authentication form """ @@ -76,7 +96,8 @@ def pull_project(endpoint, values): if not project: raise Redirect303(url_for(".create_project", project_id=project_id)) - if project.id in session and session[project.id] == project.password: + is_admin = session.get('is_admin') + if project.id in session and session[project.id] == project.password or is_admin: # add project into kwargs and call the original function g.project = project else: @@ -87,9 +108,12 @@ def pull_project(endpoint, values): @main.route("/admin", methods=["GET", "POST"]) def admin(): - """Admin authentication""" + """Admin authentication + When ADMIN_PASSWORD is empty, admin authentication is deactivated + """ form = AdminAuthenticationForm() goto = request.args.get('goto', url_for('.home')) + is_admin_auth_enabled = bool(current_app.config['ADMIN_PASSWORD']) if request.method == "POST": if form.validate(): if check_password_hash(current_app.config['ADMIN_PASSWORD'], form.admin_password.data): @@ -99,7 +123,8 @@ def admin(): else: msg = _("This admin password is not the right one") form.errors['admin_password'] = [msg] - return render_template("authenticate.html", form=form, admin_auth=True) + return render_template("admin.html", form=form, + is_admin_auth_enabled=is_admin_auth_enabled) @main.route("/authenticate", methods=["GET", "POST"]) @@ -157,18 +182,17 @@ def authenticate(project_id=None): def home(): project_form = ProjectForm() auth_form = AuthenticationForm() - # If ADMIN_PASSWORD is empty we consider that admin mode is disabled - is_admin_mode_enabled = bool(current_app.config['ADMIN_PASSWORD']) + is_public_project_creation_allowed = current_app.config['ALLOW_PUBLIC_PROJECT_CREATION'] is_demo_project_activated = current_app.config['ACTIVATE_DEMO_PROJECT'] return render_template("home.html", project_form=project_form, is_demo_project_activated=is_demo_project_activated, - is_admin_mode_enabled=is_admin_mode_enabled, + is_public_project_creation_allowed=is_public_project_creation_allowed, auth_form=auth_form, session=session) @main.route("/create", methods=["GET", "POST"]) -@requires_admin +@requires_admin(bypass=("ALLOW_PUBLIC_PROJECT_CREATION", True)) def create_project(): form = ProjectForm() if request.method == "GET" and 'project_id' in request.values: @@ -284,7 +308,7 @@ def delete_project(): g.project.remove_project() flash(_('Project successfully deleted')) - return redirect(url_for(".home")) + return redirect(request.headers.get('Referer') or url_for('.home')) @main.route("/exit") @@ -497,5 +521,8 @@ def settle_bill(): @main.route("/dashboard") +@requires_admin() def dashboard(): - return render_template("dashboard.html", projects=Project.query.all()) + is_admin_dashboard_activated = current_app.config['ACTIVATE_ADMIN_DASHBOARD'] + return render_template("dashboard.html", projects=Project.query.all(), + is_admin_dashboard_activated=is_admin_dashboard_activated) diff --git a/docs/installation.rst b/docs/installation.rst index 3cd143d0..610a844b 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -72,23 +72,30 @@ ihatemoney relies on a configuration file. If you run the application for the first time, you will need to take a few moments to configure the application properly. -+----------------------------+---------------------------+----------------------------------------------------------------------------------------+ -| Setting name | Default | What does it do? | -+============================+===========================+========================================================================================+ -| SQLALCHEMY_DATABASE_URI | ``sqlite:///budget.db`` | Specifies the type of backend to use and its location. More information | -| | | on the format used can be found on `the SQLAlchemy documentation`. | -+----------------------------+---------------------------+----------------------------------------------------------------------------------------+ -| SECRET_KEY | ``tralala`` | The secret key used to encrypt the cookies. **This needs to be changed**. | -+----------------------------+---------------------------+----------------------------------------------------------------------------------------+ -| MAIL_DEFAULT_SENDER | ``("Budget manager", | A python tuple describing the name and email adress to use when sending | -| | "budget@notmyidea.org")`` | emails. | -+----------------------------+---------------------------+----------------------------------------------------------------------------------------+ -| ACTIVATE_DEMO_PROJECT | ``True`` | If set to `True`, a demo project will be available on the frontpage. | -+----------------------------+---------------------------+----------------------------------------------------------------------------------------+ -| | ``""`` | If not empty, the specified password must be entered to create new projects. | -| ADMIN_PASSWORD | | To generate the proper password HASH, use ``./budget/manage.py generate_password_hash``| -| | | and copy its output into the value of *ADMIN_PASSWORD*. | -+----------------------------+---------------------------+----------------------------------------------------------------------------------------+ ++------------------------------+---------------------------+----------------------------------------------------------------------------------------+ +| Setting name | Default | What does it do? | ++==============================+===========================+========================================================================================+ +| SQLALCHEMY_DATABASE_URI | ``sqlite:///budget.db`` | Specifies the type of backend to use and its location. More information | +| | | on the format used can be found on `the SQLAlchemy documentation`. | ++------------------------------+---------------------------+----------------------------------------------------------------------------------------+ +| SECRET_KEY | ``tralala`` | The secret key used to encrypt the cookies. **This needs to be changed**. | ++------------------------------+---------------------------+----------------------------------------------------------------------------------------+ +| MAIL_DEFAULT_SENDER | ``("Budget manager", | A python tuple describing the name and email adress to use when sending | +| | "budget@notmyidea.org")`` | emails. | ++------------------------------+---------------------------+----------------------------------------------------------------------------------------+ +| ACTIVATE_DEMO_PROJECT | ``True`` | If set to `True`, a demo project will be available on the frontpage. | ++------------------------------+---------------------------+----------------------------------------------------------------------------------------+ +| | | Hashed password to access protected endpoints. When left empty, all administrative | +| ADMIN_PASSWORD | ``""`` | tasks are disabled. | +| | | To generate the proper password HASH, use ``./budget/manage.py generate_password_hash``| +| | | and copy its output into the value of *ADMIN_PASSWORD*. | ++------------------------------+---------------------------+----------------------------------------------------------------------------------------+ +| ALLOW_PUBLIC_PROJECT_CREATION| ``True`` | If set to `True`, everyone can create a project without entering the admin password | +| | | If set to `False`, a non empty ADMIN_PASSWORD needs to be set | ++------------------------------+---------------------------+----------------------------------------------------------------------------------------+ +| ACTIVATE_ADMIN_DASHBOARD | ``False`` | If set to `True`, the dashboard will become accessible entering the admin password | +| | | If set to `True`, a non empty ADMIN_PASSWORD needs to be set | ++------------------------------+---------------------------+----------------------------------------------------------------------------------------+ .. _`the SQLAlechemy documentation`: http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls