Commits

Anonymous committed d518f01

add possibility to dissociate an openid

Comments (0)

Files changed (6)

django_authopenid/context_processors.py

 #
 
 
-
 def authopenid(request):
     """
     Returns context variables required by apps that use django-authopenid.

django_authopenid/forms.py

                     registered in our database by another account. Please choose another."))
                     
             raise forms.ValidationError(_("You already associated this openid to your account."))
-            
+            
+class OpenidDissociateForm(OpenidSigninForm):
+    """ form used to dissociate an openid. """
+    openid_url = forms.CharField(max_length=255, widget=forms.widgets.HiddenInput())

django_authopenid/urls.py

     # manage account registration
     url(r'^associate/complete/$', oid_views.complete_associate, name='user_complete_associate'),
     url(r'^associate/$', oid_views.associate, name='user_associate'),
+    url(r'^dissociate/$', oid_views.dissociate, name='user_dissociate'),
     url(r'^register/$', oid_views.register, name='user_register'),
     url(r'^signout/$', oid_views.signout, name='user_signout'),
     url(r'^signin/complete/$', oid_views.complete_signin, name='user_complete_signin'),

django_authopenid/views.py

     openids = request.session.get('openids', [])
     openids.append(openid_)
     request.session['openids'] = openids
-    
-    print "ici"
     uassoc = UserAssociation(
                 openid_url=str(openid_),
                 user_id=request.user.id
                     reverse('user_complete_associate'),
                     urllib.urlencode({ redirect_field_name: redirect_to })
             )
-            print redirect_url
             return ask_openid(request, 
                     form.cleaned_data['openid_url'], 
                     redirect_url, 
     return render(template_name, {
         'form': form,
         redirect_field_name: redirect_to
-    }, context_instance=_build_context(request, extra_context=extra_context))          
+    }, context_instance=_build_context(request, extra_context=extra_context))     
+    
+@login_required
+def dissociate(request, template_name="authopenid/dissociate.html",
+        dissociate_form=OpenidDissociateForm, redirect_field_name=REDIRECT_FIELD_NAME, 
+        extra_context=None):
+        
+    redirect_to = request.REQUEST.get(redirect_field_name, '')
+    if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
+        redirect_to = settings.LOGIN_REDIRECT_URL
+    if request.POST:
+        form = dissociate_form(request.POST)
+        if form.is_valid():
+            rels = UserAssociation.objects.filter(user__id=request.user.id)
+            associated_openids = [rel.openid_url for rel in rels]
+            openid_url = form.cleaned_data['openid_url']
+            msg = ""
+            if openid_url not in associated_openids:
+                msg = _("%s is not associated to your account" % openid_url)
+            elif len(associated_openids) == 1 and not request.user.has_usable_password():
+                msg = _("You can't remove this openid. You should set a password first.")
+            
+            if not msg:
+                UserAssociation.objects.get(openid_url__exact=openid_url).delete()
+                if openid_url == request.session.get('openid_url'):
+                    del request.session['openid_url']
+                msg = _("openid removed.")
+            return HttpResponseRedirect("%s?%s" % (redirect_to,
+                urllib.urlencode({ "msg": msg })))
+    else:
+        openid_url = request.GET.get('openid_url', '')
+        if not openid_url:
+            msg = _("Invalid OpenID url.")
+            return HttpResponseRedirect("%s?%s" % (redirect_to,
+                urllib.urlencode({ "msg": msg })))
+        form = dissociate_form(initial={ 'openid_url': openid_url })
+    return render(template_name, {
+            "form": form,
+            "openid_url": openid_url
+    }, context_instance=_build_context(request, extra_context=extra_context))

example/templates/authopenid/dissociate.html

+{% extends "base.html" %}
+{% load i18n %}
+
+{% block head %}
+
+
+{% endblock %}
+
+
+{% block content %}
+{% if msg %}
+<br />
+	<p class="warning">{{ msg }}</p>
+{% endif %}
+
+<div class="login">
+
+<form name="fopenid" action="" method="post">
+    <input type="hidden" name="next" value="{{ next }}" />
+	{{ form.openid_url }}
+	<fieldset>
+		<legend>{% trans "Dissociate OpenID URL" %}</legend>
+        <p>Confirm to dissociate {{ openid_url }} from your account</p>
+        <div class="submit-row "><input name="bdissociate" type="submit" value="{% trans "Dissociate OpenID" %}"></div>
+		
+	</fieldset>
+</form>	
+</div>	
+{% endblock %}
+

example/templates/home.html

 <h2>Associated openids</h2>
 <ul>
 	{% for oid_url in associated_openids %}
-		<li>{{ oid_url }}</li>
+		<li>{{ oid_url }} (<a href="{% url user_dissociate %}?openid_url={{ oid_url }}">dissociate</a>)</li>
 	{% endfor %}
 </ul>
 
 
 <h2>Actions:</h2>
 <ul>
+	<li><a href="{% url user_associate %}">Associate a new openid</a></li>
 	<li><a href="{% url auth_password_change %}">Change password</a></li>
   	<li><a href="{% url user_signout %}">signout</a></li>
 </ul>