Adding role to user via code

Hi,

Please share the code to add a role to a user.

for example adding the role Library Member to a user when I insert a row in library membership

server side code(python)

frappe.get_doc("User", user).add_roles(role)

example:

    user = frappe.get_doc("User", "test2@example.com")
    user.add_roles("Blogger")
3 Likes