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]: Creating/updating user permissions results in exception #12257

Closed
Joorren opened this issue Nov 3, 2022 · 3 comments
Closed

[4.x]: Creating/updating user permissions results in exception #12257

Joorren opened this issue Nov 3, 2022 · 3 comments
Assignees

Comments

@Joorren
Copy link

Joorren commented Nov 3, 2022

What happened?

Description

When creating or updating a user in the control panel, and selecting permissions (other than 'admin') for this user, saving the user results in an exception.
Even with an admin account, the exception persists.
Saving the user without selecting permissions succeeds without any issues.

Steps to reproduce

  1. Create a new user or update an existing user
  2. Select permissions other than the 'admin' permissions
  3. Save the user

Expected behavior

User gets created/updated with the given permissions

Actual behavior

User doesn't get created/updated, an ErrorException occurs.

Error log

2022-11-03 13:51:00 [web.ERROR] [yii\base\ErrorException:2] Undefined array key "permissions" 
{
 "trace":[
    "#0 /opt/approot/build.20221103-144332-17b3b6c9/vendor/craftcms/cms/src/web/ErrorHandler.php(79): yii\\base\\ErrorHandler->handleError(2, 'Undefined array...', '/opt/approot/bu...', 759)",
    "#1 /opt/approot/build.20221103-144332-17b3b6c9/vendor/craftcms/cms/src/services/UserPermissions.php(759): craft\\web\\ErrorHandler->handleError(2, 'Undefined array...', '/opt/approot/bu...', 759)",
    "#2 /opt/approot/build.20221103-144332-17b3b6c9/vendor/craftcms/cms/src/services/UserPermissions.php(292): craft\\services\\UserPermissions->_filterOrphanedPermissions(Array, Array)",
    "#3 /opt/approot/build.20221103-144332-17b3b6c9/vendor/craftcms/cms/src/controllers/UsersController.php(2262): craft\\services\\UserPermissions->saveUserPermissions(1041359, Array)",
    "#4 /opt/approot/build.20221103-144332-17b3b6c9/vendor/craftcms/cms/src/controllers/UsersController.php(1425): craft\\controllers\\UsersController->_saveUserPermissions(Object(craft\\elements\\User), Object(craft\\elements\\User))",
    "#5 [internal function]: craft\\controllers\\UsersController->actionSaveUser()",
    "#6 /opt/approot/build.20221103-144332-17b3b6c9/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)",
    "#7 /opt/approot/build.20221103-144332-17b3b6c9/vendor/yiisoft/yii2/base/Controller.php(178): yii\\base\\InlineAction->runWithParams(Array)",
    "#8 /opt/approot/build.20221103-144332-17b3b6c9/vendor/yiisoft/yii2/base/Module.php(552): yii\\base\\Controller->runAction('save-user', Array)",
    "#9 /opt/approot/build.20221103-144332-17b3b6c9/vendor/craftcms/cms/src/web/Application.php(301): yii\\base\\Module->runAction('users/save-user', Array)",
    "#10 /opt/approot/build.20221103-144332-17b3b6c9/vendor/craftcms/cms/src/web/Application.php(625): craft\\web\\Application->runAction('users/save-user', Array)",
    "#11 /opt/approot/build.20221103-144332-17b3b6c9/vendor/craftcms/cms/src/web/Application.php(280): craft\\web\\Application->_processActionRequest(Object(craft\\web\\Request))",
    "#12 /opt/approot/build.20221103-144332-17b3b6c9/vendor/yiisoft/yii2/base/Application.php(384): craft\\web\\Application->handleRequest(Object(craft\\web\\Request))",
    "#13 /opt/approot/build.20221103-144332-17b3b6c9/public/index.php(12): yii\\base\\Application->run()",
    "#14 {main}"
 ],
 "memory":7357216,
 "exception":"[object] (yii\\base\\ErrorException(code: 2): Undefined array key \"permissions\" at /opt/approot/build.20221103-144332-17b3b6c9/vendor/craftcms/cms/src/services/UserPermissions.php:759)"
}"2022-11-03 13":"51":00[
 "web.WARNING"
][
 "application"
]"Request context":{
 "userId":8,
 "sessionId":"dface09b1113f58bf79cfab27de6874f",
 "vars":{
    "_GET":{
       "p":"admin/users/1041359",
       "site":"default"
    },
    "_POST":{
       "action":"users/save-user",
       "redirect":"a65560dd2a9f07a65deefc7f5bfcb584c39b04ab06541603d0eed47e3faa7910users",
       "CRAFT_CSRF_TOKEN":"••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••",
       "userId":"1041359",
       "username":"Joorren",
       "fullName":"Joren",
       "email":"[email protected]",
       "passwordResetRequired":"",
       "admin":"",
       "permissions":[
          "accessSiteWhenSystemIsOff",
          "accessCp",
          "accessCpWhenSystemIsOff",
          "performUpdates",
          "accessPlugin-feed-me",
          "accessPlugin-freeform",
          "accessPlugin-retour",
          "accessPlugin-seomatic",
          "accessPlugin-sprig",
          "accessPlugin-zoho"
       ]
    },
    "_COOKIE":{
       <<REDACTED>>
    },
    "_FILES":[
       <<REDACTED>>
    ],
    "_SERVER":{
      <<REDACTED>>
    },
    "_SESSION":{
       <<REDACTED>>
    }
 }
}

Craft CMS version

4.3.1

PHP version

8.1.11

Operating system and version

Ubuntu 22.04.1 LTS

Database type and version

MySQL 8.0

Image driver and version

No response

Installed plugins and versions

  • nystudio107/craft-cookies 4.0.0
  • verbb/expanded-singles 2.0.0
  • craftcms/feed-me 5.0.4
  • verbb/field-manager 3.0.2
  • solspace/craft-freeform 4.0.5
  • spacecatninja/imager-x 4.1.6
  • ether/simplemap v4.0.3
  • misterbk/mix 1.6.0
  • spicyweb/craft-neo 3.4.0
  • craftcms/redactor 3.0.2
  • carlcs/craft-redactorcustomstyles 4.0.2
  • verbb/redactor-tweaks 3.0.0
  • nystudio107/craft-retour 4.1.4
  • nystudio107/craft-seomatic 4.0.9
  • verbb/smith 2.0.0
  • putyourlightson/craft-sprig 2.2.1
  • verbb/super-table 3.0.2
  • sebastianlenz/linkfield 2.1.4
@brianjhanson
Copy link
Contributor

I'm having trouble replicating this one.

Any chance you have some custom code running on UserPermissions::EVENT_REGISTER_PERMISSIONS event? The error thrown looks like craft\services\UserPermissions::getAllPermissions() is returning a malformed array.

@Joorren
Copy link
Author

Joorren commented Nov 3, 2022

I'm having trouble replicating this one.

Any chance you have some custom code running on UserPermissions::EVENT_REGISTER_PERMISSIONS event? The error thrown looks like craft\services\UserPermissions::getAllPermissions() is returning a malformed array.

We do hook on this event, but don't actually do anything with it.

        Event::on(UserPermissions::class, UserPermissions::EVENT_REGISTER_PERMISSIONS, static function (RegisterUserPermissionsEvent $event) {
            $event->permissions[\Craft::t('lunar-module', 'Custom permissions')] = [
                // add custom permissions
            ];
        });

It's a module we built for Craft 3 before we migrated to Craft 4. Did the way of using this event change?

@brianjhanson
Copy link
Contributor

brianjhanson commented Nov 5, 2022

They did indeed! The permissions array now requires permissions and heading keys.

I'm going to close this out for now, but I'm still here if you have any additional questions.

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

2 participants