How do i update the roles a user has

Im working on a user management page, which is just replicating the user and roles page on the gateway.

Im working on the edit feature and I am trying to update the roles a users has.

Here is an example with the username “john” which has the following roles, ‘write’ and ‘read’.

Im trying to update his user profile to has no roles using the code below.

userToGet = system.user.getUser('operators', 'john')
system.user.editUser('operators', userToGet)

This does not work can some please assist

The work has already been done for you. Check out

or the more basic