@@ -159,17 +159,26 @@ sub get_credentials ($self) {
159159 return $value ;
160160 };
161161
162- if (my $user_id = $extract_claim -> ($ce -> {LTI }{v1p3 }{preferred_source_of_username })) {
163- $user_id_source = $ce -> {LTI }{v1p3 }{preferred_source_of_username };
164- $type_of_source = ' preferred_source_of_username' ;
165- $self -> {user_id } = $user_id ;
166- }
162+ # First check if there is a user with the current LMS user id saved in the lis_source_did column.
163+ if ($claims -> {sub } && (my $user = ($c -> db-> getUsersWhere({ lis_source_did => $claims -> {sub } }))[0])) {
164+ $user_id_source = ' database' ;
165+ $type_of_source = ' existing database user' ;
166+ $self -> {user_id } = $user -> user_id;
167+ } else {
168+ if (my $user_id = $extract_claim -> ($ce -> {LTI }{v1p3 }{preferred_source_of_username })) {
169+ $user_id_source = $ce -> {LTI }{v1p3 }{preferred_source_of_username };
170+ $type_of_source = " $user_id_source which was preferred_source_of_username" ;
171+ $self -> {user_id } = $user_id ;
172+ }
167173
168- # Fallback if necessary
169- if (!defined $self -> {user_id } && (my $user_id = $extract_claim -> ($ce -> {LTI }{v1p3 }{fallback_source_of_username }))) {
170- $user_id_source = $ce -> {LTI }{v1p3 }{fallback_source_of_username };
171- $type_of_source = ' fallback_source_of_username' ;
172- $self -> {user_id } = $user_id ;
174+ # Fallback if necessary
175+ if (!defined $self -> {user_id }
176+ && (my $user_id = $extract_claim -> ($ce -> {LTI }{v1p3 }{fallback_source_of_username })))
177+ {
178+ $user_id_source = $ce -> {LTI }{v1p3 }{fallback_source_of_username };
179+ $type_of_source = " $user_id_source which was fallback_source_of_username" ;
180+ $self -> {user_id } = $user_id ;
181+ }
173182 }
174183
175184 if ($self -> {user_id }) {
@@ -196,7 +205,7 @@ sub get_credentials ($self) {
196205 # For setting up it is helpful to print out what is believed to be the user id and address is at this point.
197206 if ($ce -> {debug_lti_parameters }) {
198207 warn " =========== SUMMARY ============\n " ;
199- warn " User id is |$self ->{user_id}| (obtained from $user_id_source which was $ type_of_source )\n " ;
208+ warn " User id is |$self ->{user_id}| (obtained from $type_of_source )\n " ;
200209 warn " User email address is |$self ->{email}|\n " ;
201210 warn " strip_domain_from_email is |" , $ce -> {LTI }{v1p3 }{strip_domain_from_email } // 0, " |\n " ;
202211 warn " Student id is |$self ->{student_id}|\n " ;
@@ -419,6 +428,7 @@ sub create_user ($self) {
419428 $newUser -> recitation($self -> {recitation } // ' ' );
420429 $newUser -> comment(formatDateTime(time , 0, $ce -> {siteDefaults }{timezone }, $ce -> {language }));
421430 $newUser -> student_id($self -> {student_id } // ' ' );
431+ $newUser -> lis_source_did($c -> stash-> {lti_lms_user_id }) if $c -> stash-> {lti_lms_user_id };
422432
423433 # Allow sites to customize the user.
424434 $ce -> {LTI }{v1p3 }{modify_user }($self , $newUser ) if ref ($ce -> {LTI }{v1p3 }{modify_user }) eq ' CODE' ;
@@ -508,6 +518,7 @@ sub maybe_update_user ($self) {
508518 $tempUser -> section($self -> {section } // ' ' );
509519 $tempUser -> recitation($self -> {recitation } // ' ' );
510520 $tempUser -> student_id($self -> {student_id } // ' ' );
521+ $tempUser -> lis_source_did($c -> stash-> {lti_lms_user_id }) if $c -> stash-> {lti_lms_user_id };
511522
512523 # Allow sites to customize the temp user
513524 $ce -> {LTI }{v1p3 }{modify_user }($self , $tempUser ) if ref ($ce -> {LTI }{v1p3 }{modify_user }) eq ' CODE' ;
0 commit comments