Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface IUserService
Task<bool> ResetUserPassword(User user);
Task<bool> ModifyUserEmail(string email);
Task<bool> ModifyUserPhone(string phone);
Task<bool> UpdatePassword(string newPassword, string verificationCode);
}
49 changes: 39 additions & 10 deletions src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ record = user;
return record;
}

public async Task<bool> UpdatePassword(string password, string verificationCode)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = db.GetUserByUserName(_user.UserName);

if (record == null)
{
return false;
}

if (record.VerificationCode != verificationCode)
{
return false;
}

var newPassword = Utilities.HashTextMd5($"{password}{record.Salt}");

db.UpdateUserPassword(record.Id, newPassword);
return true;
}

public async Task<Token?> GetToken(string authorization)
{
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));
Expand Down Expand Up @@ -323,24 +344,32 @@ public async Task<bool> VerifyEmailExisting(string email)

public async Task<bool> SendVerificationCodeResetPassword(User user)
{
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
{
return false;
}

var db = _services.GetRequiredService<IBotSharpRepository>();

User? record = null;

if (!string.IsNullOrEmpty(user.Email))
if (!string.IsNullOrWhiteSpace(_user.Id))
{
record = db.GetUserByEmail(user.Email);
record = db.GetUserById(_user.Id);
}

if (!string.IsNullOrEmpty(user.Phone))
else
{
record = db.GetUserByPhone(user.Phone);
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
{
return false;
}

if (!string.IsNullOrEmpty(user.Email))
{
record = db.GetUserByEmail(user.Email);
}

if (!string.IsNullOrEmpty(user.Phone))
{
record = db.GetUserByPhone(user.Phone);
}
}

if (record == null)
{
return false;
Expand Down
10 changes: 10 additions & 0 deletions src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ public async Task<bool> ResetUserPassword([FromBody] UserResetPasswordModel user
return await _userService.ResetUserPassword(user.ToUser());
}

[HttpPost("/user/updatepassword")]
public async Task<bool> UpdatePassword([FromBody] User user)
{
if (string.IsNullOrWhiteSpace(user.Password) || string.IsNullOrWhiteSpace(user.VerificationCode))
{
return false;
}
return await _userService.UpdatePassword(user.Password, user.VerificationCode);
}

[HttpPost("/user/email/modify")]
public async Task<bool> ModifyUserEmail([FromQuery] string email)
{
Expand Down