def add_staff(role, agency_id, staff_id): if role == "BR": new_broker_staff = BRStaff(staff_id, agency_id) new_broker_staff.save() # Assign staff role staff_role = "BRSTF" new_user_role = UserRolePlacement( staff_id, Role.fetch_role_by_name(staff_role)) new_user_role.save() elif role == "TA": new_ta_staff = TAStaff(staff_id, agency_id) new_ta_staff.save() # assign staff role staff_role = "TASTF" new_user_role = UserRolePlacement( staff_id, Role.fetch_role_by_name(staff_role)) new_user_role.save() elif role == "IA": new_ia_staff = IAStaff(staff_id, agency_id) new_ia_staff.save() # assign staff role staff_role = "IASTF" new_user_role = UserRolePlacement( staff_id, Role.fetch_role_by_name(staff_role)) new_user_role.save()
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 get_user_role(user_id): """ get the user role by id, this is needed to throttle permissions on modules to access """ role = UserRolePlacement.fetch_role_by_user_id(user_id) role_name = Role.fetch_role_by_id(role) return role_name
def register_user(): content = g.data session = db.create_scoped_session() users = session.query(User).all() username = content['username'] if any(user.name == username for user in users): session.close() abort(400, "User with name = %s already exist" % username) return email = content['email'] if '@' not in parseaddr(email)[1]: session.close() abort(400, "Invalid email") return if any(user.email == email for user in users): session.close() abort(400, "User with email = %s already exist" % email) return roles = list(map(lambda r: Role.get_by_name(r), content['roles'])) user = User(name=username, email=email, roles=roles) user.set_password(content['password']) session.add(user) session.commit() session.close() return 'ok'
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 role_placement(role_id, role): new_user_role = UserRolePlacement(role_id, Role.fetch_role_by_name(role)) new_user_role.save()
def get_role_name(self, uid): u_role = UserRolePlacement.fetch_role_by_user_id(uid) return Role.fetch_role_by_id(u_role)