添加修改密码接口。

pull/1/head
ZhouXY108 2022-12-12 04:01:24 +08:00
parent 263777aa70
commit 80530edf6b
5 changed files with 88 additions and 9 deletions

View File

@ -3,10 +3,13 @@ package xyz.zhouxy.plusone.system.application.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic; import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
import org.springframework.web.bind.annotation.GetMapping; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.system.application.service.AccountContextService; 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; import xyz.zhouxy.plusone.util.RestfulResult;
/** /**
@ -37,4 +40,16 @@ public class AccountContextController {
var result = service.getMenuTree(); var result = service.getMenuTree();
return RestfulResult.success("查询成功", result); 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("修改成功,请重新登录。");
}
} }

View File

@ -1,14 +1,26 @@
package xyz.zhouxy.plusone.system.application.service; 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 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.AccountQueries;
import xyz.zhouxy.plusone.system.application.query.result.AccountDetails; import xyz.zhouxy.plusone.system.application.query.result.AccountDetails;
import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject; 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 @Service
public class AccountContextService { public class AccountContextService {
private final AccountQueries accountQueries; private final static StpLogic adminAuthLogic = AuthLogic.adminAuthLogic;
private final MenuManagementService menuManagementService;
public AccountContextService(AccountQueries accountQueries, MenuManagementService menuManagementService) { @Resource
this.accountQueries = accountQueries; private AccountQueries accountQueries;
this.menuManagementService = menuManagementService; @Resource
} private MenuManagementService menuManagementService;
@Resource
private AccountRepository accountRepository;
@Resource
private MailAndSmsVerifyService mailAndSmsVerifyService;
public AccountDetails getAccountInfo() { public AccountDetails getAccountInfo() {
adminAuthLogic.checkLogin();
long accountId = adminAuthLogic.getLoginIdAsLong(); long accountId = adminAuthLogic.getLoginIdAsLong();
return accountQueries.queryAccountDetails(accountId); return accountQueries.queryAccountDetails(accountId);
} }
public List<MenuViewObject> getMenuTree() { public List<MenuViewObject> getMenuTree() {
adminAuthLogic.checkLogin();
long accountId = adminAuthLogic.getLoginIdAsLong(); long accountId = adminAuthLogic.getLoginIdAsLong();
return menuManagementService.queryByAccountId(accountId); 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();
}
} }

View File

@ -107,7 +107,7 @@ public class MailAndSmsVerifyService {
} }
private static String generateCode() { private static String generateCode() {
return RandomUtil.randomString(CODE_LENGTH); return RandomUtil.randomString(RandomUtil.BASE_NUMBER, CODE_LENGTH);
} }
} }

View File

@ -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;
}

View File

@ -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;
}