Skip to content

Commit

Permalink
feat: Supports PasswordType
Browse files Browse the repository at this point in the history
  • Loading branch information
wzh425 committed Apr 9, 2024
1 parent cd018cf commit 0eda0e7
Show file tree
Hide file tree
Showing 12 changed files with 3,187 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<WarningsAsErrors>
$(WarningsAsErrors);CS8600;CS8601;CS8602;CS8603;CS8604;CS8609;CS8610;CS8614;CS8616;CS8618;CS8619;CS8622;CS8625
</WarningsAsErrors>
<MasaFrameworkPackageVersion>1.0.1-preview.16</MasaFrameworkPackageVersion>
<MasaFrameworkPackageVersion>1.0.1-preview.17</MasaFrameworkPackageVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ public class AddThirdPartyUserDto

public AddUserDto User { get; set; } = new();

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

public AddThirdPartyUserDto()
{

}

public AddThirdPartyUserDto(Guid thirdPartyIdpId, bool enabled, string thridPartyIdentity, string extendedData, AddUserDto user)
public AddThirdPartyUserDto(Guid thirdPartyIdpId, bool enabled, string thridPartyIdentity, string extendedData, AddUserDto user, Dictionary<string, string> claimData)
{
ThirdPartyIdpId = thirdPartyIdpId;
Enabled = enabled;
ThridPartyIdentity = thridPartyIdentity;
ExtendedData = extendedData;
User = user;
ClaimData = claimData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ public class AddUserDto

public List<SubjectPermissionRelationDto> Permissions { get; set; }

public PasswordTypes PasswordType { get; set; }

public AddUserDto()
{
Address = new();
Gender = GenderTypes.Male;
Roles = new();
Permissions = new();
PasswordType = PasswordTypes.MD5;
}

public AddUserDto(Guid id,string? name, string? displayName, string? avatar, string? idCard, string? companyName, bool enabled, string? phoneNumber, string? landline, string? email, AddressValueDto address, string? department, string? position, string? account, string? password, GenderTypes gender, List<Guid>? roles, List<SubjectPermissionRelationDto>? permissions)
public AddUserDto(Guid id,string? name, string? displayName, string? avatar, string? idCard, string? companyName, bool enabled, string? phoneNumber, string? landline, string? email, AddressValueDto address, string? department, string? position, string? account, string? password, GenderTypes gender, List<Guid>? roles, List<SubjectPermissionRelationDto>? permissions, PasswordTypes passwordType)
{
Id = id;
Name = name;
Expand All @@ -69,5 +72,6 @@ public AddUserDto(Guid id,string? name, string? displayName, string? avatar, str
Gender = gender;
Roles = roles ?? new();
Permissions = permissions ?? new();
PasswordType = passwordType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Masa.Auth.Contracts.Admin.Subjects;

public class UpsertThirdPartyUserDto : AddThirdPartyUserDto
{
public UpsertThirdPartyUserDto(Guid thirdPartyIdpId, bool enabled, string thridPartyIdentity, string extendedData, AddUserDto user) : base(thirdPartyIdpId, enabled, thridPartyIdentity, extendedData, user)
public UpsertThirdPartyUserDto(Guid thirdPartyIdpId, bool enabled, string thridPartyIdentity, string extendedData, AddUserDto user, Dictionary<string, string> claimData) : base(thirdPartyIdpId, enabled, thridPartyIdentity, extendedData, user, claimData)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public async Task UpsertThirdPartyUserExternalAsync(UpsertThirdPartyUserExternal
}
else
{
var addThirdPartyUserDto = new AddThirdPartyUserDto(identityProvider.Id, true, model.ThridPartyIdentity, JsonSerializer.Serialize(model.ExtendedData), command.ThirdPartyUser.Adapt<AddUserDto>());
var addThirdPartyUserDto = new AddThirdPartyUserDto(identityProvider.Id, true, model.ThridPartyIdentity, JsonSerializer.Serialize(model.ExtendedData), command.ThirdPartyUser.Adapt<AddUserDto>(), model.ClaimData);
command.Result = await _thirdPartyUserDomainService.AddThirdPartyUserAsync(addThirdPartyUserDto);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public string ExtendedData

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

public ThirdPartyUser(Guid thirdPartyIdpId, string thridPartyIdentity, string extendedData)
public ThirdPartyUser(Guid thirdPartyIdpId, string thridPartyIdentity, string extendedData, Dictionary<string, string>? claimData = null)
{
ThirdPartyIdpId = thirdPartyIdpId;
ThridPartyIdentity = thridPartyIdentity;
ExtendedData = extendedData;
ClaimData = claimData ?? new();
}

public ThirdPartyUser(Guid thirdPartyIdpId, Guid userId, string thridPartyIdentity, string extendedData)
: this(thirdPartyIdpId, thridPartyIdentity, extendedData)
public ThirdPartyUser(Guid thirdPartyIdpId, Guid userId, string thridPartyIdentity, string extendedData, Dictionary<string, string>? claimData = null): this(thirdPartyIdpId, thridPartyIdentity, extendedData, claimData)
{
UserId = userId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class User : FullAggregateRoot<Guid, Guid>
private string _idCard = "";
private string _account = "";
private string _password = "";
private PasswordType _passwordType = default!;
private string _companyName = "";
private string _department = "";
private string _position = "";
Expand Down Expand Up @@ -65,11 +66,18 @@ public string Password
private set
{
if (string.IsNullOrEmpty(value) is false)
_password = MD5Utils.EncryptRepeat(value);
_password = _passwordType.EncryptPassword(this, value);
else _password = "";
}
}

[AllowNull]
public PasswordType PasswordType
{
get => _passwordType;
private set => _passwordType = value ?? PasswordType.MD5;
}

[AllowNull]
public string CompanyName
{
Expand Down Expand Up @@ -157,8 +165,9 @@ public User(string? name,
string? password,
string? companyName,
string? email,
string phoneNumber) :
this(name, displayName, avatar, default, account, password, companyName, default, default, phoneNumber, default, email, GenderTypes.Male)
string phoneNumber,
PasswordType? passwordType = null) :
this(name, displayName, avatar, default, account, password, companyName, default, default, phoneNumber, default, email, GenderTypes.Male, passwordType)
{
}

Expand All @@ -170,8 +179,9 @@ public User(string? name,
string? companyName,
string? email,
string phoneNumber,
ThirdPartyUser thirdPartyUser) :
this(name, displayName, avatar, account, password, companyName, email, phoneNumber)
ThirdPartyUser thirdPartyUser,
PasswordType? passwordType = null) :
this(name, displayName, avatar, account, password, companyName, email, phoneNumber, passwordType)
{
_thirdPartyUsers.Add(thirdPartyUser);
}
Expand All @@ -185,8 +195,9 @@ public User(string? name,
string? email,
string phoneNumber,
ThirdPartyUser thirdPartyUser,
Staff staff) :
this(name, displayName, avatar, account, password, companyName, email, phoneNumber, thirdPartyUser)
Staff staff,
PasswordType? passwordType = null) :
this(name, displayName, avatar, account, password, companyName, email, phoneNumber, thirdPartyUser, passwordType)
{
_staff = staff;
}
Expand All @@ -199,8 +210,9 @@ public User(string? name,
string? companyName,
string? email,
string phoneNumber,
Staff staff) :
this(name, displayName, avatar, account, password, companyName, email, phoneNumber)
Staff staff,
PasswordType? passwordType = null) :
this(name, displayName, avatar, account, password, companyName, email, phoneNumber, passwordType)
{
_staff = staff;
}
Expand All @@ -219,7 +231,8 @@ public User(Guid id,
string? landline,
string? email,
AddressValue? address,
GenderTypes gender)
GenderTypes gender,
PasswordType? passwordType = null)
{
Id = id;
Name = name;
Expand All @@ -235,6 +248,7 @@ public User(Guid id,
var value = VerifyPhonNumberEmail(phoneNumber, email);
Account = string.IsNullOrEmpty(account) ? value : account;
DisplayName = string.IsNullOrEmpty(displayName) ? value : displayName;
PasswordType = passwordType ?? PasswordType.MD5;
}

public User(string? name,
Expand All @@ -249,7 +263,8 @@ public User(string? name,
string phoneNumber,
string? landline,
string? email,
GenderTypes gender)
GenderTypes gender,
PasswordType? passwordType = null)
: this(default,
name,
displayName,
Expand All @@ -264,7 +279,8 @@ public User(string? name,
landline,
email,
new(),
gender)
gender,
passwordType)
{
}

Expand Down Expand Up @@ -359,8 +375,7 @@ public bool VerifyPassword(string password)
{
if (string.IsNullOrWhiteSpace(Password) && string.IsNullOrWhiteSpace(password))
return true;

return Password == MD5Utils.EncryptRepeat(password ?? "");
return _passwordType.VerifyPassword(this, Password, password ?? "");
}

public void AddRoles(IEnumerable<Guid> roleIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public async Task<UserModel> AddThirdPartyUserAsync(AddThirdPartyUserDto dto)
var userDto = dto.User;
_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));
new ThirdPartyUser(dto.ThirdPartyIdpId, dto.ThridPartyIdentity, dto.ExtendedData, dto.ClaimData), Enumeration.FromValue<PasswordType>((int)userDto.PasswordType));
var (existUser, e) = await _userDomainService.VerifyRepeatAsync(userDto.PhoneNumber, userDto.Email, default, userDto.Account);
if (e != null)
{
_logger.LogError("User exception:{0}", e.Message);
}
if (existUser != null)
{
var thirdPartyUser = new ThirdPartyUser(dto.ThirdPartyIdpId, existUser.Id, dto.ThridPartyIdentity, dto.ExtendedData);
var thirdPartyUser = new ThirdPartyUser(dto.ThirdPartyIdpId, existUser.Id, dto.ThridPartyIdentity, dto.ExtendedData, dto.ClaimData);
await _thirdPartyUserRepository.AddAsync(thirdPartyUser);
return existUser.Adapt<UserModel>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void Configure(EntityTypeBuilder<User> builder)
builder.HasMany(u => u.ThirdPartyUsers).WithOne(tpu => tpu.User).HasForeignKey(tpu => tpu.UserId);
builder.HasMany(u => u.UserClaims).WithOne(uc => uc.User).HasForeignKey(uc => uc.UserId);
builder.HasIndex(u => new { u.CreationTime, u.ModificationTime });//.IsDescending(); supported 7.0
builder.Property(u => u.PasswordType).HasConversion(v => v.Id, v => Enumeration.FromValue<PasswordType>(v)).HasDefaultValue(PasswordType.MD5);
}
}

Loading

0 comments on commit 0eda0e7

Please sign in to comment.