forms.py 4.03 KB
Newer Older
Valentin Samir's avatar
Valentin Samir committed
1 2 3 4 5 6 7 8 9 10
# 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.
#
# (c) 2015 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 22 23 24 25 26 27
class WarnForm(forms.Form):
    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
28

Valentin Samir's avatar
style  
Valentin Samir committed
29

Valentin Samir's avatar
Valentin Samir committed
30 31 32
class FederateSelect(forms.Form):
    provider = forms.ChoiceField(
        label=_('Identity provider'),
33 34 35 36 37 38
        choices=[
            (
                p,
                utils.get_tuple(settings.CAS_FEDERATE_PROVIDERS[p], 2, p)
            ) for p in settings.CAS_FEDERATE_PROVIDERS_LIST
        ]
Valentin Samir's avatar
Valentin Samir committed
39 40 41 42 43 44 45
    )
    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)


Valentin Samir's avatar
Valentin Samir committed
46
class UserCredential(forms.Form):
Valentin Samir's avatar
Valentin Samir committed
47
    """Form used on the login page to retrive user credentials"""
Valentin Samir's avatar
Valentin Samir committed
48
    username = forms.CharField(label=_('login'))
49
    service = forms.CharField(label=_('service'), widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
50
    password = forms.CharField(label=_('password'), widget=forms.PasswordInput)
Valentin Samir's avatar
Valentin Samir committed
51
    lt = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
52
    method = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
53
    warn = forms.BooleanField(label=_('warn'), required=False)
Valentin Samir's avatar
Valentin Samir committed
54

Valentin Samir's avatar
Valentin Samir committed
55
    def __init__(self, *args, **kwargs):
Valentin Samir's avatar
Valentin Samir committed
56 57 58 59
        super(UserCredential, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(UserCredential, self).clean()
60
        auth = utils.import_attr(settings.CAS_AUTH_CLASS)(cleaned_data.get("username"))
Valentin Samir's avatar
Valentin Samir committed
61
        if auth.test_password(cleaned_data.get("password")):
Valentin Samir's avatar
Valentin Samir committed
62
            cleaned_data["username"] = auth.username
Valentin Samir's avatar
Valentin Samir committed
63
        else:
Valentin Samir's avatar
Valentin Samir committed
64
            raise forms.ValidationError(_(u"Bad user"))
Valentin Samir's avatar
Valentin Samir committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        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)

    def clean(self):
        cleaned_data = super(FederateUserCredential, self).clean()
        try:
            component = cleaned_data["username"].split('@')
            username = '@'.join(component[:-1])
            provider = component[-1]
            user = models.FederatedUser.objects.get(username=username, provider=provider)
            user.ticket = ""
            user.save()
        except models.FederatedUser.DoesNotExist:
            raise
        return cleaned_data
Valentin Samir's avatar
Valentin Samir committed
90 91 92


class TicketForm(forms.ModelForm):
Valentin Samir's avatar
Valentin Samir committed
93
    """Form for Tickets in the admin interface"""
Valentin Samir's avatar
Valentin Samir committed
94 95 96
    class Meta:
        model = models.Ticket
        exclude = []
97
    service = forms.CharField(label=_('service'), widget=forms.TextInput)