def register_new_user(self, request): register_form = request.json if request.is_json else request.form register_dict = dict(register_form) form_register_fields = ["firstName", 'lastName', 'email', 'password', 'sectorId'] form_valid_res = form_is_full(register_dict, form_register_fields) if form_valid_res.isSuccess: firstName = register_dict["firstName"] lastName = register_dict['lastName'] email = register_dict['email'] password = register_dict['password'] sectorId = register_dict['sectorId'] user = User(firstName=firstName, lastName=lastName, email=email, password=password, sectorId=sectorId) if user_is_valid(user).isSuccess: if password_is_valid(user.password).isSuccess: if sector_id_is_valid(user.sectorId).isSuccess: password_encrypt = PasswordEncryption.hash_salt(password=password, salt=None) user.password = password_encrypt if DAL.save_new_user_to_db(user) and DAL.save_user_password_history_to_db(user): return AuthorizationResult(isSuccess=True, Message="User created successfully. ") else: return sector_id_is_valid(user.sectorId) else: return password_is_valid(user.password) else: return AuthorizationResult(isSuccess=False, Message="User created failed. ") else: return form_valid_res
def create_sector(self, request): sector_form = request.json if request.is_json else request.form sector_dict = dict(sector_form) sector_fields = ['name', 'description'] sector_res = form_is_full(sector_dict, sector_fields) if sector_res.isSuccess: sector = Sectors(name=sector_dict['name'], description=sector_dict['description']) DAL.save_new_sector_to_db(sector) return True else: sector_res.isSuccess
def start_login_process(email, enteredPassword): user = UserProvider.get_user_from_db_by_email(email) if user: if verify_user_password(user, enteredPassword): user.invalidLoginAttempt = 0 return user, AuthorizationResult(isSuccess=verify_user_password(user, enteredPassword), Message="User Login!") else: #if hasattr(user, 'lockEndTime'): if user.lockEndTime != None and user.lockEndTime > datetime.now(): return None, AuthorizationResult(isSuccess=False, Message=Config.USER_IS_LOCKED_UNTIL + str(user.lockEndTime)) else: user.invalidLoginAttempt = user.invalidLoginAttempt + 1 if user.invalidLoginAttempt >= Config.LOGIN_LIMIT_TRYING: user.lockEndTime = (datetime.now() + timedelta(minutes=15)) # .strftime("%B %d, %Y %I:%M%p") user.invalidLoginAttempt = 0 DAL.save_new_user_to_db(user) return None, AuthorizationResult(isSuccess=False, Message=Config.BAD_USER_NAME_OR_PASSWORD)
def create_packages(self, request): package_form = request.json if request.is_json else request.form package_dict = dict(package_form) package_fields = ['name'] package_res = form_is_full(package_dict, package_fields) if package_res.isSuccess: package = Packages(name=package_dict['name']) if DAL.save_new_package_to_db(package): return True return package_res.isSuccess
def start_change_password(user_id,change_password_dict): enteredPassword = change_password_dict['password'] user = UserProvider.get_user_from_db_by_id(user_id) occurrences = Config.HISTORY_OF_THE_PASSWORDS res_password_is_valid = password_is_valid(enteredPassword) if res_password_is_valid.isSuccess: print(was_password_used_in_the_last_given_occurrences(user, enteredPassword, occurrences)) if was_password_used_in_the_last_given_occurrences(user, enteredPassword, occurrences): return AuthorizationResult(isSuccess=False, Message=Config.PASSWORD_WAS_USED_IN_THE_LAST_GIVEN_OCCURRENCES) else: password_encrypt = PasswordEncryption.hash_salt(password=enteredPassword, salt=None) user.password = password_encrypt if DAL.save_new_user_to_db(user) and DAL.save_user_password_history_to_db(user): return AuthorizationResult(isSuccess=True, Message=Config.PASSWORD_CHANGE_SUCCESS) else: return AuthorizationResult(isSuccess=False, Message=Config.PASSWORD_CHANGE_FAILED) # else: # return AuthorizationResult(isSuccess=False, Message=Config.PASSWORD_CHANGE_FAILED) else: return res_password_is_valid
def was_password_used_in_the_last_given_occurrences(user, enteredPassword, occurrences): password_history = DAL.get_UserPasswordsHistory_by_user_id(user.id) n = occurrences if password_history.count() < occurrences: n = password_history.count() for i in range(-n, n-occurrences): salt_from_storage = password_history[i].password[:Config.LENGTH_OF_THE_SALT] # 32 is the length of the salt key_from_storage = password_history[i].password[Config.LENGTH_OF_THE_SALT:] enteredPassword_hash_salt = PasswordEncryption.hash_salt(enteredPassword, salt_from_storage) if enteredPassword_hash_salt[Config.LENGTH_OF_THE_SALT:] == key_from_storage: return True return False
def add_purchase_to_user(self, user_id, request): if request.is_json: packagesSectorsId = request.json['id'] else: packagesSectorsId = request.form['id'] packagesSectors = PackagesSectorsProvider.get_packages_sector_by_id( packagesSectorsId) userPurchases = UserPurchases(user_id=(int)(user_id), packagesSectorsId=packagesSectorsId, price=packagesSectors.price, name=packagesSectors.name) if DAL.save_new_purchase_to_db(userPurchases): return True else: return False
def create_packages_sector(self, request): packages_sector_form = request.json if request.is_json else request.form packages_sector_dict = dict(packages_sector_form) packages_sector_fields = ['packageId', 'sectorId', 'price', 'name'] packages_sector_res = form_is_full(packages_sector_dict, packages_sector_fields) if packages_sector_res.isSuccess: packagesSector = PackagesSectors( packageId=packages_sector_dict['packageId'], sectorId=packages_sector_dict['sectorId'], price=packages_sector_dict['price'], name=packages_sector_dict['name']) if DAL.save_new_packages_sectors_to_db(packagesSector): return True return False
def get_all_packages_that_user_purchases(self, user_id): userPurchasesList = DAL.get_purchases_by_user_id(user_id) userPurchasesViewModel = UserPurchasesViewModel(many=True) return userPurchasesViewModel.dump(userPurchasesList)