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

Undefined variable $user (RestrictionService.php line 658) #125

Closed
charlyecho opened this issue Mar 14, 2023 · 5 comments
Closed

Undefined variable $user (RestrictionService.php line 658) #125

charlyecho opened this issue Mar 14, 2023 · 5 comments
Assignees
Labels
question Further information is requested

Comments

@charlyecho
Copy link

Hi James,
we have an issue on our project.

we call a custom mutation from a module with no authentication token (because it is a public mutation allowed to anyone).

In the code, restrictionService from line 654 to line 666, the code assume $user is defined, but the $user is defined only if tokenService->getHeaderToken() condition is true.

Can you fix this ?

I suggest moving line 654 to line 666 inside the getHeaderToken condition or checking $user is defined.

Regards

@jamesedmonston jamesedmonston self-assigned this Mar 21, 2023
@jamesedmonston
Copy link
Owner

@charlyecho can you show me what your custom mutation is doing?

@jamesedmonston jamesedmonston added the question Further information is requested label Mar 21, 2023
@charlyecho
Copy link
Author

Hi James,
here we go :

$event->mutations['reportStory'] = [
            'description' => 'Report a story',
            'type' => Type::nonNull(Type::string()),
            'args' => [
                'elementId' => Type::nonNull(Type::int()),
            ],
            'resolve' => function ($source, array $arguments) use ($elementsService) {
                $element = $elementsService->getElementById((int) $arguments['elementId']);
                if (!$element) {
                    throw new Exception("Element not found");
                }

                $nb = (int) $element->getFieldValue("reportCounter");
                $element->setFieldValue("reportCounter", $nb+1);
                //  Craft::$app->request->setIsCpRequest(true); (hack for not having the issue)
                if ($elementsService->saveElement($element)) {
                    return "Story reported";

                }
                return "Story not reported";
            },
        ];

@robinbeatty
Copy link
Contributor

Hey, I'm having a similar issue:

I'm calling a regular save_[section]_[entryType]_Entry mutation which should post some asset ids into an assets field. Many of the assets in the system don't have an uploader id as they were indexed from a remote source (AWS). The mutation will add these assets to the field fine, but if there is an uploader id set on the asset (they were uploaded via the craft cp) The mutation fails because of the same 'Undefined variable $user' as above (RestrictionService.php:658). Having tinkered with permissions/validation settings (Craft, Your plugin, The asset field) for some days I still can't find a solution so assuming it's a bug. Could you take a look?

@jamesedmonston
Copy link
Owner

@robinbeatty thanks for your PR! I've added your fix to the private entry checks, too. Will release this in an update shortly.

@jamesedmonston
Copy link
Owner

Released in 2.5.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants