Skip to content

Commit

Permalink
Registration finished
Browse files Browse the repository at this point in the history
  • Loading branch information
dlareau committed Jan 13, 2016
1 parent fe8eeb2 commit bc90207
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 108 deletions.
35 changes: 19 additions & 16 deletions huntserver/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ class UnlockForm(forms.Form):
puzzle_id = forms.CharField(label='puzzle_id')

class PersonForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PersonForm, self).__init__(*args, **kwargs)
self.fields['phone'].help_text = "Optional"

allergies = forms.CharField(widget = forms.Textarea, label="Allergies",
required=False, help_text="Optional")
class Meta:
model = Person
fields = ['phone', 'allergies']
fields = ['phone']

class UserForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
Expand All @@ -32,22 +38,19 @@ class Meta:
model = User
fields = ['first_name', 'last_name', 'email', 'username', 'password']

class ShibUserForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ShibUserForm, self).__init__(*args, **kwargs)
self.fields['email'].required = True
self.fields['username'].widget.attrs['readonly'] = True
self.fields['username'].help_text = "You can't change this, we need it for authentication"

class Meta:
model = User
fields = ['username', 'email', 'first_name', 'last_name']

class BaseRegisterForm(forms.Form):
def save(self, attr):
user = User.objects.create_user(attr[settings.SHIB_USERNAME], attr[settings.SHIB_EMAIL], '')
return user

# class RegistrationForm(forms.Form):
# team_name = forms.CharField(label='Team Name')
# username = forms.CharField(label='Team Username', required=False)
# password = forms.CharField(label='Team Password', widget=forms.PasswordInput())
# confirm_password = forms.CharField(label='Confirm Password', required=False, widget=forms.PasswordInput())
# location = forms.ChoiceField(label="Do you want to be provided a room on campus close to the hunt?", choices=([(1, "Yes"), (2, "No, we have a room"), (3, "No, we are a remote team")]), required=False)
# first_name = forms.CharField(label='First Name')
# last_name = forms.CharField(label='Last Name')
# phone = forms.CharField(label='Phone Number', required=False)
# email = forms.EmailField(label='Email')
# dietary_issues = forms.CharField(label='Dietary Restrictions?', required=False, widget = forms.Textarea(attrs={'rows': 4, 'cols': 40}))
# year = forms.ChoiceField(label='School Year', choices=([(1,"Freshman"), (2,"Sophomore"), (3,"Junior"), (4,"Senior"), (5,"Graduate"), (0,"N/A")]))



19 changes: 19 additions & 0 deletions huntserver/migrations/0010_auto_20160112_0928.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('huntserver', '0009_auto_20160104_1317'),
]

operations = [
migrations.RenameField(
model_name='person',
old_name='is_andrew_acct',
new_name='is_shib_acct',
),
]
20 changes: 20 additions & 0 deletions huntserver/migrations/0011_team_join_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('huntserver', '0010_auto_20160112_0928'),
]

operations = [
migrations.AddField(
model_name='team',
name='join_code',
field=models.CharField(default='FFFFF', max_length=5),
preserve_default=False,
),
]
9 changes: 7 additions & 2 deletions huntserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Team(models.Model):
unlockables = models.ManyToManyField("Unlockable", blank=True)
hunt = models.ForeignKey(Hunt)
location = models.CharField(max_length=80, blank=True)
join_code = models.CharField(max_length=5)

def __unicode__(self):
return str(len(self.person_set.all())) + " (" + self.location + ") " + self.team_name
Expand All @@ -59,10 +60,14 @@ class Person(models.Model):
allergies = models.CharField(max_length=400, blank=True)
comments = models.CharField(max_length=400, blank=True)
teams = models.ManyToManyField(Team, blank=True)
is_andrew_acct = models.BooleanField()
is_shib_acct = models.BooleanField()

def __unicode__(self):
return self.user.first_name + " " + self.user.last_name
name = self.user.first_name + " " + self.user.last_name
if(name == " "):
return "Anonymous User"
else:
return self.user.first_name + " " + self.user.last_name

class Submission(models.Model):
team = models.ForeignKey(Team)
Expand Down
99 changes: 49 additions & 50 deletions huntserver/shib_views.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,70 @@
from django.http import HttpResponseRedirect, HttpResponseForbidden
from django.template import loader, RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.contrib.auth import login
from django.contrib.auth.models import User
from django.conf import settings

from utils import parse_attributes
from forms import BaseRegisterForm


def render_forbidden(*args, **kwargs):
return HttpResponseForbidden(loader.render_to_string(*args, **kwargs))


def shib_login(request, RegisterForm=BaseRegisterForm,
register_template_name='shib_register.html'):
from forms import ShibUserForm, PersonForm

def shib_login(request):

# Get attributes from REMOTE_USER/META
attr, error = parse_attributes(request.META)

