python-3.xarcgisesri

Python 3 arcgis.gis.user for lastLogin accessing ArcGIS Portal


This is being ran on Windows 10 64 bit machine, using Python 3, jupyter labs version 1.6.1.

I am working on a small script using Jupyter Notebook to access my local portal site. With the code snippet below, you can see that I am looking to acquire the portal groups and the users in each group.

The information I am working on obtaining is when each user was created (not as important) and last accessed. This seems to work just fine, the scrip prints out the users and the dates for which their accounts were created, but the same thing for the last accessed date is throwing an error.

I have reviewed arcgis.gis module to read up on the parameters for the arcgis.gis.users class and LastLogin does exist. Another note I should include is that when I use an individual user, such as myself if I were the logged in user, the last access function works. It just seems like when I am passing the users being looped using the "user2" variable, the script doesn't like this.

I also checked the .get() for the acceptable parameters which is "username" and this seems fine.

import time
from arcgis.gis import GIS
portal = r"*" #this would be the portal or arcgis online url
username = "*" #using admin credentials here
password = "*"
gis = GIS(portal, username, password)
groups = gis.groups.search()

for group in groups:
    print(group)
    accounts = gis.users.search()
    for account in accounts:
        #user = gis.users.account
        #user = gis.users.search(query="username = {}".format(account.username))
        #print(account.email)
        user2 = gis.users.get(username="{}".format(account.username))
        print(account.username)
        created_time = time.localtime(user2.created/1000)
        print("Created: {}/{}/{}".format(created_time[0], created_time[1], created_time[2]))
        last_accessed = time.localtime(user2.lastLogin/1000)
        print("Last active: {}/{}/{}".format(last_accessed[0], last_accessed[1], last_accessed[2]))

The error is what I received when trying to pass the "user2" in line 23 for the last_accessed variable.

 OSError                                   Traceback (most recent call last)
    <ipython-input-3-3a539a5c371d> in <module>
         21         created_time = time.localtime(user2.created/1000)
         22         print("Created: {}/{}/{}".format(created_time[0], created_time[1], created_time[2]))
    ---> 23         last_accessed = time.localtime(user2.lastLogin/1000)
         24         print("Last active: {}/{}/{}".format(last_accessed[0], last_accessed[1], 
    last_accessed[2]))
         25 

OSError: [Errno 22] Invalid argument

Solution

  • After further investigation, it seems as though the .lastLogin snippet works. The problem was in the ArcGIS portal environment, where there was a user that had not yet signed in with their credentials (typically using admin creds). The value passed into the code was passing a -1 and the conversion of that value wasn't accounted for in the base library, so we added an if statement to verify that the value needs to be > 0, if it isn't then we log that the user has not logged in yet.

    Here is the updated code for the script.

    accounts = gis.users.search()

    for account in accounts: user = gis.users.get("{}".format(account.username))

    if account.username.startswith("esri"): # Disregard any generic named accounts such as those that do not seem user specific like esri_boundaries, etc... 
        pass
    elif account.username.startswith("system_"): # Disregard any system related IDs such as "system_publisher" otherwise the parameter is not valid to pass for the last login.
        pass
    else:
        print("Full Name: {}".format(user.fullName))
        print("Username: {}".format(user.username))
        created = time.localtime(user.created/1000)
        print("User created: {}/{}/{}".format(created[0], created[1], created[2]))
        if user.lastLogin > 0:
            last_accessed = time.localtime(user.lastLogin/1000)
            print("Last active: {}/{}/{}".format(last_accessed[0], last_accessed[1], last_accessed[2]))
        else:
            print("***{} Has not yet logged in".format(account.username))
        print()
        
    

    print("Process Complete")