2012-03-26 54 views
9

我有django社交認證安裝(從omab),並且用戶有一個電子郵件地址在數據庫中,這是我想保留的,但當用戶登錄從facebook使用social-auth,他們的電子郵件被他們在facebook賬戶中的電子郵件取代。我不確定這些設置是否爲默認設置,也無法找到如何阻止此行爲。如何不設置用戶的電子郵件地址在Django社會認證

回答

5

我發現,在pipeline的負責就是

social_auth.backends.pipeline.user.update_user_details 

我剛剛從管線上拆下了它,現在像電子郵件地址和姓名的細節留給用戶填寫。

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() 
+0

嗨。我如何去實現你在上面發佈在我的Django應用程序中的解決方案?上面的代碼應該去哪裏? – 2013-04-04 15:07:36

+0

@ArpitRai如果你有一個相對較新版本的'social-auth',我建議你今天使用Zhe的答案(http://stackoverflow.com/a/16267281/556413) – glarrain 2014-04-21 21:51:23

9

你試過SOCIAL_AUTH_PROTECTED_USER_FIELDS? :)

從手冊:

的update_user_details管線處理器將設置 用戶對象的某些字段,如電子郵件。這組到字段列表,你只 要爲新創建的用戶設置:

SOCIAL_AUTH_PROTECTED_USER_FIELDS = [「郵件」,]

而且更多額外的價值將被保存,如果定義。有關此 設置的詳細信息在下面的OpenId和OAuth部分中列出。

+0

我肯定會這樣做的,但回到當天那是另一回事:)受保護的領域還沒有引入。 – Bastian 2013-04-29 10:40:18

+0

是的,我知道。 昨天我試圖解決它,我差點去glarrain的解決方案。然後我在手冊中找到了這些設置。只是把它放在這裏,以防有人TL; DR手冊:) – 2013-04-29 11:22:17

+0

是的!給你+1 – Bastian 2013-05-09 23:51:35

相關問題