def check_credentials(cls, username, password): """ Returns ID of user if credentials match, 0 otherwise. """ password = hash_utils.hash_password(password) auth = db.select('users', where="username=$username&&password=$password", vars=locals()) if len(auth) == 1: return auth[0].user_id else: return 0
def update_password(cls, user_id, password): """ Updates password according to specified user_id and new password. Returns true if updated for one user or password unchanged, false otherwise. """ user_list = cls.select_users(user_id=user_id) password_hash = hash_utils.hash_password(password) if len(user_list) == 1 and password_hash == user_list[0].password: return True if db.update('users', where="user_id=$user_id", password=password_hash, vars=locals()) \ == 1: return True return False
def register(cls, username, password, email): """ Attempts to insert new user data into users table. Returns ID of user if successfully registered, 0 if user already exists, -1 if database error. """ if len(cls.select_users(username=username)) > 0: return 0 else: if username == '' or email == '': return -1 db.insert('users', username=username, email=email, password=hash_utils.hash_password(password)) user_lookup = cls.select_users(username=username) if len(user_lookup) == 1: return user_lookup[0].user_id else: return -1