Script tp read users last login

276
3
Jump to solution
11-29-2023 12:00 PM
kapalczynski
Occasional Contributor III

I have been using this script for quite some time ... It reads out AGOL account and creates a csv file for those users that have not logged in from 80-90 days.

But recently started getting an error on line 10 below   lastLogintime = user.lastLogin/1000

I am assume this is from the recent AGOL Update?

did it change from    user.lastLogin

ERROR

Traceback (most recent call last):
  File "K:\GIS Share\GIS_AGOLProcessing\UsersAboutToExpire.py", line 84, in <module>
    main()
  File "K:\GIS Share\GIS_AGOLProcessing\UsersAboutToExpire.py", line 48, in main
    lastLogintime = user.lastLogin/1000
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py", line 8920, in __getattr__
    "'%s' object has no attribute '%s'" % (type(self).__name__, name)
AttributeError: 'User' object has no attribute 'lastLogin'

CODE

def main():
    #log_obj.info("-- Starting to Process AGOL Users...".format())
    with open(output_csv, 'w', encoding='utf-8') as file:
         csvfile = csv.writer(file, delimiter=',', lineterminator='\n')
         csvfile.writerow(["username", "email", "roleID",
                           "level", "disabled", "role",
                           "licenseType", "lastLogin", "daysSince"])
         counter = 0   
         for user in user_list:
            lastLogintime = user.lastLogin/1000
            lastLoginTimeConvert = (datetime.utcfromtimestamp(lastLogintime).strftime('%Y-%m-%d %H:%M:%S'))
            format = '%Y-%m-%d %H:%M:%S'
            lastLoginDate = datetime.strptime(lastLoginTimeConvert, format)
            lastLoginDATE = datetime.now() - lastLoginDate
            lastLoginDAYS = lastLoginDATE.days
            varDisabvled = user.disabled

            #if lastLoginDAYS > 80:
            if lastLoginDAYS in range(80,90):
                if varDisabvled == False:             
                    try:
                        varusername = user.username  
                        varemail = user.email
                        varRoleID = rol_ids[user.roleId]
                        varlevel = user.level
                        vardisabledValue = user.disabled
                        varrole = user.role
                        varlicenceType = user.userLicenseTypeId
                        varLastLogin = lastLoginDate
                        varLastLoginDays = lastLoginDAYS

                        z = (varusername if isinstance(varusername, list) else [varusername]) + [varemail] + [varRoleID] + [varlevel] + [vardisabledValue] +  [varrole] + [varlicenceType] + [varLastLogin] + [varLastLoginDays]
                        output.append(z)

                        counter += 1
                        
                        csvfile.writerow([varusername, varemail, varRoleID,
                                    varlevel, vardisabledValue, varrole,
                                    varlicenceType, varLastLogin, varLastLoginDays      
                                    ])
                    except KeyError as e:
                        print(user.username, e)
            print(counter)

 

0 Kudos
1 Solution

Accepted Solutions
Tom_Laue
New Contributor III

Make sure your AGOL account you're running the script from is still an AGOL Admin in your org.

If not (like before I was made an Admin in my Org) I could only view my own user details via Python but now I can see others lastLogin.

 

I just verified this using a test account in AGOL and I get the following error (which I don't get via my Admin login).

AttributeError: 'User' object has no attribute 'lastLogin'

View solution in original post

0 Kudos
3 Replies
Tom_Laue
New Contributor III

Make sure your AGOL account you're running the script from is still an AGOL Admin in your org.

If not (like before I was made an Admin in my Org) I could only view my own user details via Python but now I can see others lastLogin.

 

I just verified this using a test account in AGOL and I get the following error (which I don't get via my Admin login).

AttributeError: 'User' object has no attribute 'lastLogin'

0 Kudos
kapalczynski
Occasional Contributor III

Fantastic that was it.... we had reset this user account and for some reason it was removed from admin level.  Pointed to new account and its working... 

THANKS @Tom_Laue 

0 Kudos
kapalczynski
Occasional Contributor III

I simplified it working from this example and same error

https://support.esri.com/en-us/knowledge-base/how-to-determine-the-last-login-in-arcgis-enterprise-p...

import arcgis
import time
from arcgis.gis import GIS

#For ArcGIS Online
gis = GIS('https://xxxx.maps.arcgis.com', username="username", password="password")

print("connected")

a_users = gis.users.search(query='', max_users=20)
a_users

for a_user in a_users:
    if a_user. lastLogin != -1:
        last_accessed = time.localtime(a_user. lastLogin/1000)
        print(str(a_user. fullName) + " was last active on: {}/{}/{}\n".format(last_accessed[0], last_accessed[1], last_accessed[2]))

    else:
      print(str(a_user. fullName) + " has never logged in.\n")

 

ERROR:

connected
Traceback (most recent call last):
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py", line 8917, in __getattr__
return dict.__getitem__(self, name)
KeyError: 'lastLogin'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "K:\GIS Share\GIS_AGOLProcessing\test.py", line 18, in <module>
if a_user. lastLogin != -1:
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py", line 8920, in __getattr__
"'%s' object has no attribute '%s'" % (type(self).__name__, name)
AttributeError: 'User' object has no attribute 'lastLogin'

 

 

0 Kudos