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

fix: Load user in a deferred task #1406

Merged

Conversation

sveneberth
Copy link
Member

Fixes #1405

@sveneberth sveneberth added bug(fix) Something isn't working or address a specific issue or vulnerability Priority: Critical This should be dealt with ASAP. It's blocking someone. labels Feb 5, 2025
@sveneberth sveneberth added this to the ViUR-core v3.7 milestone Feb 5, 2025
Copy link
Contributor

@ArneGudermann ArneGudermann left a comment

Choose a reason for hiding this comment

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

Can we Check If the Task ist deferred an the loaf the User all the time ?

@sveneberth
Copy link
Member Author

Can we Check If the Task ist deferred an the loaf the User all the time ?

Sorry, but I don't understand what you want to do?

@phorward
Copy link
Member

phorward commented Feb 5, 2025

Can we Check If the Task ist deferred an the loaf the User all the time ?

Sorry, but I don't understand what you want to do?

He means, that in case the request is on a deferred task, the current user is automatically loaded. IMHO the user is provided together with the deferred call request. @ArneGudermann's proposal is useful.

@phorward phorward added the Priority: High After critical issues are fixed, these should be dealt with before any further issues. label Feb 5, 2025
@sveneberth
Copy link
Member Author

Can we Check If the Task ist deferred an the loaf the User all the time ?

Sorry, but I don't understand what you want to do?

He means, that in case the request is on a deferred task, the current user is automatically loaded. IMHO the user is provided together with the deferred call request. @ArneGudermann's proposal is useful.

I still have no idea what I should change in this PR.
Please give me an example, suggestion or alternative PR.

@@ -1312,7 +1312,7 @@ def secondFactorProviderByClass(self, cls) -> UserSecondFactorAuthentication:
def getCurrentUser(self):
session = current.session.get()

if session and session.loaded and (user := session.get("user")):
if session and (user := session.get("user")):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if session and (user := session.get("user")):
req = current.request.get()
if session and (session.loaded or req.is_deferred) and (user := session.get("user")):

Copy link
Member Author

Choose a reason for hiding this comment

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

We can do it that way if you like.

But that doesn't really make any difference. If the session is not loaded, then session.get("user") is None anyway and the condition is falsy.

Copy link
Contributor

Choose a reason for hiding this comment

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

Now it works in your case i think. If the session is not loaded we can get the current user if the request is deferred.

Copy link
Member Author

Choose a reason for hiding this comment

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

no, I mean in the non-deferred case it wouldn't matter whether you have

if session and (user := session.get("user")):

or

if session and (session.loaded or req.is_deferred) and (user := session.get("user")):

because if the session is not loaded (session.loaded is then False) there is no content in the unloaded session object and the session.get() cannot load anything and returns the default value None.
This means that both statements are logically the same.

@phorward phorward merged commit 623b40b into viur-framework:main Feb 7, 2025
3 checks passed
@sveneberth sveneberth deleted the hotfix/task_user_session_env branch February 7, 2025 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug(fix) Something isn't working or address a specific issue or vulnerability Priority: Critical This should be dealt with ASAP. It's blocking someone. Priority: High After critical issues are fixed, these should be dealt with before any further issues.
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

User inside a CallDeferred task is ALWAYS None
3 participants