4
4
from pydantic import BaseModel , ConfigDict , field_validator
5
5
from sqlmodel import Session , select
6
6
from utils .db import get_session
7
- from utils .auth import get_authenticated_user
8
- from utils .models import Organization , User , Role , Permission , UserOrganizationLink , ValidPermissions , utc_time
7
+ from utils .auth import get_authenticated_user , get_user_with_relations
8
+ from utils .models import Organization , User , Role , utc_time , default_roles
9
9
from datetime import datetime
10
- from sqlalchemy import and_
11
- from utils .role_org import get_organization , check_user_permission
12
10
13
11
logger = getLogger ("uvicorn.error" )
14
12
@@ -23,14 +21,6 @@ def __init__(self):
23
21
)
24
22
25
23
26
- class OrganizationExistsError (HTTPException ):
27
- def __init__ (self ):
28
- super ().__init__ (
29
- status_code = 400 ,
30
- detail = "Organization already exists"
31
- )
32
-
33
-
34
24
class OrganizationNotFoundError (HTTPException ):
35
25
def __init__ (self ):
36
26
super ().__init__ (
@@ -109,64 +99,49 @@ def create_organization(
109
99
user : User = Depends (get_authenticated_user ),
110
100
session : Session = Depends (get_session )
111
101
) -> RedirectResponse :
102
+ # Check if organization already exists
112
103
db_org = session .exec (select (Organization ).where (
113
104
Organization .name == org .name )).first ()
114
105
if db_org :
115
- raise OrganizationExistsError ()
106
+ raise OrganizationNameTakenError ()
116
107
108
+ # Create organization first
117
109
db_org = Organization (name = org .name )
118
110
session .add (db_org )
119
- session . commit ()
120
- session .refresh ( db_org )
111
+ # This gets us the org ID without committing
112
+ session .flush ( )
121
113
122
- # Create default roles
123
- default_role_names = ["Owner" , "Administrator" , "Member" ]
124
- default_roles = []
125
- for role_name in default_role_names :
126
- role = Role (name = role_name , organization_id = db_org .id )
127
- session .add (role )
128
- default_roles .append (role )
129
- session .commit ()
114
+ # Create default roles with organization_id
115
+ initial_roles = [
116
+ Role (name = name , organization_id = db_org .id )
117
+ for name in default_roles
118
+ ]
119
+ session .add_all (initial_roles )
120
+ session .flush ()
130
121
131
- owner_role = session .exec (
132
- select (Role ).where (
133
- and_ (
134
- Role .organization_id == db_org .id ,
135
- Role .name == "Owner"
136
- )
137
- )
138
- ).first ()
122
+ # Get owner role for user assignment
123
+ owner_role = next (role for role in db_org .roles if role .name == "Owner" )
139
124
140
- if not owner_role :
141
- owner_role = Role (
142
- name = "Owner" ,
143
- organization_id = db_org .id
144
- )
145
- session .add (owner_role )
146
- session .commit ()
147
- session .refresh (owner_role )
148
-
149
- user_org_link = UserOrganizationLink (
150
- user_id = user .id ,
151
- organization_id = db_org .id ,
152
- role_id = owner_role .id
153
- )
154
- session .add (user_org_link )
125
+ # Assign user to owner role
126
+ user .roles .append (owner_role )
127
+
128
+ # Commit changes
155
129
session .commit ()
130
+ session .refresh (db_org )
156
131
157
132
return RedirectResponse (url = f"/profile" , status_code = 303 )
158
133
159
134
160
135
@router .put ("/{org_id}" , response_class = RedirectResponse )
161
136
def update_organization (
162
137
org : OrganizationUpdate = Depends (OrganizationUpdate .as_form ),
163
- user : User = Depends (get_authenticated_user ),
138
+ user : User = Depends (get_user_with_relations ),
164
139
session : Session = Depends (get_session )
165
140
) -> RedirectResponse :
166
141
# This will raise appropriate exceptions if org doesn't exist or user lacks access
167
- organization = get_organization (org .id , user . id , session )
142
+ organization : Organization = user . organizations . get (org .id )
168
143
169
- if not check_user_permission ( user . id , org . id , ValidPermissions . EDIT_ORGANIZATION , session ):
144
+ if not organization or not any ( role . permissions . EDIT_ORGANIZATION for role in organization . roles ):
170
145
raise InsufficientPermissionsError ()
171
146
172
147
# Check if new name already exists for another organization
@@ -178,24 +153,27 @@ def update_organization(
178
153
if existing_org :
179
154
raise OrganizationNameTakenError ()
180
155
156
+ # Update organization name
181
157
organization .name = org .name
182
158
organization .updated_at = utc_time ()
183
159
session .add (organization )
184
160
session .commit ()
185
- session .refresh (organization )
186
161
187
162
return RedirectResponse (url = f"/profile" , status_code = 303 )
188
163
189
164
190
165
@router .delete ("/{org_id}" , response_class = RedirectResponse )
191
166
def delete_organization (
192
167
org_id : int ,
193
- user : User = Depends (get_authenticated_user ),
168
+ user : User = Depends (get_user_with_relations ),
194
169
session : Session = Depends (get_session )
195
170
) -> RedirectResponse :
196
- # This will raise appropriate exceptions if org doesn't exist or user lacks access
197
- organization = get_organization (org_id , user .id , session )
171
+ # Check if user has permission to delete organization
172
+ organization : Organization = user .organizations .get (org_id )
173
+ if not organization or not any (role .permissions .DELETE_ORGANIZATION for role in organization .roles ):
174
+ raise InsufficientPermissionsError ()
198
175
176
+ # Delete organization
199
177
session .delete (organization )
200
178
session .commit ()
201
179
0 commit comments