Skip to content

feat: log warning if token is being created for inactive user#873

Merged
vgrozdanic merged 2 commits intomasterfrom
vgrozdanic/warning-when-user-is-not-active
Feb 28, 2025
Merged

feat: log warning if token is being created for inactive user#873
vgrozdanic merged 2 commits intomasterfrom
vgrozdanic/warning-when-user-is-not-active

Conversation

@vgrozdanic
Copy link
Contributor

@vgrozdanic vgrozdanic commented Feb 26, 2025

As per discussion, this PR adds a warning if a developer tries to create the token for the non-active user.

Part of #779

@vgrozdanic vgrozdanic requested a review from a team February 26, 2025 22:09
after authenticating the user's credentials.
"""

if not user.is_active:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if not user.is_active:
if hasattr(user, "is_active") and not user.is_active:

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if not user.is_active:
if not getattr(user, "is_active", True):

how about this code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, it was naive of me to assume is_active is always present in any user model...

@2ykwang thank you for the suggestion, i agree that it is shorter, but (in my personal opinion) it makes it harder for future maintainer to understand what this code is doing. The code that Andrew proposed, to me personally, is easier to read :)

Copy link
Member

@Andrew-Chen-Wang Andrew-Chen-Wang left a comment

Choose a reason for hiding this comment

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

lgtm ty


format_lazy: Callable = lazy(format_lazy, str)

logger = logging.getLogger("rest_framework_simplejwt")
Copy link
Member

Choose a reason for hiding this comment

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

hm, if we use this logger, won't all the logs come from rest_framework_simplejwt.utils?

Idk how other libraries do it; maybe they just do logging.getLogger(__name__) per file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the name will always be "rest_framework_simplejwt" - i have seen this pattern in Django in multiple places, that's why i have chosen it :)

Copy link
Member

@Andrew-Chen-Wang Andrew-Chen-Wang Feb 27, 2025

Choose a reason for hiding this comment

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

I see. Django does this on a per-"module" (or like package?) naming scheme. Since this package is smaller, wouldn't it make more sense to do this on a per-file basis with __name__?

Copy link
Contributor Author

@vgrozdanic vgrozdanic Feb 27, 2025

Choose a reason for hiding this comment

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

I have no strong opinions, but i am not sure if we do it per-file basis, how would settings look like, if someone wants to filter out this logs. I am not very familiar with logging module, but i can take a deeper look next week into this.

My main concern is how would someone for example filer out all but critical logs from this package. I think using only __name__ will only include the file/module name, and leave out rest_framework_simplejwt

There must be a way, i am just not sure what is the way to do it

Copy link
Member

Choose a reason for hiding this comment

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

My main concern is how would someone for example filer out all but critical logs from this package. I think using only name will only include the file/module name, and leave out rest_framework_simplejwt

That's a great point. I think we can merge this then

@vgrozdanic vgrozdanic merged commit 14e8b2c into master Feb 28, 2025
21 checks passed
@vgrozdanic vgrozdanic deleted the vgrozdanic/warning-when-user-is-not-active branch February 28, 2025 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants