forms.py 4.59 KB
Newer Older
Valentin Samir's avatar
Valentin Samir committed
1 2 3 4 5 6 7 8 9
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
# more details.
#
# You should have received a copy of the GNU General Public License version 3
# along with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
Valentin Samir's avatar
Valentin Samir committed
10
# (c) 2015-2016 Valentin Samir
Valentin Samir's avatar
Valentin Samir committed
11
"""forms for the app"""
12
from .default_settings import settings
Valentin Samir's avatar
Valentin Samir committed
13

Valentin Samir's avatar
Valentin Samir committed
14
from django import forms
Valentin Samir's avatar
Valentin Samir committed
15
from django.utils.translation import ugettext_lazy as _
Valentin Samir's avatar
Valentin Samir committed
16

Valentin Samir's avatar
Valentin Samir committed
17 18
import cas_server.utils as utils
import cas_server.models as models
Valentin Samir's avatar
Valentin Samir committed
19

Valentin Samir's avatar
style  
Valentin Samir committed
20

21
class WarnForm(forms.Form):
22
    """Form used on warn page before emiting a ticket"""
23 24 25 26 27 28
    service = forms.CharField(widget=forms.HiddenInput(), required=False)
    renew = forms.BooleanField(widget=forms.HiddenInput(), required=False)
    gateway = forms.CharField(widget=forms.HiddenInput(), required=False)
    method = forms.CharField(widget=forms.HiddenInput(), required=False)
    warned = forms.BooleanField(widget=forms.HiddenInput(), required=False)
    lt = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
PEP8  
Valentin Samir committed
29

Valentin Samir's avatar
style  
Valentin Samir committed
30

Valentin Samir's avatar
Valentin Samir committed
31
class FederateSelect(forms.Form):
Valentin Samir's avatar
Valentin Samir committed
32 33 34 35
    """
        Form used on the login page when CAS_FEDERATE is True
        allowing the user to choose a identity provider.
    """
36 37 38 39 40 41 42
    provider = forms.ModelChoiceField(
        queryset=models.FederatedIendityProvider.objects.all().order_by(
            "pos",
            "verbose_name",
            "suffix"
        ),
        to_field_name="suffix",
Valentin Samir's avatar
Valentin Samir committed
43 44 45 46 47 48
        label=_('Identity provider'),
    )
    service = forms.CharField(label=_('service'), widget=forms.HiddenInput(), required=False)
    method = forms.CharField(widget=forms.HiddenInput(), required=False)
    remember = forms.BooleanField(label=_('Remember the identity provider'), required=False)
    warn = forms.BooleanField(label=_('warn'), required=False)
49
    renew = forms.BooleanField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
50 51


Valentin Samir's avatar
Valentin Samir committed
52
class UserCredential(forms.Form):
Valentin Samir's avatar
Valentin Samir committed
53
    """Form used on the login page to retrive user credentials"""
Valentin Samir's avatar
Valentin Samir committed
54
    username = forms.CharField(label=_('login'))
55
    service = forms.CharField(label=_('service'), widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
56
    password = forms.CharField(label=_('password'), widget=forms.PasswordInput)
Valentin Samir's avatar
Valentin Samir committed
57
    lt = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
58
    method = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
59
    warn = forms.BooleanField(label=_('warn'), required=False)
60
    renew = forms.BooleanField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
61

Valentin Samir's avatar
Valentin Samir committed
62
    def __init__(self, *args, **kwargs):
Valentin Samir's avatar
Valentin Samir committed
63 64 65 66
        super(UserCredential, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(UserCredential, self).clean()
67
        auth = utils.import_attr(settings.CAS_AUTH_CLASS)(cleaned_data.get("username"))
Valentin Samir's avatar
Valentin Samir committed
68
        if auth.test_password(cleaned_data.get("password")):
Valentin Samir's avatar
Valentin Samir committed
69
            cleaned_data["username"] = auth.username
Valentin Samir's avatar
Valentin Samir committed
70
        else:
Valentin Samir's avatar
Valentin Samir committed
71
            raise forms.ValidationError(_(u"Bad user"))
Valentin Samir's avatar
Valentin Samir committed
72 73 74 75 76 77 78 79 80 81 82 83
        return cleaned_data


class FederateUserCredential(UserCredential):
    """Form used on the login page to retrive user credentials"""
    username = forms.CharField(widget=forms.HiddenInput())
    service = forms.CharField(widget=forms.HiddenInput(), required=False)
    password = forms.CharField(widget=forms.HiddenInput())
    ticket = forms.CharField(widget=forms.HiddenInput())
    lt = forms.CharField(widget=forms.HiddenInput(), required=False)
    method = forms.CharField(widget=forms.HiddenInput(), required=False)
    warn = forms.BooleanField(widget=forms.HiddenInput(), required=False)
84
    renew = forms.BooleanField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
85 86 87 88

    def clean(self):
        cleaned_data = super(FederateUserCredential, self).clean()
        try:
89
            user = models.FederatedUser.get_from_federated_username(cleaned_data["username"])
Valentin Samir's avatar
Valentin Samir committed
90 91
            user.ticket = ""
            user.save()
92
        # should not happed as if the FederatedUser do not exists, super should
93 94 95 96 97
        # raise before a ValidationError("bad user")
        except models.FederatedUser.DoesNotExist:  # pragma: no cover (should not happend)
            raise forms.ValidationError(
                _(u"User not found in the temporary database, please try to reconnect")
            )
Valentin Samir's avatar
Valentin Samir committed
98
        return cleaned_data
Valentin Samir's avatar
Valentin Samir committed
99 100 101


class TicketForm(forms.ModelForm):
Valentin Samir's avatar
Valentin Samir committed
102
    """Form for Tickets in the admin interface"""
Valentin Samir's avatar
Valentin Samir committed
103 104 105
    class Meta:
        model = models.Ticket
        exclude = []
106
    service = forms.CharField(label=_('service'), widget=forms.TextInput)