Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Update backend_structured/routes.py
Browse files- backend_structured/routes.py +24 -4
backend_structured/routes.py
CHANGED
|
@@ -1291,12 +1291,15 @@ def get_global_stats(current_user):
|
|
| 1291 |
'timestamp': format_iso_timestamp(a.created_at)
|
| 1292 |
})
|
| 1293 |
|
| 1294 |
-
# Get all users for Members tab
|
| 1295 |
users_data = []
|
| 1296 |
-
if current_user.role == '
|
|
|
|
|
|
|
| 1297 |
all_users = User.query.filter(User.role != 'super_admin').all()
|
| 1298 |
else:
|
| 1299 |
-
all_users = User.query.all()
|
|
|
|
| 1300 |
for u in all_users:
|
| 1301 |
org = db.session.get(Organization, u.org_id) if u.org_id else None
|
| 1302 |
if org:
|
|
@@ -1459,6 +1462,11 @@ def unlock_user(current_user, user_id):
|
|
| 1459 |
if not target:
|
| 1460 |
return jsonify({'message': 'User not found!'}), 404
|
| 1461 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1462 |
_reset_lockout(target)
|
| 1463 |
return jsonify({'message': 'User unlocked!'}), 200
|
| 1464 |
|
|
@@ -1474,8 +1482,16 @@ def update_user(current_user, user_id):
|
|
| 1474 |
|
| 1475 |
if current_user.role == 'org_admin' and target.org_id != current_user.org_id:
|
| 1476 |
return jsonify({'message': 'Unauthorized to modify this user!'}), 403
|
| 1477 |
-
|
| 1478 |
data = request.get_json() or {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1479 |
if 'first_name' in data:
|
| 1480 |
target.first_name = data['first_name']
|
| 1481 |
if 'last_name' in data:
|
|
@@ -1511,6 +1527,10 @@ def delete_user(current_user, user_id):
|
|
| 1511 |
return jsonify({'message': 'User not found!'}), 404
|
| 1512 |
if current_user.role == 'support_engineer':
|
| 1513 |
return jsonify({'message': 'Permission denied. Support Engineers cannot delete members.'}), 403
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1514 |
if current_user.role not in ('super_admin', 'admin') and target.org_id != current_user.org_id:
|
| 1515 |
return jsonify({'message': 'Unauthorized to delete this user!'}), 403
|
| 1516 |
|
|
|
|
| 1291 |
'timestamp': format_iso_timestamp(a.created_at)
|
| 1292 |
})
|
| 1293 |
|
| 1294 |
+
# Get all users for Members tab based on privacy rules
|
| 1295 |
users_data = []
|
| 1296 |
+
if current_user.role == 'super_admin':
|
| 1297 |
+
all_users = User.query.all()
|
| 1298 |
+
elif current_user.role == 'admin':
|
| 1299 |
all_users = User.query.filter(User.role != 'super_admin').all()
|
| 1300 |
else:
|
| 1301 |
+
all_users = User.query.filter(~User.role.in_(['super_admin', 'admin'])).all()
|
| 1302 |
+
|
| 1303 |
for u in all_users:
|
| 1304 |
org = db.session.get(Organization, u.org_id) if u.org_id else None
|
| 1305 |
if org:
|
|
|
|
| 1462 |
if not target:
|
| 1463 |
return jsonify({'message': 'User not found!'}), 404
|
| 1464 |
|
| 1465 |
+
if current_user.role == 'admin' and target.role == 'super_admin':
|
| 1466 |
+
return jsonify({'message': 'Permission denied. Admins cannot modify Super Admin accounts.'}), 403
|
| 1467 |
+
if current_user.role not in ('super_admin', 'admin') and target.role in ('super_admin', 'admin'):
|
| 1468 |
+
return jsonify({'message': 'Permission denied.'}), 403
|
| 1469 |
+
|
| 1470 |
_reset_lockout(target)
|
| 1471 |
return jsonify({'message': 'User unlocked!'}), 200
|
| 1472 |
|
|
|
|
| 1482 |
|
| 1483 |
if current_user.role == 'org_admin' and target.org_id != current_user.org_id:
|
| 1484 |
return jsonify({'message': 'Unauthorized to modify this user!'}), 403
|
| 1485 |
+
|
| 1486 |
data = request.get_json() or {}
|
| 1487 |
+
|
| 1488 |
+
if current_user.role == 'admin':
|
| 1489 |
+
if target.role == 'super_admin' or data.get('role') == 'super_admin':
|
| 1490 |
+
return jsonify({'message': 'Permission denied. Admins cannot view, modify, or assign Super Admin accounts.'}), 403
|
| 1491 |
+
elif current_user.role not in ('super_admin', 'admin'):
|
| 1492 |
+
if target.role in ('super_admin', 'admin') or data.get('role') in ('super_admin', 'admin'):
|
| 1493 |
+
return jsonify({'message': 'Permission denied.'}), 403
|
| 1494 |
+
|
| 1495 |
if 'first_name' in data:
|
| 1496 |
target.first_name = data['first_name']
|
| 1497 |
if 'last_name' in data:
|
|
|
|
| 1527 |
return jsonify({'message': 'User not found!'}), 404
|
| 1528 |
if current_user.role == 'support_engineer':
|
| 1529 |
return jsonify({'message': 'Permission denied. Support Engineers cannot delete members.'}), 403
|
| 1530 |
+
if current_user.role == 'admin' and target.role == 'super_admin':
|
| 1531 |
+
return jsonify({'message': 'Permission denied. Admins cannot delete Super Admin accounts.'}), 403
|
| 1532 |
+
if current_user.role not in ('super_admin', 'admin') and target.role in ('super_admin', 'admin'):
|
| 1533 |
+
return jsonify({'message': 'Permission denied.'}), 403
|
| 1534 |
if current_user.role not in ('super_admin', 'admin') and target.org_id != current_user.org_id:
|
| 1535 |
return jsonify({'message': 'Unauthorized to delete this user!'}), 403
|
| 1536 |
|