From 55650b20e2ce066936b8ede458cb8e9ceb253cd7 Mon Sep 17 00:00:00 2001 From: "B.E.N.S.O.N" Date: Wed, 31 Dec 2025 12:19:11 +0800 Subject: [PATCH] Update for the user page --- .../modules/user/service/UserService.java | 472 +++++++++--------- 1 file changed, 236 insertions(+), 236 deletions(-) diff --git a/src/main/java/com/ffii/fpsms/modules/user/service/UserService.java b/src/main/java/com/ffii/fpsms/modules/user/service/UserService.java index d7df17c..7ea1c09 100644 --- a/src/main/java/com/ffii/fpsms/modules/user/service/UserService.java +++ b/src/main/java/com/ffii/fpsms/modules/user/service/UserService.java @@ -37,242 +37,242 @@ import com.ffii.fpsms.modules.user.service.pojo.UserRecord; @Service public class UserService extends AbstractBaseEntityService { - private static final String USER_AUTH_SQL = "SELECT a.authority" - + " FROM `user` u" - + " JOIN user_authority ua ON ua.userId = u.id" - + " JOIN authority a ON a.id = ua.authId" - + " WHERE u.deleted = 0" - + " AND u.id = :userId"; - private static final String UNION_SQL = " UNION "; - private static final String GROUP_AUTH_SQL = "SELECT a.authority" - + " FROM `user` u" - + " JOIN user_group ug ON ug.userId = u.id" - + " JOIN `group` g ON g.deleted = 0 AND g.id = ug.groupId" - + " JOIN group_authority ga ON ga.groupId = g.id" - + " JOIN authority a ON a.id = ga.authId" - + " WHERE u.deleted = 0" - + " AND u.id = :userId"; - - @Autowired - private SettingsService settingsService; - @Autowired - private PasswordEncoder passwordEncoder; - - @Autowired - UserRepository userRepository; - - public UserService(JdbcDao jdbcDao, UserRepository userRepository) { - super(jdbcDao, userRepository); - } - - public Optional loadUserOptByUsername(String username) { - return findByUsername(username) - .map(user -> { - Set auths = new LinkedHashSet(); - auths.add(new SimpleGrantedAuthority("ROLE_USER")); - jdbcDao.queryForList(USER_AUTH_SQL + UNION_SQL + GROUP_AUTH_SQL, Map.of("userId", user.getId())) - .forEach(item -> auths.add(new SimpleGrantedAuthority((String) item.get("authority")))); - - user.setAuthorities(auths); - return user; - }); - } - - public User getUserById(Long id) { - User user = userRepository.findById(id).orElseThrow(); - Set auths = new LinkedHashSet(); - auths.add(new SimpleGrantedAuthority("ROLE_USER")); - jdbcDao.queryForList(USER_AUTH_SQL + UNION_SQL + GROUP_AUTH_SQL, Map.of("userId", user.getId())) - .forEach(item -> auths.add(new SimpleGrantedAuthority((String) item.get("authority")))); - user.setAuthorities(auths); - return user; - } - public Optional findByUsername(String username) { - return userRepository.findByUsernameAndDeletedFalse(username); - } - - // @Transactional(rollbackFor = Exception.class) - public List search(SearchUserReq req) { - StringBuilder sql = new StringBuilder("SELECT" - + " u.id," - + " u.created," - + " u.createdBy," - + " u.version," - + " u.modified," - + " u.modifiedBy," - + " u.username," - + " u.locked," - + " u.name," - + " u.locale," - + " u.firstname," - + " u.lastname," - + " u.title," - + " u.department," - + " u.email," - + " u.phone1," - + " u.phone2," - + " u.remarks," + private static final String USER_AUTH_SQL = "SELECT a.authority" + + " FROM `user` u" + + " JOIN user_authority ua ON ua.userId = u.id" + + " JOIN authority a ON a.id = ua.authId" + + " WHERE u.deleted = 0" + + " AND u.id = :userId"; + private static final String UNION_SQL = " UNION "; + private static final String GROUP_AUTH_SQL = "SELECT a.authority" + + " FROM `user` u" + + " JOIN user_group ug ON ug.userId = u.id" + + " JOIN `group` g ON g.deleted = 0 AND g.id = ug.groupId" + + " JOIN group_authority ga ON ga.groupId = g.id" + + " JOIN authority a ON a.id = ga.authId" + + " WHERE u.deleted = 0" + + " AND u.id = :userId"; + + @Autowired + private SettingsService settingsService; + @Autowired + private PasswordEncoder passwordEncoder; + + @Autowired + UserRepository userRepository; + + public UserService(JdbcDao jdbcDao, UserRepository userRepository) { + super(jdbcDao, userRepository); + } + + public Optional loadUserOptByUsername(String username) { + return findByUsername(username) + .map(user -> { + Set auths = new LinkedHashSet(); + auths.add(new SimpleGrantedAuthority("ROLE_USER")); + jdbcDao.queryForList(USER_AUTH_SQL + UNION_SQL + GROUP_AUTH_SQL, Map.of("userId", user.getId())) + .forEach(item -> auths.add(new SimpleGrantedAuthority((String) item.get("authority")))); + + user.setAuthorities(auths); + return user; + }); + } + + public User getUserById(Long id) { + User user = userRepository.findById(id).orElseThrow(); + Set auths = new LinkedHashSet(); + auths.add(new SimpleGrantedAuthority("ROLE_USER")); + jdbcDao.queryForList(USER_AUTH_SQL + UNION_SQL + GROUP_AUTH_SQL, Map.of("userId", user.getId())) + .forEach(item -> auths.add(new SimpleGrantedAuthority((String) item.get("authority")))); + user.setAuthorities(auths); + return user; + } + public Optional findByUsername(String username) { + return userRepository.findByUsernameAndDeletedFalse(username); + } + + // @Transactional(rollbackFor = Exception.class) + public List search(SearchUserReq req) { + StringBuilder sql = new StringBuilder("SELECT" + + " u.id," + + " u.created," + + " u.createdBy," + + " u.version," + + " u.modified," + + " u.modifiedBy," + + " u.username," + + " u.locked," + + " u.name," + + " u.locale," + + " u.firstname," + + " u.lastname," + + " u.title," + + " u.department," + + " u.email," + + " u.phone1," + + " u.phone2," + + " u.remarks," + " u.staffNo" - + " FROM `user` u" - + " left join user_group ug on u.id = ug.userId" - + " where u.deleted = false"); - - if (req != null) { - if (req.getId() != null) - sql.append(" AND u.id = :id"); - - if (req.getGroupId() != null) - sql.append(" AND ug.groupId = :groupId"); - if (StringUtils.isNotBlank(req.getUsername())) { - req.setUsername("%" + req.getUsername() + "%"); - sql.append(" AND u.username LIKE :username"); - } - if (StringUtils.isNotBlank(req.getName())) { - req.setName("%" + req.getName() + "%"); - sql.append(" AND u.name LIKE :name"); - } - if (req.getLocked() != null) { - sql.append(" AND u.locked = :locked"); - } - } - sql.append(" ORDER BY u.name"); - - if (req != null) { - if (req.getStart() != null && req.getLimit() != null) - sql.append(" LIMIT :start, :limit"); - } - - return jdbcDao.queryForList(sql.toString(), req, UserRecord.class); - } - - public List listUserAuthId(long id) { - return jdbcDao.queryForInts( - "SELECT" - + " ua.authId" - + " FROM user_authority ua" - + " WHERE ua.userId = :id", - Map.of(Params.ID, id)); - } - - public List listUserGroupId(long id) { - return jdbcDao.queryForInts( - "SELECT" - + " gu.groupId" - + " FROM user_group gu" - + " INNER JOIN `group` g ON g.deleted = FALSE AND g.id = gu.groupId" - + " WHERE gu.userId = :id", - Map.of(Params.ID, id)); - } - - private User saveOrUpdate(User instance, UpdateUserReq req) { - - if (instance.getId() == null){ - req.setLocked(false); - } - req.setName(instance.getName()); - BeanUtils.copyProperties(req,instance); - instance = save(instance); - - long id = instance.getId(); - - List> authBatchInsertValues = req.getAddAuthIds().stream() - .map(authId -> Map.of("userId", (int)id, "authId", authId)) - .collect(Collectors.toList()); - List> authBatchDeleteValues = req.getRemoveAuthIds().stream() - .map(authId -> Map.of("userId", (int)id, "authId", authId)) - .collect(Collectors.toList()); - if (!authBatchDeleteValues.isEmpty()) { - jdbcDao.batchUpdate( - "DELETE FROM user_authority" - + " WHERE userId = :userId ", + + " FROM `user` u" + + " left join user_group ug on u.id = ug.userId" + + " where u.deleted = false"); + + if (req != null) { + if (req.getId() != null) + sql.append(" AND u.id = :id"); + + if (req.getGroupId() != null) + sql.append(" AND ug.groupId = :groupId"); + if (StringUtils.isNotBlank(req.getUsername())) { + req.setUsername("%" + req.getUsername() + "%"); + sql.append(" AND u.username LIKE :username"); + } + if (StringUtils.isNotBlank(req.getName())) { + req.setName("%" + req.getName() + "%"); + sql.append(" AND u.name LIKE :name"); + } + if (req.getLocked() != null) { + sql.append(" AND u.locked = :locked"); + } + } + sql.append(" ORDER BY u.name"); + + if (req != null) { + if (req.getStart() != null && req.getLimit() != null) + sql.append(" LIMIT :start, :limit"); + } + + return jdbcDao.queryForList(sql.toString(), req, UserRecord.class); + } + + public List listUserAuthId(long id) { + return jdbcDao.queryForInts( + "SELECT" + + " ua.authId" + + " FROM user_authority ua" + + " WHERE ua.userId = :id", + Map.of(Params.ID, id)); + } + + public List listUserGroupId(long id) { + return jdbcDao.queryForInts( + "SELECT" + + " gu.groupId" + + " FROM user_group gu" + + " INNER JOIN `group` g ON g.deleted = FALSE AND g.id = gu.groupId" + + " WHERE gu.userId = :id", + Map.of(Params.ID, id)); + } + + private User saveOrUpdate(User instance, UpdateUserReq req) { + + if (instance.getId() == null){ + req.setLocked(false); + } + // Removed: req.setName(instance.getName()); - This was overwriting the new name with the old one + BeanUtils.copyProperties(req,instance); + instance = save(instance); + + long id = instance.getId(); + + List> authBatchInsertValues = req.getAddAuthIds().stream() + .map(authId -> Map.of("userId", (int)id, "authId", authId)) + .collect(Collectors.toList()); + List> authBatchDeleteValues = req.getRemoveAuthIds().stream() + .map(authId -> Map.of("userId", (int)id, "authId", authId)) + .collect(Collectors.toList()); + if (!authBatchDeleteValues.isEmpty()) { + jdbcDao.batchUpdate( + "DELETE FROM user_authority" + + " WHERE userId = :userId ", // + "AND authId = :authId", - authBatchDeleteValues); - } - if (!authBatchInsertValues.isEmpty()) { - jdbcDao.batchUpdate( - "INSERT IGNORE INTO user_authority (userId, authId)" - + " VALUES (:userId, :authId)", - authBatchInsertValues); - } - - return instance; - } - - @Transactional(rollbackFor = Exception.class) - public User newRecord(NewUserReq req) throws UnsupportedEncodingException { - if (findByUsername(req.getUsername()).isPresent()) { - throw new UnprocessableEntityException(ErrorCodes.USERNAME_NOT_AVAILABLE); - } - - System.out.println("Start Save"); - - String password = req.getPassword(); - String pwdHash = passwordEncoder.encode(password); - - User instance = new User(); - instance.setPassword(pwdHash); - instance.setUsername(req.getUsername()); - instance.setName(req.getUsername()); - instance = save(instance); - - long id = instance.getId(); - - List> authBatchInsertValues = req.getAddAuthIds().stream() - .map(authId -> Map.of("userId", (int)id, "authId", authId)) - .collect(Collectors.toList()); - List> authBatchDeleteValues = req.getRemoveAuthIds().stream() - .map(authId -> Map.of("userId", (int)id, "authId", authId)) - .collect(Collectors.toList()); - if (!authBatchDeleteValues.isEmpty()) { - jdbcDao.batchUpdate( - "DELETE FROM user_authority" - + " WHERE userId = :userId ", + authBatchDeleteValues); + } + if (!authBatchInsertValues.isEmpty()) { + jdbcDao.batchUpdate( + "INSERT IGNORE INTO user_authority (userId, authId)" + + " VALUES (:userId, :authId)", + authBatchInsertValues); + } + + return instance; + } + + @Transactional(rollbackFor = Exception.class) + public User newRecord(NewUserReq req) throws UnsupportedEncodingException { + if (findByUsername(req.getUsername()).isPresent()) { + throw new UnprocessableEntityException(ErrorCodes.USERNAME_NOT_AVAILABLE); + } + + System.out.println("Start Save"); + + String password = req.getPassword(); + String pwdHash = passwordEncoder.encode(password); + + User instance = new User(); + instance.setPassword(pwdHash); + instance.setUsername(req.getUsername()); + instance.setName(req.getUsername()); + instance = save(instance); + + long id = instance.getId(); + + List> authBatchInsertValues = req.getAddAuthIds().stream() + .map(authId -> Map.of("userId", (int)id, "authId", authId)) + .collect(Collectors.toList()); + List> authBatchDeleteValues = req.getRemoveAuthIds().stream() + .map(authId -> Map.of("userId", (int)id, "authId", authId)) + .collect(Collectors.toList()); + if (!authBatchDeleteValues.isEmpty()) { + jdbcDao.batchUpdate( + "DELETE FROM user_authority" + + " WHERE userId = :userId ", // + "AND authId = :authId", - authBatchDeleteValues); - } - if (!authBatchInsertValues.isEmpty()) { - jdbcDao.batchUpdate( - "INSERT IGNORE INTO user_authority (userId, authId)" - + " VALUES (:userId, :authId)", - authBatchInsertValues); - } - - return instance; - } - - @Transactional(rollbackFor = Exception.class) - public User newPublicUserRecord(NewPublicUserReq req) throws UnsupportedEncodingException { - if (findByUsername(req.getUsername()).isPresent()) { - throw new UnprocessableEntityException(ErrorCodes.USERNAME_NOT_AVAILABLE); - } - - String submitedPassword = req.getPassword(); - String pwdHash = passwordEncoder.encode(submitedPassword); - req.setPassword(pwdHash); - User instance = new User(); - - instance = saveOrUpdate(instance, req); - return instance; - } - - @Transactional(rollbackFor = Exception.class) - public void updateRecord(long id, UpdateUserReq req) { - saveOrUpdate( - find(id).orElseThrow(NotFoundException::new), - req); - } - - @Transactional(rollbackFor = Exception.class) - public String resetPassword(long id) throws UnsupportedEncodingException { - User instance = find(id).orElseThrow(NotFoundException::new); - String randomPassword = PasswordUtils.genPwd(new PasswordRule(settingsService)); - - instance.setPassword(passwordEncoder.encode(randomPassword)); - instance = save(instance); - return randomPassword; - } - - public List getEscalationCombo() { - return userRepository.findUserComboByTitleNotNullAndDepartmentNotNullAndNameNotNullAndDeletedFalse(); - } -} + authBatchDeleteValues); + } + if (!authBatchInsertValues.isEmpty()) { + jdbcDao.batchUpdate( + "INSERT IGNORE INTO user_authority (userId, authId)" + + " VALUES (:userId, :authId)", + authBatchInsertValues); + } + + return instance; + } + + @Transactional(rollbackFor = Exception.class) + public User newPublicUserRecord(NewPublicUserReq req) throws UnsupportedEncodingException { + if (findByUsername(req.getUsername()).isPresent()) { + throw new UnprocessableEntityException(ErrorCodes.USERNAME_NOT_AVAILABLE); + } + + String submitedPassword = req.getPassword(); + String pwdHash = passwordEncoder.encode(submitedPassword); + req.setPassword(pwdHash); + User instance = new User(); + + instance = saveOrUpdate(instance, req); + return instance; + } + + @Transactional(rollbackFor = Exception.class) + public void updateRecord(long id, UpdateUserReq req) { + saveOrUpdate( + find(id).orElseThrow(NotFoundException::new), + req); + } + + @Transactional(rollbackFor = Exception.class) + public String resetPassword(long id) throws UnsupportedEncodingException { + User instance = find(id).orElseThrow(NotFoundException::new); + String randomPassword = PasswordUtils.genPwd(new PasswordRule(settingsService)); + + instance.setPassword(passwordEncoder.encode(randomPassword)); + instance = save(instance); + return randomPassword; + } + + public List getEscalationCombo() { + return userRepository.findUserComboByTitleNotNullAndDepartmentNotNullAndNameNotNullAndDeletedFalse(); + } +} \ No newline at end of file