From b55934ab6f232472e2ff7e342032baa484ea545b Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Thu, 25 Oct 2012 10:54:24 -0700 Subject: [PATCH] Account: ensure all non-defaulted attributes are atomically created. This prevents an issue where registration_ip is not set on the account object causing downstream code to break. This happened because the app would crash for whatever reason between committing in register() and adding the IP and committing again in ApiController:_handle_register. --- r2/r2/controllers/api.py | 3 +-- r2/r2/models/account.py | 3 ++- r2/r2/models/populatedb.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/r2/r2/controllers/api.py b/r2/r2/controllers/api.py index b2e792d35..1aef9f41e 100755 --- a/r2/r2/controllers/api.py +++ b/r2/r2/controllers/api.py @@ -451,14 +451,13 @@ class ApiController(RedditController, OAuth2ResourceController): responder.has_errors('ratelimit', errors.RATELIMIT) or (not g.disable_captcha and bad_captcha)): - user = register(name, password) + user = register(name, password, request.ip) VRatelimit.ratelimit(rate_ip = True, prefix = "rate_register_") #anything else we know (email, languages)? if email: user.email = email - user.registration_ip = request.ip user.pref_lang = c.lang if c.content_langs == 'all': user.pref_content_langs = 'all' diff --git a/r2/r2/models/account.py b/r2/r2/models/account.py index a1a7bb91f..6a6496f00 100644 --- a/r2/r2/models/account.py +++ b/r2/r2/models/account.py @@ -749,7 +749,7 @@ def change_password(user, newpassword): return True #TODO reset the cache -def register(name, password): +def register(name, password, registration_ip): try: a = Account._by_name(name) raise AccountExists @@ -758,6 +758,7 @@ def register(name, password): password = bcrypt_password(password)) # new accounts keep the profanity filter settings until opting out a.pref_no_profanity = True + a.registration_ip = registration_ip a._commit() #clear the caches diff --git a/r2/r2/models/populatedb.py b/r2/r2/models/populatedb.py index eabd2f7af..594d1810e 100644 --- a/r2/r2/models/populatedb.py +++ b/r2/r2/models/populatedb.py @@ -38,7 +38,7 @@ def populate(num_srs = 10, num_users = 1000, num_links = 100, num_comments = 20, try: a = Account._by_name(g.system_user) except NotFound: - a = register(g.system_user, "password") + a = register(g.system_user, "password", "127.0.0.1") srs = [] for i in range(num_srs): @@ -60,7 +60,7 @@ def populate(num_srs = 10, num_users = 1000, num_links = 100, num_comments = 20, in range(int(random.uniform(1, 10))) ]) name = 'test_' + name_ext try: - a = register(name, name) + a = register(name, name, "127.0.0.1") except AccountExists: a = Account._by_name(name) accounts.append(a)