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

The stripe plugin tries to check for user permissions before craft is fully initialized #71

Open
okolvik-avento opened this issue Jan 2, 2025 · 1 comment
Assignees

Comments

@okolvik-avento
Copy link

The stripe plugin tries to check for user permissions before craft is fully initialized, leading the the error "Element query executed before Craft is fully initialized."

Craft::$app->getUser()->checkPermission('accessPlugin-stripe')

Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/db/QueryBuilder.php(227): craft\elements\db\ElementQuery->prepare()
#1 /var/www/html/vendor/yiisoft/yii2/db/Query.php(157): yii\db\QueryBuilder->build()
#2 /var/www/html/vendor/yiisoft/yii2/db/Query.php(287): yii\db\Query->createCommand()
#3 /var/www/html/vendor/craftcms/cms/src/db/Query.php(320): yii\db\Query->one()
#4 /var/www/html/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1918): craft\db\Query->one()
#5 /var/www/html/vendor/craftcms/cms/src/elements/User.php(600): craft\elements\db\ElementQuery->one()
#6 /var/www/html/vendor/yiisoft/yii2/web/User.php(699): craft\elements\User::findIdentity()
#7 /var/www/html/vendor/craftcms/cms/src/web/User.php(453): yii\web\User->renewAuthStatus()
#8 /var/www/html/vendor/yiisoft/yii2/web/User.php(199): craft\web\User->renewAuthStatus()
#9 /var/www/html/vendor/craftcms/cms/src/web/User.php(266): yii\web\User->getIdentity()
#10 /var/www/html/vendor/craftcms/stripe/src/Plugin.php(579): craft\web\User->checkPermission()
#11 /var/www/html/vendor/craftcms/stripe/src/Plugin.php(156): craft\stripe\Plugin->registerUserActions()
#12 /var/www/html/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\stripe\Plugin->init()
#13 /var/www/html/vendor/yiisoft/yii2/base/Module.php(161): yii\base\BaseObject->__construct()
#14 /var/www/html/vendor/craftcms/cms/src/base/Plugin.php(122): yii\base\Module->__construct()
#15 craft\base\Plugin->__construct()
#16 /var/www/html/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs()
#17 /var/www/html/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build()
#18 /var/www/html/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get()
#19 /var/www/html/vendor/craftcms/cms/src/Craft.php(71): yii\BaseYii::createObject()
#20 /var/www/html/vendor/craftcms/cms/src/services/Plugins.php(945): Craft::createObject()
#21 /var/www/html/vendor/craftcms/cms/src/services/Plugins.php(233): craft\services\Plugins->createPlugin()
#22 /var/www/html/vendor/craftcms/cms/src/base/ApplicationTrait.php(1631): craft\services\Plugins->loadPlugins()
#23 /var/www/html/vendor/craftcms/cms/src/web/Application.php(110): craft\web\Application->_postInit()
#24 /var/www/html/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init()
#25 /var/www/html/vendor/yiisoft/yii2/base/Application.php(204): yii\base\BaseObject->__construct()
#26 yii\base\Application->__construct()
#27 /var/www/html/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs()
#28 /var/www/html/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build()
#29 /var/www/html/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get()
#30 /var/www/html/vendor/craftcms/cms/src/Craft.php(71): yii\BaseYii::createObject()
#31 /var/www/html/vendor/craftcms/cms/bootstrap/bootstrap.php(342): Craft::createObject()
#32 /var/www/html/vendor/craftcms/cms/bootstrap/web.php(40): require()
#33 /var/www/html/web/index.php(30): require() {"memory":3673736} 

This check would be better off inside the actual event (EVENT_DEFINE_ACTION_MENU_ITEMS)

So instead of:

    private function registerUserActions(): void
    {
        if (
            !empty(Plugin::getInstance()->getApi()->getApiKey()) &&
            Craft::$app->getUser()->checkPermission('accessPlugin-stripe')
        ) {
            Event::on(
                User::class,
                Element::EVENT_DEFINE_ACTION_MENU_ITEMS,
                function(DefineMenuItemsEvent $event) {
                    $sender = $event->sender;
                    if ($email = $sender->email) {
                        $customers = Plugin::getInstance()->getApi()->fetchAllCustomers(['email' => $email]);
                        if ($customers) {
                            $stripeIds = collect($customers)->pluck('id');
                            $event->items[] = [
                                'action' => 'stripe/sync/customer',
                                'type' => MenuItemType::Button,
                                'params' => [
                                    'stripeIds' => $stripeIds->toArray(),
                                ],
                                'label' => Craft::t('stripe', 'Sync from Stripe'),
                            ];
                        }
                    }
                }
            );
        }
    }

Do something like this (i also removed the unnecessary email variable assignment in the if statement):

    private function registerUserActions(): void
    {
        if (!empty(Plugin::getInstance()->getApi()->getApiKey())) {
            Event::on(
                User::class,
                Element::EVENT_DEFINE_ACTION_MENU_ITEMS,
                function(DefineMenuItemsEvent $event) {
                    $sender = $event->sender;
                    if ($sender->email && Craft::$app->getUser()->checkPermission('accessPlugin-stripe')) {
                        $customers = Plugin::getInstance()->getApi()->fetchAllCustomers(['email' => $sender->email]);
                        if ($customers) {
                            $stripeIds = collect($customers)->pluck('id');
                            $event->items[] = [
                                'action' => 'stripe/sync/customer',
                                'type' => MenuItemType::Button,
                                'params' => [
                                    'stripeIds' => $stripeIds->toArray(),
                                ],
                                'label' => Craft::t('stripe', 'Sync from Stripe'),
                            ];
                        }
                    }
                }
            );
        }
    }
@i-just
Copy link
Contributor

i-just commented Jan 6, 2025

Hi, thanks for reporting! I raised a PR for this.

@i-just i-just self-assigned this Jan 6, 2025
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

No branches or pull requests

2 participants