was_redirected = False
if "next" in request.GET:
was_redirected = True
redirect_url = request.GET.get('next', settings.LOGIN_REDIRECT_URL)
context = {'shib_attrs': attr,
'was_redirected': was_redirected}
context = {'shib_attrs': attr}
if error:
return render_forbidden('attribute_error.html',
context,
context_instance=RequestContext(request))
return render(request, 'attribute_error.html', context)

# Attempt to get username out of attr
try:
username = attr[settings.SHIB_USERNAME]
# TODO this should log a misconfiguration.
eppn = attr[settings.SHIB_USERNAME]
except:
return render_forbidden('attribute_error.html',
context,
context_instance=RequestContext(request))
return render(request, 'attribute_error.html', context)

# Make sure username exists
if not attr[settings.SHIB_USERNAME] or attr[settings.SHIB_USERNAME] == '':
return render_forbidden('attribute_error.html',
context,
context_instance=RequestContext(request))
return render(request, 'attribute_error.html', context)

# For form submission
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
user = form.save(attr)
print(request.POST)
uf = ShibUserForm(request.POST)
pf = PersonForm(request.POST)
print("uf: " + str(uf.is_valid()))
print("pf: " + str(pf.is_valid()))
if uf.is_valid() and pf.is_valid():
user = uf.save()
user.is_shib_acct = True;
user.set_unusable_password()
user.save()
person = pf.save(commit=False)
person.user = user
person.save()
else:
context = {'user_form': uf, 'person_form': pf, 'next': redirect_url, 'shib_attrs': attr}
return render(request, "shib_register.html", context)
# Attempt to look up user in DB, if not found, present the registration form.
try:
user = User.objects.get(username=attr[settings.SHIB_USERNAME])
user = User.objects.get(username=eppn)
except User.DoesNotExist:
form = RegisterForm()
context = {'form': form,
'next': redirect_url,
'shib_attrs': attr,
'was_redirected': was_redirected}
return render_to_response(register_template_name,
context,
context_instance=RequestContext(request))
user.set_unusable_password()
try:
user.first_name = attr[settings.SHIB_FIRST_NAME]
user.last_name = attr[settings.SHIB_LAST_NAME]
user.email = attr[settings.SHIB_EMAIL]
except:
pass
user.save()
existing_context = {"username":eppn, "email":eppn}
try:
existing_context['first_name'] = attr[settings.SHIB_FIRST_NAME]
existing_context['last_name'] = attr[settings.SHIB_LAST_NAME]
except:
pass
user_form = ShibUserForm(initial=existing_context)
person_form = PersonForm()
context = {'user_form': user_form, 'person_form': person_form, 'next': redirect_url, 'shib_attrs': attr}
return render(request, "shib_register.html", context)

user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)

# Redirect if nessecary
if not redirect_url or '//' in redirect_url or ' ' in redirect_url:
redirect_url = settings.LOGIN_REDIRECT_URL

return HttpResponseRedirect(redirect_url)
print(redirect_url)
return HttpResponseRedirect(redirect_url)
15 changes: 14 additions & 1 deletion huntserver/static/huntserver/info_base.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ h1.title {
border-top: 2px dashed #ccc;
padding-top: 10px;
}
.container{
.container:not(.no_outline){
background: white;
padding: 20px 40px;
margin: 10px auto;
Expand All @@ -48,3 +48,16 @@ h1.title {
font-size: 10px;
color: gray;
}

input[type="text"] {
width:200px;
}

input[type="email"] {
width:200px;
}

input[readonly] {
background: #dddddd;
opacity: 0.8;
}
6 changes: 4 additions & 2 deletions huntserver/templates/login_selection.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ <h2>Select Your Login Service</h2>
<h4>(Please use a university login in possible)</h4>
<div class="row">
<div class="col-xs-4">
<a href="https://puzzlehunt.club.cc.cmu.edu/Shibboleth.sso/Login?entityID=https://login.cmu.edu/idp/shibboleth&target=https://puzzlehunt.club.cc.cmu.edu/shib/login">
<!-- This is one really nasty URL -->
<a href="https://puzzlehunt.club.cc.cmu.edu/Shibboleth.sso/Login?entityID=https://login.cmu.edu/idp/shibboleth&target=https://puzzlehunt.club.cc.cmu.edu/shib/login?next={{next}}">
<img src="/static/huntserver/cmu_login.png" alt="Login Image" width="100%">
Carnegie Mellon University
</a>
</div>
<div class="col-xs-4">
<a href="https://puzzlehunt.club.cc.cmu.edu/Shibboleth.sso/Login?entityID=https://idp.pitt.edu/idp/shibboleth&target=https://puzzlehunt.club.cc.cmu.edu/shib/login">
<!-- This is one really nasty URL -->
<a href="https://puzzlehunt.club.cc.cmu.edu/Shibboleth.sso/Login?entityID=https://idp.pitt.edu/idp/shibboleth&target=https://puzzlehunt.club.cc.cmu.edu/shib/login?next={{next}}">
<img src="/static/huntserver/pitt_login.png" alt="Login Image" width="100%">
University of Pittsburgh
</a>
Expand Down

0 comments on commit bc90207

Please sign in to comment.