66 using IdentityServer4 . Extensions ;
77 using IdentityServer4 . Models ;
88 using IdentityServer4 . Services ;
9+ using LearningHub . Nhs . Auth . Configuration ;
910 using LearningHub . Nhs . Auth . Interfaces ;
1011 using Microsoft . Extensions . Logging ;
1112
1415 /// </summary>
1516 public class LearningHubProfileService : IProfileService
1617 {
18+ private readonly WebSettings webSettings ;
19+
1720 /// <summary>
1821 /// Initializes a new instance of the <see cref="LearningHubProfileService"/> class.
1922 /// </summary>
@@ -26,11 +29,15 @@ public class LearningHubProfileService : IProfileService
2629 /// <param name="logger">
2730 /// The logger.
2831 /// </param>
29- public LearningHubProfileService ( IUserService userService , IMoodleApiService moodleApiService , ILogger < LearningHubProfileService > logger )
32+ /// <param name="webSettings">
33+ /// The webSettings.
34+ /// </param>
35+ public LearningHubProfileService ( IUserService userService , IMoodleApiService moodleApiService , ILogger < LearningHubProfileService > logger , WebSettings webSettings )
3036 {
3137 this . UserService = userService ;
3238 this . MoodleApiService = moodleApiService ;
3339 this . Logger = logger ;
40+ this . webSettings = webSettings ;
3441 }
3542
3643 /// <summary>
@@ -62,8 +69,6 @@ public async Task GetProfileDataAsync(ProfileDataRequestContext context)
6269 if ( context != null )
6370 {
6471 var user = await this . UserService . GetBasicUserByUserIdAsync ( context . Subject . GetSubjectId ( ) ) ;
65- var moodleUser = await this . MoodleApiService . GetMoodleUserIdByUsernameAsync ( user . Id ) ;
66-
6772 var roleName = await this . UserService . GetUserRoleAsync ( user . Id ) ;
6873
6974 var claims = new List < Claim >
@@ -74,9 +79,14 @@ public async Task GetProfileDataAsync(ProfileDataRequestContext context)
7479 new Claim ( "family_name" , user . LastName ) ,
7580 new Claim ( "role" , roleName ) ,
7681 new Claim ( "elfh_userName" , user . UserName ) ,
77- new Claim ( "preferred_username" , moodleUser . ToString ( ) ) ,
7882 } ;
7983
84+ if ( this . webSettings . EnableMoodle )
85+ {
86+ var moodleUser = await this . MoodleApiService . GetMoodleUserIdByUsernameAsync ( user . Id ) ;
87+ claims . Add ( new Claim ( "preferred_username" , moodleUser . ToString ( ) ) ) ;
88+ }
89+
8090 if ( context . Subject . HasClaim ( "openAthensUser" , "true" ) )
8191 {
8292 claims . Add ( new Claim ( "openAthensUser" , "true" ) ) ;
0 commit comments