def hacker_get(id): handler = UserProfileHandler() result = handler.get(id) handler.close() if result is None: response = make_response( jsonify({ 'result': 'Error', 'msg': 'Hacker does not exist' }), 404) response.headers['Content-Type'] = 'application/json' return response # create UserProfile based off the tuple returned by result profile = UserProfile(result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14]) response = make_response( jsonify({ 'result': 'Success', 'msg': 'OK', 'val': profile.asdict() }), 200) response.headers['Content-Type'] = 'application/json' return response
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 hacker_update(id): # we don't need to check if the id exists because we assume that this will be # handled client-side. if not (fr.values.get('UserTypeCode') or fr.values.get('Name') or fr.values.get('Street1') or fr.values.get('StateCode'), fr.values.get('ZipCode'), fr.values.get('CountryCode'), fr.values.get('Phone'), fr.values.get('Email'), fr.values.get('BirthDate')): response = make_response( jsonify({ 'result': 'Error', 'msg': 'One or more required fields is missing' }), 400) response.headers['Content-Type'] = 'application/json' return response handler = UserProfileHandler() profile = UserProfile(id, fr.values.get('UserTypeCode'), fr.values.get('Name'), fr.values.get('School'), fr.values.get('Major'), fr.values.get('Street1'), fr.values.get('Street2'), fr.values.get('City'), fr.values.get('StateCode'), fr.values.get('ZipCode'), fr.values.get('CountryCode'), fr.values.get('Phone'), fr.values.get('Email'), fr.values.get('BirthDate'), fr.values.get('ProfileImageUrl')) handler.update(profile) handler.close() response = make_response(jsonify({'result': 'Success', 'msg': 'OK'}), 200) response.headers['Content-Type'] = 'application/json' return response
def hacker_insert(): if not (fr.values.get('UserTypeCode') or fr.values.get('Name') or fr.values.get('Street1') or fr.values.get('StateCode'), fr.values.get('ZipCode'), fr.values.get('CountryCode'), fr.values.get('Phone'), fr.values.get('Email'), fr.values.get('BirthDate')): response = make_response( jsonify({ 'result': 'Error', 'msg': 'One or more required fields is missing' }), 400) response.headers['Content-Type'] = 'application/json' return response handler = UserProfileHandler() profile = UserProfile(None, fr.values.get('UserTypeCode'), fr.values.get('Name'), fr.values.get('School'), fr.values.get('Major'), fr.values.get('Street1'), fr.values.get('Street2'), fr.values.get('City'), fr.values.get('StateCode'), fr.values.get('ZipCode'), fr.values.get('CountryCode'), fr.values.get('Phone'), fr.values.get('Email'), fr.values.get('BirthDate'), fr.values.get('ProfileImageUrl')) handler.insert(profile) handler.close() response = make_response(jsonify({'result': 'Success', 'msg': 'OK'}), 201) response.headers['Content-Type'] = 'application/json' return response
def get_customer_details(self, user_id): # use the user id to determine the customer's role, whether IND or ORG role_id = UserRolePlacement.fetch_role_by_user_id(user_id) # get role name using role_id role = Role.fetch_role_by_id(role_id) if role == 'IND': customer_no = IndividualCustomer.get_customer_number(user_id) if customer_no: user_profile = UserProfile.get_profile_by_user_id(user_id) data = { "customer_number": customer_no, "first_name": user_profile.first_name, "last_name": user_profile.last_name, "phone_number": user_profile.phone, "kra_pin": user_profile.kra_pin, "id_passport": user_profile.id_passport, } return data else: response_msg = helper.make_rest_fail_response( "Customer does not exist") return make_response(response_msg, 404) elif role == 'ORG': # When the customer is an organization, their details are stored directly in the organization model # (not linked with the user profile) customer = OrganizationCustomer.get_customer_by_contact(user_id) if customer: # get customer details data = {} # TODO: Confirm from Tony whether to fetch organization details or contact person pass
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): try: req = request.get_json(force=True) data = UserProfileValidator().validate(req) # Sample adding explicitly # resource = UserProfile( # username=data['username'], # password=data['password'], # email=data['email'] # ) # resource.add() resource = UserProfile() query = resource.add(data) return UserProfileDetail().get(query.id) except Exception as ex: resp = jsonify({"error": str(ex)}) resp.status_code = 403 return resp
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)
def post(self): customer_details = customer_parser.parse_args() # we need to check for all the unique values to a user to ensure there is no duplicate customer = UserProfile.check_account_duplicate( customer_details['phone'], customer_details['id_passport'], customer_details['kra_pin']) customer_number = None if not customer: # Create temporary seven digit password temporary_pass = helper.create_user_password() # create a new user account if not existing user_id = uuid.uuid4() # create new user customer = User(user_id, customer_details['email'], temporary_pass) customer.save() # if on boarding an individual customer if customer_details['type'] == 'Individual': customer = User.get_user_by_email(customer_details['email']) customer_id = customer.id # create individual customer's profile new_individual_profile = UserProfile( customer_id, customer_details["first_name"], customer_details["last_name"], customer_details["phone"], customer_details["gender"], customer_details["avatar_url"], customer_details["occupation"], customer_details["id_passport"], customer_details["kra_pin"], customer_details["birth_date"], customer_details['physical_address'], customer_details['postal_address'], customer_details['postal_code'], customer_details['postal_town'], customer_details['country'], customer_details['county'], customer_details['constituency'], customer_details['ward'], customer_details['facebook'], customer_details['instagram'], customer_details['twitter']) new_individual_profile.save() # create a new individual customer detail customer_number = self.create_customer_number( "IN", customer_id, customer_details['country']) self.create_individual_customer(customer_id, customer_number, customer_details['salutation']) self.role_placement(customer_id, "IND") # send activation email self.send_activation_email(customer_details['email'], customer_id, temporary_pass) # if on boarding an organization elif customer_details['type'] == "Organization": customer_id = customer.id customer_number = self.create_customer_number( customer_details['org_type'], customer_id, customer_details['country']) # Add contact person details contact_person = UserProfile(customer_id, customer_details['first_name'], customer_details['last_name'], customer_details["phone"]) contact_person.save() new_org_customer = OrganizationCustomer( customer_details['org_type'], customer_details["org_name"], customer_details['org_phone'], customer_details['org_email'], customer_details['reg_number'], customer_details['physical_address'], customer_details['postal_address'], customer_details['postal_code'], customer_details['postal_town'], customer_details['county'], customer_details['constituency'], customer_details['ward'], customer_details['facebook'], customer_details['instagram'], customer_details['twitter'], customer_id, customer_number) new_org_customer.save() self.role_placement(customer_id, "ORG") # send activation email self.send_activation_email(customer_details['email'], customer_id, temporary_pass) # create a new affiliation between the customer and broker/agent # each affiliation must only exist once in the db # we need to fetch the role of the agent/broker and associate it into the affiliation uid = get_jwt_identity() # we need to check whether the current user is a staff member or an agent/broker role_name = self.get_role_name(uid) agent_broker = self.check_staff(uid, role_name) if agent_broker: # the current user is not a staff member # we also need to ensure the affiliation created is not a duplicate one if customer_number is None: customer_number = self.fetch_customer_number( role_name, customer.id, agent_broker) if self.is_affiliation_duplicate(role_name, agent_broker, customer_number): response_msg = helper.make_rest_fail_response( "Customer already exists") return make_response(response_msg, 409) self.register_customer(role_name, customer_number, agent_broker, uid) else: broker_agent_id = self.get_broker_agent_id(uid, role_name) if customer_number is None: customer_number = self.fetch_customer_number( role_name, customer.id, broker_agent_id, agent_broker) if self.is_affiliation_duplicate(role_name, broker_agent_id, customer_number): response_msg = helper.make_rest_fail_response( "Customer already exists") return make_response(response_msg, 409) self.register_customer(role_name, customer_number, broker_agent_id) response_msg = helper.make_rest_success_response( "Customer has been on-boarded successfully", {"customer_number": customer_number}) return make_response(response_msg, 200)