Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Update backend_structured/routes.py
Browse files- backend_structured/routes.py +30 -8
backend_structured/routes.py
CHANGED
|
@@ -750,12 +750,13 @@ def manage_organizations(current_user):
|
|
| 750 |
|
| 751 |
return jsonify({'organizations': org_list}), 200
|
| 752 |
|
| 753 |
-
if current_user.role
|
| 754 |
return jsonify({'message': 'Permission denied'}), 403
|
| 755 |
|
| 756 |
-
data = request.get_json()
|
| 757 |
org_name = data.get('name')
|
| 758 |
tier = data.get('tier', 'free')
|
|
|
|
| 759 |
|
| 760 |
if not org_name:
|
| 761 |
return jsonify({'message': 'Organization name is required'}), 400
|
|
@@ -764,6 +765,27 @@ def manage_organizations(current_user):
|
|
| 764 |
db.session.add(org)
|
| 765 |
db.session.commit()
|
| 766 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 767 |
# Needs to be after commit to get org.id
|
| 768 |
log = AuditLog(admin_id=current_user.id, action="Provisioned new tenant", target_id=org.id)
|
| 769 |
db.session.add(log)
|
|
@@ -774,7 +796,7 @@ def manage_organizations(current_user):
|
|
| 774 |
@auth_bp.route('/organizations/<org_id>', methods=['PUT', 'DELETE'])
|
| 775 |
@token_required
|
| 776 |
def manage_single_organization(current_user, org_id):
|
| 777 |
-
if current_user.role not in ('super_admin', 'org_admin'):
|
| 778 |
return jsonify({'message': 'Permission denied'}), 403
|
| 779 |
|
| 780 |
if current_user.role == 'org_admin' and str(current_user.org_id) != str(org_id):
|
|
@@ -785,8 +807,8 @@ def manage_single_organization(current_user, org_id):
|
|
| 785 |
return jsonify({'message': 'Organization not found'}), 404
|
| 786 |
|
| 787 |
if request.method == 'DELETE':
|
| 788 |
-
if current_user.role
|
| 789 |
-
return jsonify({'message': 'Permission denied: Only Super Admin can delete organizations'}), 403
|
| 790 |
|
| 791 |
db.session.delete(org)
|
| 792 |
log = AuditLog(admin_id=current_user.id, action="Deleted tenant", target_id=org.id)
|
|
@@ -795,13 +817,13 @@ def manage_single_organization(current_user, org_id):
|
|
| 795 |
return jsonify({'message': 'Tenant deleted successfully'}), 200
|
| 796 |
|
| 797 |
# PUT method
|
| 798 |
-
data = request.get_json()
|
| 799 |
if 'name' in data:
|
| 800 |
org.name = data['name']
|
| 801 |
|
| 802 |
if 'tier' in data:
|
| 803 |
-
if current_user.role
|
| 804 |
-
return jsonify({'message': 'Permission denied: Only Super Admin can change tier'}), 403
|
| 805 |
org.subscription_tier = data['tier']
|
| 806 |
|
| 807 |
log = AuditLog(admin_id=current_user.id, action="Updated tenant configuration", target_id=org.id)
|
|
|
|
| 750 |
|
| 751 |
return jsonify({'organizations': org_list}), 200
|
| 752 |
|
| 753 |
+
if current_user.role not in ['super_admin', 'admin']:
|
| 754 |
return jsonify({'message': 'Permission denied'}), 403
|
| 755 |
|
| 756 |
+
data = request.get_json() or {}
|
| 757 |
org_name = data.get('name')
|
| 758 |
tier = data.get('tier', 'free')
|
| 759 |
+
admin_email = data.get('admin_email')
|
| 760 |
|
| 761 |
if not org_name:
|
| 762 |
return jsonify({'message': 'Organization name is required'}), 400
|
|
|
|
| 765 |
db.session.add(org)
|
| 766 |
db.session.commit()
|
| 767 |
|
| 768 |
+
# Optional admin email link or creation
|
| 769 |
+
if admin_email and str(admin_email).strip():
|
| 770 |
+
email_clean = str(admin_email).strip().lower()
|
| 771 |
+
existing_user = User.query.filter_by(email=email_clean).first()
|
| 772 |
+
if existing_user:
|
| 773 |
+
existing_user.org_id = org.id
|
| 774 |
+
if existing_user.role not in ['super_admin', 'admin', 'support_engineer']:
|
| 775 |
+
existing_user.role = 'org_admin'
|
| 776 |
+
db.session.commit()
|
| 777 |
+
else:
|
| 778 |
+
import secrets
|
| 779 |
+
temp_pw = secrets.token_urlsafe(8)
|
| 780 |
+
new_user = User(
|
| 781 |
+
email=email_clean,
|
| 782 |
+
role='org_admin',
|
| 783 |
+
org_id=org.id
|
| 784 |
+
)
|
| 785 |
+
new_user.set_password(temp_pw)
|
| 786 |
+
db.session.add(new_user)
|
| 787 |
+
db.session.commit()
|
| 788 |
+
|
| 789 |
# Needs to be after commit to get org.id
|
| 790 |
log = AuditLog(admin_id=current_user.id, action="Provisioned new tenant", target_id=org.id)
|
| 791 |
db.session.add(log)
|
|
|
|
| 796 |
@auth_bp.route('/organizations/<org_id>', methods=['PUT', 'DELETE'])
|
| 797 |
@token_required
|
| 798 |
def manage_single_organization(current_user, org_id):
|
| 799 |
+
if current_user.role not in ('super_admin', 'admin', 'org_admin'):
|
| 800 |
return jsonify({'message': 'Permission denied'}), 403
|
| 801 |
|
| 802 |
if current_user.role == 'org_admin' and str(current_user.org_id) != str(org_id):
|
|
|
|
| 807 |
return jsonify({'message': 'Organization not found'}), 404
|
| 808 |
|
| 809 |
if request.method == 'DELETE':
|
| 810 |
+
if current_user.role not in ('super_admin', 'admin'):
|
| 811 |
+
return jsonify({'message': 'Permission denied: Only Admin or Super Admin can delete organizations'}), 403
|
| 812 |
|
| 813 |
db.session.delete(org)
|
| 814 |
log = AuditLog(admin_id=current_user.id, action="Deleted tenant", target_id=org.id)
|
|
|
|
| 817 |
return jsonify({'message': 'Tenant deleted successfully'}), 200
|
| 818 |
|
| 819 |
# PUT method
|
| 820 |
+
data = request.get_json() or {}
|
| 821 |
if 'name' in data:
|
| 822 |
org.name = data['name']
|
| 823 |
|
| 824 |
if 'tier' in data:
|
| 825 |
+
if current_user.role not in ('super_admin', 'admin'):
|
| 826 |
+
return jsonify({'message': 'Permission denied: Only Admin or Super Admin can change tier'}), 403
|
| 827 |
org.subscription_tier = data['tier']
|
| 828 |
|
| 829 |
log = AuditLog(admin_id=current_user.id, action="Updated tenant configuration", target_id=org.id)
|