Skip to content

Conversation

@denamwangi
Copy link

Not sure how frequently we want to update this field- currently I have this set to hourly and depending on what ops thinks is reasonable without overloading we can get more precise and narrow the window.

@denamwangi denamwangi requested review from dcramer and ehfeng June 26, 2017 19:58
@ghost
Copy link

ghost commented Jun 26, 2017

1 Warning
⚠️ PR includes migrations

Migration Checklist

  • new columns need to be nullable (unless table is new)
  • migration with any new index needs to be done concurrently
  • data migrations should not be done inside a transaction
  • before merging, check to make sure there aren't conflicting migration ids

Generated by 🚫 danger

if last_active and freq > (now - last_active):
return

request.user.last_active = now
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should use request.user.update(last_active=now) to avoid rewriting the entire row (which is what Django does with save())

request.user.last_active = now
request.user.save()

view = view_func
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like all of this code was copy/pasta and is unused

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was to limit allowed_paths, to avoid event or commit ingestion endpoints, which aren't reflective of an active user. However...that might actually just be caught by the user.is_authenticated, given that there's no user in those requests.

Try testing those out locally, see what happened.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont need it at all

this wont work with the API though, you need to do that through their authentication frameworks

@denamwangi
Copy link
Author

Ah good point on update vs save- made that change. Took out the allowed paths logic (Eric - double checked events aren't affecting as dcramer said) and added in TokenAuth to pull out the user from the API side. Let me know if there's a different way we handle this.

def process_view(self, request, view_func, view_args, view_kwargs):
try:
auth = TokenAuthentication()
request.user = auth.authenticate(request)[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you actually should be doing this as part of the existing auth paths rather than in the middleware -- otherwise this is causing duplicate auth logic and it won't work correctly with various API setups

you can do this in api/authentication in authenticate_credentials as its at least closer to limiting the scope. alternatively you could figure out where to stuff in it in api/base.py::Endpoint

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey dcramer- take a look at the screencast of a replay with the auth middleware. It looks like get_user() in auth.py is handling the session auth. Thoughts?

active_user_authMW.zip

Copy link
Member

@dcramer dcramer Jun 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so:

  1. if request.user is an authenticated user when they hit an API request thats fired via the javascript app (e.g. loading the stream), then we dont need to do anything special here
  2. if request.user is filled in later by SessionAuthentication in the API, we should handle it there
  3. in either situation, we dont care about TokenAuth (I forgot what it meant on original review), and we wouldn't want to approach capturing the api auth here even if we did

(ehfeng: adding numbers instead of bullets)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the need for case 2—the auth.py middleware fires before the SessionAuthentication and if we're not looking to handle TokenAuth, I don't see what requests would not have a user in case 1, but have a user for case 2.

class UserActiveMiddleware(object):
def process_view(self, request, view_func, view_args, view_kwargs):
try:
auth = TokenAuthentication()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that request.user is always set in session API calls, just remove this and then we're good to go

@denamwangi
Copy link
Author

denamwangi commented Jun 28, 2017 via email

@denamwangi denamwangi force-pushed the add-last-active branch 2 times, most recently from 256ae39 to 0c3aadf Compare June 29, 2017 17:47
@denamwangi
Copy link
Author

denamwangi commented Jun 29, 2017 via email

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could also do if hasattr(request, 'user') as I assume thats the issue you hit w/ the static paths

@denamwangi denamwangi merged commit 52d182b into master Jun 29, 2017
@denamwangi denamwangi deleted the add-last-active branch June 30, 2017 00:20
@github-actions github-actions bot locked and limited conversation to collaborators Dec 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants