diff --git a/viewer/functions/common.py b/viewer/functions/common.py index 608b1f2e9754cfd192e07408da56f540de9c2ff3..9123f44cd7ea6f2643bf6d9ea280883ff9e6c6ab 100755 --- a/viewer/functions/common.py +++ b/viewer/functions/common.py @@ -440,6 +440,39 @@ def get_pd_user(): global pd_user return pd_user +def check_and_process(code1, code2, institution): + global pd_client + + if not get_pd_client(): + process_request( code1, code2 ) + + pd_client = get_pd_client() + + if not pd_client: + logger.error('Failed in ITkPD authentication!') + return False + + user = pd_client.get("getUser", json={"userIdentity": pd_client.user.identity}) + + if ( institution is not None and + not (any(element["name"] == institution for element in user["institutions"])) ): + logger.warning('Originally authenticated in ITkPD with user from different institute than in localDB! Updating this now with latest entered user codes.') + process_request(code1, code2) + + pd_client = get_pd_client() + + if not get_pd_client(): + logger.error('Failed in ITkPD authentication!') + return False + + user = pd_client.get("getUser", json={"userIdentity": pd_client.user.identity}) + + if ( not (any(element["name"] == institution for element in user["institutions"])) ): + logger.error( 'Failed in ITkPD authentication! ITkPD user institute still different than localDB!' ) + return False + + return True + ############################################################################# # Scan diff --git a/viewer/functions/imports.py b/viewer/functions/imports.py index 3798ed816578ac3a5b2ce3eaa737e15b84364f31..cbd72feaeb4d2f3db05d518ab89f0bc7c45a38b5 100755 --- a/viewer/functions/imports.py +++ b/viewer/functions/imports.py @@ -118,18 +118,12 @@ from module_qc_database_tools.utils import ( -def download_worker( serialNumber, code1 = None, code2 = None, resetStage = None, skip_attachments = False, skip_synched = True, debug_mode = False ): +def download_worker( serialNumber, code1 = None, code2 = None, resetStage = None, skip_attachments = False, skip_synched = True, debug_mode = False, institution = None ): global logger - - if not get_pd_client(): - process_request( code1, code2 ) - - pd_client = get_pd_client() - if not pd_client: - logger.error( 'Failed in ITkPD authentication!' ) - return + if code1 is not None and code2 is not None and not check_and_process(code1, code2, institution): return + pd_client = get_pd_client() logger.info( f'download_worker(): serialNumber = {serialNumber}' ) logger.info( f'download_worker(): resetStage = {resetStage}' ) @@ -151,19 +145,13 @@ def download_worker( serialNumber, code1 = None, code2 = None, resetStage = None return -def upload_worker( serialNumber, code1 = None, code2 = None ): +def upload_worker( serialNumber, code1 = None, code2 = None, institution = None ): global logger - if not get_pd_client(): - process_request( code1, code2 ) - + if code1 is not None and code2 is not None and not check_and_process(code1, code2, institution): return pd_client = get_pd_client() - if not pd_client: - logger.error( 'Failed in ITkPD authentication!' ) - return - logger.info( 'upload_worker(): serialNumber = {}'.format( serialNumber ) ) logger.info( 'upload_worker(): RecursiveUploader' ) @@ -174,7 +162,7 @@ def upload_worker( serialNumber, code1 = None, code2 = None ): RecursiveUploader( pd_client ).exec( serialNumber ) # step 2: sync FE configs - download_configs( serialNumber ) + download_configs( serialNumber, code1, code2, institution ) except Exception as e: logger.warning( traceback.format_exc() ) @@ -185,19 +173,13 @@ def upload_worker( serialNumber, code1 = None, code2 = None ): return -def download_configs( serialNumber ): +def download_configs( serialNumber, code1, code2, institution ): global logger - if not get_pd_client(): - process_request( code1, code2 ) - + if code1 is not None and code2 is not None and not check_and_process(code1, code2, institution): return pd_client = get_pd_client() - if not pd_client: - logger.error( 'Failed in ITkPD authentication!' ) - return - try: cpt_doc = localdb.component.find_one( { 'serialNumber': serialNumber } ) diff --git a/viewer/pages/qc.py b/viewer/pages/qc.py index 01035ebab0b48937fa9aee470826b06f44ddbe60..fc61d8400e6a43711b99111c1d38ae3ca5e830d4 100644 --- a/viewer/pages/qc.py +++ b/viewer/pages/qc.py @@ -154,7 +154,14 @@ def create_configs(): session['url'] = url_for( 'qc_api.create_configs', componentId = componentId ) logger.info( f'create_configs(): redirecting to itkdb_authenticate:' + url_for( 'user_api.itkdb_authenticate' ) ) return redirect( url_for( 'user_api.itkdb_authenticate' ) ) - + + user = pd_client.get("getUser", json={"userIdentity": pd_client.user.identity}) + if ( session["institution"] is not None and + not (any(element["name"] == session["institution"] for element in user["institutions"])) ): + session['url'] = url_for( 'qc_api.create_configs', componentId = componentId ) + logger.info( f'create_configs(): redirecting to itkdb_authenticate:' + url_for( 'user_api.itkdb_authenticate' ) ) + return redirect( url_for('user_api.itkdb_authenticate') ) + # Call config generation alg try: module = Module(pd_client, serialNumber) @@ -2402,8 +2409,9 @@ def result_transceiver(): code1 = request.form.get('code1', None) code2 = request.form.get('code2', None) + institution = session["institution"] - thread = threading.Thread( target = upload_worker, args = ( module, code1, code2 ) ) + thread = threading.Thread( target = upload_worker, args = ( module, code1, code2, institution ) ) thread.start() return redirect( url_for('component_api.show_component', id=doc['_id'], collection='component') ) diff --git a/viewer/pages/toppage.py b/viewer/pages/toppage.py index 11041be48b8db4d4234397b8a207f0009340478c..d7bb44c4e3c4726c78018cb9a931c79fff3c3ac9 100644 --- a/viewer/pages/toppage.py +++ b/viewer/pages/toppage.py @@ -499,7 +499,12 @@ def assemble_module(): return redirect( url_for('user_api.itkdb_authenticate') ) pd_client = get_pd_client() - userinfo = request.form.get("userinfo", type=itkdb.core.User) + + user = pd_client.get("getUser", json={"userIdentity": pd_client.user.identity}) + if ( session["institution"] is not None and + not (any(element["name"] == session["institution"] for element in user["institutions"])) ): + session['url'] = url_for('toppage_api.assemble_module') + return redirect( url_for('user_api.itkdb_authenticate') ) userdb = client.localdbtools diff --git a/viewer/pages/user.py b/viewer/pages/user.py index 2ba9c4c15aeecb36731664578063b268dc185974..53d1fcdd123dd6ac64b1d65151cd14cbc1be158d 100755 --- a/viewer/pages/user.py +++ b/viewer/pages/user.py @@ -240,7 +240,9 @@ def download_component(): elif stage == "submit": try: - thread = threading.Thread( target = download_worker, args = ( component_id, code1, code2, None, skip_attachments, skip_synched, debug_mode ) ) + institution = session["institution"] + + thread = threading.Thread( target = download_worker, args = ( component_id, code1, code2, None, skip_attachments, skip_synched, debug_mode, institution ) ) thread.start() time.sleep(3)