-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Capture when user was last active #5630
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
Conversation
Migration Checklist
Generated by 🚫 danger |
src/sentry/middleware/user.py
Outdated
| if last_active and freq > (now - last_active): | ||
| return | ||
|
|
||
| request.user.last_active = now |
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.
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 |
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.
looks like all of this code was copy/pasta and is unused
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.
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.
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.
you dont need it at all
this wont work with the API though, you need to do that through their authentication frameworks
cc0f366 to
8287ae3
Compare
|
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. |
src/sentry/middleware/user.py
Outdated
| def process_view(self, request, view_func, view_args, view_kwargs): | ||
| try: | ||
| auth = TokenAuthentication() | ||
| request.user = auth.authenticate(request)[0] |
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.
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
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.
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?
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.
Ok so:
- 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
- if request.user is filled in later by SessionAuthentication in the API, we should handle it there
- 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)
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.
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.
src/sentry/middleware/user.py
Outdated
| class UserActiveMiddleware(object): | ||
| def process_view(self, request, view_func, view_args, view_kwargs): | ||
| try: | ||
| auth = TokenAuthentication() |
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.
given that request.user is always set in session API calls, just remove this and then we're good to go
|
Will do! Thanks
…On Wed, Jun 28, 2017 at 4:48 PM, David Cramer ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/sentry/middleware/user.py
<#5630 (comment)>:
> @@ -0,0 +1,28 @@
+from __future__ import absolute_import
+
+from datetime import timedelta
+
+from django.utils import timezone
+from sentry.api.authentication import TokenAuthentication
+
+
+class UserActiveMiddleware(object):
+ def process_view(self, request, view_func, view_args, view_kwargs):
+ try:
+ auth = TokenAuthentication()
given that request.user is always set in session API calls, just remove
this and then we're good to go
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5630 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/APef3SANVS2XKfL68M0Zqe4_jmFJa3ncks5sIuY1gaJpZM4OFx-d>
.
|
256ae39 to
0c3aadf
Compare
|
Ok- took out all the TokenAuth logic. I had to add in some disallowed paths
for a couple of paths we don't need that were breaking tests on static
pages.
On Wed, Jun 28, 2017 at 4:49 PM, Dena Mwangi <notifications@github.com>
wrote:
… Will do! Thanks
On Wed, Jun 28, 2017 at 4:48 PM, David Cramer ***@***.***>
wrote:
> ***@***.**** commented on this pull request.
> ------------------------------
>
> In src/sentry/middleware/user.py
> <#5630 (comment)>:
>
> > @@ -0,0 +1,28 @@
> +from __future__ import absolute_import
> +
> +from datetime import timedelta
> +
> +from django.utils import timezone
> +from sentry.api.authentication import TokenAuthentication
> +
> +
> +class UserActiveMiddleware(object):
> + def process_view(self, request, view_func, view_args, view_kwargs):
> + try:
> + auth = TokenAuthentication()
>
> given that request.user is always set in session API calls, just remove
> this and then we're good to go
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <#5630#
pullrequestreview-46997589>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-
auth/APef3SANVS2XKfL68M0Zqe4_jmFJa3ncks5sIuY1gaJpZM4OFx-d>
> .
>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#5630 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/APef3RJTbsauUegKvolS9FTB3KH9JuBmks5sIuZ0gaJpZM4OFx-d>
.
|
src/sentry/middleware/user.py
Outdated
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.
you could also do if hasattr(request, 'user') as I assume thats the issue you hit w/ the static paths
… and that are breaking their tests
0c3aadf to
de77476
Compare
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.