def populate_user(self, request, sociallogin, data): first_name = data.get('first_name') last_name = data.get('last_name') email = data.get('email') name = data.get('name') username = data.get('username') user = sociallogin.account.user print(sociallogin.account.provider) # TODO: Consider using the super class for the stuff that is not unique to facebook! if sociallogin.account.provider == 'facebook': user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) # Save a good username if username: username_suggestions = [username, first_name, last_name, email, 'user'] else: username_suggestions = [first_name, last_name, email, 'user'] user_username(user, generate_unique_username(username_suggestions)) return user elif sociallogin.account.provider == 'twitter': if "screen_name" in sociallogin.account.extra_data.keys(): username = sociallogin.account.extra_data['screen_name'] user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) # Save a good username if username: username_suggestions = [username, first_name, last_name, email, 'user'] else: username_suggestions = [first_name, last_name, email, 'user'] user_username(user, generate_unique_username(username_suggestions)) return user elif sociallogin.account.provider == 'google': #if "screen_name" in sociallogin.account.extra_data.keys(): # username = sociallogin.account.extra_data['screen_name'] user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) print("sociallogin.account.extra_data:", str(sociallogin.account.extra_data).encode('utf-8')) if username: username_suggestions = [username, first_name, last_name, email, 'user'] else: username_suggestions = [first_name, last_name, email, 'user'] user_username(user, generate_unique_username(username_suggestions)) return user else: return super(SocialAccountAdapter, self).populate_user(request, sociallogin, data)
def populate_user(self, request, sociallogin, data): first_name = data.get('first_name') last_name = data.get('last_name') email = data.get('email') name = data.get('name') username = data.get('username') user = sociallogin.account.user # TODO: Consider using the super class for the stuff that is not unique to facebook! if sociallogin.account.provider == 'facebook': user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) # Save a good username if username: username_suggestions = [username, first_name, last_name, email, 'user'] else: username_suggestions = [first_name, last_name, email, 'user'] user_username(user, generate_unique_username(username_suggestions)) return user elif sociallogin.account.provider == 'twitter': if "screen_name" in sociallogin.account.extra_data.keys(): username = sociallogin.account.extra_data['screen_name'] user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) # Save a good username if username: username_suggestions = [username, first_name, last_name, email, 'user'] else: username_suggestions = [first_name, last_name, email, 'user'] user_username(user, generate_unique_username(username_suggestions)) return user elif sociallogin.account.provider == 'google': #if "screen_name" in sociallogin.account.extra_data.keys(): # username = sociallogin.account.extra_data['screen_name'] user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) if username: username_suggestions = [username, first_name, last_name, email, 'user'] else: username_suggestions = [first_name, last_name, email, 'user'] user_username(user, generate_unique_username(username_suggestions)) return user else: return super(SocialAccountAdapter, self).populate_user(request, sociallogin, data)
def populate_user(self, request, sociallogin, data): first_name = data.get("first_name") last_name = data.get("last_name") email = data.get("email") name = data.get("name") username = data.get("username") user = sociallogin.account.user # TODO: Consider using the super class for the stuff that is not unique to facebook! if sociallogin.account.provider == "facebook": user_email(user, valid_email_or_none(email) or "") name_parts = (name or "").partition(" ") user_field(user, "first_name", first_name or name_parts[0]) user_field(user, "last_name", last_name or name_parts[2]) # Save a good username if username: username_suggestions = [username, first_name, last_name, email, "user"] else: username_suggestions = [first_name, last_name, email, "user"] user_username(user, generate_unique_username(username_suggestions)) return user elif sociallogin.account.provider == "twitter": if "screen_name" in sociallogin.account.extra_data.keys(): username = sociallogin.account.extra_data["screen_name"] user_email(user, valid_email_or_none(email) or "") name_parts = (name or "").partition(" ") user_field(user, "first_name", first_name or name_parts[0]) user_field(user, "last_name", last_name or name_parts[2]) # Save a good username if username: username_suggestions = [username, first_name, last_name, email, "user"] else: username_suggestions = [first_name, last_name, email, "user"] user_username(user, generate_unique_username(username_suggestions)) return user elif sociallogin.account.provider == "google": # if "screen_name" in sociallogin.account.extra_data.keys(): # username = sociallogin.account.extra_data['screen_name'] user_email(user, valid_email_or_none(email) or "") name_parts = (name or "").partition(" ") user_field(user, "first_name", first_name or name_parts[0]) user_field(user, "last_name", last_name or name_parts[2]) if username: username_suggestions = [username, first_name, last_name, email, "user"] else: username_suggestions = [first_name, last_name, email, "user"] user_username(user, generate_unique_username(username_suggestions)) return user else: return super(SocialAccountAdapter, self).populate_user(request, sociallogin, data)
def _get_email_from_response(response): email = None sreg = SRegResponse.fromSuccessResponse(response) if sreg: email = valid_email_or_none(sreg.get(SRegField.EMAIL)) if not email: ax = FetchResponse.fromSuccessResponse(response) if ax: try: values = ax.get(AXAttribute.CONTACT_EMAIL) if values: email = valid_email_or_none(values[0]) except KeyError: pass return email
def get_email_from_response(response): email = None sreg = SRegResponse.fromSuccessResponse(response) if sreg: email = valid_email_or_none(sreg.get(SRegField.EMAIL)) if not email: ax = FetchResponse.fromSuccessResponse(response) if ax: try: values = ax.get(AXAttribute.CONTACT_EMAIL) if values: email = valid_email_or_none(values[0]) except KeyError: pass return email
def 設定欄位內容(self, 資料內容={}): "讓allauth的接口使用" email = valid_email_or_none(資料內容.get('email')) or '' if email: user_email(self, email) try: self.來源 except: name = 資料內容.get('name') username = 資料內容.get('username') last_name = 資料內容.get('last_name') first_name = 資料內容.get('first_name') if name: 來源名 = name elif username: 來源名 = username elif last_name and first_name: 來源名 = last_name + first_name else: 來源名 = email self.來源 = 來源表. 加來源({ '名':來源名}) if 'password1' in 資料內容: self.set_password(資料內容["password1"]) else: self.set_unusable_password() return
def populate_user(self, request, sociallogin, data): """ Hook that can be used to further populate the user instance. For convenience, we populate several common fields. Note that the user instance being populated represents a suggested User instance that represents the social user that is in the process of being logged in. The User instance need not be completely valid and conflict free. For example, verifying whether or not the username already exists, is not a responsibility. """ username = data.get('username') first_name = data.get('first_name') last_name = data.get('last_name') email = username time_zone = data.get('time_zone') locale = data.get('locale') gravatar = data.get('profile_image_url') user = sociallogin.user user_username(user, username or '') user_email(user, valid_email_or_none(email) or '') user_field(user, 'given_name', first_name or '') user.is_active = True user.is_researcher = True user_field(user, 'family_name', last_name or '') user_field(user, 'time_zone', time_zone) user_field(user, 'locale', locale) user_field(user, '_identicon', self.get_base64_gravatar(gravatar)) return user
def cleanup_email_addresses(request, addresses): """ Takes a list of EmailAddress instances and cleans it up, making sure only valid ones remain, without multiple primaries etc. Order is important: e.g. if multiple primary e-mail addresses exist, the first one encountered will be kept as primary. """ from .models import EmailAddress adapter = get_adapter() # Let's group by `email` e2a = SortedDict() # maps email to EmailAddress primary_addresses = [] verified_addresses = [] primary_verified_addresses = [] for address in addresses: # Pick up only valid ones... email = valid_email_or_none(address.email) if not email: continue # ... and non-conflicting ones... if (app_settings.UNIQUE_EMAIL and EmailAddress.objects .filter(email__iexact=email) .exists()): continue a = e2a.get(email.lower()) if a: a.primary = a.primary or address.primary a.verified = a.verified or address.verified else: a = address a.verified = a.verified or adapter.is_email_verified(request, a.email) e2a[email.lower()] = a if a.primary: primary_addresses.append(a) if a.verified: primary_verified_addresses.append(a) if a.verified: verified_addresses.append(a) # Now that we got things sorted out, let's assign a primary if primary_verified_addresses: primary_address = primary_verified_addresses[0] elif verified_addresses: # Pick any verified as primary primary_address = verified_addresses[0] elif primary_addresses: # Okay, let's pick primary then, even if unverified primary_address = primary_addresses[0] elif e2a: # Pick the first primary_address = e2a.keys()[0] else: # Empty primary_address = None # There can only be one primary for a in e2a.values(): a.primary = primary_address.email.lower() == a.email.lower() return list(e2a.values()), primary_address
def 設定欄位內容(self, 資料內容={}): "讓allauth的接口使用" email = valid_email_or_none(資料內容.get('email')) or '' if email: user_email(self, email) try: self.來源 except Exception: name = 資料內容.get('name') username = 資料內容.get('username') last_name = 資料內容.get('last_name') first_name = 資料內容.get('first_name') if name: 來源名 = name elif username: 來源名 = username elif last_name and first_name: 來源名 = last_name + first_name else: 來源名 = email self.來源 = 來源表.加來源({'名': 來源名}) if 'password1' in 資料內容: self.set_password(資料內容["password1"]) else: self.set_unusable_password() return
def login(request): ret = None if request.method == 'POST': form = FacebookConnectForm(request.POST) if form.is_valid(): token = form.cleaned_data['access_token'] g = GraphAPI(token) facebook_me = g.get_object("me") email = valid_email_or_none(facebook_me.get('email')) social_id = facebook_me['id'] try: account = FacebookAccount.objects.get(social_id=social_id) except FacebookAccount.DoesNotExist: account = FacebookAccount(social_id=social_id) account.link = facebook_me['link'] account.name = facebook_me['name'] if account.pk: account.save() data = dict(email=email, facebook_me=facebook_me) # some facebook accounts don't have this data data.update((k, v) for (k, v) in facebook_me.items() if k in ['username', 'first_name', 'last_name']) ret = complete_social_login(request, data, account) if not ret: ret = render_authentication_error(request) return ret
def login(request): ret = None if request.method == 'POST': form = FacebookConnectForm(request.POST) if form.is_valid(): token = form.cleaned_data['access_token'] g = GraphAPI(token) data = g.get_object("me") email = valid_email_or_none(data.get('email')) social_id = data['id'] try: account = FacebookAccount.objects.get(social_id=social_id) except FacebookAccount.DoesNotExist: account = FacebookAccount(social_id=social_id) account.link = data['link'] account.name = data['name'] #adding the storing of users accesstoken account.access_token = token if account.pk: account.save() data = dict(email=email,facebook_me=data) ret = complete_social_login(request, data, account) if not ret: ret = render_authentication_error(request) return ret
def populate_user(self, request, sociallogin, data): """ Hook that can be used to further populate the user instance. For convenience, we populate several common fields. Note that the user instance being populated represents a suggested User instance that represents the social user that is in the process of being logged in. The User instance need not be completely valid and conflict free. For example, verifying whether or not the username already exists, is not a responsibility. """ username = data.get('username') first_name = data.get('first_name') last_name = data.get('last_name') email = data.get('email') name = data.get('name') time_zone = data.get('time_zone') locale = data.get('locale') gravatar = data.get('profile_image_url') user = sociallogin.user user_username(user, username or '') user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) user_field(user, 'time_zone', time_zone) user_field(user, 'locale', locale) user_field(user, 'gravatar', gravatar) return user
def populate_user(self, request, sociallogin, data): user = sociallogin.user username = data.get('username') first_name = data.get('first_name') last_name = data.get('last_name') if sociallogin.account.provider == 'twitter': email = '{}@twitter.com'.format(username) else: email = data.get('email') birthday = sociallogin.account.extra_data.get('birthday') if birthday: user_field( user, 'data_de_nascimento', '{}'.format( datetime.strptime(birthday, '%m/%d/%Y').strftime('%Y-%m-%d'))) name = data.get('name') user_username(user, username or '') user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') first_name = first_name or name_parts[0] last_name = last_name or name_parts[2] user_field(user, 'nome', u'{} {}'.decode('utf-8').format(first_name, last_name)) if not user.data_ativacao_email: user.data_ativacao_email = timezone.now() return user
def login(request): ret = None if request.method == 'POST': form = FacebookConnectForm(request.POST) if form.is_valid(): try: token = form.cleaned_data['access_token'] g = GraphAPI(token) facebook_me = g.get_object("me") email = valid_email_or_none(facebook_me.get('email')) social_id = facebook_me['id'] try: account = FacebookAccount.objects.get(social_id=social_id) except FacebookAccount.DoesNotExist: account = FacebookAccount(social_id=social_id) data = dict(email=email, facebook_access_token=token, facebook_me=facebook_me) # some facebook accounts don't have this data data.update((k,v) for (k,v) in facebook_me.items() if k in ['username', 'first_name', 'last_name']) # Don't save partial/temporary accounts that haven't # gone through the full signup yet, as there is no # User attached yet. if account.pk: account.sync(data) ret = complete_social_login(request, data, account) except (GraphAPIError, IOError): pass if not ret: ret = render_authentication_error(request) return ret
def populate_user(self,request,sociallogin,data): socialaccount = sociallogin.account username = data.get('username') first_name = data.get('first_name') last_name = data.get('last_name') email = data.get('email') name = data.get('name') user = sociallogin.account.user user_model = get_user_model() try: query = {"username" + '__iexact': username} user_model.objects.get(**query) user_username(user,generate_unique_username([first_name,last_name,email,'user'])) except Exception as e: if username == None: user_username(user,generate_unique_username([first_name,last_name,email,'user'])) else: user_username(user,username.replace(".","")) user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) return user
def login(request): ret = None if request.method == 'POST': form = FacebookConnectForm(request.POST) if form.is_valid(): try: token = form.cleaned_data['access_token'] g = GraphAPI(token) facebook_me = g.get_object("me") email = valid_email_or_none(facebook_me.get('email')) social_id = facebook_me['id'] try: account = FacebookAccount.objects.get(social_id=social_id) except FacebookAccount.DoesNotExist: account = FacebookAccount(social_id=social_id) data = dict(email=email, facebook_access_token=token, facebook_me=facebook_me) # some facebook accounts don't have this data data.update((k, v) for (k, v) in facebook_me.items() if k in ['username', 'first_name', 'last_name']) # Don't save partial/temporary accounts that haven't # gone through the full signup yet, as there is no # User attached yet. if account.pk: account.sync(data) ret = complete_social_login(request, data, account) except (GraphAPIError, IOError): pass if not ret: ret = render_authentication_error(request) return ret
def login(request): ret = None if request.method == 'POST': form = FacebookConnectForm(request.POST) if form.is_valid(): token = form.cleaned_data['access_token'] g = GraphAPI(token) facebook_me = g.get_object("me") email = valid_email_or_none(facebook_me.get('email')) social_id = facebook_me['id'] try: account = FacebookAccount.objects.get(social_id=social_id) except FacebookAccount.DoesNotExist: account = FacebookAccount(social_id=social_id) account.link = facebook_me['link'] account.name = facebook_me['name'] if account.pk: account.save() data = dict(email=email, facebook_me=facebook_me) # some facebook accounts don't have this data data.update((k,v) for (k,v) in facebook_me.items() if k in ['username', 'first_name', 'last_name']) ret = complete_social_login(request, data, account) if not ret: ret = render_authentication_error(request) return ret
def populate_user(self, request, sociallogin, data): """ Hook that can be used to further populate the user instance. For convenience, we populate several common fields. Note that the user instance being populated represents a suggested User instance that represents the social user that is in the process of being logged in. The User instance need not be completely valid and conflict free. For example, verifying whether or not the username already exists, is not a responsibility. """ first_name = data.get('first_name') last_name = data.get('last_name') email = data.get('email') name = data.get('name') user = sociallogin.user name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) username = utils.generateRandomUsername(first_name or name_parts[0] or '') user_email(user, valid_email_or_none(email) or (username + '@redzza.com')) user_username(user, username) return user
def populate_user(self, request, sociallogin, data): username = data.get('username') email = data.get('email') user = sociallogin.user user_username(user, username or '') user_email(user, valid_email_or_none(email) or '') date_of_birth = data.get('birthdate') if date_of_birth: try: parsed = datetime.strptime(date_of_birth, "%Y-%m-%d") user.date_of_birth = parsed except ValueError: pass name = data.get('name') first_name = data.get('first_name') last_name = data.get('last_name') # if a full name exists, use that, if not, # join first_name and last_name if name: user_field(user, 'full_name', name) else: merged = ' '.join(x for x in [first_name, last_name] if x) if merged != '': user_field(user, 'full_name', merged) return user
def cleanup_email_addresses(request, addresses): """ Takes a list of EmailAddress instances and cleans it up, making sure only valid ones remain, without multiple primaries etc. Order is important: e.g. if multiple primary e-mail addresses exist, the first one encountered will be kept as primary. """ from .models import EmailAddress adapter = get_adapter(request) # Let's group by `email` e2a = OrderedDict() # maps email to EmailAddress primary_addresses = [] verified_addresses = [] primary_verified_addresses = [] for address in addresses: # Pick up only valid ones... email = valid_email_or_none(address.email) if not email: continue # ... and non-conflicting ones... if (app_settings.UNIQUE_EMAIL and EmailAddress.objects.filter(email__iexact=email).exists()): continue a = e2a.get(email.lower()) if a: a.primary = a.primary or address.primary a.verified = a.verified or address.verified else: a = address a.verified = a.verified or adapter.is_email_verified( request, a.email) e2a[email.lower()] = a if a.primary: primary_addresses.append(a) if a.verified: primary_verified_addresses.append(a) if a.verified: verified_addresses.append(a) # Now that we got things sorted out, let's assign a primary if primary_verified_addresses: primary_address = primary_verified_addresses[0] elif verified_addresses: # Pick any verified as primary primary_address = verified_addresses[0] elif primary_addresses: # Okay, let's pick primary then, even if unverified primary_address = primary_addresses[0] elif e2a: # Pick the first primary_address = e2a.keys()[0] else: # Empty primary_address = None # There can only be one primary for a in e2a.values(): a.primary = primary_address.email.lower() == a.email.lower() return list(e2a.values()), primary_address
def populate_user(self, request, sociallogin, data): """ This method was overridden due to a bug with an empty username. """ email = data.get('email') user = sociallogin.user user_email(user, valid_email_or_none(email) or '') return user
def populate_user(self, request, sociallogin, data): """ 이 메소드가 호출되기까지의 과정은 대략적으로 다음과 같다. 1. 인증 코드를 이용하여 액세스 토큰을 요청하는 API를 호출한다. 2. 받아온 엑세스 토큰을 이용하여 SocialToken 인스턴스를 생성한다. 3. 액세스 토큰을 이용하여 소셜 계정의 정보를 조회하는 API를 호출한다. 4. 받아온 소셜 계정의 정보를 가지고 SocialAccount 인스턴스를 생성한다. 5. 비어 있는 User 인스턴스를 생성하여 SocialAccount 인스턴스에 연결한다. 6. 이 메소드를 호출하여 User 인스턴스의 필드 값들을 채워준다. 인자로 전달받는 sociallogin 인스턴스의 account 필드는 SocialAccount 인스턴스를 가리킨다. 따라서 sociallogin.account 인스턴스로부터 소셜 계정의 정보를 참조할 수 있다. """ # 자동 회원가입으로 생성될 유저 user = sociallogin.user # 소셜 서비스 이름 (페이스북, 카카오, 네이버) provider_name = sociallogin.account.provider.upper() # 엑세스 토큰으로 소셜 서비스 서버에게 API를 요청하여 받아온 소셜 계정의 정보 (= API 응답 데이터) social_account_data = sociallogin.account.extra_data # 페이스북 로그인 if provider_name == 'FACEBOOK': email = valid_email_or_none(social_account_data.get('email')) name = social_account_data.get('name') # 카카오 로그인 elif provider_name == 'KAKAO': email = valid_email_or_none( social_account_data['kakao_account'].get('email')) name = social_account_data.get('properties', {}).get('nickname') # 네이버 로그인 else: email = valid_email_or_none(social_account_data.get('email')) name = social_account_data.get('name') # 유저 인스턴스의 이메일/이름 필드 값 설정 user.email = email user.name = name return user
def populate_user(self, request, sociallogin, data): name = data.get('name') email = data.get('email') provider = data.get('provider') user = sociallogin.user user_email(user, valid_email_or_none(email) or '') user_field(user, 'name', name) user_field(user, 'provider', provider) return user
def _save_email_addresses(self, request): adapter = get_adapter() # user.email may not be listed as an EmailAddress ... user = self.account.user try: primary_email_address = EmailAddress.objects.get(user=user, primary=True) except EmailAddress.DoesNotExist: primary_email_address = None if (user.email and (user.email.lower() not in [e.email.lower() for e in self.email_addresses])): # ... so let's append it email_address \ = EmailAddress(user=user, email=user.email, verified=adapter.is_email_verified(request, user.email), primary=(not primary_email_address)) if not primary_email_address: primary_email_address = email_address self.email_addresses.append(email_address) for email_address in self.email_addresses: # Pick up only valid ones... email = valid_email_or_none(email_address.email) if not email: continue # ... and non-conflicting ones... if (account_settings.UNIQUE_EMAIL and EmailAddress.objects.filter(email__iexact=email).exists()): continue if email_address.primary: if not primary_email_address: primary_email_address = email_address else: email_address.primary = False email_address.user = user email_address.verified \ = (email_address.verified or adapter.is_email_verified(request, email_address.email)) email_address.save() if primary_email_address and ( not user.email or primary_email_address.email.lower() != user.email.lower()): user.email = primary_email_address.email user.save() adapter.stash_email_verified(request, None)
def complete_login(self, request, app, token): client = LinkedInAPI(request, app.key, app.secret, self.request_token_url) extra_data = client.get_user_info() uid = extra_data['id'] email = valid_email_or_none(extra_data.get('email-address', '')) user = User(first_name=extra_data.get('first-name', ''), last_name=extra_data.get('last-name', ''), email=email) account = SocialAccount(user=user, provider=self.provider_id, extra_data=extra_data, uid=uid) return SocialLogin(account)
def _save_email_addresses(self, request): adapter = get_adapter() # user.email may not be listed as an EmailAddress ... user = self.account.user try: primary_email_address = EmailAddress.objects.get(user=user, primary=True) except EmailAddress.DoesNotExist: primary_email_address = None if (user.email and (user.email.lower() not in [e.email.lower() for e in self.email_addresses])): # ... so let's append it email_address \ = EmailAddress(user=user, email=user.email, verified=adapter.is_email_verified(request, user.email), primary=(not primary_email_address)) if not primary_email_address: primary_email_address = email_address self.email_addresses.append(email_address) for email_address in self.email_addresses: # Pick up only valid ones... email = valid_email_or_none(email_address.email) if not email: continue # ... and non-conflicting ones... if (account_settings.UNIQUE_EMAIL and EmailAddress.objects.filter(email__iexact=email).exists()): continue if email_address.primary: if not primary_email_address: primary_email_address = email_address else: email_address.primary = False email_address.user = user email_address.verified \ = (email_address.verified or adapter.is_email_verified(request, email_address.email)) email_address.save() if primary_email_address and (not user.email or primary_email_address.email.lower() != user.email.lower()): user.email = primary_email_address.email user.save() adapter.stash_email_verified(request, None)
def populate_user(self, request, sociallogin, data): sociallogin.user.type_profile = 'postulant' # original code from lib username = data.get('username') first_name = data.get('first_name') last_name = data.get('last_name') email = data.get('email') name = data.get('name') user = sociallogin.user user_username(user, username or '') user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) return user
def populate_new_user(self, username=None, first_name=None, last_name=None, email=None, name=None): """ Spawns a new User instance, safely and leniently populating several common fields. """ user = get_user_model()() user.username = username or '' user.email = valid_email_or_none(email) or '' name_parts= (name or '').partition(' ') user.first_name = first_name or name_parts[0] user.last_name = last_name or name_parts[2] return user
def fb_complete_login(app, token): resp = requests.get('https://graph.facebook.com/me', params={'access_token': token.token}) extra_data = resp.json email = valid_email_or_none(extra_data.get('email')) uid = extra_data['id'] user = User(email=email) # some facebook accounts don't have this data for k in ['username', 'first_name', 'last_name']: v = extra_data.get(k) if v: setattr(user, k, v) account = SocialAccount(uid=uid, provider=FacebookProvider.id, extra_data=extra_data, user=user) return SocialLogin(account)
def fb_complete_login(app, token): resp = requests.get('https://graph.facebook.com/me', params={ 'access_token': token.token }) extra_data = resp.json email = valid_email_or_none(extra_data.get('email')) uid = extra_data['id'] user = User(email=email) # some facebook accounts don't have this data for k in ['username', 'first_name', 'last_name']: v = extra_data.get(k) if v: setattr(user, k, v) account = SocialAccount(uid=uid, provider=FacebookProvider.id, extra_data=extra_data, user=user) return SocialLogin(account)
def populate_user(self, request, sociallogin, data): social_app_name = sociallogin.account.provider.upper() if social_app_name == "GOOGLE": username = data.get('last_name') + data.get('first_name') else: username = data.get('username') first_name = data.get('first_name') last_name = data.get('last_name') email = data.get('email') name = data.get('name') user = sociallogin.user user_username(user, username or '') user_email(user, valid_email_or_none(email) or '') name_parts = (name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) return user
def save(self): user = self.account.user user.save() self.account.user = user self.account.save() if self.token: self.token.account = self.account self.token.save() for email_address in self.email_addresses: # Pick up only valid ones... email = valid_email_or_none(email_address.email) if not email: continue # ... and non-conflicting ones... if (account_settings.UNIQUE_EMAIL and EmailAddress.objects.filter(email__iexact=email).exists()): continue email_address.user = user email_address.save()
def populate_user(self, request, sociallogin, data): """ Hook that can be used to further populate the user instance. For convenience, we populate several common fields. Note that the user instance being populated represents a suggested User instance that represents the social user that is in the process of being logged in. The User instance need not be completely valid and conflict free. For example, verifying whether or not the username already exists, is not a responsibility. Overwriting original function to pull in extra information """ username = data.get('username') full_name = data.get('full_name') slack_display_name = data.get('slack_display_name') first_name = data.get('first_name') last_name = data.get('last_name') email = data.get('email') profile_image = data.get('profile_image') about = data.get('about') timezone = data.get('timezone') user = sociallogin.user user_username(user, username or '') user_email(user, valid_email_or_none(email) or '') name_parts = (full_name or '').partition(' ') user_field(user, 'first_name', first_name or name_parts[0]) user_field(user, 'last_name', last_name or name_parts[2]) user_field(user, 'full_name', full_name) user_field(user, 'slack_display_name', slack_display_name) user_field(user, 'username', username) user_field(user, 'profile_image', profile_image) user_field(user, 'about', about) user_field(user, 'timezone', timezone) return user
def _save_email_addresses(self): # user.email may not be listed as an EmailAddress ... user = self.account.user if (user.email and (user.email.lower() not in [e.email.lower() for e in self.email_addresses])): # ... so let's append it self.email_addresses.append(EmailAddress(user=user, email=user.email, verified=False, primary=True)) for email_address in self.email_addresses: # Pick up only valid ones... email = valid_email_or_none(email_address.email) if not email: continue # ... and non-conflicting ones... if (account_settings.UNIQUE_EMAIL and EmailAddress.objects.filter(email__iexact=email).exists()): continue email_address.user = user email_address.save()
def populate_new_user(self, username=None, first_name=None, last_name=None, email=None, name=None): """ Spawns a new User instance, safely and leniently populating several common fields. This method is used to create a suggested User instance that represents the social user that is in the process of being logged in. Validation is not a requirement. For example, verifying whether or not a username already exists, is not a responsibility. """ user = get_user_model()() user.username = username or '' user.email = valid_email_or_none(email) or '' name_parts= (name or '').partition(' ') user.first_name = first_name or name_parts[0] user.last_name = last_name or name_parts[2] return user
def login(request): ret = None if request.method == 'POST': form = FacebookConnectForm(request.POST) if form.is_valid(): token = form.cleaned_data['access_token'] g = GraphAPI(token) data = g.get_object("me") email = valid_email_or_none(data.get('email')) social_id = data['id'] try: account = FacebookAccount.objects.get(social_id=social_id) except FacebookAccount.DoesNotExist: account = FacebookAccount(social_id=social_id) account.link = data['link'] account.name = data['name'] if account.pk: account.save() data = dict(email=email, facebook_me=data) ret = complete_social_login(request, data, account) if not ret: ret = render_authentication_error(request) return ret
def populate_new_user(self, username=None, first_name=None, last_name=None, email=None, name=None): """ Spawns a new User instance, safely and leniently populating several common fields. This method is used to create a suggested User instance that represents the social user that is in the process of being logged in. Validation is not a requirement. For example, verifying whether or not a username already exists, is not a responsibility. """ user = get_user_model()() user.username = username or '' user.email = valid_email_or_none(email) or '' name_parts = (name or '').partition(' ') user.first_name = first_name or name_parts[0] user.last_name = last_name or name_parts[2] return user
def test_email_validation(self): s = '*****@*****.**' # noqa self.assertEqual(s, utils.valid_email_or_none(s))