Writes user object for a user with a given name.

def WriteGRRUser(self,
                   username,
                   password=None,
                   ui_mode=None,
                   canary_mode=None,
                   user_type=None,
                   cursor=None):
    """Writes user object for a user with a given name."""

    values = {"username": username, "username_hash": mysql_utils.Hash(username)}

    if password is not None:
      values["password"] = password.SerializeToString()
    if ui_mode is not None:
      values["ui_mode"] = int(ui_mode)
    if canary_mode is not None:
      # TODO(amoser): This int conversion is dirty but necessary with
      # the current MySQL driver.
      # TODO: We can remove this once the bug is fixed.
      values["canary_mode"] = int(bool(canary_mode))
    if user_type is not None:
      values["user_type"] = int(user_type)

    query = "INSERT INTO grr_users {cols} VALUES {vals}".format(
        cols=mysql_utils.Columns(values),
        vals=mysql_utils.NamedPlaceholders(values))

    updates = ", ".join("{0} = VALUES({0})".format(col) for col in values)
    query += " ON DUPLICATE KEY UPDATE " + updates

    cursor.execute(query, values)