Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update authorization tutorial #6704

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions doc/how_to/authentication/authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ You may return either a boolean `True`/`False` value OR a string, which will be
```python
def authorize(user_info, page):
user = user_info['user']
if page == '/':
if page == "/":
if user in ADMINS:
return '/admin'
else:
return '/user'
return '/admin'
else:
return '/user'
elif user in ADMINS:
return True
else:
return page.startswith('/user')
return True
else:
return page.startswith('/user')
philippjfr marked this conversation as resolved.
Show resolved Hide resolved
```

The callback above would direct users visiting the root (`/`) to the appropriate endpoint depending on whether they are in the list of `ADMINS`. If the user is an admin they are granted access to both the `/user` and `/admin` endpoints while non-admin users will only be granted access to the `/user` endpoint.
Expand Down Expand Up @@ -60,13 +60,10 @@ authorized_user_paths = {
}

def authorize(user_info, request_path):
current_user = user_info['username']
current_path = parse.urlparse(request_path).path
if current_user not in authorized_user_paths:
return False
current_user_paths = authorized_user_paths[current_user]
if current_path in current_user_paths:
return True
if current_user := authorized_user_paths.get(user_info['username']):
current_path = parse.urlparse(request_path).path
current_user_paths = authorized_user_paths[current_user]
return current_path in current_user_paths
return False

pn.config.authorize_callback = authorize
Expand Down
Loading