Skip to content

Commit cf36964

Browse files
committed
Delay activation of guest user until the end
1 parent 65b4da7 commit cf36964

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

services/web/server/src/simcore_service_webserver/studies_access.py

+37-25
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232

3333
@lru_cache()
3434
def compose_uuid(template_uuid, user_id, query="") -> str:
35-
""" Creates a new uuid composing a project's and user ids such that
36-
any template pre-assigned to a user
35+
"""Creates a new uuid composing a project's and user ids such that
36+
any template pre-assigned to a user
3737
38-
Enforces a constraint: a user CANNOT have multiple copies of the same template
38+
Enforces a constraint: a user CANNOT have multiple copies of the same template
3939
"""
4040
new_uuid = str(
4141
uuid.uuid5(BASE_UUID, str(template_uuid) + str(user_id) + str(query))
@@ -46,7 +46,7 @@ def compose_uuid(template_uuid, user_id, query="") -> str:
4646
# TODO: from .projects import get_public_project
4747
async def get_public_project(app: web.Application, project_uuid: str):
4848
"""
49-
Returns project if project_uuid is a template and is marked as published, otherwise None
49+
Returns project if project_uuid is a template and is marked as published, otherwise None
5050
"""
5151
from .projects.projects_db import APP_PROJECT_DBAPI
5252

@@ -57,13 +57,12 @@ async def get_public_project(app: web.Application, project_uuid: str):
5757

5858
async def create_temporary_user(request: web.Request):
5959
"""
60-
TODO: user should have an expiration date and limited persmissions!
60+
TODO: user should have an expiration date and limited persmissions!
6161
"""
6262
from .login.cfg import get_storage
63-
from .login.handlers import ACTIVE, GUEST, ANONYMOUS
63+
from .login.handlers import ACTIVE, ANONYMOUS
6464
from .login.utils import get_client_ip, get_random_string
6565
from .security_api import encrypt_password
66-
from .resource_manager.websocket_manager import WebsocketRegistry
6766

6867
db = get_storage(request.app)
6968

@@ -72,7 +71,7 @@ async def create_temporary_user(request: web.Request):
7271
email = username + "@guest-at-osparc.io"
7372
password = get_random_string(min_len=12)
7473

75-
# creates a user that is marked as ANONYMOUS
74+
# creates a user that is marked as ANONYMOUS. see update_user_as_guest
7675
user = await db.create_user(
7776
{
7877
"name": username,
@@ -84,17 +83,27 @@ async def create_temporary_user(request: web.Request):
8483
}
8584
)
8685

87-
# creates an entry in the socket's registry to avoid garbage collector (GC) deleting it
88-
# See https://github.com/ITISFoundation/osparc-simcore/issues/1853
89-
registry = WebsocketRegistry(user["id"], f"{username}-guest-session", request.app)
90-
await registry.set_socket_id(f"{username}-guest-socket-id")
86+
return user
9187

92-
# Now that we know the ID, we set the user as GUEST
93-
# This extra step is to prevent the possibility that the GC is cleaning while
94-
# the function above is creating the entry in the socket
95-
await db.update_user(user, updates={"role": GUEST})
9688

97-
return user
89+
async def register_as_guest_user(user: Dict, app: web.Application):
90+
from .login.cfg import get_storage
91+
from .login.handlers import GUEST, ANONYMOUS
92+
from .resource_manager.websocket_manager import WebsocketRegistry
93+
94+
if user.get("role") == ANONYMOUS:
95+
db = get_storage(app)
96+
username = user["name"]
97+
98+
# creates an entry in the socket's registry to avoid garbage collector (GC) deleting it
99+
# See https://github.com/ITISFoundation/osparc-simcore/issues/1853
100+
registry = WebsocketRegistry(user["id"], f"{username}-guest-session", app)
101+
await registry.set_socket_id(f"{username}-guest-socket-id")
102+
103+
# Now that we know the ID, we set the user as GUEST
104+
# This extra step is to prevent the possibility that the GC is cleaning while
105+
# the function above is creating the entry in the socket
106+
await db.update_user(user, updates={"role": GUEST})
98107

99108

100109
# TODO: from .users import get_user?
@@ -113,10 +122,10 @@ async def copy_study_to_account(
113122
request: web.Request, template_project: Dict, user: Dict
114123
):
115124
"""
116-
Creates a copy of the study to a given project in user's account
125+
Creates a copy of the study to a given project in user's account
117126
118-
- Replaces template parameters by values passed in query
119-
- Avoids multiple copies of the same template on each account
127+
- Replaces template parameters by values passed in query
128+
- Avoids multiple copies of the same template on each account
120129
"""
121130
from .projects.projects_db import APP_PROJECT_DBAPI
122131
from .projects.projects_exceptions import ProjectNotFoundError
@@ -163,11 +172,11 @@ async def copy_study_to_account(
163172
# HANDLERS --------------------------------------------------------
164173
async def access_study(request: web.Request) -> web.Response:
165174
"""
166-
Handles requests to get and open a public study
175+
Handles requests to get and open a public study
167176
168-
- public studies are templates that are marked as published in the database
169-
- if user is not registered, it creates a temporary guest account with limited resources and expiration
170-
- this handler is NOT part of the API and therefore does NOT respond with json
177+
- public studies are templates that are marked as published in the database
178+
- if user is not registered, it creates a temporary guest account with limited resources and expiration
179+
- this handler is NOT part of the API and therefore does NOT respond with json
171180
"""
172181
# TODO: implement nice error-page.html
173182
project_id = request.match_info["id"]
@@ -231,6 +240,7 @@ async def access_study(request: web.Request) -> web.Response:
231240
log.debug("Auto login for anonymous user %s", user["name"])
232241
identity = user["email"]
233242
await remember(request, response, identity)
243+
await register_as_guest_user(user, request.app)
234244

235245
raise response
236246

@@ -250,7 +260,9 @@ def setup(app: web.Application):
250260

251261
# TODO: make sure that these routes are filtered properly in active middlewares
252262
app.router.add_routes(
253-
[web.get(r"/study/{id}", study_handler, name="study"),]
263+
[
264+
web.get(r"/study/{id}", study_handler, name="study"),
265+
]
254266
)
255267

256268
return True

0 commit comments

Comments
 (0)