def username_available(request, username): if User.objects.filter(username__iexact=username): return {'available': False} return { 'available': True, 'reserved_from_canvas': User.is_username_reserved(username), }
def signup(request, username, password, email, facebook_access_token=None): if check_rate_limit(request, username): raise ServiceError("Too many signup attempts. Wait a minute and try again.") try: return _login(request, password, username=username, email=email) except ValidationError: pass migrated_from_canvas_account = False errors = defaultdict(list) fb_user = None if facebook_access_token: fb_user = FacebookUser.create_from_access_token(facebook_access_token) def username_taken(): # Ugly hack. for error in errors['username']: if 'taken' in error: return errors['username'].append("Sorry! That username is already taken.") if not password: errors['password'].append("Please enter a password.") if not User.validate_password(password): errors['password'].append("Sorry, your password is too short. " "Please use {} or more characters.".format(User.MINIMUM_PASSWORD_LENGTH)) if not email: errors['email'].append("Please enter your email address.") elif not User.validate_email(email): errors['email'].append("Please enter a valid email address.") username_error = User.validate_username(username) if username_error: errors['username'].append(username_error) if not User.email_is_unused(email): errors['email'].append("Sorry! That email address is already being used for an account.") elif User.is_username_reserved(username): try: user = User.migrate_canvas_user(request, username, password, email=email) except IntegrityError: username_taken() except ValidationError: errors['username'] = ["""Sorry! This username is taken. Please pick a different username, """ + """or if you are "{}," enter your password to sign in.""".format(username)] else: migrated_from_canvas_account = True if errors: if fb_user: fb_user.delete() raise ValidationError(errors) if not migrated_from_canvas_account: try: user = User.objects.create_user(username, email, password) except IntegrityError: username_taken() raise ValidationError(errors) UserInfo.objects.create(user=user) if fb_user: fb_user.user = user fb_user.save() fb_user.notify_friends_of_signup(facebook_access_token) user.migrate_facebook_avatar(request, facebook_access_token) user = auth.authenticate(username=username, password=password) # auth.login starts a new session and copies the session data from the old one to the new one auth.login(request, user) return { 'user': PrivateUserDetails.from_id(user.id).to_client(), 'user_bio': user.userinfo.bio_text, 'user_subscribed_to_starred': is_subscribed(user, 'starred'), 'sessionid': request.session.session_key, 'migrated_from_canvas_account': migrated_from_canvas_account, }
def _login(request, password, username=None, email=None): migrated_from_canvas_account = False def wrong_password(): raise ValidationError({ 'password': "******" "Please try again (make sure your caps lock is off)." }) def wrong_username(username): raise ValidationError({ 'username': """The username you entered, "{}", doesn't exist. """.format(username) + """Please try again, or enter the e-mail address you used to sign up.""" }) def get_username_from_email(email): if not email: wrong_username(email) try: return User.objects.get(email=email).username except User.DoesNotExist: wrong_username(email) if not username and not email: raise ValidationError({'username': "******"}) if not username: username = get_username_from_email(email) user = auth.authenticate(username=username, password=password) if user is None: # Maybe they entered an email into the username field? try: User.objects.get(username=username) except User.DoesNotExist: try: #TODO This might be broken - should probably pass username. username = get_username_from_email(email) user = auth.authenticate(username=username, password=password) except ValidationError: # No such username exists. # See if it's a example.com account we need to migrate over. if User.is_username_reserved(username): try: user = User.migrate_canvas_user(request, username, password, email=email) except ValidationError as e: wrong_password() else: migrated_from_canvas_account = True if user is None: wrong_password() if not user.is_active: return inactive_user_http_response() auth.login(request, user) return { 'user': PrivateUserDetails.from_id(user.id).to_client(), 'user_bio': user.userinfo.bio_text, 'user_subscribed_to_starred': is_subscribed(user, 'starred'), 'sessionid': request.session.session_key, 'migrated_from_canvas_account': migrated_from_canvas_account, }
def signup(request, username, password, email, facebook_access_token=None): if check_rate_limit(request, username): raise ServiceError( "Too many signup attempts. Wait a minute and try again.") try: return _login(request, password, username=username, email=email) except ValidationError: pass migrated_from_canvas_account = False errors = defaultdict(list) fb_user = None if facebook_access_token: fb_user = FacebookUser.create_from_access_token(facebook_access_token) def username_taken(): # Ugly hack. for error in errors['username']: if 'taken' in error: return errors['username'].append("Sorry! That username is already taken.") if not password: errors['password'].append("Please enter a password.") if not User.validate_password(password): errors['password'].append("Sorry, your password is too short. " "Please use {} or more characters.".format( User.MINIMUM_PASSWORD_LENGTH)) if not email: errors['email'].append("Please enter your email address.") elif not User.validate_email(email): errors['email'].append("Please enter a valid email address.") username_error = User.validate_username(username) if username_error: errors['username'].append(username_error) if not User.email_is_unused(email): errors['email'].append( "Sorry! That email address is already being used for an account.") elif User.is_username_reserved(username): try: user = User.migrate_canvas_user(request, username, password, email=email) except IntegrityError: username_taken() except ValidationError: errors['username'] = [ """Sorry! This username is taken. Please pick a different username, """ + """or if you are "{}," enter your password to sign in.""". format(username) ] else: migrated_from_canvas_account = True if errors: if fb_user: fb_user.delete() raise ValidationError(errors) if not migrated_from_canvas_account: try: user = User.objects.create_user(username, email, password) except IntegrityError: username_taken() raise ValidationError(errors) UserInfo.objects.create(user=user) if fb_user: fb_user.user = user fb_user.save() fb_user.notify_friends_of_signup(facebook_access_token) user.migrate_facebook_avatar(request, facebook_access_token) user = auth.authenticate(username=username, password=password) # auth.login starts a new session and copies the session data from the old one to the new one auth.login(request, user) return { 'user': PrivateUserDetails.from_id(user.id).to_client(), 'user_bio': user.userinfo.bio_text, 'user_subscribed_to_starred': is_subscribed(user, 'starred'), 'sessionid': request.session.session_key, 'migrated_from_canvas_account': migrated_from_canvas_account, }
def _login(request, password, username=None, email=None): migrated_from_canvas_account = False def wrong_password(): raise ValidationError({ 'password': "******" "Please try again (make sure your caps lock is off)." }) def wrong_username(username): raise ValidationError({ 'username': """The username you entered, "{}", doesn't exist. """.format( username) + """Please try again, or enter the e-mail address you used to sign up.""" }) def get_username_from_email(email): if not email: wrong_username(email) try: return User.objects.get(email=email).username except User.DoesNotExist: wrong_username(email) if not username and not email: raise ValidationError({'username': "******"}) if not username: username = get_username_from_email(email) user = auth.authenticate(username=username, password=password) if user is None: # Maybe they entered an email into the username field? try: User.objects.get(username=username) except User.DoesNotExist: try: #TODO This might be broken - should probably pass username. username = get_username_from_email(email) user = auth.authenticate(username=username, password=password) except ValidationError: # No such username exists. # See if it's a example.com account we need to migrate over. if User.is_username_reserved(username): try: user = User.migrate_canvas_user(request, username, password, email=email) except ValidationError as e: wrong_password() else: migrated_from_canvas_account = True if user is None: wrong_password() if not user.is_active: return inactive_user_http_response() auth.login(request, user) return { 'user': PrivateUserDetails.from_id(user.id).to_client(), 'user_bio': user.userinfo.bio_text, 'user_subscribed_to_starred': is_subscribed(user, 'starred'), 'sessionid': request.session.session_key, 'migrated_from_canvas_account': migrated_from_canvas_account, }