def __save_new_account(self, username, password, phone_number): """ Takes a sanitized username, password, and phone number, then saves a row for them in the users table. Returns a User object if successful - None otherwise. """ activation_key = utils.generate_new_activation_key() self.model.save_activation_key_pair(phone_number, activation_key) if not self.__send_activation_text_msg(phone_number, activation_key): logging.error( "An error occurred trying to send activation text message") cherrypy.response.status = 400 else: return self.model.save_user(phone_number, username, utils.encrypt(password))
def create_account(card_num, username, role="user", credit_total=settings.CREDIT_TOTAL, pwd=settings.DEFAULT_PWD, statement_date=settings.STATEMENT_DATE): acc_path = os.path.join(USER_PATH, card_num, "account.json") name_path = os.path.join(USER_PATH, "username.json") sdate_path = os.path.join(USER_PATH, "usersdate.json") num_path = os.path.join(USER_PATH, card_num) username_list = utils.load_file(name_path) usersdate = utils.load_file(sdate_path) today = arrow.now() card_num = utils.to_num(card_num) repayment_date = today.replace(days=+settings.GRACE_PERIOD).format("DD") if not card_num: # print("卡号必须是16位数字") return "卡号必须是16位数字" if username in username_list: # print("用户名已存在") return "用户名已存在" mkdir_ret = utils.mkdir(num_path) if mkdir_ret: # print("账户已存在") return "账户已存在" else: acc_info = { "num": card_num, "username": username, "role": role, "credit_total": credit_total, "credit_balance": credit_total, "pwd": utils.encrypt(pwd), "state": 0, "deposit": 0, "statement_date": statement_date, "repayment_date": repayment_date } username_list.append(username) usersdate[str(statement_date)].append(card_num) utils.dump_to_file(sdate_path, usersdate) utils.dump_to_file(acc_path, acc_info) utils.dump_to_file(name_path, username_list) card_path = os.path.join(USER_PATH, "card.json") if os.path.exists(card_path): card = utils.load_file(card_path) else: card = list() card.append(card_num) utils.dump_to_file(card_path, card)
def login(username, password): """用户登录 :param acc_file:账户文件 :param username:用户名 :param password:密码 :return:成功-True,密码不对-False,用户不存在-None,已锁定-"locked" """ password = utils.encrypt(password) acc = utils.load_file(ACC_PATH) if not acc.get(username): return "用户名不存在!" else: if username == acc[username]["username"] and\ password == acc[username]["pwd"]: return acc[username] else: return "用户名或密码不正确"
def login(username, password): global USER_INFO num_path = os.path.join(DB_PATH, "credit", "users", username) if os.path.exists(num_path) and username: acc_path = os.path.join(DB_PATH, "credit", "users", username, "account.json") else: return check_login() acc_info = utils.load_file(acc_path) # print(acc_info) password = utils.encrypt(password) if password == acc_info["pwd"]: USER_INFO["username"] = username USER_INFO["login_flag"] = 1 print("登录成功") # return USER_INFO elif USER_INFO["err_list"].count(username) == 3: return lock(username) else: USER_INFO["err_list"].append(username) return login_page()
def draw_cash(pwd, amount_of_money): """ 取现 """ global USER_INFO card_num = USER_INFO["username"] print(card_num) if card_num: num_path = os.path.join(USER_PATH, card_num) acc_path = os.path.join(num_path, "account.json") acc = utils.load_file(acc_path) if acc["state"] == 0: print("状态正常") if utils.encrypt(pwd) != acc["pwd"]: print("取款密码不正确") return "取款密码不正确" else: print("密码正确") amount_of_money = float(amount_of_money) if amount_of_money <= acc["deposit"]: acc["deposit"] -= amount_of_money utils.dump_to_file(acc_path, acc) print("账户修改成功") system.recode_trade(card_num, "1", "1", amount_of_money, 0) else: temp = amount_of_money - acc["deposit"] if acc["credit_balance"] >= temp: acc["deposit"] = 0 temp_final = temp + temp * settings.FETCH_MONEY_RATE acc["credit_balance"] -= temp_final utils.dump_to_file(acc_path, acc) system.recode_trade(card_num, "1", "1", amount_of_money, temp_final) else: return "额度不足" else: return "您的账户已冻结"
def login(username, password): global ADMIN_INFO admin_path = os.path.join(DB_PATH, "credit", "admin", "admin.json") ad_info = utils.load_file(admin_path) # print(ad_info) password = utils.encrypt(password) if ad_info.get(username): if ad_info[username]["lock_flag"] == 0: if password == ad_info[username]["password"]: ADMIN_INFO["username"] = username ADMIN_INFO["login_flag"] = 1 return ADMIN_INFO elif ADMIN_INFO["err_list"].count(username) == 3: return lock(username) else: ADMIN_INFO["err_list"].append(username) print("用户名或密码不正确,请重新输入!") return login_page() else: print("您的账户已锁定。") return "已锁定!" else: print("用户不存在") return login_page()
def test_encrypt_works(self): pwd = "sample_pwd" x = utils.encrypt(pwd) self.assertTrue(utils.verify(pwd, x)) self.assertFalse(utils.verify("bad pwd", x))
def spend_api(amount): card_num = input("请输入信用卡卡号:") card_pwd = getpass.getpass("请输入支付密码:") card_pwd = utils.encrypt(card_pwd) users.spend(card_num, card_pwd, amount)