Select to view content in your preferred language

Validate whether user is signed-in to ArcGIS Online\Portal with arcpy

161
1
05-28-2024 04:05 AM
Status: Open
Ofir_Mazor
New Contributor II

When working with data and layers from ArcGIS Portal, users must be signed in to create new versions, perform edits, and post new changes. Given that each user has a specific role within the organization, it is crucial to ensure the correct user is signed in before any operations are performed. Currently, there is no straightforward Pythonic method for this validation. Implementing such a validation method would allow developers to ensure that the correct user is signed in and has the necessary permissions to edit the data even before starting an editing session.

This idea came to me after reading a community question. In response, I suggested a workaround using the role value returned from the arcpy.GetPortalInfo function.

My suggested idea is to add the username as a key in the output dictionary of GetPortalInfo. This way, developers can build tools that works only if a user is signed-in.

Another suggested method is to retrieve the user information from the ArcGISProject object in the arcpy.mp module. For example:

from arcpy.mp import ArcGISProject

my_project = ArcGISProject('current')
#option1
current_user:str = my_project.loggedUser 
#option2
condition: bool = my_project.isUserLogged

 

Tags (2)
1 Comment
Matthew_Muehlhauser

I would take a look at this page that details some ways you can check to see if appropriate licensing / extensions are available without needing to check user information. 

https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/accessing-licenses-and-exten...

If you really want to work with the User info for ArcGIS Online / Portal, you could also use the ArcGIS API for Python:

https://developers.arcgis.com/python/guide/working-with-different-authentication-schemes/

More specifically, something like this:

 

from arcgis.gis import GIS

gis = GIS("home")
print("Logged in as: " + gis.properties.user.username)