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

[4.x]: Logged in user info not available in plugin init() #11033

Closed
wbrowar opened this issue Apr 28, 2022 · 6 comments
Closed

[4.x]: Logged in user info not available in plugin init() #11033

wbrowar opened this issue Apr 28, 2022 · 6 comments

Comments

@wbrowar
Copy link

wbrowar commented Apr 28, 2022

What happened?

Description

In Craft 3 I am able to get a user’s getIdentity info, or check if the user is an admin, from within my plugin’s init() method. In Craft 4 I am seeing null when I check for Craft::$app->getUser()->getIdentity() and I get false when checking for Craft::$app->getUser()->getIsAdmin().

This is happening to me and at least one other developer, however, other folks in Discord tried this out and get the expected behavior. 🤷‍♂️

Steps to reproduce

  1. Log into Craft as an admin or an active user.
  2. Add the following to your plugin class: use craft\elements\User;
  3. In your init() function, below parent::init();, add Craft::dd(Craft::$app->getUser()->getIdentity());3.
  4. Reload a page (when the plugin is installed and enabled), and you may or may not see null

Expected behavior

When checking for Craft::$app->getUser()->getIdentity() you should get the logged in User and access to their user properties.

Actual behavior

For my plugins, I'm getting null as a result of checking Craft::$app->getUser()->getIdentity().

Craft CMS version

4.0.0-RC1 (also beta.4)

PHP version

8.0.13

Operating system and version

Linux 5.10.104-linuxkit

Database type and version

MySQL 8.0.28

Image driver and version

Imagick @PACKAGE_VERSION@ (ImageMagick 7.0.11-14)

Installed plugins and versions

  • Guide | 3.2.0-beta.1 (unreleased)
  • Little Layout 1.1.0 (unreleased)
  • Redactor 3.0.0-beta.3
  • SEOmatic 4.0.0-beta.8
@brandonkelly
Copy link
Member

Meanwhile @mmikkel is seeing 4.0 fully solving #2473 for him.

Thinking both of these are just a bit of a crapshoot right now (and in 3.x).

Would it be possible to move your logic to after the app is fully initialized?

use craft\web\Application;

Craft::$app->on(Application::EVENT_INIT, function() {
    // ...
});

@mmikkel
Copy link
Contributor

mmikkel commented Apr 28, 2022

@brandonkelly For Craft 4, would it be possible (and a good idea) to make Craft throw an exception if a plugin or custom module tries to query the current user via Craft::$app->getUser()->getIdentity() too early in the request lifecycle (i.e. before the Application::EVENT_INIT event has fired)?

Admittedly, after ~5 years I still don't completely understand the actual/underlying problem 😅 But assuming I was wrong before and getIdentity() still has the potential to cause weird behaviours like this in 4.0, it'd be great if there was some way to avoid this gotcha altogether.

(Personally I'm careful to always defer any session-dependent logic to Application::EVENT_INIT (or Plugins::EVENT_AFTER_PLUGINS_LOADED) event handlers – in my experience the problem is mainly with people adding Craft::$app->getUser()->getIdentity() to their project module's init methods, which has the potential to break stuff without it being clear at all what's actually going on :/)

@wbrowar
Copy link
Author

wbrowar commented Apr 28, 2022

Would it be possible to move your logic to after the app is fully initialized?

This is similar to how I solved it on my end. I had 3 different events that needed the currently logged in user info and I went on to get that info at those points, instead of trying to save them to a static variable. It was more for convenience, so this wasn't a big blocker for me.

For my purpose, this is all set, but I wanted to put the bug out there in case it breaks other plugins.

brandonkelly added a commit that referenced this issue Apr 28, 2022
@brandonkelly
Copy link
Member

But assuming I was wrong before and getIdentity() still has the potential to cause weird behaviours like this in 4.0, it'd be great if there was some way to avoid this gotcha altogether.

I don’t think Craft should outright prevent it from working, but as of the next Craft 4 release it will start logging a warning if any element queries are executed before the app is fully initialized. Also doing the same if Twig is instantiated, as we’ve seen similar reliability issues with that, if a not-yet-initialized plugin was planning to register a Twig extension.

@brandonkelly
Copy link
Member

4.0.0-RC2 is out now with those warnings.

joshuapease added a commit to vigetlabs/craft-viget-base that referenced this issue Sep 20, 2022
joshuapease added a commit to vigetlabs/craft-viget-base that referenced this issue Sep 20, 2022
joshuapease added a commit to vigetlabs/craft-viget-base that referenced this issue Sep 20, 2022
joshuapease added a commit to vigetlabs/craft-viget-base that referenced this issue Sep 20, 2022
@khalwat
Copy link
Contributor

khalwat commented Oct 21, 2022

I don’t think Craft should outright prevent it from working, but as of the next Craft 4 release it will start logging a warning if any element queries are executed before the app is fully initialized. Also doing the same if Twig is instantiated, as we’ve seen similar reliability issues with that, if a not-yet-initialized plugin was planning to register a Twig extension.

I still think Craft could use a more formalized loading process for plugins to help alleviate all of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants