Updated GetUser and change name to GetUsers
This commit is contained in:
24
GLPIAPI.py
24
GLPIAPI.py
@@ -103,13 +103,27 @@ class GLPIAPI:
|
||||
|
||||
return None, None, 0
|
||||
|
||||
def GetUser(self, username=None, email=None):
|
||||
def GetUsers(self, username=None, email=None):
|
||||
'''
|
||||
Search for users in GLPI based on one of the possibles parameters :
|
||||
- username : username of the glpi user
|
||||
- email : email of the glpi user
|
||||
If no parameters are set, it will search for all users
|
||||
|
||||
Returns a tuple with user id, user data and user count
|
||||
'''
|
||||
searchAll = False
|
||||
if(username != None):
|
||||
search_parameter = f'is_deleted=0&criteria[0][field]=1&withindexes=true&criteria[0][searchtype]=contains&criteria[0][value]=^{username}$'
|
||||
elif(email != None):
|
||||
search_parameter = f'is_deleted=0&criteria[0][field]=5&withindexes=true&criteria[0][searchtype]=contains&criteria[0][value]=^{email}$'
|
||||
searchUri = f"{self.Server}/apirest.php/search/user?{search_parameter}"
|
||||
else:
|
||||
searchAll = True
|
||||
|
||||
if(searchAll):
|
||||
searchUri = f"{self.Server}/apirest.php/search/User/?range=0-9999999&is_deleted=0"
|
||||
else:
|
||||
searchUri = f"{self.Server}/apirest.php/search/User?{search_parameter}"
|
||||
search = requests.get(searchUri, headers=self.Headers)
|
||||
if(search.status_code == 200):
|
||||
search = search.json()
|
||||
@@ -119,7 +133,11 @@ class GLPIAPI:
|
||||
|
||||
return userID, data, search["totalcount"]
|
||||
elif(search["totalcount"] > 1):
|
||||
userID = list(search["data"].keys())
|
||||
if(searchAll):
|
||||
# requires id to be in the display preferences of the api user
|
||||
userID = [i["2"] for i in search["data"]]
|
||||
else:
|
||||
userID = list(search["data"].keys())
|
||||
return userID, search["data"], search["totalcount"]
|
||||
|
||||
return None, None, 0
|
||||
|
||||
Reference in New Issue
Block a user