django-cas-server/cas_server/forms.py

40 lines
1.4 KiB
Python
Raw Normal View History

2015-05-17 21:24:41 +00:00
import default_settings
2015-05-16 21:43:46 +00:00
from django import forms
from django.conf import settings
2015-05-22 15:55:00 +00:00
from django.utils.translation import ugettext_lazy as _
2015-05-16 21:43:46 +00:00
import models
class UserCredential(forms.Form):
2015-05-22 15:55:00 +00:00
username = forms.CharField(label=_('login'))
2015-05-16 21:43:46 +00:00
service = forms.CharField(widget=forms.HiddenInput(), required=False)
2015-05-22 15:55:00 +00:00
password = forms.CharField(label=_('password'), widget=forms.PasswordInput)
2015-05-16 21:43:46 +00:00
method = forms.CharField(widget=forms.HiddenInput(), required=False)
2015-05-22 15:55:00 +00:00
warn = forms.BooleanField(label=_('warn'), required=False)
2015-05-16 21:43:46 +00:00
def __init__(self, *args, **kwargs):
super(UserCredential, self).__init__(*args, **kwargs)
def clean(self):
cleaned_data = super(UserCredential, self).clean()
auth = settings.CAS_AUTH_CLASS(cleaned_data.get("username"))
if auth.test_password(cleaned_data.get("password")):
try:
user = models.User.objects.get(username=auth.username)
user.attributs=auth.attributs()
user.save()
except models.User.DoesNotExist:
user = models.User.objects.create(username=auth.username, attributs=auth.attributs())
user.save()
self.user = user
else:
raise forms.ValidationError("Bad user")
class TicketForm(forms.ModelForm):
class Meta:
model = models.Ticket
exclude = []
service = forms.CharField(widget=forms.TextInput)