Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. from okta.framework.ApiClient import ApiClient
  2. from okta.framework.Utils import Utils
  3. from okta.framework.PagedResults import PagedResults
  4. from okta.models.user.User import User
  5. from okta.models.user.TempPassword import TempPassword
  6. from okta.models.user.ResetPasswordToken import ResetPasswordToken
  7. from okta.models.user.LoginCredentials import LoginCredentials
  8.  
  9.  
  10. class UsersClient(ApiClient):
  11. def __init__(self, base_url, api_token):
  12. ApiClient.__init__(self, base_url + '/api/v1/users', api_token)
  13.  
  14. # CRUD
  15.  
  16. def get_users(self, limit=None, query=None, filter_string=None):
  17. """Get a list of Users
  18.  
  19. :param limit: maximum number of users to return
  20. :type limit: int or None
  21. :param query: string to search users' first names, last names, and emails
  22. :type query: str or None
  23. :param filter_string: string to filter users
  24. :type filter_string: str or None
  25. :rtype: list of User
  26. """
  27. params = {
  28. 'limit': limit,
  29. 'query': query,
  30. 'filter': filter_string
  31. }
  32. response = ApiClient.get_path(self, '/', params=params)
  33. return Utils.deserialize(response.text, User)
  34.  
  35. def get_user(self, uid):
  36. """Get a single user
  37.  
  38. :param uid: the user id or login
  39. :type uid: str
  40. :rtype: User
  41. """
  42. response = ApiClient.get_path(self, '/{0}'.format(uid))
  43. return Utils.deserialize(response.text, User)
  44.  
  45. def update_user(self, user):
  46. """Update a user
  47.  
  48. :param user: the user to update
  49. :type user: User
  50. :rtype: User
  51. """
  52. return self.update_user_by_id(user.id, user)
  53.  
  54. def update_user_by_id(self, uid, user):
  55. """Update a user, defined by an id
  56.  
  57. :param uid: the target user id
  58. :type uid: str
  59. :param user: the data to update the target user
  60. :type user: User
  61. :rtype: User
  62. """
  63. response = ApiClient.put_path(self, '/{0}'.format(uid), user)
  64. return Utils.deserialize(response.text, User)
  65.  
  66. def create_user(self, user, activate=False):
  67. """Create a user
  68.  
  69. :param user: the data to create a user
  70. :type user: User
  71. :param activate: whether to activate the user
  72. :type activate: bool
  73. :rtype: User
  74. """
  75. if activate is None:
  76. response = ApiClient.post_path(self, '/', user)
  77. else:
  78. params = {
  79. 'activate': activate
  80. }
  81. response = ApiClient.post_path(self, '/', user, params=params)
  82. return Utils.deserialize(response.text, User)
  83.  
  84. def delete_user(self, uid):
  85. """Delete user by target id
  86.  
  87. :param uid: the target user id
  88. :type uid: str
  89. :return: None
  90. """
  91. response = ApiClient.delete_path(self, '/{0}'.format(uid))
  92. return Utils.deserialize(response.text, User)
  93.  
  94. def get_paged_users(self, limit=None, filter_string=None, after=None, url=None):
  95. """Get a paged list of Users
  96.  
  97. :param limit: maximum number of users to return
  98. :type limit: int or None
  99. :param filter_string: string to filter users
  100. :type filter_string: str or None
  101. :param after: user id that filtering will resume after
  102. :type after: str
  103. :param url: url that returns a list of User
  104. :type url: str
  105. :rtype: PagedResults of User
  106. """
  107. if url:
  108. response = ApiClient.get(self, url)
  109.  
  110. else:
  111. params = {
  112. 'limit': limit,
  113. 'after': after,
  114. 'filter': filter_string
  115. }
  116. response = ApiClient.get_path(self, '/', params=params)
  117.  
  118. return PagedResults(response, User)
  119.  
  120. # LIFECYCLE
  121.  
  122. def activate_user(self, uid, send_email=True):
  123. """Activate user by target id
  124.  
  125. :param uid: the target user id
  126. :type uid: str
  127. :param send_email: whether a welcome email should be sent
  128. :type send_email: bool
  129. :return: User
  130. """
  131. params = {
  132. 'sendEmail': send_email
  133. }
  134.  
  135. response = ApiClient.post_path(self, '/{0}/lifecycle/activate'.format(uid), params=params)
  136. return Utils.deserialize(response.text, User)
  137.  
  138. def deactivate_user(self, uid):
  139. """Deactivate user by target id
  140.  
  141. :param uid: the target user id
  142. :type uid: str
  143. :return: User
  144. """
  145. response = ApiClient.post_path(self, '/{0}/lifecycle/deactivate'.format(uid))
  146. return Utils.deserialize(response.text, User)
  147.  
  148. def unlock_user(self, uid):
  149. """Unlock user by target id
  150.  
  151. :param uid: the target user id
  152. :type uid: str
  153. :return: User
  154. """
  155. response = ApiClient.post_path(self, '/{0}/lifecycle/unlock'.format(uid))
  156. return Utils.deserialize(response.text, User)
  157.  
  158. def reset_password(self, uid, send_email=True):
  159. """Reset user's password by target user id
  160.  
  161. :param uid: the target user id
  162. :type uid: str
  163. :param send_email: whether a password reset email should be sent
  164. :type send_email: bool
  165. :return: None or ResetPasswordToken
  166. """
  167. params = {
  168. 'sendEmail': send_email
  169. }
  170. response = ApiClient.post_path(self, '/{0}/lifecycle/reset_password'.format(uid), params=params)
  171. return Utils.deserialize(response.text, ResetPasswordToken)
  172.  
  173. def change_password(self, uid, old_password, new_password):
  174. """Change user's password by target user id
  175.  
  176. :param uid: the target user id
  177. :type uid: str
  178. :param old_password: the user's old password
  179. :type old_password: str
  180. :param new_password: the desired new password
  181. :type new_password: str
  182. :return: None or LoginCredentials
  183. """
  184. data = {
  185. 'oldPassword': {
  186. 'value': old_password
  187. },
  188. 'newPassword': {
  189. 'value': new_password
  190. }
  191. }
  192. response = ApiClient.post_path(self, '/{0}/credentials/change_password'.format(uid), data)
  193. return Utils.deserialize(response.text, LoginCredentials)
  194.  
  195. def expire_password(self, uid, temp_password=False):
  196. """Expire user's password by target user id
  197.  
  198. :param uid: the target user id
  199. :type uid: str
  200. :param temp_password: whether a temporary password should be set
  201. :type temp_password: bool
  202. :return: None or TempPassword
  203. """
  204. if not temp_password:
  205. ApiClient.post_path(self, '/{0}/lifecycle/expire_password'.format(uid))
  206. else:
  207. params = {
  208. 'tempPassword': temp_password
  209. }
  210. response = ApiClient.post_path(self, '/{0}/lifecycle/expire_password'.format(uid), params=params)
  211. return Utils.deserialize(response.text, TempPassword)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement