如何在django social-auth中设置用户的电子邮件地址

时间:2012-03-26 22:21:42

标签: django django-socialauth

我安装了django social-auth(来自omab)并且用户在数据库中有一个我想保留的电子邮件地址但是当用户使用social-auth从facebook登录时,他们的电子邮件被替换为一个他们在他们的Facebook帐户。我不确定默认设置是否为默认设置,无法找到如何停止此行为。

3 个答案:

答案 0 :(得分:9)

您是否尝试过 SOCIAL_AUTH_PROTECTED_USER_FIELDS ? :)

从手册:

  

update_user_details管道处理器将设置某些字段   用户对象,例如电子邮件。将其设置为您只有的字段列表   想要为新创建的用户设置:

     
    
      

SOCIAL_AUTH_PROTECTED_USER_FIELDS = ['email',]

    
  
     

如果已定义,还将存储更多额外值。详细信息   设置在下面的OpenId和OAuth部分中列出。

答案 1 :(得分:5)

我找到了它,pipeline负责的是

social_auth.backends.pipeline.user.update_user_details

我刚从管道中删除它,现在电子邮件地址和名称等详细信息留给用户填写。

答案 2 :(得分:3)

我正在发布我的解决方案(更新用户详细信息,而不是覆盖它们),因此它可以帮助某人。基于pipeline.user.update_user_details,我编写了以下代码:

def fill_user_details(backend, details, response, user, is_new=False, *args,
                        **kwargs):
    """Fills user details using data from provider, without overwriting
    existing values.

    backend: Current social authentication backend
    details: User details given by authentication provider
    response: ?
    user: User ID given by authentication provider
    is_new: flag

    source: social_auth.backends.pipeline.user.update_user_details
    """
    # Each pipeline entry must return a dict or None, any value in the dict
    # will be used in the kwargs argument for the next pipeline entry.
    #
    # If any function returns something else beside a dict or None, the
    # workflow will be cut and the value returned immediately, this is useful
    # to return HttpReponse instances like HttpResponseRedirect.

    changed = False  # flag to track changes

    for name, value in details.iteritems():
        # do not update username, it was already generated
        if name in (USERNAME, 'id', 'pk'):
            continue

        # set it only if the existing value is not set or is an empty string
        existing_value = getattr(user, name, None)
        if value is not None and (existing_value is None or
                                  not is_valid_string(existing_value)):
            setattr(user, name, value)
            changed = True

    # Fire a pre-update signal sending current backend instance,
    # user instance (created or retrieved from database), service
    # response and processed details.
    #
    # Also fire socialauth_registered signal for newly registered
    # users.
    #
    # Signal handlers must return True or False to signal instance
    # changes. Send method returns a list of tuples with receiver
    # and it's response.
    signal_response = lambda (receiver, response): response
    signal_kwargs = {'sender': backend.__class__, 'user': user,
                     'response': response, 'details': details}

    changed |= any(filter(signal_response, pre_update.send(**signal_kwargs)))

    # Fire socialauth_registered signal on new user registration
    if is_new:
        changed |= any(filter(signal_response,
            socialauth_registered.send(**signal_kwargs)))

    if changed:
        user.save()