Commit c2a132e8 authored by 赵啸非's avatar 赵啸非

更新用户同步逻辑

parent a0640cae
......@@ -6,13 +6,14 @@ import java.util.Objects;
import com.mortals.framework.ap.SysConstains;
import com.mortals.framework.model.BaseEntityLong;
import com.mortals.framework.service.IUser;
import com.mortals.xhx.base.system.user.model.vo.UserVo;
/**
*
* Description:User
* date: 2021-9-26 16:11:48
*/
public class UserEntity extends UserEntityExt implements IUser {
public class UserEntity extends UserVo implements IUser {
private static final long serialVersionUID = 1632643908537L;
......@@ -126,6 +127,8 @@ public class UserEntity extends UserEntityExt implements IUser {
*/
private String lastModPwdAddr;
private String siteIds;
public UserEntity(){
}
......@@ -330,7 +333,7 @@ public class UserEntity extends UserEntityExt implements IUser {
@Override
public String getSiteIds() {
return super.getSiteIds();
return this.siteIds;
}
@Override
......@@ -338,6 +341,10 @@ public class UserEntity extends UserEntityExt implements IUser {
return null;
}
public void setSiteIds(String siteIds) {
this.siteIds = siteIds;
}
/**
* 设置 站点id
* @param siteId
......
......@@ -30,7 +30,6 @@ public class UserEntityExt extends BaseEntityLong {
private Long expireTime;
private String siteName;
private List<Long> roleIds;
private String siteCode;
private String oldPassword;
......
package com.mortals.xhx.base.system.user.model.vo;
import com.mortals.framework.model.BaseEntityLong;
import lombok.Data;
/**
* 用户信息业务视图对象
*
* @author zxfei
* @date 2022-07-05
*/
@Data
public class UserVo extends BaseEntityLong {
/**
* 站点名称
*/
private String siteName;
private String roleIds;
/**
* 唯一标识
*/
private String token;
/**
* 菜单栏
*/
private String menuUrl;
/**
* 登录时间
*/
private Long loginTime;
/**
* 过期时间
*/
private Long expireTime;
private String oldPassword;
private String newPassword;
private String siteIds;
}
\ No newline at end of file
......@@ -31,35 +31,7 @@ import java.util.Set;
*/
public interface UserService extends ICRUDCacheService<UserEntity,Long> {
/**
* 用户登录
*
* @param loginName 登录用户名
* @param password 登录密码
* @param loginIp 登录IP
* @return
* @throws AppException
*/
public UserEntity doLogin(String loginName, String password, String loginIp) throws AppException;
/**
* 校验用户名与密码是否正确
*
* @param loginName
* @param password
* @return
* @throws AppException
*/
public UserEntity doCheckUser(String loginName, String password) throws AppException;
/**
* 检查用户是否存在
*
* @param loginName 登录用户名
* @param userId 密码
* @return
*/
public boolean existUser(String loginName, Long userId) throws AppException;
/**
* 通过登录用户获取菜单功能权限
......@@ -67,7 +39,7 @@ public interface UserService extends ICRUDCacheService<UserEntity,Long> {
* @param user
* @return
*/
public List<MenuEntity> findOutlookBarList(IUser user);
List<MenuEntity> findOutlookBarList(IUser user);
/**
* 查询用户所有有权限的菜单ID
......@@ -75,47 +47,7 @@ public interface UserService extends ICRUDCacheService<UserEntity,Long> {
* @param userEntity
* @return
*/
public Set<Long> findAllAuthIds(UserEntity userEntity) throws AppException;
/**
* 查询用户记录
*
* @param platformId
* @param params
* @param currPage
* @param prePageResult
* @return
* @throws AppException
*/
public Result<UserEntity> find(Long platformId, UserEntity params, int currPage, int prePageResult) throws AppException;
/**
* 为客户创建用户
*
* @param currUser 当前用户
* @param customerId 客户ID
* @param customerName 客户名称
* @param loginName
* @param password
* @param userName
* @param mobile
* @return
* @throws AppException
*/
public UserEntity createUser(IUser currUser, Long customerId, String customerName, String loginName, String password,
String userName, String mobile) throws AppException;
/**
* 用户修改密码
*
* @param loginName
* @param oldPwd
* @param newPwd
* @return
* @throws AppException
*/
public boolean updateUserPwd(String loginName, String oldPwd, String newPwd) throws AppException;
Set<Long> findAllAuthIds(UserEntity userEntity) throws AppException;
Rest<Void> refreshUser();
......
......@@ -26,6 +26,7 @@ import com.mortals.xhx.base.system.resource.model.ResourceEntity;
import com.mortals.xhx.base.system.resource.service.ResourceService;
import com.mortals.xhx.base.system.role.dao.RoleUserDao;
import com.mortals.xhx.base.system.role.model.RoleUserEntity;
import com.mortals.xhx.base.system.role.model.RoleUserQuery;
import com.mortals.xhx.base.system.role.service.RoleUserService;
import com.mortals.xhx.base.system.user.dao.UserDao;
import com.mortals.xhx.base.system.user.model.UserEntity;
......@@ -79,146 +80,36 @@ public class UserServiceImpl extends AbstractCRUDCacheServiceImpl<UserDao, UserE
}
private void doHandlerUser(UserEntity entity) throws AppException {
if (StringUtils.isNotEmpty(entity.getLoginPwd())) {
try {
entity.setLoginPwd(SecurityUtil.md5DoubleEncoding(entity.getLoginPwd()));
} catch (Exception e) {
throw new AppException("密码转换异常");
}
} else {
entity.setLoginPwd(null);
}
// if (entity.isSystemUser()) {
// entity.setUserType(UserType.SYSTEM.getValue());
// } else {
// entity.setUserType(UserType.CUSTOMER.getValue());
// }
}
@Override
protected void saveBefore(UserEntity entity, Context context) throws AppException {
this.doHandlerUser(entity);
}
@Override
protected void saveAfter(UserEntity entity, Context context) throws AppException {
if (CollectionUtils.isNotEmpty(entity.getRoleIds())) {
List<RoleUserEntity> roleUserEntityList = new ArrayList<>();
entity.getRoleIds().stream().forEach(item -> {
RoleUserEntity roleUserEntity = new RoleUserEntity();
roleUserEntity.setUserId(entity.getId());
roleUserEntity.setRoleId(item);
roleUserEntityList.add(roleUserEntity);
});
roleUserDao.insertBatch(roleUserEntityList);
}
}
@Override
protected void updateBefore(UserEntity entity, Context context) throws AppException {
if (entity.getId().longValue() == SysConstains.ADMIN_ID && !context.getUser().isAdmin()) {
throw new AppException("你没有权限执行该操作");
}
this.doHandlerUser(entity);
updateUserRole(entity);
}
@Override
protected void updateAfter(UserEntity entity, Context context) throws AppException {
if (CollectionUtils.isNotEmpty(entity.getRoleIds())) {
List<RoleUserEntity> roleUserEntityList = new ArrayList<>();
entity.getRoleIds().stream().forEach(item -> {
RoleUserEntity roleUserEntity = new RoleUserEntity();
roleUserEntity.setUserId(entity.getId());
roleUserEntity.setRoleId(item);
roleUserEntityList.add(roleUserEntity);
});
RoleUserEntity condition = new RoleUserEntity();
condition.setUserId(entity.getId());
roleUserDao.delete(condition);
roleUserDao.insertBatch(roleUserEntityList);
}
updateUserRole(entity);
}
@Override
protected UserEntity findBefore(UserEntity params, Context context) throws AppException {
// if (StringUtils.isNotEmpty(params.getDeptIds())) {
// params.setDeptIds(StringUtils.fillWithMark(params.getDeptIds(), ","));
// }
return super.findBefore(params, context);
}
@Override
protected UserEntity findBefore(UserEntity params, PageInfo pageInfo, Context context) throws AppException {
// if (StringUtils.isNotEmpty(params.getDeptIds())) {
// params.setDeptIds(StringUtils.fillWithMark(params.getDeptIds(), ","));
// }
return super.findBefore(params, pageInfo, context);
private void updateUserRole(UserEntity entity) {
if (!ObjectUtils.isEmpty(entity.getId()) && entity.getId().longValue() != SysConstains.ADMIN_ID && !ObjectUtils.isEmpty(entity.getRoleIds())) {
RoleUserQuery roleUserQuery = new RoleUserQuery();
roleUserQuery.setUserId(entity.getId());
List<Long> idList = Arrays.asList(entity.getRoleIds().split(",")).stream().map(Long::parseLong).collect(Collectors.toList());
roleUserQuery.setRoleIdList(idList);
roleUserService.doDistributionRole(roleUserQuery);
}
}
@Override
protected void findAfter(UserEntity params, PageInfo pageInfo, Context context, List<UserEntity> list) throws AppException {
if (CollectionUtils.isNotEmpty(list)) {
list.stream().forEach(item -> {
RoleUserEntity query = new RoleUserEntity();
query.setUserId(item.getId());
List<RoleUserEntity> roleUserEntityList = roleUserDao.getList(query);
List<Long> roleIds = new ArrayList<>();
roleUserEntityList.stream().forEach(role -> {
roleIds.add(role.getRoleId());
});
item.setRoleIds(roleIds);
});
//List<RoleUserEntity> roleUserEntityList =
}
}
public UserEntity findByLoginName(String loginName) {
UserQuery params = new UserQuery();
params.setLoginName(loginName);
List<UserEntity> userList = super.dao.getList(params);
if (userList != null && userList.size() > 0) {
return userList.get(0);
}
return null;
list.stream().peek(item -> {
RoleUserQuery roleUserQuery = new RoleUserQuery();
roleUserQuery.setUserId(item.getId());
String roleIds = roleUserService.find(roleUserQuery).stream().map(RoleUserEntity::getRoleId).map(String::valueOf).collect(Collectors.joining(","));
item.setRoleIds(roleIds);
}).count();
}
@Override
public UserEntity doLogin(String loginName, String password, String loginIp) throws AppException {
UserEntity sysUser = this.findByLoginName(loginName);
if (sysUser == null || !sysUser.getLoginName().equals(loginName)) {
throw new AppException("用户名不存在!");
}
try {
if (!sysUser.getLoginPwd().equals(SecurityUtil.md5DoubleEncoding(password))) {
throw new AppException("登录密码错误!");
}
} catch (Exception e) {
throw new AppException("密码验认出错!", e);
}
UserEntity update = new UserEntity();
update.setId(sysUser.getId());
update.setLastLoginAddress(loginIp);
update.setLastLoginTime(new Date());
this.dao.update(update);
return sysUser;
}
@Override
public UserEntity doCheckUser(String loginName, String password) throws AppException {
UserEntity sysUser = this.findByLoginName(loginName);
if (sysUser == null || !sysUser.getLoginName().equals(loginName)) {
return null;
}
try {
if (!sysUser.getLoginPwd().equals(SecurityUtil.md5DoubleEncoding(password))) {
return null;
}
} catch (Exception e) {
return null;
}
return sysUser;
}
@Override
public Set<Long> findAllAuthIds(UserEntity userEntity) throws AppException {
......@@ -285,79 +176,6 @@ public class UserServiceImpl extends AbstractCRUDCacheServiceImpl<UserDao, UserE
return outlookBar;
}
@Override
public Result<UserEntity> find(Long platformId, UserEntity params, int currPage, int prePageResult) throws AppException {
PageInfo pageInfo = new PageInfo(prePageResult);
pageInfo.setCurrPage(currPage);
return super.find(params, pageInfo, null);
}
@Override
public UserEntity createUser(IUser currUser, Long customerId, String customerName, String loginName, String password,
String userName, String mobile) throws AppException {
try {
UserEntity entity = new UserEntity();
entity.initAttrValue();
entity.setLoginName(loginName);
entity.setRealName(userName);
entity.setCustomerId(customerId);
entity.setLoginPwd(password);
entity.setMobile(mobile);
entity.setUserType(UserType.CUSTOMER.getValue());
entity.setCreateTime(new Date());
if (currUser != null) {
entity.setCreateUser(currUser.getLoginName());
entity.setCreateUserName(currUser.getRealName());
}
this.doHandlerUser(entity);
dao.insert(entity);
return entity;
} catch (Exception e) {
log.error("给客户创建用户异常-->customerId:" + customerId + ",customerName:" + customerName + ",loginName:" + loginName +
"-->原因:" + e.getMessage());
return null;
}
}
@Override
public boolean existUser(String loginName, Long userId) throws AppException {
UserEntity sysUser = this.findByLoginName(loginName);
boolean bRet = false;
if (sysUser != null) {
if (userId == null || userId == 0 || userId.longValue() != sysUser.getId().longValue()) {
bRet = true;
}
}
return bRet;
}
@Override
public boolean updateUserPwd(String loginName, String oldPwd, String newPwd) throws AppException {
UserEntity sysUser = this.findByLoginName(loginName);
if (sysUser == null || !sysUser.getLoginName().equals(loginName)) {
throw new AppException("帐号错误!");
}
try {
if (!sysUser.getLoginPwd().equals(SecurityUtil.md5DoubleEncoding(oldPwd))) {
throw new AppException("原始密码错误!");
}
} catch (Exception e) {
throw new AppException("密码验认出错!", e);
}
try {
sysUser.setLoginPwd(SecurityUtil.md5DoubleEncoding(newPwd));
sysUser.setLoginPwd3(sysUser.getLoginPwd2());
sysUser.setLoginPwd2(sysUser.getLoginPwd1());
sysUser.setLoginPwd1(sysUser.getLoginPwd());
sysUser.setLastModPwdTime(new Date());
} catch (Exception e) {
throw new AppException("密码转换异常!", e);
}
dao.update(sysUser);
return true;
}
@Override
public Rest<Void> refreshUser() {
UserPdu userPdu = new UserPdu();
......@@ -420,6 +238,15 @@ public class UserServiceImpl extends AbstractCRUDCacheServiceImpl<UserDao, UserE
return Rest.ok();
}
@Override
protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
super.removeAfter(ids, context, result);
RoleUserQuery roleUserQuery = new RoleUserQuery();
roleUserQuery.setUserIdList(Arrays.asList(ids));
Long[] userIds = roleUserService.find(roleUserQuery).stream().map(RoleUserEntity::getId).toArray(Long[]::new);
roleUserService.remove(userIds, context);
}
@Override
public UserDao getUserDao() {
......
......@@ -21,7 +21,10 @@ import com.mortals.framework.common.Rest;
import com.mortals.framework.service.IUser;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.role.model.RoleEntity;
import com.mortals.xhx.base.system.role.model.RoleUserEntity;
import com.mortals.xhx.base.system.role.model.RoleUserQuery;
import com.mortals.xhx.base.system.role.service.RoleService;
import com.mortals.xhx.base.system.role.service.RoleUserService;
import com.mortals.xhx.common.code.UserStatus;
import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.common.code.UserType;
......@@ -55,6 +58,9 @@ public class UserController extends BaseCRUDJsonBodyMappingController<UserServic
@Autowired
private RoleService roleService;
@Autowired
private RoleUserService roleUserService;
public UserController() {
super.setModuleDesc("用户信息");
}
......@@ -69,34 +75,6 @@ public class UserController extends BaseCRUDJsonBodyMappingController<UserServic
super.init(model, context);
}
@Override
protected int editAfter(Long id, Map<String, Object> model, UserEntity entity, Context context) throws AppException {
entity.setLoginPwd(null);
entity.setLoginPwd1(null);
entity.setLoginPwd2(null);
entity.setLoginPwd3(null);
return 1;
}
@RequestMapping(value = "change/password", method = RequestMethod.POST)
public String changePassword(@RequestBody UserEntity entity) {
IUser user = this.getCurUser();
if(user == null){
return this.createFailJsonResp("请先登录");
}
JSONObject ret = new JSONObject();
try {
service.updateUserPwd(super.getCurUser().getLoginName(), entity.getOldPassword(), entity.getNewPassword());
ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
ret.put(KEY_RESULT_MSG, "密码修改成功!");
} catch (Exception e) {
ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
ret.put(KEY_RESULT_MSG, super.convertException(e));
}
return ret.toJSONString();
}
@PostMapping({"refreshUser"})
@UnAuth
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment