5
5
from sqlmodel import Session , select
6
6
from utils .db import get_session
7
7
from utils .auth import get_authenticated_user
8
- from utils .models import Organization , User , Role , UserOrganizationLink , ValidPermissions , RolePermissionLink , Permission , utc_time
8
+ from utils .models import Organization , User , Role , UserOrganizationLink , ValidPermissions , utc_time
9
9
from datetime import datetime
10
10
from sqlalchemy import and_
11
- from typing import List
11
+ from utils . role_org import get_organization , check_user_permission
12
12
13
13
logger = getLogger ("uvicorn.error" )
14
14
@@ -102,128 +102,6 @@ async def as_form(cls, id: int = Form(...), name: str = Form(...)):
102
102
return cls (id = id , name = name )
103
103
104
104
105
- # -- Helper Functions --
106
-
107
- def get_user_organizations (
108
- user_id : int ,
109
- session : Session ,
110
- include_deleted : bool = False
111
- ) -> List [Organization ]:
112
- """
113
- Retrieve all organizations a user is a member of.
114
-
115
- Args:
116
- user_id: ID of the user
117
- session: Database session
118
- include_deleted: Whether to include soft-deleted organizations
119
-
120
- Returns:
121
- List of Organization objects the user belongs to
122
- """
123
- query = (
124
- select (Organization )
125
- .join (UserOrganizationLink )
126
- .where (UserOrganizationLink .user_id == user_id )
127
- )
128
-
129
- if not include_deleted :
130
- query = query .where (Organization .deleted == False )
131
-
132
- return list (session .exec (query ))
133
-
134
-
135
- def get_organization (
136
- org_id : int ,
137
- user_id : int ,
138
- session : Session ,
139
- ) -> Organization :
140
- """
141
- Retrieve a specific organization if the user is a member.
142
-
143
- Args:
144
- org_id: ID of the organization
145
- user_id: ID of the user
146
- session: Database session
147
-
148
- Returns:
149
- Organization object
150
-
151
- Raises:
152
- OrganizationNotFoundError: If organization doesn't exist
153
- InsufficientPermissionsError: If user is not a member
154
- """
155
- # Check if user is a member of the organization
156
- user_org = session .exec (
157
- select (UserOrganizationLink ).where (
158
- and_ (
159
- UserOrganizationLink .user_id == user_id ,
160
- UserOrganizationLink .organization_id == org_id
161
- )
162
- )
163
- ).first ()
164
-
165
- if not user_org :
166
- raise InsufficientPermissionsError ()
167
-
168
- db_org = session .get (Organization , org_id )
169
- if not db_org or db_org .deleted :
170
- raise OrganizationNotFoundError ()
171
-
172
- return db_org
173
-
174
-
175
- def check_user_permission (
176
- user_id : int ,
177
- org_id : int ,
178
- permission : ValidPermissions ,
179
- session : Session ,
180
- ) -> bool :
181
- """
182
- Check if user has the specified permission for the organization.
183
-
184
- Args:
185
- user_id: ID of the user
186
- org_id: ID of the organization
187
- permission: Permission to check
188
- session: Database session
189
-
190
- Returns:
191
- True if user has permission, False otherwise
192
- """
193
- # Get user's role in the organization
194
- user_org = session .exec (
195
- select (UserOrganizationLink ).where (
196
- and_ (
197
- UserOrganizationLink .user_id == user_id ,
198
- UserOrganizationLink .organization_id == org_id
199
- )
200
- )
201
- ).first ()
202
-
203
- if not user_org :
204
- return False
205
-
206
- # Get permission ID
207
- permission_record = session .exec (
208
- select (Permission ).where (Permission .name == permission )
209
- ).first ()
210
-
211
- if not permission_record :
212
- return False
213
-
214
- # Check if role has the permission
215
- role_permission = session .exec (
216
- select (RolePermissionLink ).where (
217
- and_ (
218
- RolePermissionLink .role_id == user_org .role_id ,
219
- RolePermissionLink .permission_id == permission_record .id
220
- )
221
- )
222
- ).first ()
223
-
224
- return bool (role_permission )
225
-
226
-
227
105
# -- Routes --
228
106
229
107
@router .post ("/" , response_class = RedirectResponse )
@@ -268,7 +146,7 @@ def create_organization(
268
146
session .add (user_org_link )
269
147
session .commit ()
270
148
271
- return RedirectResponse (url = f"/organizations/ { db_org . id } " , status_code = 303 )
149
+ return RedirectResponse (url = f"/profile " , status_code = 303 )
272
150
273
151
274
152
@router .put ("/{org_id}" , response_class = RedirectResponse )
@@ -298,7 +176,7 @@ def update_organization(
298
176
session .commit ()
299
177
session .refresh (organization )
300
178
301
- return RedirectResponse (url = f"/organizations/ { org . id } " , status_code = 303 )
179
+ return RedirectResponse (url = f"/profile " , status_code = 303 )
302
180
303
181
304
182
@router .delete ("/{org_id}" , response_class = RedirectResponse )
@@ -318,4 +196,4 @@ def delete_organization(
318
196
session .add (organization )
319
197
session .commit ()
320
198
321
- return RedirectResponse (url = "/organizations " , status_code = 303 )
199
+ return RedirectResponse (url = "/profile " , status_code = 303 )
0 commit comments