-
Notifications
You must be signed in to change notification settings - Fork 0
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
Expire user permissions #72
Changes from 1 commit
837b5f4
28815f5
011ebda
df017ea
3622bbf
b437aba
764d677
2d002ca
e678353
716651c
c8111d4
7ef5429
a195929
b4eb1b3
a5f2c58
e92b419
e98e66b
da46257
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
import superset | ||
|
||
from datetime import timedelta | ||
from flask import flash, g, redirect, request, session, url_for | ||
|
||
from .utils import SESSION_USER_DOMAINS_KEY | ||
from superset.config import USER_DOMAIN_ROLE_EXPIRY | ||
|
||
from .utils import ( | ||
SESSION_DOMAIN_ROLE_LAST_SYNCED_AT, | ||
SESSION_USER_DOMAINS_KEY, | ||
DomainSyncUtil, | ||
datetime_utcnow_naive, | ||
) | ||
|
||
|
||
def before_request_hook(): | ||
|
@@ -9,7 +19,10 @@ def before_request_hook(): | |
if any hook returns a response, | ||
break the chain and return that response | ||
""" | ||
hooks = [ensure_domain_selected] | ||
hooks = [ | ||
ensure_domain_selected, | ||
sync_user_domain_role | ||
] | ||
for _function in hooks: | ||
response = _function() | ||
if response: | ||
|
@@ -64,6 +77,28 @@ def ensure_domain_selected(): | |
return redirect(url_for('SelectDomainView.list', next=request.url)) | ||
|
||
|
||
def sync_user_domain_role(): | ||
if is_user_admin() or ( | ||
request.url_rule | ||
and request.url_rule.endpoint in DOMAIN_EXCLUDED_VIEWS | ||
): | ||
return | ||
if _domain_role_expired(): | ||
_sync_domain_role() | ||
|
||
|
||
def _domain_role_expired(): | ||
if not session.get(SESSION_DOMAIN_ROLE_LAST_SYNCED_AT): | ||
return True | ||
|
||
time_since_last_sync = datetime_utcnow_naive() - session[SESSION_DOMAIN_ROLE_LAST_SYNCED_AT] | ||
return time_since_last_sync >= timedelta(minutes=USER_DOMAIN_ROLE_EXPIRY) | ||
|
||
|
||
def _sync_domain_role(): | ||
DomainSyncUtil(superset.appbuilder.sm).sync_domain_role(g.hq_domain) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mkangia There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya, Great question. I guess I will follow the same pattern you have followed otherwise in case sync domain role returns false and redirect user to the select domain page, though possibly with a different error message like "Your permissions have expired and the sync failed due to an unexpected error. Please select project space again or login to continue" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This turned out to be a little challenging, but I got it working like this |
||
|
||
|
||
def is_valid_user_domain(hq_domain): | ||
# Admins have access to all domains | ||
return is_user_admin() or hq_domain in user_domains() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,3 +180,5 @@ class CeleryConfig: | |
"session_cookie_secure": False, | ||
} | ||
|
||
USER_DOMAIN_ROLE_EXPIRY = 60 # minutes | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkangia
Nit: for more robust implementation we could check if
session[SESSION_DOMAIN_ROLE_LAST_SYNCED_AT]
is a datetime-like object that could be operated on.How about:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. I can add that. This value is only always set by CCA and can't edited by the user so it would certainly be a datetime but no harm in adding safety.