@@ -27,6 +27,14 @@ public function redcap_every_page_top($project_id)
2727 return ;
2828 }
2929
30+ // Note: This handles the survey condition as well, since it shares the same $page
31+ $ initialized = $ this ->initializeCas ();
32+ if ( $ initialized === false ) {
33+ $ this ->logCas ('CAS Authenticator: Error initializing CAS ' );
34+ $ this ->framework ->exitAfterHook ();
35+ return ;
36+ }
37+
3038 $ dashboard_hash = filter_input (INPUT_GET , '__dashboard ' , FILTER_SANITIZE_STRING );
3139 $ report_hash = filter_input (INPUT_GET , '__report ' , FILTER_SANITIZE_STRING );
3240
@@ -66,7 +74,13 @@ public function redcap_survey_page_top(
6674 ]);
6775
6876 try {
69- $ id = $ this ->authenticate ();
77+ $ alreadyAuthenticated = $ this ->isAuthenticated ();
78+ if ( $ alreadyAuthenticated ) {
79+ $ this ->framework ->log ('CAS Authenticator: Already authenticated ' );
80+ $ id = $ this ->getAuthenticatedUser ();
81+ } else {
82+ $ id = $ this ->authenticate ();
83+ }
7084 } catch ( \CAS_GracefullTerminationException $ e ) {
7185 if ( $ e ->getCode () !== 0 ) {
7286 $ this ->framework ->log ('CAS Authenticator: Error getting code ' , [ 'error ' => $ e ->getMessage () ]);
@@ -82,12 +96,21 @@ public function redcap_survey_page_top(
8296 }
8397
8498 // Successful authentication
85- $ this ->casLog ('CAS Authenticator: Survey Auth Succeeded ' , [
86- "CASAuthenticator_NetId " => $ id ,
87- "instrument " => $ instrument ,
88- "event_id " => $ event_id ,
89- "response_id " => $ response_id
90- ]);
99+ if ( !$ alreadyAuthenticated ) {
100+ $ this ->casLog ('CAS Authenticator: Survey Auth Succeeded ' , [
101+ "CASAuthenticator_NetId " => $ id ,
102+ "instrument " => $ instrument ,
103+ "event_id " => $ event_id ,
104+ "response_id " => $ response_id
105+ ]);
106+ } else {
107+ $ this ->framework ->log ('CAS Authenticator: Already authenticated ' , [
108+ "CASAuthenticator_NetId " => $ id ,
109+ "instrument " => $ instrument ,
110+ "event_id " => $ event_id ,
111+ "response_id " => $ response_id
112+ ]);
113+ }
91114
92115 $ field = $ projectSettings ["id-field " ][$ index ];
93116
@@ -312,13 +335,7 @@ private function getReports($pid)
312335 return $ reports ;
313336 }
314337
315- /**
316- * Initiate CAS authentication
317- *
318- *
319- * @return string|boolean username of authenticated user (false if not authenticated)
320- */
321- private function authenticate ()
338+ private function initializeCas ()
322339 {
323340 try {
324341
@@ -349,18 +366,57 @@ private function authenticate()
349366
350367 // Don't exit, let me handle instead
351368 \CAS_GracefullTerminationException::throwInsteadOfExiting ();
369+ return true ;
370+ } catch ( \Throwable $ e ) {
371+ $ this ->log ('CAS Authenticator: Error initializing CAS ' , [ 'error ' => $ e ->getMessage () ]);
372+ return false ;
373+ }
374+ }
352375
376+ /**
377+ * Initiate CAS authentication
378+ *
379+ *
380+ * @return string|boolean username of authenticated user (false if not authenticated)
381+ */
382+ private function authenticate ()
383+ {
384+ try {
353385 // force CAS authentication
354386 \phpCAS::forceAuthentication ();
355387
356388 // Return authenticated username
357389 return \phpCAS::getUser ();
358390 } catch ( \Throwable $ e ) {
359- $ this ->log ('CAS Authenticator: Error authenticating ' , [ 'error ' => $ e ->getMessage () ]);
391+ $ this ->framework -> log ('CAS Authenticator: Error authenticating ' , [ 'error ' => $ e ->getMessage () ]);
360392 return false ;
361393 }
362394 }
363395
396+ private function isAuthenticated ()
397+ {
398+ try {
399+ $ authenticated = \phpCAS::isAuthenticated ();
400+ } catch ( \Throwable $ e ) {
401+ $ this ->framework ->log ('CAS Authenticator: Error checking authentication ' , [ 'error ' => $ e ->getMessage () ]);
402+ $ authenticated = false ;
403+ } finally {
404+ return $ authenticated ;
405+ }
406+ }
407+
408+ private function getAuthenticatedUser ()
409+ {
410+ try {
411+ $ user = \phpCAS::getUser ();
412+ } catch ( \Throwable $ e ) {
413+ $ this ->framework ->log ('CAS Authenticator: Error getting authenticated user ' , [ 'error ' => $ e ->getMessage () ]);
414+ $ user = false ;
415+ } finally {
416+ return $ user ;
417+ }
418+ }
419+
364420
365421 /**
366422 * Get url to file with provided edoc ID.
0 commit comments