Created
February 8, 2016 08:53
-
-
Save ohadperry/50951e68f2375f0b9002 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random # random password generate for auto new cloud user | |
class StormpathHelper(object): | |
def __init__(self, stormpath_manager, StormpathUserKlass): | |
self.stormpath_manager = stormpath_manager | |
self.main_cloud_directory = None | |
self.StormpathUser = StormpathUserKlass | |
# self.accounts = stormpath_manager.application.accounts | |
self.main_cloud_accounts = {} | |
self.is_social_users_dict = {} | |
self.is_logged_in = {} | |
@staticmethod | |
def is_user_logged_in(user): | |
return hasattr(user,"username") | |
def is_social_user(self, user): | |
user_id = str(user.modified_at) | |
if self.is_social_users_dict.get(user_id) is None: | |
is_social = user.provider_data.provider_id == 'google' | |
self.is_social_users_dict[user_id] = is_social | |
return self.is_social_users_dict[user_id] | |
def find_or_create_cloned_cloud_user(self, social_user_account): | |
email = social_user_account.email | |
if self.main_cloud_accounts.get(email): | |
return self.main_cloud_accounts[email] | |
self.get_main_cloud_directory() | |
if self.main_cloud_directory: | |
accounts = self.main_cloud_directory.accounts.search({'email': email}) | |
if accounts: | |
self.main_cloud_accounts[email] = accounts[0] | |
return self.main_cloud_accounts[email] | |
else: | |
return self.create_cloned_cloud_user(social_user_account) | |
# this will auto create a cloud user so we can assign the user to a group | |
# social users (like google connect) can't be assigned to groups.. | |
def create_cloned_cloud_user(self, social_user): | |
random_password = StormpathHelper.generate_password() | |
auto_generated_cloud_account = self.StormpathUser.create(email = social_user['email'], | |
password = random_password, | |
given_name = social_user['given_name'], | |
surname = social_user['surname'], | |
username = social_user['email'], | |
custom_data = {'auto_generated': True}, | |
) | |
return auto_generated_cloud_account | |
# doesn't really matter (because log in is strictly via the google sign in) | |
# but required in order to create a new user. | |
@staticmethod | |
def generate_password(): | |
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
pw_length = 8 | |
mypw = "" | |
for i in range(pw_length): | |
next_index = random.randrange(len(alphabet)) | |
mypw = mypw + alphabet[next_index] | |
return mypw | |
def get_main_cloud_directory(self): | |
if self.main_cloud_directory: | |
return self.main_cloud_directory | |
try: | |
self.main_cloud_directory = self.stormpath_manager.client.directories.search({'name': 'Main Cloud Directory'})[0] | |
return self.main_cloud_directory | |
except Exception, error: | |
print error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment