Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Update backend_structured/routes.py
Browse files
backend_structured/routes.py
CHANGED
|
@@ -1049,6 +1049,9 @@ def manage_users(current_user):
|
|
| 1049 |
}), 200
|
| 1050 |
|
| 1051 |
# POST method - Create/Add new member
|
|
|
|
|
|
|
|
|
|
| 1052 |
data = request.get_json() or {}
|
| 1053 |
email = data.get('email')
|
| 1054 |
role = data.get('role', 'admin')
|
|
@@ -1449,7 +1452,7 @@ def update_user(current_user, user_id):
|
|
| 1449 |
if not target:
|
| 1450 |
return jsonify({'message': 'User not found!'}), 404
|
| 1451 |
|
| 1452 |
-
if current_user.role not in ('org_admin', 'super_admin', 'admin'):
|
| 1453 |
return jsonify({'message': 'Permission denied'}), 403
|
| 1454 |
|
| 1455 |
if current_user.role == 'org_admin' and target.org_id != current_user.org_id:
|
|
@@ -1468,6 +1471,8 @@ def update_user(current_user, user_id):
|
|
| 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']
|
|
@@ -1475,7 +1480,7 @@ def update_user(current_user, user_id):
|
|
| 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
|
| 1479 |
db.session.add(log)
|
| 1480 |
db.session.commit()
|
| 1481 |
|
|
@@ -1487,6 +1492,8 @@ def delete_user(current_user, user_id):
|
|
| 1487 |
target = db.session.get(User, user_id)
|
| 1488 |
if not target:
|
| 1489 |
return jsonify({'message': 'User not found!'}), 404
|
|
|
|
|
|
|
| 1490 |
if current_user.role not in ('super_admin', 'admin') and target.org_id != current_user.org_id:
|
| 1491 |
return jsonify({'message': 'Unauthorized to delete this user!'}), 403
|
| 1492 |
|
|
|
|
| 1049 |
}), 200
|
| 1050 |
|
| 1051 |
# POST method - Create/Add new member
|
| 1052 |
+
if current_user.role == 'support_engineer':
|
| 1053 |
+
return jsonify({'message': 'Permission denied. Support Engineers cannot create new members.'}), 403
|
| 1054 |
+
|
| 1055 |
data = request.get_json() or {}
|
| 1056 |
email = data.get('email')
|
| 1057 |
role = data.get('role', 'admin')
|
|
|
|
| 1452 |
if not target:
|
| 1453 |
return jsonify({'message': 'User not found!'}), 404
|
| 1454 |
|
| 1455 |
+
if current_user.role not in ('org_admin', 'super_admin', 'admin', 'support_engineer'):
|
| 1456 |
return jsonify({'message': 'Permission denied'}), 403
|
| 1457 |
|
| 1458 |
if current_user.role == 'org_admin' and target.org_id != current_user.org_id:
|
|
|
|
| 1471 |
return jsonify({'message': 'Email already in use'}), 400
|
| 1472 |
target.email = email_clean
|
| 1473 |
if 'password' in data and data['password'] and str(data['password']).strip():
|
| 1474 |
+
if current_user.role == 'support_engineer':
|
| 1475 |
+
return jsonify({'message': 'Permission denied. Support Engineers cannot modify user passwords.'}), 403
|
| 1476 |
target.set_password(str(data['password']).strip())
|
| 1477 |
if 'role' in data and data['role']:
|
| 1478 |
target.role = data['role']
|
|
|
|
| 1480 |
new_org_id = data['org_id']
|
| 1481 |
target.org_id = new_org_id if new_org_id not in ('', 'none', 'null', None) else None
|
| 1482 |
|
| 1483 |
+
log = AuditLog(admin_id=current_user.id, action=f"Updated user details/role for {target.email}", target_id=target.org_id)
|
| 1484 |
db.session.add(log)
|
| 1485 |
db.session.commit()
|
| 1486 |
|
|
|
|
| 1492 |
target = db.session.get(User, user_id)
|
| 1493 |
if not target:
|
| 1494 |
return jsonify({'message': 'User not found!'}), 404
|
| 1495 |
+
if current_user.role == 'support_engineer':
|
| 1496 |
+
return jsonify({'message': 'Permission denied. Support Engineers cannot delete members.'}), 403
|
| 1497 |
if current_user.role not in ('super_admin', 'admin') and target.org_id != current_user.org_id:
|
| 1498 |
return jsonify({'message': 'Unauthorized to delete this user!'}), 403
|
| 1499 |
|