def request_password_reset(user): secret_info = { 'userId': user.id, 'action': 'password_reset', } hours_duration = 48 secret = Secret.create_secret(secret_info, hours_duration) url = '{BASEURL}/#/resetpassword?key={secret_key}'.format( BASEURL=config.BASEURL, secret_key=secret.key, ) content = '''<p>We received a request to reset your password for CommunityShare.</p> <p>To reset your password please click on the following link and follow the instructions.</p> <a href={url}>{url}</a> <p>If you cannot click on the link copy it into the addressbar of your browser.</p> ''' content = content.format(url=url) email = mail.Email( from_address=config.DONOTREPLY_EMAIL_ADDRESS, to_address=user.email, subject='CommunityShare Password Reset Request', content=content, new_content=content, ) error_message = mail.get_mailer().send(email) return error_message
def request_signup_email_confirmation(user, template=None, subject=None): secret_info = { 'userId': user.id, 'email': user.email, 'action': 'email_confirmation', } hours_duration = 24 * 14 secret = Secret.create_secret(secret_info, hours_duration) url = '{BASEURL}/#/confirmemail?key={secret_key}'.format( BASEURL=config.BASEURL, secret_key=secret.key, ) if template is None: template = '''<p>A community share account has been created and attached to this email address.<p> <p>To confirm that you created the account, please click on the following link.</p> <p><a href={url}>{url}</a></p> <p>If you did not create this account, simply ignore this email.</p> ''' content = template.format(url=url) if subject is None: subject = 'CommunityShare Account Creation' email = mail.Email(from_address=config.DONOTREPLY_EMAIL_ADDRESS, to_address=user.email, subject=subject, content=content, new_content=content) error_message = mail.get_mailer().send(email) return error_message
def setup(n_random_users=100): logger.info('Starting setup script.') init_db() first_admin = None logger.info('Making labels.') make_labels() import os from community_share.models.secret import Secret admin_emails = config.ADMIN_EMAIL_ADDRESSES.split(',') admin_emails = [x.strip() for x in admin_emails] logger.info('admin_emails is {0}'.format(admin_emails)) logger.info('Making Admin Users') for email in admin_emails: if email: user = make_admin_user(email, email, Secret.make_key(20)) if user is not None and first_admin is None: first_admin = user logger.info('Making {0} random users'.format(n_random_users)) for i in range(n_random_users): make_random_user() store.session.commit() creator = get_creator() questions = setup_data.get_questions(creator) update_questions(questions) store.session.commit()
def setup(n_random_users=100): logger.info('Starting setup script.') wait_for_db() init_db() logger.info('Making labels.') make_labels() from community_share.models.secret import Secret logger.info('Making Admin Users') make_admin_user('*****@*****.**', '*****@*****.**', 'admin') admin_emails = config.ADMIN_EMAIL_ADDRESSES.split(',') admin_emails = [x.strip() for x in admin_emails] logger.info('admin_emails is {0}'.format(admin_emails)) for email in admin_emails: make_admin_user(email, email, Secret.make_key(20)) logger.info('Making {0} random users'.format(n_random_users)) password_hash = User.pwd_context.encrypt('password') for i in range(n_random_users): make_random_user(password_hash=password_hash) creator = get_creator() logger.info('Creator of questions is {}'.format(creator.email)) questions = setup_data.get_questions(creator) update_questions(questions) store.session.commit() creator = get_creator() questions = setup_data.get_questions(creator) update_questions(questions) store.session.commit()
def request_password_reset(user): secret_info = { 'userId': user.id, 'action': 'password_reset', } hours_duration = 48 secret = Secret.create_secret(secret_info, hours_duration) content = '''We received a request to reset your password for CommunityShare. To reset your password please click on the following link and follow the instructions. {BASEURL}/#/resetpassword?key={secret_key} If you cannot click on the link copy it into the addressbar of your browser. ''' content = content.format(BASEURL=config.BASEURL, secret_key=secret.key) if not user.email_confirmed: error_message = 'The email address is not confirmed.' else: email = mail.Email( from_address=config.DONOTREPLY_EMAIL_ADDRESS, to_address=user.confirmed_email, subject='CommunityShare Password Reset Request', content=content, new_content=content, ) error_message = mail.get_mailer().send(email) return error_message
def make_random_user(): # Make the user first_name, last_name = gen_new_name(user_names_used, first_names, last_names) if first_name is None: return user_names_used.add((first_name, last_name)) password = Secret.make_key(20) password_hash = User.pwd_context.encrypt(password) if random.randint(0, 1): searcher_role = 'educator' searching_for_role = 'partner' bio = generate_educator_bio() associations = [gen_random_institution(schools, educator_roles)] else: searcher_role = 'partner' searching_for_role = 'educator' bio = generate_expert_bio() associations = [ gen_random_institution(companies, partner_roles) for _ in range(random.randint(1, 2)) ] new_user = User( name='{0} {1}'.format(first_name, last_name), email=gen_email(first_name, last_name), password_hash=password_hash, picture_filename=random.choice(profile_picture_filenames), bio=bio, institution_associations=associations, is_administrator=False, email_confirmed=True ) store.session.add(new_user) store.session.commit() # Make the search latitude, longitude = make_random_location() search = Search( searcher_user_id=new_user.id, searcher_role=searcher_role, searching_for_role=searching_for_role, latitude=latitude, longitude=longitude, ) search.labels = Label.name_list_to_object_list(gen_labels()) store.session.add(search) store.session.commit() if search.searcher_role == 'educator': new_user.educator_profile_search = search else: new_user.community_partner_profile_search = search store.session.add(new_user) store.session.commit()
def make_api_key(self): secret_data = { 'userId': self.id, 'action': 'api_key', } secret = Secret.create_secret(info=secret_data, hours_duration=24) return secret
def setup(n_random_users=100): logger.info('Starting setup script.') init_db() logger.info('Making labels.') make_labels() from community_share.models.secret import Secret logger.info('Making Admin Users') make_admin_user('*****@*****.**', '*****@*****.**', 'admin') admin_emails = config.ADMIN_EMAIL_ADDRESSES.split(',') admin_emails = [x.strip() for x in admin_emails] logger.info('admin_emails is {0}'.format(admin_emails)) for email in admin_emails: make_admin_user(email, email, Secret.make_key(20)) logger.info('Making {0} random users'.format(n_random_users)) for i in range(n_random_users): make_random_user() creator = get_creator() logger.info('Creator of questions is {}'.format(creator.email)) questions = setup_data.get_questions(creator) update_questions(questions) store.session.commit() creator = get_creator() questions = setup_data.get_questions(creator) update_questions(questions) store.session.commit()
def request_password_reset(user): secret_info = { 'userId': user.id, 'action': 'password_reset', } hours_duration = 48 secret = Secret.create_secret(secret_info, hours_duration) url = '{BASEURL}/#/resetpassword?key={secret_key}'.format( BASEURL=config.BASEURL, secret_key=secret.key) content = '''<p>We received a request to reset your password for CommunityShare.</p> <p>To reset your password please click on the following link and follow the instructions.</p> <a href={url}>{url}</a> <p>If you cannot click on the link copy it into the addressbar of your browser.</p> ''' content = content.format(url=url) email = mail.Email( from_address=config.DONOTREPLY_EMAIL_ADDRESS, to_address=user.email, subject='CommunityShare Password Reset Request', content=content, new_content=content, ) error_message = mail.get_mailer().send(email) return error_message
def request_signup_email_confirmation(user, template=None, subject=None): secret_info = { 'userId': user.id, 'email': user.email, 'action': 'email_confirmation', } hours_duration = 24*14 secret = Secret.create_secret(secret_info, hours_duration) url = '{BASEURL}/#/confirmemail?key={secret_key}'.format( BASEURL=config.BASEURL, secret_key=secret.key) if template is None: template = '''<p>A community share account has been created and attached to this email address.<p> <p>To confirm that you created the account, please click on the following link.</p> <p><a href={url}>{url}</a></p> <p>If you did not create this account, simply ignore this email.</p> ''' content = template.format(url=url) if subject is None: subject = 'CommunityShare Account Creation' email = mail.Email( from_address=config.DONOTREPLY_EMAIL_ADDRESS, to_address=user.email, subject=subject, content=content, new_content=content ) error_message = mail.get_mailer().send(email) return error_message
def request_signup_email_confirmation(user): secret_info = { 'userId': user.id, 'email': user.email, 'action': 'email_confirmation', } hours_duration = 48 secret = Secret.create_secret(secret_info, hours_duration) content = '''A community share account has been created and attached to this email address. To confirm that you created the account, please click on the following link. {BASEURL}/#/confirmemail?key={secret_key} If you did not create this account, simply ignore this email. ''' content = content.format(BASEURL=config.BASEURL, secret_key=secret.key) email = mail.Email( from_address=config.DONOTREPLY_EMAIL_ADDRESS, to_address=user.email, subject='CommunityShare Account Creation', content=content, new_content=content ) error_message = mail.get_mailer().send(email) return error_message
def make_random_user(): # Make the user finished = False while not finished: first_name = random_item_from_list(first_names) last_name = random_item_from_list(last_names) combined = (first_name, last_name) if combined not in user_names_used: finished = True user_names_used.add(combined) password = Secret.make_key(20) email = make_email(first_name, last_name) password_hash = User.pwd_context.encrypt(password) name = '{0} {1}'.format(first_name, last_name) picture_filename = random_item_from_list(profile_picture_filenames) randombinary = random.randint(0, 1) if randombinary: searcher_role = 'educator' searching_for_role = 'partner' bio = generate_educator_bio() institution_associations = [ InstitutionAssociation( institution=random_item_from_list(schools), role=random_item_from_list(educator_roles) )] else: searcher_role = 'partner' searching_for_role = 'educator' bio = generate_expert_bio() n_institutions = random.randint(1, 2) institution_associations = [ InstitutionAssociation( institution=random_item_from_list(companies), role=random_item_from_list(partner_roles)) for x in range(n_institutions)] new_user = User(name=name, email=email, password_hash=password_hash, picture_filename=picture_filename, bio=bio, institution_associations=institution_associations, is_administrator=False, email_confirmed=True) store.session.add(new_user) store.session.commit() # Make the search location = make_random_location() search = Search( searcher_user_id=new_user.id, searcher_role=searcher_role, searching_for_role=searching_for_role, latitude=location[0], longitude=location[1], ) search.labels = Label.name_list_to_object_list(get_labels()) store.session.add(search) store.session.commit() if search.searcher_role == 'educator': new_user.educator_profile_search = search else: new_user.community_partner_profile_search = search store.session.add(new_user) store.session.commit()
def from_api_key(self, key): secret = Secret.lookup_secret(key) logger.debug('key is {0}'.format(key)) logger.debug('secret is {0}'.format(secret)) user_id = None if secret is not None: info = secret.get_info() if info.get('action', None) == 'api_key': user_id = info.get('userId', None) if user_id is not None: user = store.session.query(User).filter_by(id=user_id).first() logger.debug('user from api_key is {0}'.format(user)) else: user = None return user
def main(): logger.info('Loading settings from environment') config.load_from_environment() logger.info('Starting setup script produced on 2014 June 14th.') setup.init_db() first_admin = None logger.info('Making labels.') setup.make_labels() admin_emails = os.environ.get('COMMUNITYSHARE_ADMIN_EMAILS', '').split(',') admin_emails = [x.strip() for x in admin_emails] logger.info('admin_emails is {0}'.format(admin_emails)) logger.info('Making Admin Users') for email in admin_emails: if email: user = setup.make_admin_user(email, email, Secret.make_key(20)) if user is not None and first_admin is None: first_admin = user logger.info('Making questions') setup.make_questions(first_admin) store.session.commit()
def setup(n_random_users=100): logger.info('Starting setup script.') init_db() first_admin = None logger.info('Making labels.') make_labels() import os from community_share.models.secret import Secret admin_emails = os.environ.get('COMMUNITYSHARE_ADMIN_EMAILS', '').split(',') admin_emails = [x.strip() for x in admin_emails] logger.info('admin_emails is {0}'.format(admin_emails)) logger.info('Making Admin Users') for email in admin_emails: if email: user = make_admin_user(email, email, Secret.make_key(20)) if user is not None and first_admin is None: first_admin = user logger.info('Making {0} random users'.format(n_random_users)) for i in range(n_random_users): make_random_user() store.session.commit()
def process_password_reset(secret_key, new_password): user = None error_messages = User.is_password_valid(new_password) if not error_messages: secret = Secret.lookup_secret(secret_key) error_message = '' if secret is not None: secret_info = secret.get_info() userId = secret_info.get('userId', None) action = secret_info.get('action', None) if action == 'password_reset' and userId is not None: user = store.session.query(User).filter_by(id=userId).first() if user is not None: error_messages += user.set_password(new_password) if not error_messages: secret.used = True store.session.add(user) store.session.add(secret) store.session.commit() else: error_messages.append('Authorization for this action is invalid or expired.') return (user, error_messages)
def process_password_reset(secret_key, new_password): user = None error_messages = User.is_password_valid(new_password) if not error_messages: secret = Secret.lookup_secret(secret_key) error_message = '' if secret is not None: secret_info = secret.get_info() userId = secret_info.get('userId', None) action = secret_info.get('action', None) if action == 'password_reset' and userId is not None: user = store.session.query(User).filter_by(id=userId).first() if user is not None: error_messages += user.set_password(new_password) if not error_messages: secret.used = True store.session.add(user) store.session.add(secret) store.session.commit() else: error_messages.append( 'Authorization for this action is invalid or expired.') return (user, error_messages)
def process_confirm_email(secret_key): error_messages = [] user = None secret = Secret.lookup_secret(secret_key) if secret is not None: secret_info = secret.get_info() userId = secret_info.get('userId', None) action = secret_info.get('action', None) if action == 'email_confirmation' and userId is not None: user = store.session.query(User).filter_by(id=userId).first() if user is not None: user.email_confirmed = True secret.used = True store.session.add(user) store.session.add(secret) store.session.commit() else: error_messages.append('Authorization is for an unknown user.') else: error_mesage('Authorization is not valid for this action.') else: error_messages.append('Authorization for this action is invalid or expired.') return (user, error_messages)
def process_confirm_email(secret_key): error_messages = [] user = None secret = Secret.lookup_secret(secret_key) if secret is not None: secret_info = secret.get_info() userId = secret_info.get('userId', None) action = secret_info.get('action', None) if action == 'email_confirmation' and userId is not None: user = store.session.query(User).filter_by(id=userId).first() if user is not None: user.email_confirmed = True secret.used = True store.session.add(user) store.session.add(secret) store.session.commit() else: error_messages.append('Authorization is for an unknown user.') else: error_mesage('Authorization is not valid for this action.') else: error_messages.append( 'Authorization for this action is invalid or expired.') return (user, error_messages)