1 from django.shortcuts import render_to_response
\r
2 from django.template import RequestContext
\r
3 from django.core.urlresolvers import reverse
\r
4 from django.contrib.auth.models import User
\r
5 from django.http import HttpResponseRedirect
\r
6 from django.utils.safestring import mark_safe
\r
7 from django.utils.translation import ugettext as _
\r
8 from django.contrib.auth.decorators import login_required
\r
9 from django.contrib.auth import login, logout
\r
10 from django.http import get_host
\r
13 from forum.models import AuthKeyUserAssociation
\r
14 from forum.authentication.forms import SimpleRegistrationForm, SimpleEmailSubscribeForm
\r
16 from forum.authentication.base import InvalidAuthentication
\r
17 from forum.authentication import AUTH_PROVIDERS
\r
19 from forum.models import Question, Answer
\r
21 def signin_page(request, action=None):
\r
23 request.session['on_signin_url'] = request.META.get('HTTP_REFERER', '/')
\r
25 request.session['on_signin_action'] = action
\r
27 all_providers = [provider.context for provider in AUTH_PROVIDERS.values()]
\r
29 sort = lambda c1, c2: c1.weight - c2.weight
\r
30 can_show = lambda c: not request.user.is_authenticated() or c.show_to_logged_in_user
\r
32 bigicon_providers = sorted([
\r
33 context for context in all_providers if context.mode == 'BIGICON' and can_show(context)
\r
36 smallicon_providers = sorted([
\r
37 context for context in all_providers if context.mode == 'SMALLICON' and can_show(context)
\r
40 stackitem_providers = sorted([
\r
41 context for context in all_providers if context.mode == 'STACK_ITEM' and can_show(context)
\r
45 msg = request.session['auth_error']
\r
46 del request.session['auth_error']
\r
50 return render_to_response(
\r
54 'all_providers': all_providers,
\r
55 'bigicon_providers': bigicon_providers,
\r
56 'stackitem_providers': stackitem_providers,
\r
57 'smallicon_providers': smallicon_providers,
\r
59 RequestContext(request))
\r
61 def prepare_provider_signin(request, provider):
\r
62 force_email_request = request.REQUEST.get('validate_email', 'yes') == 'yes'
\r
63 request.session['force_email_request'] = force_email_request
\r
65 if provider in AUTH_PROVIDERS:
\r
66 provider_class = AUTH_PROVIDERS[provider].consumer
\r
69 request_url = provider_class.prepare_authentication_request(request,
\r
70 reverse('auth_provider_done', kwargs={'provider': provider}))
\r
72 return HttpResponseRedirect(request_url)
\r
73 except NotImplementedError, e:
\r
74 return process_provider_signin(request, provider)
\r
75 except InvalidAuthentication, e:
\r
76 request.session['auth_error'] = e.message
\r
78 return HttpResponseRedirect(reverse('auth_signin'))
\r
81 def process_provider_signin(request, provider):
\r
82 if provider in AUTH_PROVIDERS:
\r
83 provider_class = AUTH_PROVIDERS[provider].consumer
\r
86 assoc_key = provider_class.process_authentication_request(request)
\r
87 except InvalidAuthentication, e:
\r
88 request.session['auth_error'] = e.message
\r
89 return HttpResponseRedirect(reverse('auth_signin'))
\r
91 if request.user.is_authenticated():
\r
92 if isinstance(assoc_key, (type, User)):
\r
93 if request.user != assoc_key:
\r
94 request.session['auth_error'] = _("Sorry, these login credentials belong to anoother user. Plese terminate your current session and try again.")
\r
96 request.session['auth_error'] = _("You are already logged in with that user.")
\r
99 assoc = AuthKeyUserAssociation.objects.get(key=assoc_key)
\r
100 if assoc.user == request.user:
\r
101 request.session['auth_error'] = _("These login credentials are already associated with your account.")
\r
103 request.session['auth_error'] = _("Sorry, these login credentials belong to anoother user. Plese terminate your current session and try again.")
\r
105 uassoc = AuthKeyUserAssociation(user=request.user, key=assoc_key, provider=provider)
\r
107 request.session['auth_error'] = _("These new credentials are now associated with your account.")
\r
108 return HttpResponseRedirect(reverse('auth_signin'))
\r
111 assoc = AuthKeyUserAssociation.objects.get(key=assoc_key)
\r
113 return login_and_forward(request, user_)
\r
115 request.session['assoc_key'] = assoc_key
\r
116 request.session['auth_provider'] = provider
\r
117 return HttpResponseRedirect(reverse('auth_external_register'))
\r
119 return HttpResponseRedirect(reverse('auth_signin'))
\r
121 def external_register(request):
\r
122 if request.method == 'POST' and 'bnewaccount' in request.POST:
\r
123 form1 = SimpleRegistrationForm(request.POST)
\r
124 email_feeds_form = SimpleEmailSubscribeForm(request.POST)
\r
126 if (form1.is_valid() and email_feeds_form.is_valid()):
\r
127 tmp_pwd = User.objects.make_random_password()
\r
128 user_ = User.objects.create_user(form1.cleaned_data['username'],
\r
129 form1.cleaned_data['email'], tmp_pwd)
\r
131 user_.set_unusable_password()
\r
133 uassoc = AuthKeyUserAssociation(user=user_, key=request.session['assoc_key'], provider=request.session['auth_provider'])
\r
136 email_feeds_form.save(user_)
\r
138 del request.session['assoc_key']
\r
139 del request.session['auth_provider']
\r
140 return login_and_forward(request, user_)
\r
142 provider_class = AUTH_PROVIDERS[request.session['auth_provider']].consumer
\r
143 user_data = provider_class.get_user_data(request.session['assoc_key'])
\r
145 username = user_data.get('username', '')
\r
146 email = user_data.get('email', '')
\r
149 email = request.session.get('auth_email_request', '')
\r
151 form1 = SimpleRegistrationForm(initial={
\r
153 'username': username,
\r
156 email_feeds_form = SimpleEmailSubscribeForm()
\r
158 provider_context = AUTH_PROVIDERS[request.session['auth_provider']].context
\r
160 return render_to_response('auth/complete.html', {
\r
162 'email_feeds_form': email_feeds_form,
\r
163 'provider':mark_safe(provider_context.human_name),
\r
164 'login_type':provider_context.id,
\r
165 'gravatar_faq_url':reverse('faq') + '#gravatar',
\r
166 }, context_instance=RequestContext(request))
\r
168 def newquestion_signin_action(user):
\r
169 question = Question.objects.filter(author=user).order_by('-added_at')[0]
\r
170 return question.get_absolute_url()
\r
172 def newanswer_signin_action(user):
\r
173 answer = Answer.objects.filter(author=user).order_by('-added_at')[0]
\r
174 return answer.get_absolute_url()
\r
176 POST_SIGNIN_ACTIONS = {
\r
177 'newquestion': newquestion_signin_action,
\r
178 'newanswer': newanswer_signin_action,
\r
181 def login_and_forward(request, user):
\r
182 old_session = request.session.session_key
\r
183 user.backend = "django.contrib.auth.backends.ModelBackend"
\r
184 login(request, user)
\r
186 from forum.models import user_logged_in
\r
187 user_logged_in.send(user=user,session_key=old_session,sender=None)
\r
189 redirect = request.session.get('on_signin_url', None)
\r
192 signin_action = request.session.get('on_signin_action', None)
\r
193 if not signin_action:
\r
194 redirect = reverse('index')
\r
197 redirect = POST_SIGNIN_ACTIONS[signin_action](user)
\r
199 redirect = reverse('index')
\r
201 return HttpResponseRedirect(redirect)
\r
204 def signout(request):
\r
206 signout from the website. Remove openid from session and kill it.
\r
212 return HttpResponseRedirect(reverse('index'))