05/22 Enhance codes and fix bugs 2

This commit is contained in:
2026-05-22 10:46:43 -04:00
parent 3231c8eb56
commit f5dc6660c5
7 changed files with 80 additions and 29 deletions
+13 -7
View File
@@ -1,7 +1,10 @@
import logging
import re
import time
from flask import Blueprint, request, jsonify, g
_log = logging.getLogger(__name__)
from app import db, limiter, client_ip
from app.models.user import User
from app.models.audit_log import AuditLog
@@ -584,7 +587,7 @@ def change_password():
user = db.session.get(User, g.current_user_id)
if not verify_auth_token(current_auth_hash, user.master_hash):
if not verify_auth_token(current_auth_hash, user.master_hash, user=user):
AuditLog.log(
user_id=user.id,
action='auth.change_password_failed',
@@ -646,9 +649,10 @@ def change_password():
ip_address=client_ip(),
)
db.session.commit()
except Exception as e:
except Exception:
db.session.rollback()
return jsonify({'error': f'Password change failed: {str(e)}'}), 500
_log.exception('change_password failed for user %s', g.current_user_id)
return jsonify({'error': 'Password change failed. Please try again.'}), 500
return jsonify({'message': 'Password changed successfully. Please log in again.'}), 200
@@ -696,9 +700,10 @@ def delete_account():
)
db.session.delete(user)
db.session.commit()
except Exception as e:
except Exception:
db.session.rollback()
return jsonify({'error': f'Account deletion failed: {str(e)}'}), 500
_log.exception('delete_account failed for user %s', user_id)
return jsonify({'error': 'Account deletion failed. Please try again.'}), 500
return jsonify({'message': 'Account deleted'}), 200
@@ -849,9 +854,10 @@ def recover_account():
ip_address=client_ip(),
)
db.session.commit()
except Exception as e:
except Exception:
db.session.rollback()
return jsonify({'error': f'Recovery failed: {str(e)}'}), 500
_log.exception('recover_account failed for user %s', user.id)
return jsonify({'error': 'Account recovery failed. Please try again.'}), 500
tokens = generate_tokens(user.id)
return jsonify({
+10 -3
View File
@@ -29,14 +29,21 @@ def list_emergency():
)
).order_by(EmergencyAccess.created_at.desc()).all()
grantor_ids = {ea.grantor_id for ea in access}
grantors = (
{u.id: u for u in User.query.filter(User.id.in_(grantor_ids)).all()}
if grantor_ids else {}
)
return jsonify({
'grants': [ea.to_dict(grantor_email=user.email) for ea in grants],
'access': [_ea_as_grantee(ea) for ea in access],
'access': [_ea_as_grantee(ea, grantors.get(ea.grantor_id)) for ea in access],
}), 200
def _ea_as_grantee(ea: EmergencyAccess) -> dict:
grantor = db.session.get(User, ea.grantor_id)
def _ea_as_grantee(ea: EmergencyAccess, grantor: 'User | None' = None) -> dict:
if grantor is None:
grantor = db.session.get(User, ea.grantor_id)
d = ea.to_dict(grantor_email=grantor.email if grantor else None)
d['grantor_public_key'] = grantor.sharing_public_key if grantor else None
return d
+6 -1
View File
@@ -242,10 +242,15 @@ def inbox():
.order_by(SharedItem.created_at.desc())
.all()
)
owner_ids = {s.owner_id for s in shares}
owners = (
{u.id: u for u in User.query.filter(User.id.in_(owner_ids)).all()}
if owner_ids else {}
)
result = []
for s in shares:
d = s.to_dict()
owner = db.session.get(User, s.owner_id)
owner = owners.get(s.owner_id)
d['owner_email'] = owner.email if owner else 'Unknown'
d['owner_public_key'] = owner.sharing_public_key if owner else None
result.append(d)
+10 -4
View File
@@ -1,5 +1,9 @@
import logging
from flask import Blueprint, request, jsonify, g
from app import db, limiter, client_ip
_log = logging.getLogger(__name__)
from app.models.vault_item import VaultItem, ItemType
from app.models.folder import Folder
from app.models.audit_log import AuditLog
@@ -87,9 +91,10 @@ def create_item():
ip_address=client_ip(),
)
db.session.commit()
except Exception as e:
except Exception:
db.session.rollback()
return jsonify({'error': f'Database error: {str(e)}'}), 500
_log.exception('create_item failed for user %s', g.current_user_id)
return jsonify({'error': 'Failed to save item. Please try again.'}), 500
return jsonify(item.to_dict()), 201
@@ -148,9 +153,10 @@ def update_item(item_id):
ip_address=client_ip(),
)
db.session.commit()
except Exception as e:
except Exception:
db.session.rollback()
return jsonify({'error': f'Database error: {str(e)}'}), 500
_log.exception('update_item failed for user %s item %s', g.current_user_id, item_id)
return jsonify({'error': 'Failed to update item. Please try again.'}), 500
return jsonify(item.to_dict()), 200