userfrosting / UserFrosting

Modern PHP user login and management framework
https://www.userfrosting.com
Other
1.63k stars 367 forks source link

[Sprinkle-Admin] User_defaults.group doesn't work on Form "Create User" #1230

Closed H1ghSyst3m closed 9 months ago

H1ghSyst3m commented 11 months ago

In the Dashboard -> Users -> [Create User]

The predefined group is always the group of the current logged-in User that is creating the user.

Even when I change in default.php -> registration.user_defaults.group => 'example group' grafik

As Example when my User is in the Group "Admin" then the predefined Group in the Create User Form is "Admin" and when my Group is "rater" then the predefined Group in the Form is "Rater".

The problem could lie in the file admin/src/Controller/UserController.php in the line 623 at code:

        $data = [
            'group_id' => $currentUser->group_id,
            'locale'   => $config['site.registration.user_defaults.locale'],
            'theme'    => '',
        ];
H1ghSyst3m commented 11 months ago

Also I forgot to mention When I change the default group from terran to something else, I still can't delete the Group Terran in the Dashboard

grafik

Both logs don't show anything.

I am writing it here because I think the Problems are connected, if not please tell me I will open a new Issue.

lcharette commented 11 months ago

Chat discussion : https://chat.userfrosting.com/channel/support?msg=ASwCH8JuSiq35unRF

Culprit is here : https://github.com/userfrosting/UserFrosting/blob/ca9cf963888c7eab77769ba51155d6b2c2f97168/app/sprinkles/admin/src/Controller/UserController.php#L623-L624

It shouldn't be the current user group here, but the default group ID. And it must be an ID, since "edit" modal use the same template, and it's comparing ID with ID.

https://github.com/userfrosting/UserFrosting/blob/ca9cf963888c7eab77769ba51155d6b2c2f97168/app/sprinkles/admin/templates/forms/user.html.twig#L31-L32

Note it's not as easy as to switch like this :

      // Determine if currentUser has permission to modify the group.  If so, show the 'group' dropdown.
      // Otherwise, set to the currentUser's group and disable the dropdown.
      $defaultGroupSlug = $this->config->get('site.registration.user_defaults.group');
      $defaultGroup = $this->groupModel::where('slug', $defaultGroupSlug)->firstOrFail();
      if ($this->authenticator->checkAccess('create_user_field', [
          'fields' => ['group'],
      ])) {
          // Get a list of all groups
          $groups = $this->groupModel::all();
      } else {
          // Get the current user's group
          $groups = [$defaultGroup];
          $fields['disabled'][] = 'group';
      }

      // Create a dummy user to pre-populate fields
      $user = new $this->userModel([
          'group_id' => $defaultGroup->id,
          'locale'   => $this->config->get('site.registration.user_defaults.locale'),
      ]);

This could work, however an error would be thrown if the $defaultGroupSlug doesn't exist in the database. It also need to account "No Group" (empty string in config) could be a valid default group (and the new default in V5).

lcharette commented 9 months ago

After consideration, In UF5 this is not really an issue. Default group is not selected when creating a user, but I think this is acceptable.

https://github.com/userfrosting/sprinkle-admin/blob/f77413aa701810d4c22f41e31c78f89477a44539/app/src/Controller/User/UserCreateModal.php#L118

The real issue is the default group should be set in the database. It would then be easier to set. I'm closing this for now, until group is reworked in a future version.