118 lines
3.1 KiB
Python
118 lines
3.1 KiB
Python
import json
|
|
import os
|
|
|
|
|
|
def user_add(new_user, new_password):
|
|
"""Dodaje nowego użytkownika lub aktualizuje hasło"""
|
|
file_path = "file.json"
|
|
|
|
# Wczytanie pliku JSON
|
|
if os.path.exists(file_path):
|
|
with open(file_path, "r") as f:
|
|
try:
|
|
data = json.load(f)
|
|
except json.JSONDecodeError:
|
|
data = {"users": []}
|
|
else:
|
|
data = {"users": []}
|
|
|
|
# Sprawdź czy użytkownik już istnieje
|
|
for entry in data["users"]:
|
|
if entry.get("user") == new_user:
|
|
# Aktualizuj hasło
|
|
entry["password"] = new_password
|
|
with open(file_path, "w") as f:
|
|
json.dump(data, f, indent=4)
|
|
return False # Zaktualizowano
|
|
|
|
# Dodaj nowego użytkownika
|
|
new_entry = {"user": new_user, "password": new_password}
|
|
data["users"].append(new_entry)
|
|
|
|
# Zapis do pliku JSON
|
|
with open(file_path, "w") as f:
|
|
json.dump(data, f, indent=4)
|
|
return True # Dodano nowego
|
|
|
|
|
|
def is_person(user_to_find):
|
|
"""Sprawdza czy użytkownik istnieje - zwraca 1 lub 0"""
|
|
file_path = "file.json"
|
|
|
|
try:
|
|
with open(file_path, "r") as f:
|
|
data = json.load(f)
|
|
except (FileNotFoundError, json.JSONDecodeError):
|
|
return 0
|
|
|
|
for entry in data.get("users", []):
|
|
if entry.get("user") == user_to_find:
|
|
return 1
|
|
return 0
|
|
|
|
|
|
def verify_password(user_to_check, password_to_check):
|
|
"""Sprawdza hasło bez usuwania użytkownika"""
|
|
file_path = "file.json"
|
|
|
|
try:
|
|
with open(file_path, "r") as f:
|
|
data = json.load(f)
|
|
except (FileNotFoundError, json.JSONDecodeError):
|
|
return False
|
|
|
|
for entry in data.get("users", []):
|
|
if entry.get("user") == user_to_check:
|
|
return entry.get("password") == password_to_check
|
|
|
|
return False
|
|
|
|
|
|
def get_user_data(user_to_find):
|
|
"""Pobiera dane użytkownika bez usuwania"""
|
|
file_path = "file.json"
|
|
|
|
try:
|
|
with open(file_path, "r") as f:
|
|
data = json.load(f)
|
|
except (FileNotFoundError, json.JSONDecodeError):
|
|
return None
|
|
|
|
for entry in data.get("users", []):
|
|
if entry.get("user") == user_to_find:
|
|
return {"user": entry["user"], "password": entry["password"]}
|
|
|
|
return None
|
|
|
|
|
|
def get_all_users():
|
|
"""Zwraca listę wszystkich użytkowników"""
|
|
file_path = "file.json"
|
|
|
|
try:
|
|
with open(file_path, "r") as f:
|
|
data = json.load(f)
|
|
except (FileNotFoundError, json.JSONDecodeError):
|
|
return []
|
|
|
|
return data.get("users", [])
|
|
|
|
|
|
def delete_user(user_to_delete):
|
|
"""Usuwa użytkownika z bazy"""
|
|
file_path = "file.json"
|
|
|
|
try:
|
|
with open(file_path, "r") as f:
|
|
data = json.load(f)
|
|
except (FileNotFoundError, json.JSONDecodeError):
|
|
return False
|
|
|
|
for i, entry in enumerate(data.get("users", [])):
|
|
if entry.get("user") == user_to_delete:
|
|
data["users"].pop(i)
|
|
with open(file_path, "w") as f:
|
|
json.dump(data, f, indent=4)
|
|
return True
|
|
|
|
return False |