def update_user(self, user, attributes, attribute_mapping, force_save=False): """Update a user with a set of attributes and returns the updated user. By default it uses a mapping defined in the settings constant SAML_ATTRIBUTE_MAPPING. For each attribute, if the user object has that field defined it will be set, otherwise it will try to set it in the profile object. """ if not attribute_mapping: return user try: profile = user.get_profile() except ObjectDoesNotExist: profile = None except SiteProfileNotAvailable: profile = None # Django 1.5 custom model assumed except AttributeError: profile = user user_modified = False profile_modified = False for saml_attr, django_attrs in attribute_mapping.items(): try: for attr in django_attrs: if hasattr(user, attr): modified = self._set_attribute( user, attr, attributes[saml_attr][0]) user_modified = user_modified or modified elif profile is not None and hasattr(profile, attr): modified = self._set_attribute( profile, attr, attributes[saml_attr][0]) profile_modified = profile_modified or modified except KeyError: # the saml attribute is missing pass logger.debug('Sending the pre_save signal') signal_modified = any([ response for receiver, response in pre_user_save.send_robust( sender=user, attributes=attributes, user_modified=user_modified) ]) if user_modified or signal_modified or force_save: user.save() if (profile is not None and (profile_modified or signal_modified or force_save)): profile.save() return user
def update_user(self, user, attributes, attribute_mapping, force_save=False): """Update a user with a set of attributes and returns the updated user. By default it uses a mapping defined in the settings constant SAML_ATTRIBUTE_MAPPING. For each attribute, if the user object has that field defined it will be set, otherwise it will try to set it in the profile object. """ if not attribute_mapping: return user try: profile = user.get_profile() except ObjectDoesNotExist: profile = None except SiteProfileNotAvailable: profile = None # Django 1.5 custom model assumed except AttributeError: profile = user user_modified = False profile_modified = False for saml_attr, django_attrs in attribute_mapping.items(): try: for attr in django_attrs: if hasattr(user, attr): modified = self._set_attribute( user, attr, self._get_saml_value(attributes, saml_attr)) user_modified = user_modified or modified elif profile is not None and hasattr(profile, attr): modified = self._set_attribute( profile, attr, self._get_saml_value(attributes, saml_attr)) profile_modified = profile_modified or modified except KeyError: # the saml attribute is missing pass logger.debug('Sending the pre_save signal') signal_modified = any( [response for receiver, response in pre_user_save.send_robust(sender=user, attributes=attributes, user_modified=user_modified)] ) if user_modified or signal_modified or force_save: user.save() if (profile is not None and (profile_modified or signal_modified or force_save)): profile.save() return user
def update_user(self, user, attributes, attribute_mapping, force_save=False): """Update a user with a set of attributes and returns the updated user. By default it uses a mapping defined in the settings constant SAML_ATTRIBUTE_MAPPING. For each attribute, if the user object has that field defined it will be set. """ if not attribute_mapping: return user user_modified = False for saml_attr, django_attrs in attribute_mapping.items(): attr_value_list = attributes.get(saml_attr) if not attr_value_list: logger.debug( 'Could not find value for "%s", not updating fields "%s"', saml_attr, django_attrs) continue for attr in django_attrs: if hasattr(user, attr): user_attr = getattr(user, attr) if callable(user_attr): modified = user_attr(attr_value_list) else: modified = self._set_attribute(user, attr, attr_value_list[0]) user_modified = user_modified or modified else: logger.debug( 'Could not find attribute "%s" on user "%s"', attr, user) logger.debug('Sending the pre_save signal') signal_modified = any( [response for receiver, response in pre_user_save.send_robust(sender=user.__class__, instance=user, attributes=attributes, user_modified=user_modified)] ) if user_modified or signal_modified or force_save: user.save() return user
def update_user(self, user, attributes, attribute_mapping, force_save=False): """Update a user with a set of attributes and returns the updated user. By default it uses a mapping defined in the settings constant SAML_ATTRIBUTE_MAPPING. For each attribute, if the user object has that field defined it will be set. """ if not attribute_mapping: return user user_modified = False for saml_attr, django_attrs in attribute_mapping.items(): attr_value_list = attributes.get(saml_attr) if not attr_value_list: logger.debug( 'Could not find value for "%s", not updating fields "%s"', saml_attr, django_attrs) continue for attr in django_attrs: if hasattr(user, attr): user_attr = getattr(user, attr) if callable(user_attr): modified = user_attr(attr_value_list) else: modified = self._set_attribute(user, attr, attr_value_list[0]) user_modified = user_modified or modified else: logger.debug( 'Could not find attribute "%s" on user "%s"', attr, user) logger.debug('Sending the pre_save signal') signal_modified = any( [response for receiver, response in pre_user_save.send_robust(sender=user.__class__, instance=user, attributes=attributes, user_modified=user_modified)] ) if user_modified or signal_modified or force_save: user.save() return user