Skip to content

Commit a00b562

Browse files
authoredMar 3, 2025
Check for user default workspace when logging in with google (#493)
* Check for user default workspace when logging in with google * Fix docstring
1 parent 5726038 commit a00b562

File tree

1 file changed

+24
-45
lines changed

1 file changed

+24
-45
lines changed
 

‎core_backend/app/auth/routers.py

+24-45
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
from ..database import get_sqlalchemy_async_engine
1212
from ..users.models import (
1313
UserNotFoundError,
14-
check_if_user_exists_in_workspace,
1514
create_user_workspace_role,
1615
get_user_by_username,
16+
get_user_default_workspace,
1717
get_user_role_in_workspace,
1818
save_user_to_db,
1919
)
2020
from ..users.schemas import UserCreate, UserRoles
2121
from ..utils import update_api_limits
22-
from ..workspaces.utils import create_workspace, get_workspace_by_workspace_name
22+
from ..workspaces.utils import create_workspace
2323
from .config import NEXT_PUBLIC_GOOGLE_LOGIN_CLIENT_ID
2424
from .dependencies import authenticate_credentials, create_access_token
2525
from .schemas import AuthenticatedUser, AuthenticationDetails, GoogleLoginData
@@ -164,23 +164,19 @@ async def authenticate_or_create_google_user(
164164
1. The default workspace name for Google users is f"{gmail}'s Workspace" (and the
165165
default username is the gmail, and the default role is ADMIN).
166166
2. Check if the user exists in `UserDB`.
167-
3. If the username already exists in `UserDB`, then the default workspace should
168-
have already been created.
169-
4. However, it is possible that another user also created a workspace using the
170-
same gmail. Thus, we have to check if the authenticating user exists in the
171-
workspace.
172-
5. If the authenticating user exists in the workspace, then we return the
173-
`AuthenticatedUser` model with the correct user's role in the workspace.
174-
6. Otherwise, we have a situation where someone else already created a workspace
175-
using the authenticated user's gmail and we raise an exception.
176-
7. If the user does not exist in `UserDB`, then this is the first time that the
167+
3. If the username already exists in `UserDB`, then a default workspace should
168+
have already been associated with the user.
169+
4. Check the user role in the workspace. If the authenticating user exists in the
170+
workspace, then we return the`AuthenticatedUser` model with the correct user's
171+
role in the workspace.
172+
5. If the user does not exist in `UserDB`, then this is the first time that the
177173
Google user is authenticating.
178-
8. We try to create the workspace using the default workspace name for the new
174+
6. We try to create the workspace using the default workspace name for the new
179175
user. If the default workspace name already exists, then we raise an exception.
180176
This corresponds to the situation where another user has already created a
181177
workspace under the same name and the Google user is signing in for the very
182178
time.
183-
9. Finally, we update the API limits for the new workspace, create the user in
179+
7. Finally, we update the API limits for the new workspace, create the user in
184180
`UserDB`, and assign the user to the workspace with the role of ADMIN.
185181
186182
Parameters
@@ -216,47 +212,30 @@ async def authenticate_or_create_google_user(
216212

217213
if user_db is not None:
218214
# 3.
219-
workspace_db = await get_workspace_by_workspace_name(
220-
asession=asession, workspace_name=workspace_name
215+
workspace_db = await get_user_default_workspace(
216+
asession=asession, user_db=user_db
221217
)
222218

223-
# 4
224-
user_exists_in_workspace = await check_if_user_exists_in_workspace(
225-
asession=asession,
226-
user_id=user_db.user_id,
227-
workspace_id=workspace_db.workspace_id,
219+
# 4.
220+
user_role = await get_user_role_in_workspace(
221+
asession=asession, user_db=user_db, workspace_db=workspace_db
228222
)
229-
230-
# 5
231-
if user_exists_in_workspace:
232-
user_role = await get_user_role_in_workspace(
233-
asession=asession, user_db=user_db, workspace_db=workspace_db
234-
)
235-
assert (
236-
user_role is not None and user_role in UserRoles
237-
), f"{user_role = }"
238-
return AuthenticatedUser(
239-
access_level="fullaccess",
240-
user_role=user_role,
241-
username=user_db.username,
242-
workspace_name=workspace_db.workspace_name,
243-
)
244-
245-
# 6
246-
raise HTTPException(
247-
status_code=status.HTTP_400_BAD_REQUEST,
248-
detail=f"Workspace for '{gmail}' already exists. Contact the admin of "
249-
f"that workspace to create an account for you.",
223+
assert user_role is not None and user_role in UserRoles, f"{user_role = }"
224+
return AuthenticatedUser(
225+
access_level="fullaccess",
226+
user_role=user_role,
227+
username=user_db.username,
228+
workspace_name=workspace_db.workspace_name,
250229
)
251230

252-
# 7.
231+
# 5.
253232
user = UserCreate(
254233
role=UserRoles.ADMIN, username=gmail, workspace_name=workspace_name
255234
)
256235
user_role = user.role
257236
assert user_role is not None and user_role in UserRoles
258237

259-
# 8.
238+
# 6.
260239
workspace_db, is_new_workspace = await create_workspace(
261240
api_daily_quota=DEFAULT_API_QUOTA,
262241
asession=asession,
@@ -270,7 +249,7 @@ async def authenticate_or_create_google_user(
270249
f"that workspace to create an account for you.",
271250
)
272251

273-
# 9.
252+
# 7.
274253
await update_api_limits(
275254
api_daily_quota=workspace_db.api_daily_quota,
276255
redis=request.app.state.redis,

0 commit comments

Comments
 (0)