Skip to content

Commit

Permalink
fix:thrid user bind
Browse files Browse the repository at this point in the history
  • Loading branch information
MayueCif committed Jul 18, 2023
1 parent 46ca744 commit 87e10ef
Show file tree
Hide file tree
Showing 51 changed files with 59 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -970,12 +970,17 @@ async Task BindVerifyAsync(RegisterThirdPartyUserModel model)
public async Task AddThirdPartyUserAsync(AddThirdPartyUserCommand command)
{
var thirdPartyUserDto = command.ThirdPartyUser;
var thirdPartyUser = await VerifyUserRepeatAsync(thirdPartyUserDto.ThirdPartyIdpId, thirdPartyUserDto.ThridPartyIdentity, command.WhenExisReturn);
if (thirdPartyUser is not null)
var (thirdPartyUser, exception) = await _thirdPartyUserDomainService.VerifyRepeatAsync(thirdPartyUserDto.ThirdPartyIdpId, thirdPartyUserDto.ThridPartyIdentity);

if (command.WhenExisReturn && thirdPartyUser != null)
{
command.Result = thirdPartyUser.User.Adapt<UserModel>();
return;
}
if (exception is not null)
{
throw exception;
}

command.Result = await _thirdPartyUserDomainService.AddThirdPartyUserAsync(thirdPartyUserDto);
}
Expand All @@ -993,7 +998,7 @@ public async Task UpsertThirdPartyUserExternalAsync(UpsertThirdPartyUserExternal
var identityProviderQuery = new IdentityProviderBySchemeQuery(model.Scheme);
await _eventBus.PublishAsync(identityProviderQuery);
var identityProvider = identityProviderQuery.Result;
var thirdPartyUser = await VerifyUserRepeatAsync(identityProvider.Id, model.ThridPartyIdentity, false);
var (thirdPartyUser, _) = await _thirdPartyUserDomainService.VerifyRepeatAsync(identityProvider.Id, model.ThridPartyIdentity);
if (thirdPartyUser is not null)
{
if (model.Id != default && thirdPartyUser.UserId != model.Id) throw new UserFriendlyException(errorCode: UserFriendlyExceptionCodes.USER_NOT_FOUND);
Expand All @@ -1019,7 +1024,11 @@ public async Task UpdateThirdPartyUserAsync(UpdateThirdPartyUserCommand command)
if (thirdPartyUser is null)
throw new UserFriendlyException(errorCode: UserFriendlyExceptionCodes.THIRD_PARTY_IDP_NOT_EXIST);

await VerifyUserRepeatAsync(thirdPartyUser.ThirdPartyIdpId, thirdPartyUserDto.ThridPartyIdentity);
var (_, exception) = await _thirdPartyUserDomainService.VerifyRepeatAsync(thirdPartyUser.ThirdPartyIdpId, thirdPartyUserDto.ThridPartyIdentity);
if (exception is not null)
{
throw exception;
}
thirdPartyUser.Update(thirdPartyUserDto.ThridPartyIdentity, thirdPartyUserDto.ExtendedData);
if (thirdPartyUserDto.Enabled)
{
Expand Down Expand Up @@ -1052,19 +1061,6 @@ public async Task RemoveThirdPartyUserAsync(RemoveThirdPartyUserCommand command)
await _thirdPartyUserRepository.RemoveAsync(tpu => tpu.ThirdPartyIdpId == command.ThirdPartyIdpId);
}

private async Task<ThirdPartyUser?> VerifyUserRepeatAsync(Guid thirdPartyIdpId, string thridPartyIdentity, bool throwException = true)
{
var thirdPartyUser = await _authDbContext.Set<ThirdPartyUser>()
.Include(tpu => tpu.User)
.ThenInclude(user => user.Roles)
.FirstOrDefaultAsync(tpu => tpu.ThirdPartyIdpId == thirdPartyIdpId && tpu.ThridPartyIdentity == thridPartyIdentity);
if (thirdPartyUser != null && throwException)
{
throw new UserFriendlyException(UserFriendlyExceptionCodes.THIRD_PARTY_USER_EXIST, thridPartyIdentity);
}
return thirdPartyUser;
}

#endregion

#region UserSystemData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ public async Task GetThirdPartyUserAsync(ThirdPartyUserQuery query)
{
var tpUser = await _authDbContext.Set<ThirdPartyUser>()
.Include(tpu => tpu.User)
.ThenInclude(user => new { user.Roles, user.Staff })
.Include(tpu => tpu.User.Staff)
.Include(tpu => tpu.User.Roles)
.FirstOrDefaultAsync(tpu => tpu.ThridPartyIdentity == query.ThridPartyIdentity);
var userModel = tpUser?.User?.Adapt<UserModel>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public ThirdPartyUser(Guid thirdPartyIdpId, bool enabled, string thridPartyIdent
ExtendedData = extendedData;
}

public ThirdPartyUser(Guid thirdPartyIdpId, Guid userId, bool enabled, string thridPartyIdentity, string extendedData)
: this(thirdPartyIdpId, enabled, thridPartyIdentity, extendedData)
{
UserId = userId;
}

public void Update(string extendedData)
{
ExtendedData = extendedData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,45 @@ public class ThirdPartyUserDomainService : DomainService
{
readonly AuthDbContext _authDbContext;
readonly UserDomainService _userDomainService;
readonly IThirdPartyUserRepository _thirdPartyUserRepository;

public ThirdPartyUserDomainService(UserDomainService userDomainService, AuthDbContext authDbContext)
public ThirdPartyUserDomainService(
UserDomainService userDomainService,
AuthDbContext authDbContext,
IThirdPartyUserRepository thirdPartyUserRepository)
{
_userDomainService = userDomainService;
_authDbContext = authDbContext;
_thirdPartyUserRepository = thirdPartyUserRepository;
}

public async Task<UserModel> AddThirdPartyUserAsync(AddThirdPartyUserDto dto)
{
var userDto = dto.User;
var user = new User(userDto.Name, userDto.DisplayName ?? "", userDto.Avatar, userDto.Account, userDto.Password, "", userDto.Email, userDto.PhoneNumber ?? "",
new ThirdPartyUser(dto.ThirdPartyIdpId, true, dto.ThridPartyIdentity, dto.ExtendedData));
var (existUser, _) = await _userDomainService.VerifyRepeatAsync(userDto.PhoneNumber, userDto.Email, default, userDto.Account);
if (existUser != null)
{
var thirdPartyUser = new ThirdPartyUser(dto.ThirdPartyIdpId, existUser.Id, true, dto.ThridPartyIdentity, dto.ExtendedData);
await _thirdPartyUserRepository.AddAsync(thirdPartyUser);
return existUser.Adapt<UserModel>();
}
await _userDomainService.AddAsync(user);
return user.Adapt<UserModel>();
}

public async Task<ThirdPartyUser?> VerifyRepeatAsync(Guid thirdPartyIdpId, string thridPartyIdentity)
public async Task<(ThirdPartyUser?, UserFriendlyException?)> VerifyRepeatAsync(Guid thirdPartyIdpId, string thridPartyIdentity)
{
var thirdPartyUser = await _authDbContext.Set<ThirdPartyUser>()
.Include(tpu => tpu.User)
.ThenInclude(user => user.Roles)
.FirstOrDefaultAsync(tpu => tpu.ThirdPartyIdpId == thirdPartyIdpId && tpu.ThridPartyIdentity == thridPartyIdentity);
return thirdPartyUser;
UserFriendlyException? exception = null;
if (thirdPartyUser != null)
{
exception = new UserFriendlyException(UserFriendlyExceptionCodes.THIRD_PARTY_USER_EXIST, thridPartyIdentity);
}
return (thirdPartyUser, exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,6 @@ private async Task RemoveAsync(
{
await eventBus.PublishAsync(new RemoveThirdPartyIdpCommand(dto));
}

private async Task<UserModel> AddThirdPartyUserAsync(
IEventBus eventBus,
[FromBody] AddThirdPartyUserModel user,
[FromQuery] bool whenExistReturn)
{
var command = new AddThirdPartyUserExternalCommand(user, whenExistReturn);
await eventBus.PublishAsync(command);
return command.Result;
}
#endregion

private async Task LdapSaveAsync(IEventBus eventBus, [FromBody] LdapDetailDto ldapDetailDto)
Expand Down
42 changes: 0 additions & 42 deletions src/Web/Masa.Auth.Web.Admin.Rcl/Global/Config/GlobalConfig.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,10 @@ private Task OnParameterChangedAsync()

public async Task GetOperationLogsAsync()
{
Loading = true;
var request = new GetOperationLogsDto(Page, PageSize, UserId, _startTime?.UtcDateTime, _endTime?.UtcDateTime, OperationType, Search);
var response = await OperationLogService.GetListAsync(request);
_operationLogs = response.Items;
_operationLogs.ForEach(operationLog => operationLog.OperationTime = operationLog.OperationTime.Add(JsInitVariables.TimezoneOffset));
_total = response.Total;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ public async Task AddPositionAsync(FormContext context)
var success = context.Validate();
if (success)
{
Loading = true;
await PositionService.AddAsync(Position);
OpenSuccessMessage(T("Add position success"));
await UpdateVisible(false);
await OnSubmitSuccess.InvokeAsync();
Loading = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public int Page
{
_oldPage = value;
InvokeAsync(StateHasChanged);
});
});
}
}

Expand Down Expand Up @@ -74,12 +74,10 @@ protected override async Task OnInitializedAsync()

public async Task GetPositionsAsync()
{
Loading = true;
var reuquest = new GetPositionsDto(Page, PageSize, Search);
var response = await PositionService.GetListAsync(reuquest);
Positions = response.Items;
Total = response.Total;
Loading = false;
}

public void OpenAddApiResourceDialog()
Expand All @@ -101,11 +99,9 @@ public async Task OpenRemovePositionDialog(PositionDto position)

public async Task RemovePositionAsync(Guid positionId)
{
Loading = true;
await PositionService.RemoveAsync(positionId);
OpenSuccessMessage(T("Delete position data success"));
await GetPositionsAsync();
Loading = false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ public async Task UpdatetPositionAsync(FormContext context)
var success = context.Validate();
if (success)
{
Loading = true;
await PositionService.UpdateAsync(Position);
OpenSuccessMessage(T("Edit position data success"));
await UpdateVisible(false);
await OnSubmitSuccess.InvokeAsync();
Loading = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ public async Task AddRoleAsync(FormContext context)
var success = context.Validate();
if (success)
{
Loading = true;
await RoleService.AddAsync(Role);
OpenSuccessMessage(T("Add role data success"));
await UpdateVisible(false);
await OnSubmitSuccess.InvokeAsync();
Loading = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@ protected override async Task OnInitializedAsync()

public async Task GetRolesAsync()
{
Loading = true;
var reuquest = new GetRolesDto(Page, PageSize, Search, Enabled);
var response = await RoleService.GetListAsync(reuquest);
Roles = response.Items;
Total = response.Total;
Loading = false;
}

public void OpenAddRoleDialog()
Expand All @@ -113,11 +111,9 @@ public async Task OpenRemoveRoleDialog(RoleDto role)

public async Task RemoveRoleAsync(Guid roleId)
{
Loading = true;
await RoleService.RemoveAsync(roleId);
OpenSuccessMessage(T("Delete role data success"));
await GetRolesAsync();
Loading = false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Masa.Auth.Web.Admin.Rcl.Pages.RolePermissions.Roles;
public partial class UpdateRoleDialog
{
private List<Guid> _roles = new();

[Parameter]
public bool Visible { get; set; }

Expand Down Expand Up @@ -65,12 +65,10 @@ public async Task UpdateRoleAsync(FormContext context)
var success = context.Validate();
if (success)
{
Loading = true;
await RoleService.UpdateAsync(Role);
OpenSuccessMessage(T("Edit role data success"));
await UpdateVisible(false);
await OnSubmitSuccess.InvokeAsync();
Loading = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ public async Task AddApiResourceAsync(FormContext context)
var success = context.Validate();
if (success)
{
Loading = true;
await ApiResourceService.AddAsync(ApiResource);
OpenSuccessMessage(T("Add apiResource success"));
await UpdateVisible(false);
await OnSubmitSuccess.InvokeAsync();
Loading = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ protected override async Task OnInitializedAsync()

public async Task GetApiResourcesAsync()
{
Loading = true;
var reuquest = new GetApiResourcesDto(Page, PageSize, Search);
var response = await ApiResourceService.GetListAsync(reuquest);
ApiResources = response.Items;
Total = response.Total;
Loading = false;
}

public void OpenAddApiResourceDialog()
Expand All @@ -97,11 +95,9 @@ public async Task OpenRemoveApiResourceDialog(ApiResourceDto apiResource)

public async Task RemoveApiResourceAsync(Guid apiResourceId)
{
Loading = true;
await ApiResourceService.RemoveAsync(apiResourceId);
OpenSuccessMessage(T("Delete apiResource data success"));
await GetApiResourcesAsync();
Loading = false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ public async Task UpdatetApiResourceAsync(FormContext context)
var success = context.Validate();
if (success)
{
Loading = true;
await ApiResourceService.UpdateAsync(ApiResource);
OpenSuccessMessage(T("Edit apiResource data success"));
await UpdateVisible(false);
await OnSubmitSuccess.InvokeAsync();
Loading = false;
}
}
}
Loading

0 comments on commit 87e10ef

Please sign in to comment.