Yes, you can easily add or remove fields from the form with event listeners in the boot method your plugin.
Docs: extending forms
public function boot()
{
// Extend all backend form usage
Event::listen('backend.form.extendFields', function($widget) {
// Only for the User controller
if (!$widget->getController() instanceof \RainLab\User\Controllers\Users) {
return;
}
// Only for the User model
if (!$widget->model instanceof \RainLab\User\Models\User) {
return;
}
// Add an extra birthday field
$widget->addFields([
'birthday' => [
'label' => 'Birthday',
'comment' => 'Select the users birthday',
'type' => 'datepicker'
]
]);
// Remove a Surname field
$widget->removeField('surname');
});
}
However I have had issues with the boot method, so I do my modifications in a custom middleware. The middleware executes before user authentication, however, and so may this boot method. You may have issues checking user permissions.
In this case you may want to simply replace the elements you are trying to modify.
In my application, I needed more control over groups and permissions. So I copied the controllers and models to my plugin and use that for user management instead. The filtering is done in controller methods instead of a plugin registration method, which allows me to check permissions and add / remove fields before it is rendered.
In my Users controller
public function formExtendFields($form) {
if (!$this->user->hasAccess('company.plugin.manage_permissions')) {
$form->removeField('permissions');
}
/*
*
*/
}