bulkuserupdate 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. import posixpath
  3. import sys
  4. import requests
  5. try:
  6. import urllib.parse as urlparse
  7. except ImportError:
  8. import urlparse
  9. # change base_url to point to your SFTPGo installation
  10. base_url = "http://127.0.0.1:8080"
  11. # set to False if you want to skip TLS certificate validation
  12. verify_tls_cert = True
  13. # set the credentials for a valid admin here
  14. admin_user = "admin"
  15. admin_password = "password"
  16. # insert here the users you want to update
  17. users_to_update = ["user1", "user2", "user3"]
  18. # set here the fields you need to update
  19. fields_to_update = {"status":0, "quota_files": 1000, "additional_info":"updated using the bulkuserupdate example script"}
  20. # get a JWT token
  21. auth = requests.auth.HTTPBasicAuth(admin_user, admin_password)
  22. r = requests.get(urlparse.urljoin(base_url, "api/v2/token"), auth=auth, verify=verify_tls_cert, timeout=10)
  23. if r.status_code != 200:
  24. print("error getting access token: {}".format(r.text))
  25. sys.exit(1)
  26. access_token = r.json()["access_token"]
  27. auth_header = {"Authorization": "Bearer " + access_token}
  28. for username in users_to_update:
  29. r = requests.get(urlparse.urljoin(base_url, posixpath.join("api/v2/users", username)),
  30. headers=auth_header, verify=verify_tls_cert, timeout=10)
  31. if r.status_code != 200:
  32. print("error getting user {}: {}".format(username, r.text))
  33. continue
  34. user = r.json()
  35. user.update(fields_to_update)
  36. r = requests.put(urlparse.urljoin(base_url, posixpath.join("api/v2/users", username)),
  37. headers=auth_header, verify=verify_tls_cert, json=user, timeout=10)
  38. if r.status_code == 200:
  39. print("user {} updated".format(username))
  40. else:
  41. print("error updating user {}, response code: {} response text: {}".format(username,
  42. r.status_code,
  43. r.text))