mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-20 23:12:15 +00:00
Made it work :)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
from __future__ import unicode_literals
|
||||
import os
|
||||
from social.backends.base import BaseAuth
|
||||
from social.exceptions import AuthException
|
||||
from django.conf import settings
|
||||
|
||||
from .sso import DiscourseSSO
|
||||
|
||||
@@ -20,8 +19,8 @@ class DiscourseAssociation(object):
|
||||
class DiscourseAuth(BaseAuth):
|
||||
"""Discourse authentication backend"""
|
||||
name = 'discourse'
|
||||
secret = os.environ['DISCOURSE_SSO_SECRET']
|
||||
host = os.environ['DISCOURSE_HOST']
|
||||
secret = settings.DISCOURSE_SSO_SECRET
|
||||
host = settings.DISCOURSE_HOST
|
||||
|
||||
EXTRA_DATA = [
|
||||
('username', 'username'),
|
||||
|
||||
62
RIGS/discourse/pipeline.py
Normal file
62
RIGS/discourse/pipeline.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.contrib.auth import REDIRECT_FIELD_NAME
|
||||
from django.shortcuts import render_to_response
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from social.pipeline.partial import partial
|
||||
|
||||
from RIGS.models import Profile
|
||||
from RIGS import forms
|
||||
|
||||
|
||||
class SocialRegisterForm(forms.ProfileRegistrationFormUniqueEmail):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SocialRegisterForm, self).__init__(*args, **kwargs)
|
||||
self.fields.pop('password1')
|
||||
self.fields.pop('password2')
|
||||
self.fields.pop('captcha')
|
||||
|
||||
self.fields['email'].widget.attrs['readonly'] = True
|
||||
|
||||
def clean_email(self):
|
||||
initial = getattr(self, 'initial', None)
|
||||
if(initial['email'] != self.cleaned_data['email']):
|
||||
raise ValidationError("You cannot change the email")
|
||||
|
||||
return initial['email']
|
||||
|
||||
|
||||
@partial
|
||||
def new_connection(backend, details, response, user=None, is_new=False, social=None, request=None, *args, **kwargs):
|
||||
if social is not None:
|
||||
return
|
||||
|
||||
data = backend.strategy.request_data()
|
||||
|
||||
if data.get('UseCurrentAccount') is not None:
|
||||
return
|
||||
|
||||
alreadyLoggedIn = user is not None
|
||||
|
||||
context = {
|
||||
'details': details,
|
||||
'alreadyLoggedIn': alreadyLoggedIn,
|
||||
'loggedInUser': user,
|
||||
}
|
||||
|
||||
if not alreadyLoggedIn:
|
||||
completeUrl = reverse('social:complete', kwargs={'backend': backend.name})
|
||||
context['login_url'] = "{0}?{1}={2}".format(reverse('login'), REDIRECT_FIELD_NAME, completeUrl)
|
||||
|
||||
if data.get('username') is None:
|
||||
form = SocialRegisterForm(initial=details)
|
||||
else:
|
||||
form = SocialRegisterForm(data, initial=details)
|
||||
|
||||
if form.is_valid():
|
||||
new_user = Profile.objects.create_user(**form.cleaned_data)
|
||||
return {'user': new_user}
|
||||
|
||||
context['form'] = form
|
||||
|
||||
return render_to_response('RIGS/social-associate.html', context)
|
||||
@@ -3,6 +3,7 @@ from django import forms
|
||||
from django.utils import formats
|
||||
from django.conf import settings
|
||||
from django.core import serializers
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.contrib.auth.forms import UserCreationForm, UserChangeForm, AuthenticationForm, PasswordResetForm
|
||||
from registration.forms import RegistrationFormUniqueEmail
|
||||
from captcha.fields import ReCaptchaField
|
||||
@@ -27,6 +28,16 @@ class ProfileRegistrationFormUniqueEmail(RegistrationFormUniqueEmail):
|
||||
raise forms.ValidationError("These initials are already in use. Please supply different initials.")
|
||||
return self.cleaned_data['initials']
|
||||
|
||||
def clean_first_name(self):
|
||||
if self.cleaned_data["first_name"].strip() == '':
|
||||
raise ValidationError("First name is required.")
|
||||
return self.cleaned_data["first_name"]
|
||||
|
||||
def clean_last_name(self):
|
||||
if self.cleaned_data["last_name"].strip() == '':
|
||||
raise ValidationError("Last name is required.")
|
||||
return self.cleaned_data["last_name"]
|
||||
|
||||
|
||||
# Login form
|
||||
class PasswordReset(PasswordResetForm):
|
||||
|
||||
BIN
RIGS/static/imgs/forum-logo.gif
Normal file
BIN
RIGS/static/imgs/forum-logo.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 300 KiB |
@@ -133,6 +133,20 @@
|
||||
{% endif %}
|
||||
</dd>
|
||||
</dl>
|
||||
|
||||
<h4>Linked to {{object.social_auth.count}} Forum Account(s)</h4>
|
||||
|
||||
{% if object.social_auth.count > 0 %}
|
||||
|
||||
<a href="{% url 'unlink_forum' %}" class="btn btn-danger">
|
||||
Unlink Forum Account(s) <span class="glyphicon glyphicon-pencil"></span>
|
||||
</a>
|
||||
{% else %}
|
||||
<a href="{% url "social:begin" "discourse" %}" class="btn btn-success">
|
||||
Link Forum Account <span class="glyphicon glyphicon-pencil"></span>
|
||||
</a>
|
||||
{% endif %}
|
||||
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
55
RIGS/templates/RIGS/social-associate.html
Normal file
55
RIGS/templates/RIGS/social-associate.html
Normal file
@@ -0,0 +1,55 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load widget_tweaks %}
|
||||
{% block title %}Associate{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="col-sm-10 col-sm-offset-1">
|
||||
|
||||
<div class="text-center">
|
||||
<h1>R<small>ig</small> I<small>nformation</small> G<small>athering</small> S<small>ystem</small></h1>
|
||||
</div>
|
||||
<h2 class="text-center">Welcome <strong>{{details.username}}</strong></h2>
|
||||
<h4 class="text-center">This is the first time you've visited RIGS with your forum account, so we need a few details to get you set up</h4>
|
||||
<hr/>
|
||||
|
||||
|
||||
{% if alreadyLoggedIn %}
|
||||
<h2 class="text-center">You are logged in to RIGS as <strong>{{loggedInUser.username}}</strong></h2>
|
||||
<div class="col-sm-8 col-sm-offset-2">
|
||||
<form action="", method="post">{% csrf_token %}
|
||||
<input type="hidden" name="UseCurrentAccount" value="1"/>
|
||||
<button type="submit" class="btn btn-lg btn-primary center btn-block">Link Forum account to RIGS Account</button>
|
||||
</form>
|
||||
<a class="btn btn-lg btn-warning center btn-block" href="{% url 'logout' %}" role="button">Logout</a>
|
||||
</div>
|
||||
{% else %}
|
||||
<h4 class="text-center"><a class="btn btn-info" href="{{login_url}}" role="button">I already have a RIGS account</a></h4>
|
||||
{% if form.errors or supplement_form.errors %}
|
||||
<div class="alert alert-danger">
|
||||
{{form.errors}}
|
||||
{{supplement_form.errors}}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="col-sm-8 col-sm-offset-2">
|
||||
<form action="" method="post" class="form-horizontal" role="form">{% csrf_token %}
|
||||
{% for field in form %}
|
||||
<div class="form-group">
|
||||
<label for="{{ field.id_for_label }}" class="control-label col-sm-4">{{ field.label }}</label>
|
||||
<div class="controls col-sm-8">
|
||||
{% render_field field class+="form-control" placeholder=field.label %}
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
<p><input type="submit" value="Register" class="btn btn-primary pull-right"></p>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -1,6 +1,7 @@
|
||||
from django.conf.urls import patterns, url
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from RIGS import models, views, rigboard, finance, ical, versioning, forms
|
||||
from RIGS.discourse import views as discourseViews
|
||||
from django.views.generic import RedirectView
|
||||
from django.views.decorators.clickjacking import xframe_options_exempt
|
||||
|
||||
@@ -15,6 +16,7 @@ urlpatterns = patterns('',
|
||||
url(r'^closemodal/$', views.CloseModal.as_view(), name='closemodal'),
|
||||
|
||||
url('^user/login/$', 'RIGS.views.login', name='login'),
|
||||
url('^user/associate/$', discourseViews.Associate.as_view(), name='associate'),
|
||||
url('^user/login/embed/$', xframe_options_exempt(views.login_embed), name='login_embed'),
|
||||
url(r'^user/password_reset/$', 'django.contrib.auth.views.password_reset', {'password_reset_form': forms.PasswordReset}),
|
||||
|
||||
@@ -153,6 +155,7 @@ urlpatterns = patterns('',
|
||||
url(r'^user/edit/$', login_required(views.ProfileUpdateSelf.as_view()),
|
||||
name='profile_update_self'),
|
||||
url(r'^user/reset_api_key$', login_required(views.ResetApiKey.as_view(permanent=False)), name='reset_api_key'),
|
||||
url(r'^user/unlink_forum$', login_required(views.UnlinkForum.as_view(permanent=False)), name='unlink_forum'),
|
||||
|
||||
# ICS Calendar - API key authentication
|
||||
url(r'^ical/(?P<api_pk>\d+)/(?P<api_key>\w+)/rigs.ics$', api_key_required(ical.CalendarICS()), name="ics_calendar"),
|
||||
|
||||
@@ -382,3 +382,12 @@ class ResetApiKey(generic.RedirectView):
|
||||
self.request.user.save()
|
||||
|
||||
return reverse_lazy('profile_detail')
|
||||
|
||||
class UnlinkForum(generic.RedirectView):
|
||||
def get_redirect_url(self, *args, **kwargs):
|
||||
for link in self.request.user.social_auth.all():
|
||||
link.delete()
|
||||
|
||||
self.request.user.save()
|
||||
|
||||
return reverse_lazy('profile_detail')
|
||||
|
||||
Reference in New Issue
Block a user