-
Notifications
You must be signed in to change notification settings - Fork 638
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
Comments
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() {
// ...
}); |
@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 Admittedly, after ~5 years I still don't completely understand the actual/underlying problem 😅 But assuming I was wrong before and (Personally I'm careful to always defer any session-dependent logic to |
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. |
…efore Craft is initialized Resolves #11033
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. |
4.0.0-RC2 is out now with those warnings. |
I still think Craft could use a more formalized loading process for plugins to help alleviate all of this. |
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’sinit()
method. In Craft 4 I am seeingnull
when I check forCraft::$app->getUser()->getIdentity()
and I getfalse
when checking forCraft::$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
use craft\elements\User;
init()
function, belowparent::init();
, addCraft::dd(Craft::$app->getUser()->getIdentity());
3.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 checkingCraft::$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
The text was updated successfully, but these errors were encountered: