import os
import json
def is_allowed_to_diagnose(user_id):
"""
Checks if a user is allowed to have their data used for diagnostics.
This is a placeholder - replace with your actual authorization logic.
"""
# Example: Allow only specific user IDs or based on a flag in a config file.
allowed_ids = ["user123", "user456"]
if user_id in allowed_ids:
return True
else:
return False
def safeguard_data_for_diagnostics(user_data, user_id, diagnostics_file="diagnostics.json"):
"""
Safeguards user data for diagnostics, only if authorized.
"""
if is_allowed_to_diagnose(user_id):
try:
with open(diagnostics_file, "r") as f:
diagnostics = json.load(f)
except FileNotFoundError:
diagnostics = {}
if user_id not in diagnostics:
diagnostics[user_id] = {}
# Copy user data to diagnostics, excluding sensitive fields.
data_to_diagnose = {k: v for k, v in user_data.items() if not k.startswith("_")} #example: exclude fields starting with underscore
diagnostics[user_id].update(data_to_diagnose)
with open(diagnostics_file, "w") as f:
json.dump(diagnostics, f, indent=4)
print(f"Data for user {user_id} saved for diagnostics.")
else:
print(f"Diagnostics not enabled for user {user_id}.")
if __name__ == '__main__':
# Example Usage
user_data = {
"name": "Alice",
"email": "alice@example.com",
"age": 30,
"_password": "secret", #sensitive data
"location": "New York"
}
user_id = "user123" # Replace with actual user ID
safeguard_data_for_diagnostics(user_data, user_id)
user_id = "user789"
safeguard_data_for_diagnostics(user_data, user_id)
Add your comment