-1

I have 5 roles (Admin, HRM, Registrar, Staff, and HOD). Admin users will give special access to HRM users to add new users whose role is Staff.

Registrar and HOD roles shouldn't be shown in the Add users form. When I try to give permissions, Registrar and HOD roles are available in the form.

Can anyone help me in this?

2 Answers 2

2

There are two contrib modules which will do that for you:

Role Delegation

This module allows site administrators to grant specific roles the authority to assign selected roles to users, without them needing the administer permissions permission.

RoleAssign

RoleAssign specifically allows site administrators to further delegate the task of managing user's roles while withholding the Administer permissions permission.

The former is somewhat more configurable, and has the added benefit of a stable release.

If neither of those do what you're looking for, it's pretty easy to alter the user form manually. Here's a very contrived example:

function MYMODULE_form_user_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  if ($some_condition) {
    $form['account']['roles']['#options'] = [
      'foo' => 'Foo role',
      'bar' => 'Bar role',
    ];
  }
}
0

From a custom module you can build a custom form + a custom permission that you will give to the HRM users to access this form.

On form submit, gather the informations filled in the fields and create a Staff user programmatically.

Not the answer you're looking for? Browse other questions tagged or ask your own question.