def post(self): """ send the user an email containing a link to set a new password :arg email {string} user email whose account we intend to recover :return: """ user_details = user_parser.parse_args() user_row = User.get_user_by_email(user_details['email']) if user_row: profile_details = UserProfile.get_profile_by_user_id(user_row.id) account_token = token_handler.user_account_confirmation_token( user_row.id) email_text = f"To reset your account password, please follow this link " \ f"{application.config['ACCOUNT_RESET_ENDPOINT']}/{account_token}" email_template = helper.generate_account_recovery_template( application.config['ACCOUNT_RESET_ENDPOINT'], account_token, profile_details.first_name) subject = "Account Password Recovery" helper.send_email(user_details['email'], subject, email_template, email_text) response_msg = helper.make_rest_success_response( "Successfully sent account recovery steps, check your" " email") return make_response(response_msg, 200) response_msg = helper.make_rest_fail_response( "There is not account associated with this email") return make_response(response_msg, 404)
def post(self): # get the user details from the request sent by the client user_details = user_parser.parse_args() # check if the user exists before registering them user_db_row = User.get_user_by_email(user_details['email']) if user_db_row: err_msg = f"{user_details['email']} already exists" response_msg = helper.make_rest_fail_response(err_msg) return make_response(response_msg, 409) # check if user phone number exists phone_number = UserProfile.get_profile_by_phone_number( user_details["phone"]) if phone_number: err_msg = f"{user_details['phone']} already exists" response_msg = helper.make_rest_fail_response(err_msg) return make_response(response_msg, 409) # save the user authentication details and profile details # in their respective database tables user_uuid = uuid.uuid4() new_user_authentication = User(user_uuid, user_details['email'], user_details['password']) new_user_authentication.save() new_user_profile = UserProfile(new_user_authentication.id, user_details['first_name'], user_details['last_name'], user_details['phone']) new_user_profile.save() new_user_role = UserRolePlacement( new_user_authentication.id, Role.fetch_role_by_name(user_details['role'])) new_user_role.save() # Account confirmation email generation # Save extra user details depending on their role role = user_details["role"] self.onboard_client(role, new_user_authentication.id, user_details) # Send a confirmation link to the user for account confirmation confirmation_code = token_handler.user_account_confirmation_token( new_user_authentication.id) email_template = helper.generate_confirmation_template( application.config['CONFIRMATION_ENDPOINT'], confirmation_code) subject = "Your account is inactive, please confirm account or check with your administrator" email_text = f"Use this link {application.config['CONFIRMATION_ENDPOINT']}/{confirmation_code}" \ f" to confirm your account" helper.send_email(user_details['email'], subject, email_template, email_text) response_msg = helper.make_rest_success_response( "Registration successful, kindly" " check your email for confirmation link") return make_response(response_msg, 200)
def send_activation_email(email, customer_id, temporary_pass): email_template = helper.generate_confirmation_template( application.config['LOGIN_ENDPOINT'], temporary_pass) subject = "Nexure Temporary Password" email_text = f"Follow {application.config['LOGIN_ENDPOINT']} to login and use {temporary_pass} " \ f"as your temporary password" helper.send_email(email, subject, email_template, email_text) # Generate a user account activation email confirmation_code = token_handler.user_account_confirmation_token( customer_id) email_template = helper.generate_confirmation_template( application.config['CONFIRMATION_ENDPOINT'], confirmation_code) subject = "Please confirm your account" email_text = f"Use this link {application.config['CONFIRMATION_ENDPOINT']}/{confirmation_code}" \ f" to confirm your account" helper.send_email(email, subject, email_template, email_text)
def get(self, user_id): """ If the jwt token has expired a user can request for another token here simple by passing in the user_id """ user_row = User.get_user_by_id(user_id) if user_row: # awesome, user account exists, let's go ahead and resend the activation email to the user confirmation_code = token_handler.user_account_confirmation_token( user_id) email_template = helper.generate_confirmation_template( application.config['CONFIRMATION_ENDPOINT'], confirmation_code) subject = "Please confirm your account" email_text = f"Use this link {application.config['CONFIRMATION_ENDPOINT']}/{confirmation_code}" \ f" to confirm your account" helper.send_email(user_row.email, subject, email_template, email_text) response = helper.make_rest_success_response( "Please check your email to confirm your account") return make_response(response, 200) response = helper.make_rest_fail_response( "User was not found, please try again or register a new account") return make_response(response, 404)
def post(self): # get the staff details from the request sent by the client user_details = user_parser.parse_args() # check if the staff exists before registering them user_db_row = User.get_user_by_email(user_details['email']) if user_db_row: err_msg = f"{user_details['first_name']} {user_details['last_name']} already exists" response_msg = helper.make_rest_fail_response(err_msg) return make_response(response_msg, 409) # create user account user_uuid = uuid.uuid4() # Create temporary seven digit password temporary_pass = helper.create_user_password() new_user = User(user_uuid, user_details['email'], temporary_pass) new_user.save() # create user profile new_user_profile = UserProfile(new_user.id, user_details['first_name'], user_details['last_name'], user_details['phone']) new_user_profile.save() # get organization details from JWT, such as the role of the client enrolling the staff, and their UID uid = get_jwt_identity() # get user role claims = get_jwt_claims() role = claims['role'] # role = 'BR' # get agency_id agency_id = staff_handler.get_agency_id(role, uid) # Add staff to the appropriate table: i.e BRStaff, TRStaff, IAStaff # We also assign the staff roles at this stage, # depending on the entities they operate under, i.e BRSTF, IASTF, TASTF self.add_staff(role, agency_id, new_user.id) # store staff permissions self.set_permissions(user_details['permissions'], new_user.id) # send email to with the activation details for the staff # Temporary password email email_template = helper.generate_temporary_password_template( application.config['LOGIN_ENDPOINT'], temporary_pass) subject = "Nexure Temporary Password" email_text = f"Follow {application.config['LOGIN_ENDPOINT']} to login and use {temporary_pass} as your temporary password" helper.send_email(user_details['email'], subject, email_template, email_text) # Generate a user account activation email confirmation_code = token_handler.user_account_confirmation_token( new_user.id) email_template = helper.generate_confirmation_template( application.config['CONFIRMATION_ENDPOINT'], confirmation_code) subject = "Please confirm your account" email_text = f"Use this link {application.config['CONFIRMATION_ENDPOINT']}/{confirmation_code}" \ f" to confirm your account" helper.send_email(user_details['email'], subject, email_template, email_text) response = helper.make_rest_success_response( "Registration successful. Please check the staff email to activate your account." ) return make_response(response, 200)