Allow admins to customize /user-redirect/ behavior

/user-redirect/ is used to help link to a particular url
in the logged in user's authenticated notebook. For example,
if I'm logged in as user 'yuvipanda' and hit the URL
/hub/user-redirect/git-pull, it'll redirect me to
/user/yuvipanda/git-pull. This is extremely useful in
connecting hub links to notebook server extensions, such
as nbgitpuller.

Admins might want to customize how this redirection is done -
for example, redirect users to different running servers
based on the nbgitpuller repository they are linking from.
Adding a hook here helps accomplish that.
This commit is contained in:
YuviPanda
2019-10-20 11:29:02 -07:00
parent 66f8d6a626
commit 400c64b4ef
2 changed files with 41 additions and 10 deletions

View File

@@ -140,6 +140,10 @@ class BaseHandler(RequestHandler):
def hub(self):
return self.settings['hub']
@property
def app(self):
return self.settings['app']
@property
def proxy(self):
return self.settings['proxy']
@@ -1470,20 +1474,31 @@ class UserRedirectHandler(BaseHandler):
If the user is not logged in, send to login URL, redirecting back here.
If c.JupyterHub.user_redirect_hook is set, the return value of that
callable is used to generate the redirect URL.
.. versionadded:: 0.7
"""
@web.authenticated
def get(self, path):
user = self.current_user
user_url = url_path_join(user.url, path)
if self.request.query:
user_url = url_concat(user_url, parse_qsl(self.request.query))
async def get(self, path):
# If hook is present to generate URL to redirect to, use that instead
# of the default. The configurer is responsible for making sure this
# URL is right. If None is returned by the hook, we do our normal
# processing
url = None
if self.app.user_redirect_hook:
url = await self.app.user_redirect_hook(self, path)
if url is None:
user = self.current_user
user_url = url_path_join(user.url, path)
if self.request.query:
user_url = url_concat(user_url, parse_qsl(self.request.query))
url = url_concat(
url_path_join(self.hub.base_url, "spawn", user.escaped_name),
{"next": user_url},
)
url = url_concat(
url_path_join(self.hub.base_url, "spawn", user.escaped_name),
{"next": user_url},
)
self.redirect(url)