How to give remove role editing permission from user

This will remove the option to change user group except if this is the administrator.

The Codex states:

While checking against particular roles in place of a capability is
supported in part, this practice is discouraged as it may produce
unreliable results.

For this reason, I check for activate_plugins capability that normally only Admins will have.

function dont_allow_role_change($all_roles) {
    global $user_id;
    $user_meta = get_userdata($user_id);

    if(!current_user_can('activate_plugins') && is_int($user_id)){
        foreach ( $all_roles as $name => $role ) {
            if (!in_array($name,$user_meta->roles)) {
                unset($all_roles[$name]);
            }
        }
    }
    return $all_roles;
}

add_filter('editable_roles', 'dont_allow_role_change');