@@ -209,6 +209,60 @@ async def add_study_authorization():
209209 return {"error" : f"{ type (e )} { str (e )} " }, 500
210210
211211
212+ @app .route ('/study/<path:study_id>' )
213+ async def authorize_study_for_users (study_id ):
214+ study_dict = await connexion .request .json ()
215+ study_dict ["study_id" ] = study_id
216+ service = "opa"
217+ if "X-Test-Mode" in connexion .request .headers and connexion .request .headers ["X-Test-Mode" ] == os .getenv ("TEST_KEY" ):
218+ service = "test"
219+ try :
220+ if auth .is_action_allowed_for_study (connexion .request , method = "POST" , path = f"/study/{ study_id } " , study = study_dict ["study_id" ]):
221+ # we need to check to see if the study even exists in the system
222+ all_studies , status_code = auth .list_studies (service = service )
223+ if status_code != 200 :
224+ return all_studies , status_code
225+ if study_dict ["study_id" ] not in all_studies :
226+ return {"error" : f"Study { study_dict ['study_id' ]} does not exist in { all_studies } " }
227+
228+ # for each user, look up user by email:
229+ user_emails = list (set (study_dict ["user_emails" ]))
230+ result = {"success" : [], "error" : []}
231+ study_auth = {
232+ "study_id" : study_dict ["study_id" ],
233+ "start_date" : study_dict ["start_date" ],
234+ "end_date" : study_dict ["end_date" ]
235+ }
236+ for user_email in user_emails :
237+ user_dict , status_code = auth .lookup_user_by_email (user_email )
238+ if status_code == 404 :
239+ # create a temp user
240+ user_dict = {"study_authorizations" : {}, "id" : user_email }
241+ user_dict ["study_authorizations" ][study_dict ["study_id" ]] = study_auth
242+ response , status_code = auth .write_user (user_dict , service = service )
243+ if status_code == 200 :
244+ result ["success" ].append (user_email )
245+ else :
246+ result ["error" ].append (f"failed to write auth for { user_email } : { response } " )
247+ else :
248+ # the result from lookup_user_by_email is an array, so grab the first thing:
249+ user_dict = user_dict [0 ]
250+ user_dict ["study_authorizations" ][study_dict ["study_id" ]] = study_auth
251+ response , status_code = auth .write_user (user_dict , service = service )
252+ if status_code == 200 :
253+ result ["success" ].append (user_email )
254+ else :
255+ result ["error" ].append (f"failed to write auth for { user_email } " )
256+ return result , 200
257+ return {"error" : "User is not authorized to authorize studies" }, 403
258+ except auth .UserTokenError as e :
259+ return {"error" : f"{ type (e )} { str (e )} " }, 401
260+ except auth .AuthzError as e :
261+ return {"error" : f"{ type (e )} { str (e )} " }, 403
262+ # except Exception as e:
263+ # return {"error": f"{type(e)} {str(e)}"}, 500
264+
265+
212266@app .route ('/study/<path:study_id>' )
213267def get_study_authorization (study_id ):
214268 service = "opa"
@@ -356,7 +410,7 @@ def lookup_user(email=None):
356410 # only add users that have a pcgl id:
357411 if "pcglid" in user :
358412 result .append (user ["pcglid" ])
359- return result , status_code
413+ return { " result" : result } , status_code
360414 return {"error" : "User is not authorized to look up users" }, 403
361415 except auth .UserTokenError as e :
362416 return {"error" : f"{ type (e )} { str (e )} " }, 401
@@ -394,6 +448,12 @@ async def reload_comanage():
394448 return {"error" : f"{ type (e )} { str (e )} " }, 403
395449 except Exception as e :
396450 return {"error" : f"{ type (e )} { str (e )} " }, 500
397- result , status_code = auth .reload_comanage ()
398- print (result )
399- return result , status_code
451+
452+ try :
453+ open ("/app/reload" , "x" )
454+ except FileExistsError :
455+ pass
456+ except Exception as e :
457+ return {"error" : f"couldn't reload: { type (e )} { str (e )} " }, 500
458+
459+ return {"status" : "reloading: should be complete in a minute or two" }, 200
0 commit comments