mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-16 06:22:59 +00:00
Call add_user
more often
- Ensures add_user is called as part of startup *for all users*. This was previously only true for users not already in the db. - Normalize usernames in whitelist and admin sets - Call add_user on new users logged in when there is no whitelist.
This commit is contained in:
@@ -677,6 +677,7 @@ class JupyterHub(Application):
|
|||||||
self.authenticator.normalize_username(name)
|
self.authenticator.normalize_username(name)
|
||||||
for name in self.authenticator.admin_users
|
for name in self.authenticator.admin_users
|
||||||
]
|
]
|
||||||
|
self.authenticator.admin_users = set(admin_users) # force normalization
|
||||||
for username in admin_users:
|
for username in admin_users:
|
||||||
if not self.authenticator.validate_username(username):
|
if not self.authenticator.validate_username(username):
|
||||||
raise ValueError("username %r is not valid" % username)
|
raise ValueError("username %r is not valid" % username)
|
||||||
@@ -704,6 +705,7 @@ class JupyterHub(Application):
|
|||||||
self.authenticator.normalize_username(name)
|
self.authenticator.normalize_username(name)
|
||||||
for name in self.authenticator.whitelist
|
for name in self.authenticator.whitelist
|
||||||
]
|
]
|
||||||
|
self.authenticator.whitelist = set(whitelist) # force normalization
|
||||||
for username in whitelist:
|
for username in whitelist:
|
||||||
if not self.authenticator.validate_username(username):
|
if not self.authenticator.validate_username(username):
|
||||||
raise ValueError("username %r is not valid" % username)
|
raise ValueError("username %r is not valid" % username)
|
||||||
@@ -719,24 +721,21 @@ class JupyterHub(Application):
|
|||||||
new_users.append(user)
|
new_users.append(user)
|
||||||
db.add(user)
|
db.add(user)
|
||||||
|
|
||||||
if whitelist:
|
db.commit()
|
||||||
# fill the whitelist with any users loaded from the db,
|
|
||||||
# so we are consistent in both directions.
|
# Notify authenticator of all users.
|
||||||
# This lets whitelist be used to set up initial list,
|
# This ensures Auth whitelist is up-to-date with the database.
|
||||||
# but changes to the whitelist can occur in the database,
|
# This lets whitelist be used to set up initial list,
|
||||||
# and persist across sessions.
|
# but changes to the whitelist can occur in the database,
|
||||||
for user in db.query(orm.User):
|
# and persist across sessions.
|
||||||
self.authenticator.whitelist.add(user.name)
|
for user in db.query(orm.User):
|
||||||
|
yield gen.maybe_future(self.authenticator.add_user(user))
|
||||||
|
db.commit() # can add_user touch the db?
|
||||||
|
|
||||||
# The whitelist set and the users in the db are now the same.
|
# The whitelist set and the users in the db are now the same.
|
||||||
# From this point on, any user changes should be done simultaneously
|
# From this point on, any user changes should be done simultaneously
|
||||||
# to the whitelist set and user db, unless the whitelist is empty (all users allowed).
|
# to the whitelist set and user db, unless the whitelist is empty (all users allowed).
|
||||||
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
for user in new_users:
|
|
||||||
yield gen.maybe_future(self.authenticator.add_user(user))
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def init_spawners(self):
|
def init_spawners(self):
|
||||||
|
@@ -200,6 +200,7 @@ class BaseHandler(RequestHandler):
|
|||||||
self.db.add(u)
|
self.db.add(u)
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
user = self._user_from_orm(u)
|
user = self._user_from_orm(u)
|
||||||
|
self.authenticator.add_user(user)
|
||||||
return user
|
return user
|
||||||
|
|
||||||
def clear_login_cookie(self, name=None):
|
def clear_login_cookie(self, name=None):
|
||||||
|
@@ -222,6 +222,16 @@ def test_logout(app):
|
|||||||
assert r.cookies == {}
|
assert r.cookies == {}
|
||||||
|
|
||||||
|
|
||||||
|
def test_login_no_whitelist_adds_user(app):
|
||||||
|
auth = app.authenticator
|
||||||
|
mock_add_user = mock.Mock()
|
||||||
|
with mock.patch.object(auth, 'add_user', mock_add_user):
|
||||||
|
cookies = app.login_user('jubal')
|
||||||
|
|
||||||
|
user = app.users['jubal']
|
||||||
|
assert mock_add_user.mock_calls == [mock.call(user)]
|
||||||
|
|
||||||
|
|
||||||
def test_static_files(app):
|
def test_static_files(app):
|
||||||
base_url = ujoin(public_url(app), app.hub.server.base_url)
|
base_url = ujoin(public_url(app), app.hub.server.base_url)
|
||||||
print(base_url)
|
print(base_url)
|
||||||
|
Reference in New Issue
Block a user