added: 管理员密码修改

This commit is contained in:
none 2023-03-02 15:46:57 +08:00
parent 8285daccc8
commit aa6bb1d845
4 changed files with 48 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import xyz.playedu.api.event.AdminUserLoginEvent;
import xyz.playedu.api.exception.JwtLogoutException;
import xyz.playedu.api.middleware.ImageCaptchaCheckMiddleware;
import xyz.playedu.api.request.backend.LoginRequest;
import xyz.playedu.api.request.backend.PasswordChangeRequest;
import xyz.playedu.api.service.AdminUserService;
import xyz.playedu.api.service.JWTService;
import xyz.playedu.api.types.JsonResponse;
@ -85,4 +86,15 @@ public class LoginController {
return JsonResponse.data(data);
}
@PutMapping("/password")
public JsonResponse changePassword(@RequestBody @Validated PasswordChangeRequest req) {
AdminUser user = PlayEduBackendThreadLocal.getAdminUser();
String password = HelperUtil.MD5(req.getOldPassword() + user.getSalt());
if (!password.equals(user.getPassword())) {
return JsonResponse.error("原密码不正确");
}
adminUserService.passwordChange(user, req.getNewPassword());
return JsonResponse.success();
}
}

View File

@ -0,0 +1,25 @@
package xyz.playedu.api.request.backend;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
/**
* @Author 杭州白书科技有限公司
* @create 2023/3/2 15:41
*/
@Data
public class PasswordChangeRequest {
@JsonProperty("old_password")
@NotNull(message = "old_password参数不存在")
@NotBlank(message = "请输入原密码")
private String oldPassword;
@JsonProperty("new_password")
@NotNull(message = "new_password参数不存在")
@NotBlank(message = "请输入新密码")
private String newPassword;
}

View File

@ -38,4 +38,6 @@ public interface AdminUserService extends IService<AdminUser> {
void removeWithRoleIds(Integer userId);
void removeRelateRolesByUserId(Integer userId);
void passwordChange(AdminUser user, String password);
}

View File

@ -167,6 +167,15 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser
QueryWrapper<AdminUserRole> wrapper = userRoleService.query().getWrapper().eq("admin_id", userId);
userRoleService.remove(wrapper);
}
@Override
public void passwordChange(AdminUser user, String password) {
String newPassword = HelperUtil.MD5(password + user.getSalt());
AdminUser newUser = new AdminUser();
newUser.setId(user.getId());
newUser.setPassword(newPassword);
updateById(newUser);
}
}