def _linkProfileToEmail(email, profile): try: current = autils.get_user_account_from_email(email) except auth.models.User.DoesNotExist: current = auth.models.User.objects.create_user( janrain.suggest_username(profile), email) try: current.first_name = profile['name']['givenName'] except KeyError: pass try: current.last_name = profile['name']['familyName'] except KeyError: pass current.is_active = True current.save() log.debug('new (active) django user created "%s"', current) else: log.debug('django user found "%s"', current) try: # se current è stato trovato tra gli utenti locali forse esiste # anche la controparte assopy user = current.assopy_user except models.User.DoesNotExist: log.debug('the current user "%s" will become an assopy user', current) user = models.User(user=current) user.save() log.debug('a new identity (for "%s") will be linked to "%s"', profile['identifier'], current) identity = models.UserIdentity.objects.create_from_profile(user, profile) return identity
def connect_profile_to_assopy(backend, user, response, *args, **kwargs): """ CB to be filled in the python-social-auth pipeline in order to verify if user is a new user and (if not) assopy and conference profiles are created. For more details about the reason for adding this method look at assopy.views.janrain_token that should be doing the same but for a janrain backend instead of python-social-auth. Params: Refer to http://python-social-auth.readthedocs.org/en/latest/pipeline.html for more details """ # TODO: `email` is not used anywhere if backend.name.startswith('google'): email = kwargs['details']['email'] try: # check if assopy user have already been created for this user asso_user = user.assopy_user except amodels.User.DoesNotExist: # create it if not...s log.debug('the current user "%s" will become an assopy user', user) asso_user = amodels.User(user=user) asso_user.save() # same for conference profile... profile = cmodels.AttendeeProfile.objects.getOrCreateForUser(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 authenticate(self, uid=None): try: user = User.objects.select_related('assopy_user').get(pk=uid, is_active=True) 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.DoesNotExist: return None
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.SEARCH_MISSING_USERS_ON_BACKEND: return None
def _assign_ticket(ticket, email): email = email.strip() try: recipient = autils.get_user_account_from_email(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: 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) # Set new ticket name ticket.name = name ticket.save() 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