Commit b941633f0f4a1e9127ca6a614bc8288aedac2287
1 parent
e4f0799b
Exists in
master
and in
39 other branches
First signal to sync xmpp - django passwords
Showing
1 changed file
with
40 additions
and
0 deletions
Show diff stats
src/accounts/models.py
| 1 | +# -*- coding: utf-8 -*- | |
| 1 | 2 | |
| 2 | 3 | import urlparse |
| 3 | 4 | |
| 4 | 5 | from django.db import models |
| 6 | +from django.db.models.signals import pre_save | |
| 5 | 7 | from django.contrib.auth.models import AbstractUser |
| 6 | 8 | from django.core.urlresolvers import reverse |
| 7 | 9 | |
| 10 | +from conversejs import xmpp | |
| 11 | + | |
| 8 | 12 | from .utils import mailman |
| 9 | 13 | |
| 10 | 14 | |
| ... | ... | @@ -18,6 +22,18 @@ class User(AbstractUser): |
| 18 | 22 | verification_hash = models.CharField(max_length=32, null=True, blank=True) |
| 19 | 23 | modified = models.DateTimeField(auto_now=True) |
| 20 | 24 | |
| 25 | + def check_password(raw_password): | |
| 26 | + """ | |
| 27 | + Returns a boolean of whether the raw_password was correct. Handles | |
| 28 | + hashing formats behind the scenes. | |
| 29 | + """ | |
| 30 | + if not raw_password or not self.has_usable_password(): | |
| 31 | + return False | |
| 32 | + return self.password == raw_password | |
| 33 | + | |
| 34 | + def set_password(self, raw_password): | |
| 35 | + self.password = unicode(raw_password) | |
| 36 | + | |
| 21 | 37 | def get_absolute_url(self): |
| 22 | 38 | return reverse('user_profile', kwargs={'username': self.username}) |
| 23 | 39 | |
| ... | ... | @@ -39,3 +55,27 @@ class User(AbstractUser): |
| 39 | 55 | # The following workaroud allows to change email field to unique |
| 40 | 56 | # without having to rewrite all AbstractUser here |
| 41 | 57 | User._meta.get_field('email')._unique = True |
| 58 | + | |
| 59 | + | |
| 60 | +def password_change(sender, instance, **kwargs): | |
| 61 | + from conversejs.models import XMPPAccount | |
| 62 | + # to register an XMPPAccount for an user, do the following: | |
| 63 | + # xmpp.register_account('username@domain', 'user_password' | |
| 64 | + # 'name', 'email') | |
| 65 | + # the domain can be found at conversejs.conf.CONVERSEJS_AUTO_REGISTER | |
| 66 | + | |
| 67 | + try: | |
| 68 | + user = sender.objects.get(pk=instance.pk) | |
| 69 | + except sender.DoesNotExist: | |
| 70 | + return # should I register a xmpp account here? | |
| 71 | + | |
| 72 | + try: | |
| 73 | + xmpp_account = XMPPAccount.objects.get(user=instance.pk) | |
| 74 | + except XMPPAccount.DoesNotExist: | |
| 75 | + return # User's XMPP account should be created here? | |
| 76 | + | |
| 77 | + if user.password != instance.password: | |
| 78 | + xmpp.change_password(xmpp_account, instance.password) | |
| 79 | + | |
| 80 | + | |
| 81 | +pre_save.connect(password_change, sender=User) | ... | ... |