userfrosting / UserFrosting

Modern PHP user login and management framework

Home Page:https://www.userfrosting.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

H1ghSyst3m opened this issue · comments

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'    => '',
        ];

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.

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

Culprit is here :

'group_id' => $currentUser->group_id,
'locale' => $config['site.registration.user_defaults.locale'],

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.

<option value="{{group.id}}" {% if (group.id == user.group_id) %}selected{% endif %}>{{group.name}}</option>
{% endfor %}

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).

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.