|
|
|
@ -8,200 +8,199 @@ using Microsoft.Extensions.Logging.Abstractions; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
using Volo.Abp.Uow; |
|
|
|
|
|
|
|
namespace Volo.Abp.Users |
|
|
|
namespace Volo.Abp.Users; |
|
|
|
|
|
|
|
public abstract class UserLookupService<TUser, TUserRepository> : IUserLookupService<TUser>, ITransientDependency |
|
|
|
where TUser : class, IUser |
|
|
|
where TUserRepository : IUserRepository<TUser> |
|
|
|
{ |
|
|
|
public abstract class UserLookupService<TUser, TUserRepository> : IUserLookupService<TUser>, ITransientDependency |
|
|
|
where TUser : class, IUser |
|
|
|
where TUserRepository : IUserRepository<TUser> |
|
|
|
protected bool SkipExternalLookupIfLocalUserExists { get; set; } = true; |
|
|
|
|
|
|
|
public IExternalUserLookupServiceProvider ExternalUserLookupServiceProvider { get; set; } |
|
|
|
public ILogger<UserLookupService<TUser, TUserRepository>> Logger { get; set; } |
|
|
|
|
|
|
|
private readonly TUserRepository _userRepository; |
|
|
|
private readonly IUnitOfWorkManager _unitOfWorkManager; |
|
|
|
|
|
|
|
protected UserLookupService( |
|
|
|
TUserRepository userRepository, |
|
|
|
IUnitOfWorkManager unitOfWorkManager) |
|
|
|
{ |
|
|
|
protected bool SkipExternalLookupIfLocalUserExists { get; set; } = true; |
|
|
|
_userRepository = userRepository; |
|
|
|
_unitOfWorkManager = unitOfWorkManager; |
|
|
|
|
|
|
|
public IExternalUserLookupServiceProvider ExternalUserLookupServiceProvider { get; set; } |
|
|
|
public ILogger<UserLookupService<TUser, TUserRepository>> Logger { get; set; } |
|
|
|
Logger = NullLogger<UserLookupService<TUser, TUserRepository>>.Instance; |
|
|
|
} |
|
|
|
|
|
|
|
private readonly TUserRepository _userRepository; |
|
|
|
private readonly IUnitOfWorkManager _unitOfWorkManager; |
|
|
|
public async Task<TUser> FindByIdAsync(Guid id, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var localUser = await _userRepository.FindAsync(id, cancellationToken: cancellationToken); |
|
|
|
|
|
|
|
protected UserLookupService( |
|
|
|
TUserRepository userRepository, |
|
|
|
IUnitOfWorkManager unitOfWorkManager) |
|
|
|
if (ExternalUserLookupServiceProvider == null) |
|
|
|
{ |
|
|
|
_userRepository = userRepository; |
|
|
|
_unitOfWorkManager = unitOfWorkManager; |
|
|
|
|
|
|
|
Logger = NullLogger<UserLookupService<TUser, TUserRepository>>.Instance; |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<TUser> FindByIdAsync(Guid id, CancellationToken cancellationToken = default) |
|
|
|
if (SkipExternalLookupIfLocalUserExists && localUser != null) |
|
|
|
{ |
|
|
|
var localUser = await _userRepository.FindAsync(id, cancellationToken: cancellationToken); |
|
|
|
|
|
|
|
if (ExternalUserLookupServiceProvider == null) |
|
|
|
{ |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
|
|
|
|
if (SkipExternalLookupIfLocalUserExists && localUser != null) |
|
|
|
{ |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
|
|
|
|
IUserData externalUser; |
|
|
|
IUserData externalUser; |
|
|
|
|
|
|
|
try |
|
|
|
try |
|
|
|
{ |
|
|
|
externalUser = await ExternalUserLookupServiceProvider.FindByIdAsync(id, cancellationToken); |
|
|
|
if (externalUser == null) |
|
|
|
{ |
|
|
|
externalUser = await ExternalUserLookupServiceProvider.FindByIdAsync(id, cancellationToken); |
|
|
|
if (externalUser == null) |
|
|
|
if (localUser != null) |
|
|
|
{ |
|
|
|
if (localUser != null) |
|
|
|
{ |
|
|
|
//TODO: Instead of deleting, should be make it inactive or something like that?
|
|
|
|
await WithNewUowAsync(() => _userRepository.DeleteAsync(localUser, cancellationToken: cancellationToken)); |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
//TODO: Instead of deleting, should be make it inactive or something like that?
|
|
|
|
await WithNewUowAsync(() => _userRepository.DeleteAsync(localUser, cancellationToken: cancellationToken)); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Logger.LogException(ex); |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
|
|
|
|
if (localUser == null) |
|
|
|
{ |
|
|
|
await WithNewUowAsync(() => _userRepository.InsertAsync(CreateUser(externalUser), cancellationToken: cancellationToken)); |
|
|
|
return await _userRepository.FindAsync(id, cancellationToken: cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
if (localUser is IUpdateUserData && ((IUpdateUserData)localUser).Update(externalUser)) |
|
|
|
{ |
|
|
|
await WithNewUowAsync(() => _userRepository.UpdateAsync(localUser, cancellationToken: cancellationToken)); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return localUser; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Logger.LogException(ex); |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
|
|
|
|
if (localUser == null) |
|
|
|
{ |
|
|
|
await WithNewUowAsync(() => _userRepository.InsertAsync(CreateUser(externalUser), cancellationToken: cancellationToken)); |
|
|
|
return await _userRepository.FindAsync(id, cancellationToken: cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<TUser> FindByUserNameAsync(string userName, CancellationToken cancellationToken = default) |
|
|
|
if (localUser is IUpdateUserData && ((IUpdateUserData)localUser).Update(externalUser)) |
|
|
|
{ |
|
|
|
await WithNewUowAsync(() => _userRepository.UpdateAsync(localUser, cancellationToken: cancellationToken)); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var localUser = await _userRepository.FindByUserNameAsync(userName, cancellationToken); |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
|
|
|
|
if (ExternalUserLookupServiceProvider == null) |
|
|
|
{ |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
return await _userRepository.FindAsync(id, cancellationToken: cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
if (SkipExternalLookupIfLocalUserExists && localUser != null) |
|
|
|
{ |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
public async Task<TUser> FindByUserNameAsync(string userName, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var localUser = await _userRepository.FindByUserNameAsync(userName, cancellationToken); |
|
|
|
|
|
|
|
IUserData externalUser; |
|
|
|
if (ExternalUserLookupServiceProvider == null) |
|
|
|
{ |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
externalUser = await ExternalUserLookupServiceProvider.FindByUserNameAsync(userName, cancellationToken); |
|
|
|
if (externalUser == null) |
|
|
|
{ |
|
|
|
if (localUser != null) |
|
|
|
{ |
|
|
|
//TODO: Instead of deleting, should be make it passive or something like that?
|
|
|
|
await WithNewUowAsync(() => _userRepository.DeleteAsync(localUser, cancellationToken: cancellationToken)); |
|
|
|
} |
|
|
|
if (SkipExternalLookupIfLocalUserExists && localUser != null) |
|
|
|
{ |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Logger.LogException(ex); |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
IUserData externalUser; |
|
|
|
|
|
|
|
if (localUser == null) |
|
|
|
try |
|
|
|
{ |
|
|
|
externalUser = await ExternalUserLookupServiceProvider.FindByUserNameAsync(userName, cancellationToken); |
|
|
|
if (externalUser == null) |
|
|
|
{ |
|
|
|
await WithNewUowAsync(() => _userRepository.InsertAsync(CreateUser(externalUser), cancellationToken: cancellationToken)); |
|
|
|
return await _userRepository.FindAsync(externalUser.Id, cancellationToken: cancellationToken); |
|
|
|
} |
|
|
|
if (localUser != null) |
|
|
|
{ |
|
|
|
//TODO: Instead of deleting, should be make it passive or something like that?
|
|
|
|
await WithNewUowAsync(() => _userRepository.DeleteAsync(localUser, cancellationToken: cancellationToken)); |
|
|
|
} |
|
|
|
|
|
|
|
if (localUser is IUpdateUserData && ((IUpdateUserData)localUser).Update(externalUser)) |
|
|
|
{ |
|
|
|
await WithNewUowAsync(() => _userRepository.UpdateAsync(localUser, cancellationToken: cancellationToken)); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return localUser; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Logger.LogException(ex); |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
|
|
|
|
if (localUser == null) |
|
|
|
{ |
|
|
|
await WithNewUowAsync(() => _userRepository.InsertAsync(CreateUser(externalUser), cancellationToken: cancellationToken)); |
|
|
|
return await _userRepository.FindAsync(externalUser.Id, cancellationToken: cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<List<IUserData>> SearchAsync( |
|
|
|
string sorting = null, |
|
|
|
string filter = null, |
|
|
|
int maxResultCount = int.MaxValue, |
|
|
|
int skipCount = 0, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
if (localUser is IUpdateUserData && ((IUpdateUserData)localUser).Update(externalUser)) |
|
|
|
{ |
|
|
|
if (ExternalUserLookupServiceProvider != null) |
|
|
|
{ |
|
|
|
return await ExternalUserLookupServiceProvider |
|
|
|
.SearchAsync( |
|
|
|
sorting, |
|
|
|
filter, |
|
|
|
maxResultCount, |
|
|
|
skipCount, |
|
|
|
cancellationToken |
|
|
|
); |
|
|
|
} |
|
|
|
await WithNewUowAsync(() => _userRepository.UpdateAsync(localUser, cancellationToken: cancellationToken)); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return localUser; |
|
|
|
} |
|
|
|
|
|
|
|
var localUsers = await _userRepository |
|
|
|
return await _userRepository.FindAsync(externalUser.Id, cancellationToken: cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<List<IUserData>> SearchAsync( |
|
|
|
string sorting = null, |
|
|
|
string filter = null, |
|
|
|
int maxResultCount = int.MaxValue, |
|
|
|
int skipCount = 0, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
if (ExternalUserLookupServiceProvider != null) |
|
|
|
{ |
|
|
|
return await ExternalUserLookupServiceProvider |
|
|
|
.SearchAsync( |
|
|
|
sorting, |
|
|
|
filter, |
|
|
|
maxResultCount, |
|
|
|
skipCount, |
|
|
|
filter, |
|
|
|
cancellationToken |
|
|
|
); |
|
|
|
|
|
|
|
return localUsers |
|
|
|
.Cast<IUserData>() |
|
|
|
.ToList(); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<long> GetCountAsync( |
|
|
|
string filter = null, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
if (ExternalUserLookupServiceProvider != null) |
|
|
|
{ |
|
|
|
return await ExternalUserLookupServiceProvider |
|
|
|
.GetCountAsync( |
|
|
|
filter, |
|
|
|
cancellationToken |
|
|
|
); |
|
|
|
} |
|
|
|
var localUsers = await _userRepository |
|
|
|
.SearchAsync( |
|
|
|
sorting, |
|
|
|
maxResultCount, |
|
|
|
skipCount, |
|
|
|
filter, |
|
|
|
cancellationToken |
|
|
|
); |
|
|
|
|
|
|
|
return localUsers |
|
|
|
.Cast<IUserData>() |
|
|
|
.ToList(); |
|
|
|
} |
|
|
|
|
|
|
|
return await _userRepository |
|
|
|
public async Task<long> GetCountAsync( |
|
|
|
string filter = null, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
if (ExternalUserLookupServiceProvider != null) |
|
|
|
{ |
|
|
|
return await ExternalUserLookupServiceProvider |
|
|
|
.GetCountAsync( |
|
|
|
filter, |
|
|
|
cancellationToken |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
protected abstract TUser CreateUser(IUserData externalUser); |
|
|
|
return await _userRepository |
|
|
|
.GetCountAsync( |
|
|
|
filter, |
|
|
|
cancellationToken |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
protected abstract TUser CreateUser(IUserData externalUser); |
|
|
|
|
|
|
|
private async Task WithNewUowAsync(Func<Task> func) |
|
|
|
private async Task WithNewUowAsync(Func<Task> func) |
|
|
|
{ |
|
|
|
using (var uow = _unitOfWorkManager.Begin(requiresNew: true)) |
|
|
|
{ |
|
|
|
using (var uow = _unitOfWorkManager.Begin(requiresNew: true)) |
|
|
|
{ |
|
|
|
await func(); |
|
|
|
await uow.CompleteAsync(); |
|
|
|
} |
|
|
|
await func(); |
|
|
|
await uow.CompleteAsync(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|