添加修改密码接口。
parent
263777aa70
commit
80530edf6b
|
@ -3,10 +3,13 @@ package xyz.zhouxy.plusone.system.application.controller;
|
|||
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import xyz.zhouxy.plusone.system.application.service.AccountContextService;
|
||||
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand;
|
||||
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand;
|
||||
import xyz.zhouxy.plusone.util.RestfulResult;
|
||||
|
||||
/**
|
||||
|
@ -37,4 +40,16 @@ public class AccountContextController {
|
|||
var result = service.getMenuTree();
|
||||
return RestfulResult.success("查询成功", result);
|
||||
}
|
||||
|
||||
@PostMapping("changePassword")
|
||||
public RestfulResult changePassword(ChangePasswordCommand command) {
|
||||
service.changePassword(command);
|
||||
return RestfulResult.success("修改成功,请重新登录。");
|
||||
}
|
||||
|
||||
@PostMapping("changePasswordWithoutLogin")
|
||||
public RestfulResult changePasswordWithoutLogin(ChangePasswordWithoutLoginCommand command) {
|
||||
service.changePasswordWithoutLogin(command);
|
||||
return RestfulResult.success("修改成功,请重新登录。");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,26 @@
|
|||
package xyz.zhouxy.plusone.system.application.service;
|
||||
|
||||
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
|
||||
import xyz.zhouxy.plusone.system.constant.AuthLogic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
|
||||
import xyz.zhouxy.plusone.system.application.query.AccountQueries;
|
||||
import xyz.zhouxy.plusone.system.application.query.result.AccountDetails;
|
||||
import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;
|
||||
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand;
|
||||
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand;
|
||||
import xyz.zhouxy.plusone.system.domain.model.account.Account;
|
||||
import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository;
|
||||
import xyz.zhouxy.plusone.system.domain.model.account.Email;
|
||||
import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone;
|
||||
import xyz.zhouxy.plusone.system.domain.model.account.Principal;
|
||||
|
||||
/**
|
||||
* 账号查询本身相关信息
|
||||
|
@ -18,21 +30,50 @@ import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;
|
|||
@Service
|
||||
public class AccountContextService {
|
||||
|
||||
private final AccountQueries accountQueries;
|
||||
private final MenuManagementService menuManagementService;
|
||||
private final static StpLogic adminAuthLogic = AuthLogic.adminAuthLogic;
|
||||
|
||||
public AccountContextService(AccountQueries accountQueries, MenuManagementService menuManagementService) {
|
||||
this.accountQueries = accountQueries;
|
||||
this.menuManagementService = menuManagementService;
|
||||
}
|
||||
@Resource
|
||||
private AccountQueries accountQueries;
|
||||
@Resource
|
||||
private MenuManagementService menuManagementService;
|
||||
@Resource
|
||||
private AccountRepository accountRepository;
|
||||
@Resource
|
||||
private MailAndSmsVerifyService mailAndSmsVerifyService;
|
||||
|
||||
public AccountDetails getAccountInfo() {
|
||||
adminAuthLogic.checkLogin();
|
||||
long accountId = adminAuthLogic.getLoginIdAsLong();
|
||||
return accountQueries.queryAccountDetails(accountId);
|
||||
}
|
||||
|
||||
public List<MenuViewObject> getMenuTree() {
|
||||
adminAuthLogic.checkLogin();
|
||||
long accountId = adminAuthLogic.getLoginIdAsLong();
|
||||
return menuManagementService.queryByAccountId(accountId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void changePassword(ChangePasswordCommand command) {
|
||||
adminAuthLogic.checkLogin();
|
||||
Account account = accountRepository.find(adminAuthLogic.getLoginIdAsLong());
|
||||
account.checkPassword(command.getPassword());
|
||||
account.changePassword(command.getNewPassword(), command.getPasswordConfirmation());
|
||||
accountRepository.save(account);
|
||||
adminAuthLogic.logout();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void changePasswordWithoutLogin(ChangePasswordWithoutLoginCommand command) {
|
||||
String principal = command.getAccount();
|
||||
Principal emailOrMobilePhone = PrincipalUtil.getEmailOrMobilePhone(principal);
|
||||
|
||||
Account account = emailOrMobilePhone instanceof Email
|
||||
? accountRepository.findByEmail((Email) emailOrMobilePhone)
|
||||
: accountRepository.findByMobilePhone((MobilePhone) emailOrMobilePhone);
|
||||
account.checkPassword(command.getOldPassword());
|
||||
account.changePassword(command.getNewPassword(), command.getPasswordConfirmation());
|
||||
accountRepository.save(account);
|
||||
adminAuthLogic.logout();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,7 +107,7 @@ public class MailAndSmsVerifyService {
|
|||
}
|
||||
|
||||
private static String generateCode() {
|
||||
return RandomUtil.randomString(CODE_LENGTH);
|
||||
return RandomUtil.randomString(RandomUtil.BASE_NUMBER, CODE_LENGTH);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package xyz.zhouxy.plusone.system.application.service.command;
|
||||
|
||||
import lombok.Data;
|
||||
import xyz.zhouxy.plusone.domain.ICommand;
|
||||
|
||||
@Data
|
||||
public class ChangePasswordCommand implements ICommand {
|
||||
private String password;
|
||||
private String newPassword;
|
||||
private String passwordConfirmation;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package xyz.zhouxy.plusone.system.application.service.command;
|
||||
|
||||
import lombok.Data;
|
||||
import xyz.zhouxy.plusone.domain.ICommand;
|
||||
|
||||
@Data
|
||||
public class ChangePasswordWithoutLoginCommand implements ICommand {
|
||||
private String account;
|
||||
private String oldPassword;
|
||||
private String newPassword;
|
||||
private String passwordConfirmation;
|
||||
}
|
Loading…
Reference in New Issue