32
32
33
33
@lru_cache ()
34
34
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
37
37
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
39
39
"""
40
40
new_uuid = str (
41
41
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:
46
46
# TODO: from .projects import get_public_project
47
47
async def get_public_project (app : web .Application , project_uuid : str ):
48
48
"""
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
50
50
"""
51
51
from .projects .projects_db import APP_PROJECT_DBAPI
52
52
@@ -57,13 +57,12 @@ async def get_public_project(app: web.Application, project_uuid: str):
57
57
58
58
async def create_temporary_user (request : web .Request ):
59
59
"""
60
- TODO: user should have an expiration date and limited persmissions!
60
+ TODO: user should have an expiration date and limited persmissions!
61
61
"""
62
62
from .login .cfg import get_storage
63
- from .login .handlers import ACTIVE , GUEST , ANONYMOUS
63
+ from .login .handlers import ACTIVE , ANONYMOUS
64
64
from .login .utils import get_client_ip , get_random_string
65
65
from .security_api import encrypt_password
66
- from .resource_manager .websocket_manager import WebsocketRegistry
67
66
68
67
db = get_storage (request .app )
69
68
@@ -72,7 +71,7 @@ async def create_temporary_user(request: web.Request):
72
71
email = username + "@guest-at-osparc.io"
73
72
password = get_random_string (min_len = 12 )
74
73
75
- # creates a user that is marked as ANONYMOUS
74
+ # creates a user that is marked as ANONYMOUS. see update_user_as_guest
76
75
user = await db .create_user (
77
76
{
78
77
"name" : username ,
@@ -84,17 +83,27 @@ async def create_temporary_user(request: web.Request):
84
83
}
85
84
)
86
85
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
91
87
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 })
96
88
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 })
98
107
99
108
100
109
# TODO: from .users import get_user?
@@ -113,10 +122,10 @@ async def copy_study_to_account(
113
122
request : web .Request , template_project : Dict , user : Dict
114
123
):
115
124
"""
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
117
126
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
120
129
"""
121
130
from .projects .projects_db import APP_PROJECT_DBAPI
122
131
from .projects .projects_exceptions import ProjectNotFoundError
@@ -163,11 +172,11 @@ async def copy_study_to_account(
163
172
# HANDLERS --------------------------------------------------------
164
173
async def access_study (request : web .Request ) -> web .Response :
165
174
"""
166
- Handles requests to get and open a public study
175
+ Handles requests to get and open a public study
167
176
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
171
180
"""
172
181
# TODO: implement nice error-page.html
173
182
project_id = request .match_info ["id" ]
@@ -231,6 +240,7 @@ async def access_study(request: web.Request) -> web.Response:
231
240
log .debug ("Auto login for anonymous user %s" , user ["name" ])
232
241
identity = user ["email" ]
233
242
await remember (request , response , identity )
243
+ await register_as_guest_user (user , request .app )
234
244
235
245
raise response
236
246
@@ -250,7 +260,9 @@ def setup(app: web.Application):
250
260
251
261
# TODO: make sure that these routes are filtered properly in active middlewares
252
262
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
+ ]
254
266
)
255
267
256
268
return True
0 commit comments