diff --git a/CHANGELOG.md b/CHANGELOG.md index ff1e048..d826dd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,10 @@ Changelog for dila ================= -0.3.1 (unreleased) +0.4.0 (unreleased) ------------------ +- Add login page using ldap backend. - Preserve PO metadata. diff --git a/dila-development-compose/docker-compose.yml b/dila-development-compose/docker-compose.yml index 2e83373..c932676 100644 --- a/dila-development-compose/docker-compose.yml +++ b/dila-development-compose/docker-compose.yml @@ -13,7 +13,7 @@ db: POSTGRES_USER: dila POSTGRES_PASSWORD: dila ldap: - image: osixia/openldap:1.1.3 + image: osixia/openldap:1.1.8 volumes: - ../acceptance_tests/test.ldif:/scripts/test.ldif:ro environment: diff --git a/dila/frontend/flask/authenticate_views.py b/dila/frontend/flask/authenticate_views.py index 8e1de7a..3eeefff 100644 --- a/dila/frontend/flask/authenticate_views.py +++ b/dila/frontend/flask/authenticate_views.py @@ -1,15 +1,34 @@ import flask +from cached_property import cached_property from flask import views -from dila.frontend.flask import template_tools +from dila.frontend.flask import forms blueprint = flask.Blueprint('authenticate', __name__) -template_tools.setup_language_context(blueprint) class LoginView(views.MethodView): def get(self): return flask.render_template('login.html', **self.context) + def post(self): + if self.form.validate(): + username = self.form.username.data + flask.session['username'] = username + return flask.redirect(flask.url_for('main.home')) + else: + return self.get() + + + @property + def context(self): + return { + 'form': self.form, + } + + @cached_property + def form(self): + return forms.LoginForm() + blueprint.add_url_rule('/login/', view_func=LoginView.as_view('login')) diff --git a/dila/frontend/flask/forms.py b/dila/frontend/flask/forms.py index 0298ab5..486c543 100644 --- a/dila/frontend/flask/forms.py +++ b/dila/frontend/flask/forms.py @@ -72,3 +72,16 @@ class NewLanguageForm(flask_wtf.FlaskForm): new_language_name = wtforms.StringField('New language') new_language_short = wtforms.StringField('Language code') next = wtforms.HiddenField() + + +class LoginForm(flask_wtf.FlaskForm): + username = wtforms.StringField('Username') + password = wtforms.PasswordField('Password') + + def validate(self, *args, **kwargs): + if super().validate(*args, **kwargs): + if application.authenticate(self.username.data, self.password.data): + return True + else: + self.password.errors.append('Invalid login or password') + return False diff --git a/dila/frontend/flask/template_tools.py b/dila/frontend/flask/template_tools.py index 18f00fe..836aed5 100644 --- a/dila/frontend/flask/template_tools.py +++ b/dila/frontend/flask/template_tools.py @@ -13,6 +13,7 @@ def static_url(filename): return urllib.parse.urljoin(config.STATIC_URL, filename) return flask.url_for('static', filename=filename) + def setup_language_context(blueprint): @blueprint.context_processor def inject_languages_menu(): diff --git a/dila/frontend/flask/templates/login.html b/dila/frontend/flask/templates/login.html index c85555f..98fd21f 100644 --- a/dila/frontend/flask/templates/login.html +++ b/dila/frontend/flask/templates/login.html @@ -12,11 +12,33 @@
-