Skip to content

Commit

Permalink
fix: Generate Staff during Ldap login (#1286)
Browse files Browse the repository at this point in the history
* fix: Domain account synchronization backend execution and logic adjustment

* close popups

* fix: Generate Staff during Ldap login
  • Loading branch information
wzh425 authored Apr 25, 2024
1 parent 99ac087 commit 8b28ce9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class AddThirdPartyUserDto

public Dictionary<string, string> ClaimData { get; set; } = new();

public bool IsLdap { get; set; }

public AddThirdPartyUserDto()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Masa.Auth.Service.Admin.Application.Subjects.Jobs;
public class SyncLdapUserJob : BackgroundJobBase<SyncLdapUserArgs>
{
private readonly LdapDomainService _ldapDomainService;

public SyncLdapUserJob(LdapDomainService ldapDomainService)
{
_ldapDomainService = ldapDomainService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ public async Task AddThirdPartyUserExternalAsync(AddThirdPartyUserExternalComman
var identityProvider = identityProviderQuery.Result;
var addThirdPartyUserDto = model.Adapt<AddThirdPartyUserDto>();
addThirdPartyUserDto.ThirdPartyIdpId = identityProvider.Id;

if (identityProvider.ThirdPartyIdpType == ThirdPartyIdpTypes.Ldap)
{
addThirdPartyUserDto.IsLdap = true;
}

var addThirdPartyUserCommand = new AddThirdPartyUserCommand(addThirdPartyUserDto, command.WhenExisReturn);
await _eventBus.PublishAsync(addThirdPartyUserCommand);
command.Result = addThirdPartyUserCommand.Result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ public async Task<UserModel> AddThirdPartyUserAsync(AddThirdPartyUserDto dto)
_logger.LogWarning("AddThirdPartyUserAsync user {0}", JsonSerializer.Serialize(userDto));
var user = new User(userDto.Name, userDto.DisplayName ?? "", userDto.Avatar, userDto.Account, userDto.Password, "", userDto.Email, userDto.PhoneNumber ?? "",
new ThirdPartyUser(dto.ThirdPartyIdpId, dto.ThridPartyIdentity, dto.ExtendedData, dto.ClaimData), Enumeration.FromValue<PasswordType>((int)userDto.PasswordType));

if (dto.IsLdap)
{
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var ldapUser = JsonSerializer.Deserialize<LdapUser>(dto.ExtendedData, options);
if (ldapUser != null)
{
var staff = new Staff(ldapUser.Name, ldapUser.DisplayName, "", "", ldapUser.Company, GenderTypes.Male, ldapUser.Phone, ldapUser.EmailAddress, GetRelativeId(ldapUser.ObjectSid), null, StaffTypes.Internal, true);
user.Bind(staff);
}

}
var (existUser, e) = await _userDomainService.VerifyRepeatAsync(userDto.PhoneNumber, userDto.Email, default, userDto.Account);
if (e != null)
{
Expand All @@ -42,11 +57,10 @@ public async Task<UserModel> AddThirdPartyUserAsync(AddThirdPartyUserDto dto)
await _userDomainService.AddAsync(user);
return user.Adapt<UserModel>();
}

public async Task<(ThirdPartyUser?, UserFriendlyException?)> VerifyRepeatAsync(Guid thirdPartyIdpId, string thridPartyIdentity)
{
var thirdPartyUser = await _authDbContext.Set<ThirdPartyUser>()
.Include(tpu => tpu.User)
.Include(tpu => tpu.User)
.ThenInclude(user => user.Roles)
.FirstOrDefaultAsync(tpu => tpu.ThirdPartyIdpId == thirdPartyIdpId && tpu.ThridPartyIdentity == thridPartyIdentity);
UserFriendlyException? exception = null;
Expand All @@ -56,4 +70,15 @@ public async Task<UserModel> AddThirdPartyUserAsync(AddThirdPartyUserDto dto)
}
return (thirdPartyUser, exception);
}

string GetRelativeId(string objectSid)
{
var parts = objectSid.Split('-');
if (parts.Length < 3)
{
return "";
}

return parts[parts.Length - 1];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ public async Task ValidateAsync(ExtensionGrantValidationContext context)
});
}

var claims = new List<Claim>();
foreach (var item in authUser.ClaimData)
{
claims.Add(new Claim(item.Key, item.Value));
}

context.Result = new GrantValidationResult(authUser.Id.ToString(), "ldap", claims);
context.Result = new GrantValidationResult(authUser.Id.ToString(), "ldap");
}
}

0 comments on commit 8b28ce9

Please sign in to comment.