Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. import random # random password generate for auto new cloud user
  2.  
  3. class StormpathHelper(object):
  4.  
  5. def __init__(self, stormpath_manager, StormpathUserKlass):
  6. self.stormpath_manager = stormpath_manager
  7. self.main_cloud_directory = None
  8. self.StormpathUser = StormpathUserKlass
  9. # self.accounts = stormpath_manager.application.accounts
  10. self.main_cloud_accounts = {}
  11. self.is_social_users_dict = {}
  12. self.is_logged_in = {}
  13.  
  14. @staticmethod
  15. def is_user_logged_in(user):
  16. return hasattr(user,"username")
  17.  
  18.  
  19. def is_social_user(self, user):
  20. user_id = str(user.modified_at)
  21. if self.is_social_users_dict.get(user_id) is None:
  22. is_social = user.provider_data.provider_id == 'google'
  23. self.is_social_users_dict[user_id] = is_social
  24.  
  25. return self.is_social_users_dict[user_id]
  26.  
  27. def find_or_create_cloned_cloud_user(self, social_user_account):
  28. email = social_user_account.email
  29.  
  30. if self.main_cloud_accounts.get(email):
  31. return self.main_cloud_accounts[email]
  32.  
  33. self.get_main_cloud_directory()
  34.  
  35. if self.main_cloud_directory:
  36. accounts = self.main_cloud_directory.accounts.search({'email': email})
  37. if accounts:
  38. self.main_cloud_accounts[email] = accounts[0]
  39. return self.main_cloud_accounts[email]
  40. else:
  41. return self.create_cloned_cloud_user(social_user_account)
  42.  
  43. # this will auto create a cloud user so we can assign the user to a group
  44. # social users (like google connect) can't be assigned to groups..
  45. def create_cloned_cloud_user(self, social_user):
  46. random_password = StormpathHelper.generate_password()
  47. auto_generated_cloud_account = self.StormpathUser.create(email = social_user['email'],
  48. password = random_password,
  49. given_name = social_user['given_name'],
  50. surname = social_user['surname'],
  51. username = social_user['email'],
  52. custom_data = {'auto_generated': True},
  53. )
  54.  
  55. return auto_generated_cloud_account
  56.  
  57. # doesn't really matter (because log in is strictly via the google sign in)
  58. # but required in order to create a new user.
  59. @staticmethod
  60. def generate_password():
  61. alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  62. pw_length = 8
  63. mypw = ""
  64.  
  65. for i in range(pw_length):
  66. next_index = random.randrange(len(alphabet))
  67. mypw = mypw + alphabet[next_index]
  68. return mypw
  69.  
  70. def get_main_cloud_directory(self):
  71. if self.main_cloud_directory:
  72. return self.main_cloud_directory
  73.  
  74. try:
  75. self.main_cloud_directory = self.stormpath_manager.client.directories.search({'name': 'Main Cloud Directory'})[0]
  76. return self.main_cloud_directory
  77. except Exception, error:
  78. print error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement