Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita59dc29

Browse files
authored
feat: update pad creation logic to utilize user session for failsafe user creation (#72)
- Modified create_pad method in PadService to accept user_session instead of token_data, enhancing user creation logic when the owner does not exist.- Improved error handling during user creation to manage potential race conditions and provide clearer logging.- Updated pad_router to pass user_session during pad creation, ensuring synchronization with user authentication data.
1 parent790da11 commita59dc29

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

‎src/backend/database/service/pad_service.py‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
Pad service for business logic related to pads.
33
"""
44

5-
fromtypingimportList,Optional,Dict,Any
5+
fromtypingimportList,Optional,Dict,Any,TYPE_CHECKING
66
fromuuidimportUUID
77

88
fromsqlalchemy.ext.asyncioimportAsyncSession
99

1010
from ..repositoryimportPadRepository,UserRepository
1111
from .user_serviceimportUserService
1212

13+
# Use TYPE_CHECKING to avoid circular imports
14+
ifTYPE_CHECKING:
15+
fromdependenciesimportUserSession
16+
1317
classPadService:
1418
"""Service for pad-related business logic"""
1519

@@ -19,7 +23,7 @@ def __init__(self, session: AsyncSession):
1923
self.repository=PadRepository(session)
2024
self.user_repository=UserRepository(session)
2125

22-
asyncdefcreate_pad(self,owner_id:UUID,display_name:str,data:Dict[str,Any],token_data:Dict[str,Any])->Dict[str,Any]:
26+
asyncdefcreate_pad(self,owner_id:UUID,display_name:str,data:Dict[str,Any],user_session:"UserSession"=None)->Dict[str,Any]:
2327
"""Create a new pad"""
2428
# Validate input
2529
ifnotdisplay_name:
@@ -28,20 +32,37 @@ async def create_pad(self, owner_id: UUID, display_name: str, data: Dict[str, An
2832
ifnotdata:
2933
raiseValueError("Pad data is required")
3034

31-
ifnottoken_data:
32-
raiseValueError("Token data is required for user creation failsafe")
33-
3435
# Check if owner exists
3536
owner=awaitself.user_repository.get_by_id(owner_id)
36-
ifnotowner:
37-
# User doesn't exist, create a user record fromtoken data
38-
print(f"WARNING: User with ID '{owner_id}' does not exist butis trying to save a pad. Creating user as failsafe.")
37+
ifnotowneranduser_session:
38+
# User doesn't exist, create a user record fromuser session
39+
print(f"ANOMALY DETECTED: User with ID '{owner_id}' does not exist buthas valid authentication. Creating user as failsafe.")
3940

4041
# Create a UserService instance
4142
user_service=UserService(self.session)
4243

43-
# Use token data to create a complete user record
44-
awaituser_service.sync_user_with_token_data(owner_id,token_data)
44+
# Create user with data from UserSession
45+
try:
46+
awaituser_service.create_user(
47+
user_id=user_session.id,
48+
username=user_session.username,
49+
email=user_session.email,
50+
email_verified=user_session.email_verified,
51+
name=user_session.name,
52+
given_name=user_session.given_name,
53+
family_name=user_session.family_name,
54+
roles=user_session.roles
55+
)
56+
print(f"Successfully created user with ID '{owner_id}' as failsafe.")
57+
exceptValueErrorase:
58+
print(f"Error creating user as failsafe:{str(e)}")
59+
# If user creation fails due to race condition, try to get the user again
60+
if"already exists"instr(e):
61+
owner=awaitself.user_repository.get_by_id(owner_id)
62+
ifnotowner:
63+
raiseValueError(f"Failed to create user with ID '{owner_id}'")
64+
else:
65+
raise
4566

4667
# Check if pad with same name already exists for this owner
4768
existing_pad=awaitself.repository.get_by_name(owner_id,display_name)

‎src/backend/routers/pad_router.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async def save_pad(
2828
owner_id=user.id,
2929
display_name=DEFAULT_PAD_NAME,
3030
data=data,
31-
token_data=user.token_data
31+
user_session=user
3232
)
3333
else:
3434
# Update existing pad
@@ -101,7 +101,7 @@ async def create_pad_from_template(
101101
owner_id=user.id,
102102
display_name=display_name,
103103
data=template["data"],
104-
token_data=user.token_data
104+
user_session=user
105105
)
106106

107107
# Create an initial backup for the new pad

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp