Changeset - d22a7430999f
[Not reviewed]
default
0 1 0
Mads Kiilerich - 7 years ago 2018-12-25 20:31:12
mads@kiilerich.com
auth: change get_allowed_ips to be more resilient when operating on a cached default user

Before, random changes to how things are fetched and cached across database
sessions could cause get_allowed_ips to fail with:

DetachedInstanceError: Instance <User> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: http://sqlalche.me/e/bhk3)

Instead, just check for user_id, using same pattern as a bit later in same function.
1 file changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/auth.py
Show inline comments
 
@@ -677,26 +677,26 @@ class AuthUser(object):
 
        au = AuthUser(
 
            user_id=cookie.get('user_id'),
 
            is_external_auth=cookie.get('is_external_auth', False),
 
        )
 
        au.is_authenticated = True
 
        return au
 

	
 
    @classmethod
 
    def get_allowed_ips(cls, user_id, cache=False, inherit_from_default=False):
 
        _set = set()
 

	
 
        if inherit_from_default:
 
            default_ips = UserIpMap.query().filter(UserIpMap.user ==
 
                                            User.get_default_user(cache=True))
 
            default_ips = UserIpMap.query().filter(UserIpMap.user_id ==
 
                                            User.get_default_user(cache=True).user_id)
 
            if cache:
 
                default_ips = default_ips.options(FromCache("sql_cache_short",
 
                                                  "get_user_ips_default"))
 

	
 
            # populate from default user
 
            for ip in default_ips:
 
                try:
 
                    _set.add(ip.ip_addr)
 
                except ObjectDeletedError:
 
                    # since we use heavy caching sometimes it happens that we get
 
                    # deleted objects here, we just skip them
 
                    pass
0 comments (0 inline, 0 general)