def authenticate(self, email=None, password=None): try: user = User.objects.select_related("assopy_user").get(email__iexact=email, is_active=True) if user.check_password(password): auser = user.assopy_user if auser is None: # questo utente esiste su django ma non ha un utente assopy # collegato; probabilmente è un admin inserito prima del # collegamento con il backend. auser = models.User(user=user) auser.save() models.user_created.send(sender=auser, profile_complete=True) self.linkUser(auser) return user except User.MultipleObjectsReturned: return None except User.DoesNotExist: # nel db di django non c'è un utente con quella email, ma potrebbe # esserci un utente legacy nel backend di ASSOPY if not settings.GENRO_BACKEND or not settings.SEARCH_MISSING_USERS_ON_BACKEND: return None rid = genro.users(email=email, password=password)["r0"] if rid is not None: log.info('"%s" is a valid remote user; a local user is needed', email) auser = models.User.objects.create_user( email, password=password, active=True, assopy_id=rid, send_mail=False ) return auser.user else: return None
def linkUser(self, user): """ collega l'utente assopy passato con il backend; crea l'utente remoto se necessario. """ if not settings.GENRO_BACKEND: return user if user.assopy_id: return user name = unicode(user.user).encode("utf-8") if not user.user.is_active: log.info('cannot link a remote user to "%s": it\'s not active', name) return log.info('a remote user is needed for "%s"', name) # il lookup con l'email può ritornare più di un id; non è un # problema dato che associo lo user con il backend solo quando ho # verificato l'email (e la verifica non è necessaria se si loggano # con janrain), quindi posso usare una qualsiasi delle identità # remote. Poi un giorno implementeremo il merge delle identità. rid = genro.users(email=user.user.email)["r0"] if rid is not None: log.info('an existing match with the email "%s" is found: %s', user.user.email, rid) user.assopy_id = rid user.save() genro.user_remote2local(user) else: rid = genro.create_user(user.user.first_name, user.user.last_name, user.user.email) log.info("new remote user id: %s", rid) user.assopy_id = rid user.save() return user
def authenticate(self, email=None, password=None): try: user = User.objects.select_related('assopy_user').get(email__iexact=email, is_active=True) if user.check_password(password): auser = user.assopy_user if auser is None: # questo utente esiste su django ma non ha un utente assopy # collegato; probabilmente è un admin inserito prima del # collegamento con il backend. auser = models.User(user=user) auser.save() models.user_created.send(sender=auser, profile_complete=True) self.linkUser(auser) return user except User.MultipleObjectsReturned: return None except User.DoesNotExist: # nel db di django non c'è un utente con quella email, ma potrebbe # esserci un utente legacy nel backend di ASSOPY if not settings.GENRO_BACKEND or not settings.SEARCH_MISSING_USERS_ON_BACKEND: return None rid = genro.users(email=email, password=password)['r0'] if rid is not None: log.info('"%s" is a valid remote user; a local user is needed', email) auser = models.User.objects.create_user(email, password=password, active=True, assopy_id=rid, send_mail=False) return auser.user else: return None
def linkUser(self, user): """ collega l'utente assopy passato con il backend; crea l'utente remoto se necessario. """ if not settings.GENRO_BACKEND: return user if user.assopy_id: return user name = unicode(user.user).encode('utf-8') if not user.user.is_active: log.info('cannot link a remote user to "%s": it\'s not active', name) return log.info('a remote user is needed for "%s"', name) # il lookup con l'email può ritornare più di un id; non è un # problema dato che associo lo user con il backend solo quando ho # verificato l'email (e la verifica non è necessaria se si loggano # con janrain), quindi posso usare una qualsiasi delle identità # remote. Poi un giorno implementeremo il merge delle identità. rid = genro.users(email=user.user.email)['r0'] if rid is not None: log.info('an existing match with the email "%s" is found: %s', user.user.email, rid) user.assopy_id = rid user.save() genro.user_remote2local(user) else: rid = genro.create_user(user.user.first_name, user.user.last_name, user.user.email) log.info('new remote user id: %s', rid) user.assopy_id = rid user.save() return user
def _assign_ticket(ticket, email): email = email.strip() try: recipient = auth.models.User.objects.get(email__iexact=email) except auth.models.User.DoesNotExist: try: # qui uso filter + [0] invece che .get perchè potrebbe accadere, # anche se non dovrebbe, che due identità abbiano la stessa email # (ad esempio se una persona a usato la stessa mail su più servizi # remoti ma ha collegato questi servizi a due utenti locali # diversi). Non è un problema se più identità hanno la stessa email # (nota che il backend di autenticazione già verifica che la stessa # email non venga usata due volte per creare utenti django) perché # in ogni caso si tratta di email verificate da servizi esterni. recipient = amodels.UserIdentity.objects.filter(email__iexact=email)[0].user.user except IndexError: recipient = None if recipient is None: from assopy.clients import genro rid = genro.users(email)['r0'] if rid is not None: # l'email non è associata ad un utente django ma genropy la # conosce. Se rid è assegnato ad un utente assopy riutilizzo # l'utente collegato. Questo check funziona quando un biglietto # viene assegnato ad un utente, quest'ultimo cambia email ma poi il # biglietto viene riassegnato nuovamente all'email originale. try: recipient = amodels.User.objects.get(assopy_id=rid).user except amodels.User.DoesNotExist: pass else: if recipient.email != email: log.info( 'email "%s" found on genropy; but user (%s) have a different email: "%s"', email.encode('utf-8'), unicode(recipient).encode('utf-8'), recipient.email.encode('utf-8')) email = recipient.email else: log.info( 'email "%s" found on genropy; user (%s)', email.encode('utf-8'), unicode(recipient).encode('utf-8')) if recipient is None: log.info('No user found for the email "%s"; time to create a new one', email) just_created = True u = amodels.User.objects.create_user(email=email, token=True, send_mail=False) recipient = u.user name = email else: log.info('Found a local user (%s) for the email "%s"', unicode(recipient).encode('utf-8'), email.encode('utf-8')) just_created = False try: auser = recipient.assopy_user except amodels.User.DoesNotExist: # uff, ho un utente su django che non è un assopy user, sicuramente # strascichi prima dell'introduzione dell'app assopy auser = amodels.User(user=recipient) auser.save() if not auser.token: recipient.assopy_user.token = str(uuid.uuid4()) recipient.assopy_user.save() name = '%s %s' % (recipient.first_name, recipient.last_name) _reset_ticket(ticket) utils.email( 'ticket-assigned', ctx={ 'name': name, 'just_created': just_created, 'ticket': ticket, 'link': settings.DEFAULT_URL_PREFIX + reverse('p3-user', kwargs={'token': recipient.assopy_user.token}), }, to=[email] ).send() return email
def _assign_ticket(ticket, email): email = email.strip() try: recipient = auth.models.User.objects.get(email__iexact=email) except auth.models.User.DoesNotExist: try: # Here I'm using filter + [0] instead of .get because it could happen, # even if it shouldn't, that two identities have the same email # (e.g. if someone used the same email on multiple remote services # but connected these services to two local users). # It's not a problem if more identities have the same email (note # that the authentication backend already checks that the same email # won't be used twice to create django users) because anway they're # email validated by external services. recipient = amodels.UserIdentity.objects.filter(email__iexact=email)[0].user.user except IndexError: recipient = None if recipient is None: from assopy.clients import genro rid = genro.users(email)['r0'] if rid is not None: # the email it's not associated to a django user, but genropy # knows it. If rid is assigned to an assopy user I'll reuse the # connected user. This check works when the ticket is assigned # to a user, the user modifies its email but later the ticket # is reassigned to the original email. try: recipient = amodels.User.objects.get(assopy_id=rid).user except amodels.User.DoesNotExist: pass else: if recipient.email != email: log.info( 'email "%s" found on genropy; but user (%s) have a different email: "%s"', email.encode('utf-8'), unicode(recipient).encode('utf-8'), recipient.email.encode('utf-8')) email = recipient.email else: log.info( 'email "%s" found on genropy; user (%s)', email.encode('utf-8'), unicode(recipient).encode('utf-8')) if recipient is None: log.info('No user found for the email "%s"; time to create a new one', email) just_created = True u = amodels.User.objects.create_user(email=email, token=True, send_mail=False) recipient = u.user name = email else: log.info('Found a local user (%s) for the email "%s"', unicode(recipient).encode('utf-8'), email.encode('utf-8')) just_created = False try: auser = recipient.assopy_user except amodels.User.DoesNotExist: # doh... this django user is not an assopy user, surely something # coming from before the introduction of assopy app. auser = amodels.User(user=recipient) auser.save() if not auser.token: recipient.assopy_user.token = str(uuid.uuid4()) recipient.assopy_user.save() name = '%s %s' % (recipient.first_name, recipient.last_name) _reset_ticket(ticket) utils.email( 'ticket-assigned', ctx={ 'name': name, 'just_created': just_created, 'ticket': ticket, 'link': settings.DEFAULT_URL_PREFIX + reverse('p3-user', kwargs={'token': recipient.assopy_user.token}), }, to=[email] ).send() return email
def _assign_ticket(ticket, email): email = email.strip() try: recipient = auth.models.User.objects.get(email__iexact=email) except auth.models.User.DoesNotExist: try: # Here I'm using filter + [0] instead of .get because it could happen, # even if it shouldn't, that two identities have the same email # (e.g. if someone used the same email on multiple remote services # but connected these services to two local users). # It's not a problem if more identities have the same email (note # that the authentication backend already checks that the same email # won't be used twice to create django users) because anway they're # email validated by external services. recipient = amodels.UserIdentity.objects.filter( email__iexact=email)[0].user.user except IndexError: recipient = None if recipient is None: from assopy.settings import GENRO_BACKEND if GENRO_BACKEND: from assopy.clients import genro rid = genro.users(email)['r0'] if rid is not None: # the email it's not associated to a django user, but genropy # knows it. If rid is assigned to an assopy user I'll reuse the # connected user. This check works when the ticket is assigned # to a user, the user modifies its email but later the ticket # is reassigned to the original email. try: recipient = amodels.User.objects.get(assopy_id=rid).user except amodels.User.DoesNotExist: pass else: if recipient.email != email: log.info( 'email "%s" found on genropy; but user (%s) have a different email: "%s"', email.encode('utf-8'), unicode(recipient).encode('utf-8'), recipient.email.encode('utf-8')) email = recipient.email else: log.info('email "%s" found on genropy; user (%s)', email.encode('utf-8'), unicode(recipient).encode('utf-8')) if recipient is None: log.info('No user found for the email "%s"; time to create a new one', email) just_created = True u = amodels.User.objects.create_user(email=email, token=True, send_mail=False) recipient = u.user name = email else: log.info('Found a local user (%s) for the email "%s"', unicode(recipient).encode('utf-8'), email.encode('utf-8')) just_created = False try: auser = recipient.assopy_user except amodels.User.DoesNotExist: # doh... this django user is not an assopy user, surely something # coming from before the introduction of assopy app. auser = amodels.User(user=recipient) auser.save() if not auser.token: recipient.assopy_user.token = str(uuid.uuid4()) recipient.assopy_user.save() name = '%s %s' % (recipient.first_name, recipient.last_name) _reset_ticket(ticket) utils.email( 'ticket-assigned', ctx={ 'name': name, 'just_created': just_created, 'ticket': ticket, 'link': settings.DEFAULT_URL_PREFIX + reverse('p3-user', kwargs={'token': recipient.assopy_user.token}), }, to=[email]).send() return email
def _assign_ticket(ticket, email): email = email.strip() try: recipient = auth.models.User.objects.get(email__iexact=email) except auth.models.User.DoesNotExist: try: # qui uso filter + [0] invece che .get perchè potrebbe accadere, # anche se non dovrebbe, che due identità abbiano la stessa email # (ad esempio se una persona a usato la stessa mail su più servizi # remoti ma ha collegato questi servizi a due utenti locali # diversi). Non è un problema se più identità hanno la stessa email # (nota che il backend di autenticazione già verifica che la stessa # email non venga usata due volte per creare utenti django) perché # in ogni caso si tratta di email verificate da servizi esterni. recipient = amodels.UserIdentity.objects.filter( email__iexact=email)[0].user.user except IndexError: recipient = None if recipient is None: from assopy.clients import genro rid = genro.users(email)['r0'] if rid is not None: # l'email non è associata ad un utente django ma genropy la # conosce. Se rid è assegnato ad un utente assopy riutilizzo # l'utente collegato. Questo check funziona quando un biglietto # viene assegnato ad un utente, quest'ultimo cambia email ma poi il # biglietto viene riassegnato nuovamente all'email originale. try: recipient = amodels.User.objects.get(assopy_id=rid).user except amodels.User.DoesNotExist: pass else: if recipient.email != email: log.info( 'email "%s" found on genropy; but user (%s) have a different email: "%s"', email.encode('utf-8'), unicode(recipient).encode('utf-8'), recipient.email.encode('utf-8')) email = recipient.email else: log.info('email "%s" found on genropy; user (%s)', email.encode('utf-8'), unicode(recipient).encode('utf-8')) if recipient is None: log.info('No user found for the email "%s"; time to create a new one', email) just_created = True u = amodels.User.objects.create_user(email=email, token=True, send_mail=False) recipient = u.user name = email else: log.info('Found a local user (%s) for the email "%s"', unicode(recipient).encode('utf-8'), email.encode('utf-8')) just_created = False try: auser = recipient.assopy_user except amodels.User.DoesNotExist: # uff, ho un utente su django che non è un assopy user, sicuramente # strascichi prima dell'introduzione dell'app assopy auser = amodels.User(user=recipient) auser.save() if not auser.token: recipient.assopy_user.token = str(uuid.uuid4()) recipient.assopy_user.save() name = '%s %s' % (recipient.first_name, recipient.last_name) _reset_ticket(ticket) utils.email( 'ticket-assigned', ctx={ 'name': name, 'just_created': just_created, 'ticket': ticket, 'link': settings.DEFAULT_URL_PREFIX + reverse('p3-user', kwargs={'token': recipient.assopy_user.token}), }, to=[email]).send() return email