2626using System . Linq ;
2727using System . Security . Claims ;
2828using System . Threading . Tasks ;
29+ using System . Transactions ;
2930
3031namespace Shesha . Authorization
3132{
@@ -41,13 +42,15 @@ public class TokenAuthController : SheshaControllerBase
4142 private readonly IRepository < ShaUserRegistration , Guid > _userRegistration ;
4243 private readonly IExternalAuthConfiguration _externalAuthConfiguration ;
4344 private readonly IExternalAuthManager _externalAuthManager ;
44- private readonly UserRegistrationManager _userRegistrationManager ;
45+ private readonly IUserRegistrationManager _userRegistrationManager ;
4546 private readonly IRepository < Person , Guid > _personRepository ;
4647 private readonly IRepository < MobileDevice , Guid > _mobileDeviceRepository ;
4748 private readonly ITokenBlacklistService _tokenBlacklistService ;
4849 private readonly UserManager < User > _userManager ;
4950 private readonly AbpUserClaimsPrincipalFactory < User , Role > _claimsPrincipalFactory ;
5051 private readonly IConfiguration _appConfiguration ;
52+ private readonly IRepository < User , long > _userRepository ;
53+
5154
5255 public TokenAuthController (
5356 LogInManager logInManager ,
@@ -56,14 +59,15 @@ public TokenAuthController(
5659 TokenAuthConfiguration configuration ,
5760 IExternalAuthConfiguration externalAuthConfiguration ,
5861 IExternalAuthManager externalAuthManager ,
59- UserRegistrationManager userRegistrationManager ,
62+ IUserRegistrationManager userRegistrationManager ,
6063 IRepository < Person , Guid > personRepository ,
6164 IRepository < ShaUserRegistration , Guid > userRegistration ,
6265 IRepository < MobileDevice , Guid > mobileDeviceRepository ,
6366 ITokenBlacklistService tokenBlacklistService ,
6467 UserManager < User > userManager ,
6568 AbpUserClaimsPrincipalFactory < User , Role > claimsPrincipalFactory ,
66- IConfiguration appConfiguration )
69+ IConfiguration appConfiguration ,
70+ IRepository < User , long > userRepository )
6771 {
6872 _logInManager = logInManager ;
6973 _tenantCache = tenantCache ;
@@ -79,6 +83,7 @@ public TokenAuthController(
7983 _userManager = userManager ;
8084 _claimsPrincipalFactory = claimsPrincipalFactory ;
8185 _appConfiguration = appConfiguration ;
86+ _userRepository = userRepository ;
8287 }
8388
8489 [ HttpPost ]
@@ -335,28 +340,33 @@ public async Task<ExternalAuthenticateResultModel> ExternalAuthenticateAsync([Fr
335340
336341 private async Task < User > RegisterExternalUserAsync ( ExternalAuthUserInfo externalUser )
337342 {
338- var user = await _userRegistrationManager . RegisterAsync (
339- externalUser . Name ,
340- externalUser . Surname ,
341- externalUser . EmailAddress ,
342- externalUser . EmailAddress ,
343- Authorization . Users . User . CreateRandomPassword ( ) ,
344- true
345- ) ;
346-
347- user . Logins = new List < UserLogin >
343+ using ( var uow = UnitOfWorkManager . Begin ( TransactionScopeOption . RequiresNew ) )
348344 {
349- new UserLogin
345+ var user = await _userRegistrationManager . RegisterAsync (
346+ externalUser . Name ,
347+ externalUser . Surname ,
348+ externalUser . EmailAddress ,
349+ externalUser . EmailAddress ,
350+ Authorization . Users . User . CreateRandomPassword ( ) ,
351+ true
352+ ) ;
353+
354+ if ( user . Logins == null )
355+ user . Logins = new List < UserLogin > ( ) ;
356+
357+ user . Logins . Add ( new UserLogin
350358 {
351359 LoginProvider = externalUser . Provider ,
352360 ProviderKey = externalUser . ProviderKey ,
353- TenantId = user . TenantId
354- }
355- } ;
361+ TenantId = user . TenantId ,
362+ UserId = user . Id
363+ } ) ;
356364
357- await CurrentUnitOfWork . SaveChangesAsync ( ) ;
365+ await _userRepository . UpdateAsync ( user ) ;
366+ await uow . CompleteAsync ( ) ;
358367
359- return user ;
368+ return user ;
369+ }
360370 }
361371
362372 private async Task < ExternalAuthUserInfo > GetExternalUserInfoAsync ( ExternalAuthenticateModel model )
@@ -392,7 +402,7 @@ private async Task<ShaLoginResult<User>> GetLoginResultAsync(string usernameOrEm
392402 }
393403 }
394404
395- private string CreateAccessToken ( IEnumerable < Claim > claims )
405+ private string CreateAccessToken ( IEnumerable < Claim > claims )
396406 {
397407 var validFrom = DateTime . UtcNow ;
398408 var expiresOn = validFrom . Add ( _configuration . Expiration ) ;
0 commit comments