1515from rest_framework .request import Request
1616from rest_framework .response import Response
1717
18- from backend .account .authentication_plugin_registry import (
19- AuthenticationPluginRegistry ,
20- )
18+ from backend .account .authentication_plugin_registry import AuthenticationPluginRegistry
2119from backend .account .authentication_service import AuthenticationService
2220from backend .utils .tenant_context import get_current_tenant
2321
@@ -73,9 +71,7 @@ def user_signup(self, request: Request) -> Response:
7371 # Authorization Callback (SSO)
7472 # =========================================================================
7573
76- def handle_authorization_callback (
77- self , request : HttpRequest , backend : str = ""
78- ) -> HttpResponse :
74+ def handle_authorization_callback (self , request : HttpRequest , backend : str = "" ) -> HttpResponse :
7975 """Handle SSO authorization callback."""
8076 if hasattr (self .auth_service , "handle_authorization_callback" ):
8177 return self .auth_service .handle_authorization_callback (request , backend )
@@ -93,10 +89,7 @@ def user_organizations(self, request: HttpRequest) -> Response:
9389 organizations = self .auth_service .user_organizations (request )
9490 # Cloud plugin returns Pydantic Membership models, OSS returns
9591 # plain dicts. Normalize to dicts for consistent DRF serialization.
96- org_list = [
97- org .model_dump () if hasattr (org , "model_dump" ) else org
98- for org in organizations
99- ]
92+ org_list = [org .model_dump () if hasattr (org , "model_dump" ) else org for org in organizations ]
10093 return Response (
10194 status = status .HTTP_200_OK ,
10295 data = {
@@ -105,9 +98,7 @@ def user_organizations(self, request: HttpRequest) -> Response:
10598 },
10699 )
107100
108- def switch_organization (
109- self , request : HttpRequest , user_id : str , organization_id : str
110- ) -> HttpResponse :
101+ def switch_organization (self , request : HttpRequest , user_id : str , organization_id : str ) -> HttpResponse :
111102 """Switch user's current organization."""
112103 return self .auth_service .switch_organization (request , user_id , organization_id )
113104
@@ -148,25 +139,15 @@ def get_roles(self) -> list:
148139 """Get available roles."""
149140 return self .auth_service .get_roles ()
150141
151- def add_organization_user_role (
152- self , organization_id : str , user : Any , user_role_name : str
153- ) -> Optional [list ]:
142+ def add_organization_user_role (self , organization_id : str , user : Any , user_role_name : str ) -> Optional [list ]:
154143 """Add role to user."""
155- return self .auth_service .add_organization_user_role (
156- organization_id , user , user_role_name
157- )
144+ return self .auth_service .add_organization_user_role (organization_id , user , user_role_name )
158145
159- def assign_role_to_org_user (
160- self , organization_id : str , user : Any , user_role_name : str = "admin"
161- ) -> list :
146+ def assign_role_to_org_user (self , organization_id : str , user : Any , user_role_name : str = "admin" ) -> list :
162147 """Assign role to organization user."""
163- return self .auth_service .assign_role_to_org_user (
164- organization_id , user , user_role_name
165- )
148+ return self .auth_service .assign_role_to_org_user (organization_id , user , user_role_name )
166149
167- def get_organization_role_of_user (
168- self , user_id : str , organization_id : str
169- ) -> list :
150+ def get_organization_role_of_user (self , user_id : str , organization_id : str ) -> list :
170151 """Get user's role in organization."""
171152 return self .auth_service .get_organization_role_of_user (user_id , organization_id )
172153
@@ -196,16 +177,16 @@ def invite_user(
196177 self .auth_service .invite_user (admin , org_id , user_email , user_role )
197178 except Exception as e :
198179 logging .exception (f"Failed to invite { user_email } : { e } " )
199- failed_invites .append ({
200- "email" : user_email ,
201- "status" : "failed" ,
202- "message" : str (e ),
203- })
180+ failed_invites .append (
181+ {
182+ "email" : user_email ,
183+ "status" : "failed" ,
184+ "message" : str (e ),
185+ }
186+ )
204187 return failed_invites
205188
206- def remove_users_from_organization (
207- self , admin : Any , organization_id : str , user_emails : list
208- ) -> list :
189+ def remove_users_from_organization (self , admin : Any , organization_id : str , user_emails : list ) -> list :
209190 """Remove users from organization by email.
210191
211192 Looks up users by email, deletes their OrganizationMember
@@ -226,11 +207,13 @@ def remove_users_from_organization(
226207 user = User .objects .get (email = email )
227208 except User .DoesNotExist :
228209 Logger .error (f"User with email { email } not found" )
229- failed_removals .append ({
230- "email" : email ,
231- "status" : "failed" ,
232- "message" : "User not found" ,
233- })
210+ failed_removals .append (
211+ {
212+ "email" : email ,
213+ "status" : "failed" ,
214+ "message" : "User not found" ,
215+ }
216+ )
234217 continue
235218
236219 deleted_count , _ = OrganizationMember .objects .filter (
@@ -239,14 +222,14 @@ def remove_users_from_organization(
239222 ).delete ()
240223
241224 if not deleted_count :
242- Logger .error (
243- f"No membership found for { email } in org { organization_id } "
225+ Logger .error (f"No membership found for { email } in org { organization_id } " )
226+ failed_removals .append (
227+ {
228+ "email" : email ,
229+ "status" : "failed" ,
230+ "message" : "No membership found" ,
231+ }
244232 )
245- failed_removals .append ({
246- "email" : email ,
247- "status" : "failed" ,
248- "message" : "No membership found" ,
249- })
250233
251234 return failed_removals
252235
@@ -270,9 +253,7 @@ def get_organization_by_org_id(self, org_id: str) -> Any:
270253 """Get organization by ID."""
271254 return self .auth_service .get_organization_by_org_id (org_id )
272255
273- def is_user_member_of_organization (
274- self , user_id : str , organization_id : str
275- ) -> bool :
256+ def is_user_member_of_organization (self , user_id : str , organization_id : str ) -> bool :
276257 """Check if user is member of organization."""
277258 return self .auth_service .is_user_member_of_organization (user_id , organization_id )
278259
@@ -338,15 +319,11 @@ def reset_user_password(self, user: Any) -> Response:
338319 # Cloud-compatible Methods (for multi-tenant operations)
339320 # =========================================================================
340321
341- def authorization_callback (
342- self , request : HttpRequest , backend : str = ""
343- ) -> HttpResponse :
322+ def authorization_callback (self , request : HttpRequest , backend : str = "" ) -> HttpResponse :
344323 """Alias for handle_authorization_callback (cloud naming)."""
345324 return self .handle_authorization_callback (request , backend )
346325
347- def set_user_organization (
348- self , request : HttpRequest , organization_id : str
349- ) -> HttpResponse :
326+ def set_user_organization (self , request : HttpRequest , organization_id : str ) -> HttpResponse :
350327 """Alias for switch_organization (cloud naming)."""
351328 user = request .user
352329 user_id = getattr (user , "user_id" , str (user .id )) if user .is_authenticated else ""
@@ -371,6 +348,7 @@ def get_organization_members_by_user(self, user: Any) -> Optional[Any]:
371348 return self .auth_service .get_organization_members_by_user (user )
372349 # OSS: Get from OrganizationMember model
373350 from backend .core .models .organization_member import OrganizationMember
351+
374352 return OrganizationMember .objects .filter (user = user ).first ()
375353
376354 def get_user_roles (self ) -> list :
@@ -389,9 +367,7 @@ def get_user_invitations(self, organization_id: str) -> list:
389367 """Alias for get_invitations (cloud naming)."""
390368 return self .get_invitations (organization_id )
391369
392- def delete_user_invitation (
393- self , organization_id : str , invitation_id : str
394- ) -> bool :
370+ def delete_user_invitation (self , organization_id : str , invitation_id : str ) -> bool :
395371 """Alias for delete_invitation (cloud naming)."""
396372 return self .delete_invitation (organization_id , invitation_id )
397373
@@ -404,24 +380,24 @@ def _resolve_role_name(role: str) -> str:
404380 """
405381 try :
406382 from pluggable_apps .user_access_control .models .roles import Roles
383+
407384 role_obj = Roles .objects .filter (role_id = role ).first ()
408385 if role_obj :
409386 return role_obj .name
410387 except Exception :
411388 pass
412389 return role
413390
414- def add_user_role (
415- self , admin : Any , org_id : str , email : str , role : str
416- ) -> Optional [dict ]:
391+ def add_user_role (self , admin : Any , org_id : str , email : str , role : str ) -> Optional [dict ]:
417392 """Change a user's role in an organization.
418393
419394 Looks up the user by email, updates the OrganizationMember
420395 record, and delegates to Scalekit if available.
421396 """
422- from backend .core .models .organization_member import OrganizationMember
423397 from django .contrib .auth import get_user_model
424398
399+ from backend .core .models .organization_member import OrganizationMember
400+
425401 User = get_user_model ()
426402 try :
427403 user = User .objects .get (email = email )
@@ -446,9 +422,7 @@ def add_user_role(
446422
447423 return {"email" : email , "role" : role_name }
448424
449- def remove_user_role (
450- self , admin : Any , org_id : str , email : str , role : str
451- ) -> Optional [str ]:
425+ def remove_user_role (self , admin : Any , org_id : str , email : str , role : str ) -> Optional [str ]:
452426 """Remove a role from a user in an organization."""
453427 if hasattr (self .auth_service , "remove_user_role" ):
454428 return self .auth_service .remove_user_role (admin , org_id , email , role )
0 commit comments