larxius commited on
Commit
2a57182
·
verified ·
1 Parent(s): 9126147

Update backend_structured/routes.py

Browse files
Files changed (1) hide show
  1. backend_structured/routes.py +12 -10
backend_structured/routes.py CHANGED
@@ -1455,7 +1455,7 @@ def update_user(current_user, user_id):
1455
  if current_user.role == 'org_admin' and target.org_id != current_user.org_id:
1456
  return jsonify({'message': 'Unauthorized to modify this user!'}), 403
1457
 
1458
- data = request.get_json()
1459
  if 'first_name' in data:
1460
  target.first_name = data['first_name']
1461
  if 'last_name' in data:
@@ -1463,17 +1463,19 @@ def update_user(current_user, user_id):
1463
  if 'email' in data:
1464
  new_email = data['email']
1465
  if new_email and new_email != target.email:
1466
- if User.query.filter_by(email=new_email).first():
 
1467
  return jsonify({'message': 'Email already in use'}), 400
1468
- target.email = new_email
1469
- if 'password' in data and data['password']:
1470
- target.set_password(data['password'])
1471
- if 'role' in data:
1472
- new_role = data['role']
1473
- if new_role != target.role:
1474
- target.role = new_role
 
1475
 
1476
- log = AuditLog(admin_id=current_user.id, action=f"Updated user {target.email}", target_id=target.org_id)
1477
  db.session.add(log)
1478
  db.session.commit()
1479
 
 
1455
  if current_user.role == 'org_admin' and target.org_id != current_user.org_id:
1456
  return jsonify({'message': 'Unauthorized to modify this user!'}), 403
1457
 
1458
+ data = request.get_json() or {}
1459
  if 'first_name' in data:
1460
  target.first_name = data['first_name']
1461
  if 'last_name' in data:
 
1463
  if 'email' in data:
1464
  new_email = data['email']
1465
  if new_email and new_email != target.email:
1466
+ email_clean = str(new_email).strip().lower()
1467
+ if User.query.filter(func.lower(User.email) == email_clean).first():
1468
  return jsonify({'message': 'Email already in use'}), 400
1469
+ target.email = email_clean
1470
+ if 'password' in data and data['password'] and str(data['password']).strip():
1471
+ target.set_password(str(data['password']).strip())
1472
+ if 'role' in data and data['role']:
1473
+ target.role = data['role']
1474
+ if 'org_id' in data:
1475
+ new_org_id = data['org_id']
1476
+ target.org_id = new_org_id if new_org_id not in ('', 'none', 'null', None) else None
1477
 
1478
+ log = AuditLog(admin_id=current_user.id, action=f"Updated user credentials & role for {target.email}", target_id=target.org_id)
1479
  db.session.add(log)
1480
  db.session.commit()
1481