panelx / database.py
Aqso's picture
Update database.py
3f1b8d4 verified
import aiosqlite, bcrypt, os
D_P = "cyber_vault.db"
async def init_db():
async with aiosqlite.connect(D_P) as db:
await db.execute("CREATE TABLE IF NOT EXISTS u_tab (u TEXT PRIMARY KEY, p TEXT)")
await db.commit()
async def add_user(u, p):
un = u.upper().strip()
pw = bcrypt.hashpw(p.encode(), bcrypt.gensalt()).decode()
try:
async with aiosqlite.connect(D_P) as db:
await db.execute("INSERT INTO u_tab (u, p) VALUES (?, ?)", (un, pw))
await db.commit()
return True
except: return False
async def del_user(u):
async with aiosqlite.connect(D_P) as db:
await db.execute("DELETE FROM u_tab WHERE u = ?", (u.upper().strip(),))
await db.commit()
async def check_user(u):
async with aiosqlite.connect(D_P) as db:
async with db.execute("SELECT u FROM u_tab WHERE u = ?", (u.upper().strip(),)) as cur:
return await cur.fetchone() is not None
async def list_users():
async with aiosqlite.connect(D_P) as db:
async with db.execute("SELECT u FROM u_tab") as cur:
r = await cur.fetchall()
return [x[0] for x in r]