~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/byceps/blueprints/authentication/forms.py -rw-r--r-- 1.9 KiB
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
byceps.blueprints.authentication.forms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from flask import g
from wtforms import BooleanField, PasswordField, StringField
from wtforms.validators import InputRequired, EqualTo, Length, ValidationError

from ...services.authentication.password import service as password_service
from ...util.l10n import LocalizedForm


MINIMUM_PASSWORD_LENGTH = 8
MAXIMUM_PASSWORD_LENGTH = 100


class LoginForm(LocalizedForm):
    screen_name = StringField('Benutzername', [InputRequired()])
    password = PasswordField('Passwort', [InputRequired()])
    permanent = BooleanField()


class RequestPasswordResetForm(LocalizedForm):
    screen_name = StringField('Benutzername', [InputRequired()])


def _get_new_password_validators(companion_field_name):
    return [
        InputRequired(),
        EqualTo(
            companion_field_name,
            message='Das neue Passwort muss mit der Wiederholung übereinstimmen.',
        ),
        Length(min=MINIMUM_PASSWORD_LENGTH, max=MAXIMUM_PASSWORD_LENGTH),
    ]


class ResetPasswordForm(LocalizedForm):
    new_password = PasswordField(
        'Neues Passwort',
        _get_new_password_validators('new_password_confirmation'),
    )
    new_password_confirmation = PasswordField(
        'Neues Passwort (Wiederholung)',
        _get_new_password_validators('new_password'),
    )


class UpdatePasswordForm(ResetPasswordForm):
    old_password = PasswordField('Bisheriges Passwort', [InputRequired()])

    @staticmethod
    def validate_old_password(form, field):
        user_id = g.current_user.id
        password = field.data

        if not password_service.is_password_valid_for_user(user_id, password):
            raise ValidationError(
                'Das eingegebene Passwort stimmt nicht mit dem bisherigen überein.'
            )