11
11
from ..database import get_sqlalchemy_async_engine
12
12
from ..users .models import (
13
13
UserNotFoundError ,
14
- check_if_user_exists_in_workspace ,
15
14
create_user_workspace_role ,
16
15
get_user_by_username ,
16
+ get_user_default_workspace ,
17
17
get_user_role_in_workspace ,
18
18
save_user_to_db ,
19
19
)
20
20
from ..users .schemas import UserCreate , UserRoles
21
21
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
23
23
from .config import NEXT_PUBLIC_GOOGLE_LOGIN_CLIENT_ID
24
24
from .dependencies import authenticate_credentials , create_access_token
25
25
from .schemas import AuthenticatedUser , AuthenticationDetails , GoogleLoginData
@@ -164,23 +164,19 @@ async def authenticate_or_create_google_user(
164
164
1. The default workspace name for Google users is f"{gmail}'s Workspace" (and the
165
165
default username is the gmail, and the default role is ADMIN).
166
166
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
177
173
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
179
175
user. If the default workspace name already exists, then we raise an exception.
180
176
This corresponds to the situation where another user has already created a
181
177
workspace under the same name and the Google user is signing in for the very
182
178
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
184
180
`UserDB`, and assign the user to the workspace with the role of ADMIN.
185
181
186
182
Parameters
@@ -216,47 +212,30 @@ async def authenticate_or_create_google_user(
216
212
217
213
if user_db is not None :
218
214
# 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
221
217
)
222
218
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
228
222
)
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 ,
250
229
)
251
230
252
- # 7 .
231
+ # 5 .
253
232
user = UserCreate (
254
233
role = UserRoles .ADMIN , username = gmail , workspace_name = workspace_name
255
234
)
256
235
user_role = user .role
257
236
assert user_role is not None and user_role in UserRoles
258
237
259
- # 8 .
238
+ # 6 .
260
239
workspace_db , is_new_workspace = await create_workspace (
261
240
api_daily_quota = DEFAULT_API_QUOTA ,
262
241
asession = asession ,
@@ -270,7 +249,7 @@ async def authenticate_or_create_google_user(
270
249
f"that workspace to create an account for you." ,
271
250
)
272
251
273
- # 9 .
252
+ # 7 .
274
253
await update_api_limits (
275
254
api_daily_quota = workspace_db .api_daily_quota ,
276
255
redis = request .app .state .redis ,
0 commit comments