opened
paramasivan1
username_0
MY server environment is set up as local authentication. I am planning to write a script which help sync the DL's to Tableau groups. I can get the DL member's using our internal DL API's which give user name ,email,fullname. So I want to check if the users in the DL already exists on the server Group and if doesn't exist I want to create the user and add it to the Group on server. I can the API to find the user by userid but not name. Can anyone help how to pass the username and find if the user exists on the server or not?
How to get user id by using username as input
issue
created
VoMimbre
username_1
To do this 1st I've made a list of names and in 2nd step I try to get them in postgresql DB. In 3rd step I create the user if it is not already present.
It is python 3.6 

import psycopg2
...
def compare_in_tableau_admin (p_list_of_new_users):
    """
        This proc will check if users in my list list_of_new_users
        are present in ANY Site 
        Input : list_of_new_users
        output : list_of_found_users
    """   
myConnection = psycopg2.connect( host=PG_HOSTNAME,port=PG_PORT, user=PG_USERNAME, password=PG_PASSWORD, dbname=PG_DATABASE )
    
    my_compare_list =[]      
    postgres_cur = myConnection.cursor()  
       
    postgres_cur.execute('SAVEPOINT sp1')
    try:   
        for my_user in p_list_of_new_users:
            if my_user[0] != 'NoUserId':
                # execute the sql with param
                sql_command = """SELECT U.name, users.site_role_id , S.name, COALESCE(system_users.email,'No Email')
                                        FROM _users U , _sites S , users , system_users
                                        WHERE S.Id = U.Site_Id 
                                        AND   U.id = users.id
                                        AND   users.system_user_id = system_users.id
                                        AND lower(U.name) = lower(%s)
                                        ORDER BY S.id ;"""
                postgres_cur.execute( sql_command, (my_user[0],))
                nb_record=0
                for record in postgres_cur: 
                    # 1 or more rows return : rebuild a row for each result 
                    # adding _site.Name and user.site_role_id                       
                    nb_record = nb_record+1
                    my_compare_list.append([record[0],record[1],record[2],record[3],my_user[2]])
                    
                if nb_record == 0:
                    # no row return : rebuild a row adding a specific values to mark it as OK for creation
                    my_compare_list.append([my_user[0],"None","NotPresentInAnySite",my_user[1],my_user[2]])
            else:
                my_compare_list.append(["NoUserId","None","NotPresentInAnySite",my_user[1],my_user[2]])

    except Exception as e:   
        postgres_cur.execute('ROLLBACK TO SAVEPOINT sp1')
        logger.critical(e)
        errorDateUTC=format(datetime.datetime.utcnow(), DATE_HOUR_FMT)
        emsg = errorDateUTC+process_name+'\n'+e
        sendEmail(str(emsg),'ERROR')        
        logging.shutdown() 
        os.remove(RUN)
        exit (9)
    else:
        postgres_cur.execute('RELEASE SAVEPOINT sp1')

    postgres_cur.close()
    myConnection.close()

    return(my_compare_list) 

When you have thousands of users it is much more efficient to test it this way and insert only missing users
comment
created
paramasivan1
username_0
Yes using Postgres it can be done. I looking for options to do it thru API
comment
created
jacalata
username_2
So you have a user Robert Smith in your DL, and the DL will give you their information as name = "Robert Smith", email = rsmith@email.com and username  = rsmith, is that right? If the username they have in the DL is that same as the username they have on Tableau, then you can look them up in Get Users On Site and filter the query by the 'name' field. (In the API, 'name' is a username and 'full name' is a proper human name). 
https://onlinehelp.tableau.com/current/api/rest_api/en-us/REST/rest_api_ref.htm#get_users_on_site
example if you ask for all fields for a user:
user email="rwilliams@example.com" externalAuthUserId="" fullName="Robert Williams" id="ididid" lastLogin="2019-04-24T16:19:21Z" name="rwilliams" siteRole="ServerAdministrator"
comment
created
irwando
username_3
As mentioned this can be done with Get User and filtering on Username. Closing as this seems resolved.
comment
closed
irwando
username_3
issue